One of the great advantages of using a good GNU/Linux distribution is that there is lots of documentation readily available. The disadvantages are that the documentation is not always as up-to-date or complete as you would like or, as is the case with the cdrecord program, the documentation contains so much information that it is easy to get lost within it. It is to help overcome these disadvantages that articles such as this one exist. Nonetheless your first source of documentation should always be the manual pages. If you are somewhat unfamiliar with your Linux system and don't know what a manual page is then find yourself a command prompt and type: man cdrecord
If the manual pages have been installed properly then you will be presented with screenfuls of information concerning the cdrecord program.
This article was created after I established a small backup facility for a web server. The same principles could be used to perform backups to CD for any other GNU/Linux based computer.
Creating a backup copy of your hard disk is simple enough but there are a few details to take care of. First you will need to create a disk image file because that is what you will actually use to create the CD, rather than copying files directly from your hard disk. Within the disk image you want to include all the important files but avoid including certain parts of the file system such as /dev and /proc and /tmp which don't contain information that you would want to backup. The scripts provided below give you all the tools needed to perform these tasks. Before you begin, please note that the cdrecord program needs to be run as root.
You might think you know where your CD drive is ... just at the top of the box with a small flashing light and a retractable tray, no? Ah, but do you know where the cdrecord program thinks your CD drive is? In fact the cdrecord program quite likely won't know where your CD drive is until you tell it and it certainly won't be interested to know about the flashing light or tray.
The cdrecord program wants to know about bus numbers and device numbers and something called a lun which, as far as I could work out, is an acronym for “logical unit number”; I'd be happy to tell you more about a lun if I knew anything else to tell you. Since I wasn't clever enough to know how to obtain these numbers automatically I had to find them by trial and error. It turned out that on our web server the CD writer is known to the cdrecord program as:
This information roughly translates to inform us that the CD writer is an IDE device on bus 0, that it is device number 1 on the bus, and has lun number 0. In turn this implies that the CD writer will also be known to the system as /dev/hdb since /dev/hdb also corresponds to device number 1 on bus number 0. Or, in other words, the CD writer is the primary slave IDE device.
Finding this information was the hard part. Once it was known everything else was quite straightforward. If your CD writer is connected somewhere else (such as the secondary master) then you could try using the following table to guess what the correct device parameters for cdrecord will be:
Connection of IDE CD writer
Location in Linux file system
If you have SCSI devices rather than IDE ones, try leaving out the “ATAPI:” and just use the three comma separated numbers, for example use dev=0,1,0 instead of using dev=ATAPI:0,1,0.
For convenience I created a new directory called /Backups and then, within it, another directory called Images, so I then had /Backups and /Backups/Images. Into the /Backups directory I placed the various script files shown below.
Below is the listing of the “/Backups/MakeImage” file that creates the disk image. The options used for the mkisofs command allow the CD to contain the file names that don't comply strictly with the ISO9660 specification. For your GNU/Linux system this is almost certainly necessary.
#! /bin/sh # This script is called "MakeWeekly" because it is intended to # create an image file for a weekly backup. Feel free to give # your file a name appropriate to your purpose! rm /Backups/Images/Weekly.iso du / > /Backups/List_of_files_on_disk.txt mkisofs -U -R -l -x /dev \ -x /Backups/Images -x /tmp -x /proc -x /mnt -x /media \ -no-bak -o /Backups/Images/Weekly.iso /
The above script first deletes any existing disk image file. Then it creates a list of all of the files on the disk and stores this list in a file in the backups directory; this list file will also be included in the backup. Finally the above script creates a new disk image file called “Weekly.iso” and places into the image every file on the disk except backup files, those files in the /dev, /tmp, /proc, /mnt and /media directories, and the image file itself (to avoid recursion).
Below is the listing of the file named “/Backups/DiskEject”. This simply opens the CD tray.
#! /bin/sh # This script is called "DiskEject" and will # cause the CD tray to open. cdrecord -eject dev=ATAPI:0,1,0 > /dev/null
Below is the listing of the file named “/Backups/DiskErase”. This script will perform a quick erase of a re-writeable CD. The hdparm command is used to enable DMA for the drive. After the CD is erased the disk is ejected. For some CD drives the ejection is essential and if it is not performed the following CD write operation will fail.
#! /Bin/sh # This script is called "DiskErase" and will # cause the table of contents on the CD to be # wiped clean. If you need to erase the disk # fully you can use the parameter: # -blank=all # instead of -blank=fast but this will take # much longer. hdparm d 1 /dev/hdb cdrecord -blank=fast -gracetime=5 dev=ATAPI:0,1,0 cdrecord -eject dev=ATAPI:0,1,0
The following is the listing of the file named “/Backups/DiskBurn”. The hdparm command is used to ensure that DMA is enabled for the CD writer. The cdrecord command is then told to be verbose while writing the image file in “disk at once” mode. At the end of the write operation the disk is ejected, partly for your convenience and partly, as noted earlier, to ensure that the following CD write operation doesn't fail spuriously.
#! /Bin/sh # This script is called "DiskBurn" and will # cause the image file to be written to the CD. hdparm d 1 /dev/hdb cdrecord -v -dao -data driveropts=burnfree \ dev=ATAPI:0,1,0 /Backups/Images/Weekly.iso cdrecord -eject dev=ATAPI:0,1,0
When the DiskWrite script completes you should have a CD with your system's files safely contained upon it. Naturally it would be a good idea to mount the CD and then check that it contains everything you expect. This can be accomplished by pushing the CD back into the computer and then mounting it into the file system with a command similar to one of the following:
mount /dev/hdb or mount /dev/cdrecorder
Then browse around the CD using your favourite file browsing software. If you haven't got any other preference then the midnight commander is a reasonable choice for file browsing and is installed by default on SuSE Linux 9.2. The midnight commander is started with the command mc. When you are ready to extract the CD you should un-mount it with a command similar to one of these:
umount /dev/hdb or umount /dev/cdrecorder
You cannot unmount a device that is in use which means you will not be able to unmount the CD if your current directory is somewhere within the CD directory structure so you might need to change to another a directory somewhere on your hard disk before you can unmount the CD and extract it. Also, in the above mount and umount commands you need to replace /dev/hdb with whatever is appropriate on your computer system.