MORE USEFUL TIPS – LESS HARD-EARNED CASH Lockergnome’s Top 50 PenguinCore Tips and Top 50 PenguinTweaks have made the GnomeTomes lineup. $5.00? Are you kidding? For a mere $10.00, you can have both volumes – more than 120 pages of useful Linux tips, tricks and explanations of the core of the Linux operating system. These little knowledge gems are available in 7 formats. Did we mention they’re only $5.00 per volume? This is a limited-time price so get your Linux fix today.
CD Backups – Part II
Yesterday, we started a short two-day series on writing critical backups to CD-ROM. When we left off, we’d just tarred and zipped up all the files and created a list of the files that were added to the archive. We’ll pick up there today with a look at the last half of the script.
# tar and gzip critical files using
# a list from /home/tony – this simplifies
# adding or removing files from the backup
tar -czf $HOME/backup/$DATE.tgz
# more feedback
echo “Creating CD Image …”
# make an isofs image of the .tgz file
# and tgz list file just created
mkisofs -r -f -o $HOME/backup/$DATE.iso
# more feedback
echo “Burning CD …”
# burn the newly-created iso image to disc
/usr/bin/cdrecord -v -data speed=12 dev=0,0,0
# more feedback
echo “Mailing diff results …”
# run diff, checking for differences between
# the old backup list and the new one
diff -aBH $HOME/backup/lists/prev_backup.list
| mail [email protected] -s “$DATE Backup Results”
# more feedback
echo “Deleting temporary files …”
# clean up the backup directory
rm -f $HOME/backup/*.tgz
rm -f $HOME/backup/*.iso
rm -f $HOME/backup/*.sql
# final feedback
We’re clearly into the meat of the script. After creating the archive (remember that tar actually means [t]ape [ar]chive) we need to create an iso image to burn to disc. The iso9660 format is actually a filesystem type. The program mkisofs is the program we’ll use to convert the archive and the list file to the iso9660 format for burning to CD. In the case of our script, we’re also going to pass mkisofs a few options.
First, we pass the -r option. This sets the userid and groupid (UID and GID) bits to zero. In other words, these files really have no owner, because ownership is generally only relevant to the file creator’s system. Next, we pass the -f option. This tells tar to [f]ollow any symbolic links. If, for example, I’d created a symlink to /home/tony/html and named it /www in the /var directory, the -f option would signal to tar that it’s okay to actually archive the files in /home/tony/html. Next, the -o option signals that what follows in the command line is the name of the file to be created, in this case $DATE.iso (where $DATE is the current value of the DATE variable as discussed yesterday). Finally, we tell mkisofs what files to use to create the iso image. By the way – the characters break a line without breaking the code. It’s necessary to do that in this case because of the relatively narrow area of both the web and mail versions of Penguin Shell. You may or may not need them in your script.
So, we’ve now got an iso image named $DATE.iso and it’s ready to burn to disc. We call the cdrecord program with a few options to do just that. First, we want a [v]erbose reporting on the burn process. In other words, when we run this script manually, the standard output from cdrecord prints to the screen. This is great for testing and debugging, but isn’t an operational requirement for the program. We also let cdrecord know that the data being used for the burn is, in fact, data rather than audio. We then define the burning speed, the device and the file we’re going to burn to disc. If you have any doubt about the device reference for your machine, run cdrecord -scanbus as root. It will tell you exactly where your machine sees your burner. With all that done, we should now have a CD-ROM with all your critical files.
But, that’s never enough. I always want some record for posterity of these processes. In this case, I’m using the diff program to compare the contents of the /home/tony/backup/lists/prev_backup.list file (the list created by tar in the previous backup and renamed at the beginning of the cdbackup.sh script) to the contents of the new backup list. diff will note any changes from the old file. By piping that output to the mail program [ | mail ], we create the body of an email that will get sent to my inbox. We add the subject line to the email with the -s option and the $DATE variable, plus a bit more text that makes the subject line much more descriptive.
With everything else done, we do a bit of housecleaning. We remove the .tgz, .iso and .sql files we created at the beginning of the script. That leaves no room for confusion in subsequent runnings of the script. Finally, we feedback a finish time with the /bin/date command, and exit the script.
Clearly, you’ll need to modify this script to work on your system. Pay particular attention to directories, as you’ll have both the critical files and the .tgz, .iso, .sql, and .list files somewhere other than /home/tony/backup on your system. Given that the script also uses a list file for tarring your critical files, you’ll need to create this file prior to running the script. This is a simple text file with one directory, path or file location per line.
That’s it. You should be set to backup your critical data to CD-ROM on your Linux system. This capability has saved my butt more than once. I hope you find it useful in your daily Linux use.