Thread: Backups?
View Single Post
Old 03-03-2008, 01:42 AM   #3 (permalink)
munkyeetr
Platinum Member
 
munkyeetr's Avatar
 
Join Date: Mar 2007
Age: 85
Posts: 629
Default

In case you were curious, here's the code:

The actual script starts at the bottom of the code, the rest are the functions that make it work.

Code:
#!/bin/sh

# Filename:	backup.sh
# Description:	Runs an automated backup of system and user files
# Arguments:	None
# Author:	munkyeetr
# Date:		Completed on 05/15/07
#----------------------------------------------------------

## START FUNCTION DECLARATIONS AND DEFINITIONS
#----------------------------------------------------------

# Funct Name:	MountVolume()
# Description:	Passed a path, it will test if the path is mounted
#		and if not, it will attempt to mount the path.
# Arguments:	${vol} - path to mount
# Returns:	n/a
#----------------------------------------------------------
MountVolume() {

	vol=$1

	mount | grep "on ${vol} type" > /dev/null
	if [ $? -eq 0 ]; then
		echo ${vol}" is already mounted"
	else
		sudo mount ${vol}
	fi
}

# Funct Name:	UnmountVolume()
# Description:	Passed a path, it will test if the path is mounted
#		and if it is, it will attempt to unmount the path.
# Arguments:	${vol} - path to unmount
# Returns:	n/a
#----------------------------------------------------------
UnmountVolume() {

	vol=$1

	mount | grep "on ${vol} type" > /dev/null
	if [ $? -eq 0 ]; then
		sudo umount ${vol}
	else
		echo ${vol}" is already unmounted"
	fi	
}

# Funct Name:	CreateBackup()
# Description:	Backs up important system folders and the user's
# 		Home directory to ${dest}
# Arguments:	${dest} - path to backup directory
# Returns:	n/a
#----------------------------------------------------------
CreateBackup() {
	
# set backup destinations; create folder
#----------------------------------------------------------
	dest=${1}"/"`date +%m-%d-%y`
	sudo mkdir ${dest}

# backup select filesystems
#----------------------------------------------------------
	source="/etc"
	sudo tar -zcf ${dest}"/etc".tar ${source}

	source="/boot"
	sudo tar -zcf ${dest}"/boot".tar ${source}

	source="/home"
	sudo tar -zcf ${dest}"/home".tar ${source}

	source="/usr/local"
	sudo tar -zcf ${dest}"/usr-local".tar ${source}

	source="/var/log"
	sudo tar -zcf ${dest}"/var-log".tar ${source}

# generate system reports
#----------------------------------------------------------
	dest=${dest}"/reports"

	sudo mkdir ${dest}

	sudo dpkg --get-selections | grep -v deinstall > installed-packages
	sudo mv installed-packages ${dest}

	sudo uptime > system-uptime
	sudo mv system-uptime ${dest}

	sudo ps aux > system-processes
	sudo mv system-processes ${dest}

	sudo ifconfig > system-ifconfig
	sudo mv system-ifconfig ${dest}

	sudo fdisk -l > system-fdisk
	sudo mv system-fdisk ${dest}

	sudo df -l > system-df
	sudo mv system-df ${dest}
}

#----------------------------------------------------------
## END FUNCTION DECLARATIONS AND DEFINITIONS

## SCRIPT BEGINS HERE
#----------------------------------------------------------

backup_locations=( /media/Backup1	  ## < additional backup locations can be put here >
		   /media/Backup2 )	

# cycle through each backup location and perform the backup
for location in ${backup_locations[@]}
do
	MountVolume ${location}
	CreateBackup ${location}
	UnmountVolume ${location}	
done

#----------------------------------------------------------
## SCRIPT ENDS HERE

exit
__________________
If the advice you receive helps, please post a reply saying so as courtesy to others who will come here with the same problem.

MSI MS-7032 MB - Sempron 3000+ 1.8Ghz - 2GB PC3200
500GB Western Digital - 80GB Samsung - 80GB Maxtor (Dualbooting Ubuntu 7.10/Debian Lenny)
nVidia GF 5700LE 256MB (AGP) - SoundBlaster 24bit PCI
LiteOn DVDRW - Acer AL1916W 19" (2x)
Registered Linux User: 448689
munkyeetr is offline   Reply With Quote