Last week’s article left off with a basic CD writing command set that uses mkisofs and cdrecord to make CD backups. This week, let’s first look at a few more command options for these programs before moving on to using tar for more complex needs and writing scripts to automate backups.
Linux permissions can be a bit confusing when making backups. Recall the basic mkisofs command string to put our backup file into a CD-recordable format:
$ mkisofs -o monday_backup.iso -JRVv /home/carla/
This preserves the original file permisssions. Sometimes this is what you want, sometimes not. Substitute a small r for the big R to create a publicly readable and executable disk:
$ mkisofs -o monday_backup.iso -JrVv /home/carla/
Here is a very simple script named backup.sh to illustrate automating a CD backup. While it does work, do not rely on it and do not use it on valuable data. Practice using backup scripts on test files until you’re certain you’ll get the correct results. See Resources for more backup scripts, and scripting howtos. Remember to use chmod +x to make backup.sh executable.
mkisofs -log-file /logfiles/mkisofs -o monday_backup.iso -JrVv /home/carla/
cdrecord -v -eject -multi speed=8 dev=0,1,0 monday_backup.iso
Put the script in crontab to run it regularly. This example runs it every evening at 10:30:
30 10 * * * root /usr/local/scripts/backup.sh
This particular scheme creates CDs that can be read in any CD drive. Restore files simply by copying them from the CD.
info mkisofs is extensive and useful. Beware of single and double hypen inconsistencies when using mkisofs. The standard convention is a single hyphen for a single-letter option and a double hyphen for the complete word. For example -a and –apple should mean the same thing. However, mkisofs does not follow this convention.
A few more useful command options for mkisofs are:
- -apple — Create an ISO9660 CD with Apple’s extensions
- -nobak — Do not include backup files
- -exclude-list filename — File with list of file names to exclude
- -log-file logfilename — Re-direct messages to a log file
- -L, -allow-leading-dots — Allow ISO9660 filenames to start with ‘.’ (violates ISO9660)
A few more useful command options for cdrecord are:
- -dummy — Do everything with laser turned off; use this for testing a new CD writer.
- -multi — Create a multi-session disk. The default is to close the disk so that no more files can be written to it. This allows adding more files later.
You may have noticed that Windows CD writing software uses the exact same terminology and commands, underneath the user interface. CD writing works the same on all platforms — first create an .iso, then write to disk.
Good Ole tar
GNU tar (Tape Archiver) is the reliable old Unix archiving workhorse. Using tar with a compression utility such as gzip or the new tool bzip2, zillions more bits can be packed onto a disk. tar preserves permissions, hard and soft links, and directory structures. Here is a simple recipe for using tar to create a compressed archive to burn to disk:
# tar -cvzWf /monday_backup.tar.gz /home/carla/
# mkisofs -o monday_backup.iso -JrVv /monday_backup.tar.gz
# cdrecord -v -eject -multi speed=8 dev=0,1,0 monday_backup.iso
Scripting is little more than stringing all the commands for a job together in one nice neat file. Plus, with scripting you can take advantage of additional elements to make a job easier, such as if/else statements, echos to screen, prompts, and error-handling.
tar is a funny little thing. It is not always necessary to use the single hyphen, as:
# tar cvzWf /monday_backup.tar.gz /home/carla/
is the same as the previous command (notice the lack of a single hyphen preceding “cvzfW”). But for tar commands with double hyphens, you must use them. It’s best to play it safe and hyphenate everything correctly:
# tar –exclude=/home/carla/tmp -cvzWf /monday_backup.tar.gz /home/carla/
This example will archive, compress, and verify everything in carla’s home directory, except /tmp. tar has many options for file selection which help allow for a finely-tuned backup.
Good Ole Split
What do you do when your backup is larger than a single CD? Split to the rescue. When the tarball is larger than a single CD-R/RW, use split to break it up into manageable chunks. Split needs to be told how big each chunk should be. A 650-megabyte CDR is about 681,000,000 bytes. (1,048,576 x 650).
Leave room for filesystem overhead, such as table of contents and other housekeeping. I like to leave a good margin, as cdrecord will warn when the data becomes too large but won’t stop, which of course creates errors.
# split -b 650000000 /backup/big_backup.tar.gz
Split names these files xaa, xab, xac, and so on. If it runs out of x-prefixes (more than 676) it starts a new series with zaa, zab, and so forth. If you’re trying to split a tarball over 676 CDs, I suggest looking for larger media. cd to the directory containing the tarball splits and convert each x file to .iso.
#for i in ‘ls xa*’; do echo -e “$i”; mkisofs -o $i.iso $i; done
This will take some time. When it has completed, burn each iso to disk.
To restore files, the split files must all be copied to a hard drive and concatenated. Then the newly-rebuilt big tarball needs to be unpacked. Call the file anything you want as long as it has the .tar.gz extension:
# cat /cdrom/xaa /cdrom/xab /cdrom/xac > /restore/wholefile.tar.gz
Once the original mondo tarball has been reassembled, extract files as needed. tar has a huge set of command options — here are a few of the more commonly used ones:
- -p — Preserve permissions when extracting files
- -P — Keep absolute file paths
- -S — Record sparse files efficiently
- -W — Verify archive after creation
- -T — Get names of files to backup from file
tar can also perform incremental backups. If multiple disks are needed for the full backup, it will mean having to periodically feed piles of disks to the computer and will also involve keeping them sorted and organized. If the full backup uses no more than 3 or 4 disks, and a week’s worth of incremental backups use a single disk, it is still manageable; anything beyond this becomes questionable in terms of efficiently managing the backups.
The advantages here are that tar and split are standard GNU/Linux utilities and CD writers are much less expensive — and faster — than tape drives. The downside is one bad bit ruins the entire set. However, even that is not fatal, as Linux has many tools for reading and recovering data. These days I primarily rely on CDs and extra hard drives for backups, rather than tape. Of course, hard drives and rsync are wonderful backup tools, but that’s a subject for another day.