Cron Job for Data Backup to ISO Files
BackgroundThese are some scripts I wrote to automatically backup my data on my school machine. These scripts can be adapted to Linux systems in general by changing the path names appropriately.
The basic goal of these scripts is to automatically compress all your data, make an ISO image that can be burned to a CD, automate it with a cron job, and email you whenever the backup is finished.
- Create a directory on your local hard drive where you will create
the all the gzip'd tar files for your work. By having it on the local
drive, it will be faster and avoid having to exclude the directory
when you are gzip'ing the files. However, this is not necessary, it
can be on a network drive if you want.
- Place this
in your directory created in the previous step. Now, you
have to decide if/how you want to divide up your files. My files are
currently large enough that they need to be burnt to two CDs. Thus, I
place all the gzip files in one of two directories, corresponding to
what will become a CD image. My current division is to place all my
local files (i.e., files in /scr/)
to be backed up (e.g., ns-2 files) on one image
and everything from my nfs drives (i.e., /home/crhc9/mjmille2
and /home/mobiserve2/mjmille2) on another image.
mkdir nfs_backup; mkdir scr_backup;You'll need to modify the Makefile based on your own division. Also, the Makefile currently excludes all *.o files as well as the two ns-2 executables (ns, nse) from being backed up. If you want to include these files, update the Makefile appropriately. Also, you'll obviously have to choose what you want to be backed up on your own local machine and update the Makefile appropriately.
- Now, we're going to get a little bit fancier and make a shell
script that will automatically create ISO images that you can burn to
a CD and move these files to a specified location on your network
drives. The reason that I move the files to a network drive is that
my current setup does not allow me to burn CDs in Linux without root
access, so I have to switch to Windows to burn the CDs. By placing
the ISO files on a network drive, I can access them in Windows. If
they were to remain on your local Linux partition, this would not be
possible unless you have set up a local FAT32 partition that is shared
between Linux and Windows.
To burn ISOs in Windows XP, you're going to need Alex Feinman's ISO Recorder Power Tool. Note that there are different versions for SP1 and SP2 that are incompatible if not used with the correct XP version.
Enough talk, here's the script (TXT link). The script needs to be updated for your configuration. The directory in the cd command needs to be the one you created earlier. The NFS_DIR needs to be the location where you want the ISO files to be placed in the end. The mkisofs command needs to be updated based on your locations. Also, I added the Rock Ridge and Joliet extensions to the image, which should be fine, but you can change it if need be.
Finally, the script will do a listing of the files that you just copied to their new location and email it to the address you specify in the script (assuming you have mutt on your system). This email can serve as a reminder that the backup was done when we automate it (in the next step). Also, the directory listing gives the sizes of the files, so you can do a sanity check to make sure that everything looks like it completed without error.
Just as a warning, it currently takes me about a half hour to run this script to completion. Of course, this depends on how big your backup is. gzip-ing all the files and copying the ISO files over the network are rather time consuming.
- Finally, we will create a cron job to do the backup automatically
so you don't have to worry about remembering.
crontab -eWill bring up your default text editor. Here's a guide on the cron format. For mine, I set it up to do the backup once a week at a time I'm not likely to be working on my computer (e.g., 3 am).0 3 * * 1 ~/misc/scripts/cron-backup.sh
Of course, you'll need to put the appropriate path to your backup script in the cron job.
Also, if you want to disable the output of the cron script from being emailed to you automatically, you need to redirect the output.# Redirect both stdout and stderr to /dev/null
0 3 * * 1 ~/misc/scripts/cron-backup.sh > /dev/null 2>&1