Creating an Ubuntu Backup Files Script
Very Simple Backup
Home users may find that they only need to backup their home directory in order to satisfy their backup needs. The following script meets that need nicely:
#!/bin/bash
## The directory that should be backed up
folder=/home/$USER
## name of backup without file extensions
backup=/path/to/backup
##The tar command creates a backup of the file with the date appended to it.
tar -cpzf “${backup%}`date +%m%d%Y`.tar.gz” –ignore-failed-read $folder
The most important part of the above script is the line “tar -cpzf “${backup%}`date +%m%d%Y`.tar.gz” –ignore-failed-read $folder.” This line says to create a gzipped tar file using the name supplied in the $backup variable (in this case “backup”), appended with the current date in the form mmddYY. For example, the filename could be “/mount/usb1/backup093009.tar.gz.”
This script can be saved as “backup.sh” and set to run as a regular cron job. If you set the path to the backup file to an external hard drive, you don’t even have to worry about moving the file once it is created.
Backing up to CD or DVD
If you don’t have an external hard drive, or you simply prefer to store your backups on CD- or DVD-rom, you can create ISO images and burn the image to optical media. This particular script places the temporary .iso file in the Ubuntu /tmp directory, then removes it before the script ends. This ensures that your hard drive will not be clogged up with large .iso files.
#! /bin/bash
#The directory that should be backed up
folder=/home/$USER
#The temporary .iso file (this will be removed)
temp_file=/tmp/backup.iso
mkisofs -o $temp_file $folder
##Uncomment the line that corresponds to the media you want to use
##cdrecord is used for cd-rom
##growisofs is used for dvd-rom
#cdrecord -v -dao dev=2,0,0 $temp_file
#growisofs -dvd-compat -Z /dev/cdrom=$temp_file
#Remove the temporary .iso file.
rm -f $temp_file
This script can be used to backup to either a CD or DVD. To use it with a CD, uncomment the “cdrecord” line. To use it with a DVD line, uncomment the “growisofs” line. The only caveat here is that the backups cannot exceed the size of the removable media. If you have a lot of data that needs to be backed up, you will have to separate the data and create multiple scripts.
Backing up to a file server
If you have a file server at your disposal, you can use the rsync to create a backup. The rsync utility syncs two versions of a file system or directory structure across a network. The utility only updates the differences between files and directories, without requiring that the complete set of files be present on both sides of the transfer. A simple backup script for rsync would look like:
#! /bin/bash
#The directory that should be backed up
folder=/home/$USER
#The name of the server
server=Server_Name
#The backup directory on the file server
backup=/$USER/backup
#Password on the file server
export RSYNC_PASS=XXXXXX
#Create and maintain the backup
rsync -avz $folder $server::$backup
This script does not create incremental backups, it simply creates a mirror of the original, and then ensures that the mirror is maintained. The rsync documentation has a nice script for a 7 day incremental backup that you can change to suit your needs.
Conclusion
The above are very simple backup scripts that can be used on a home system. They can be tweaked and changed to suit your particular needs. The most important thing to remember is to choose a backup method that meets your needs and then remember to use it regularly. The first and the third method can be placed in a cron job to create an automatic backup.