CD backup of hard disk

Using cdrecord to backup a server hard disk to CD

If a server fails for whatever reason you will likely want to get it up and running again as fast as possible. However if you didn't keep a copy of all the configuration files then resurrecting your server might be easier said than done. This article describes the process that was used to create a backup of a web server that was running SuSE Linux 9.2 and that was equipped with a single IDE hard disk..
Content revision history:
Article first written: 14th February 2005
Updated:
Corrected:

Preamble

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.

General description of what is to happen

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.

Finding your CD drive

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:

dev=ATAPI:0,1,0

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

dev=

Primary master /dev/hda ATAPI:0,0,0
Primary slave /dev/hdb ATAPI:0,1,0
Secondary master /dev/hdc ATAPI:1,0,0
Secondary slave /dev/hdd ATAPI:1,1,0

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.

Directories and files created

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

What to do?

  1. Create the directories as described and copy the script files into them.
  2. Use trial and error or some cleverer method to decide where your CD drive is located. Then modify the scripts as necessary. Remember to change both the dev= parameter for the cdrecord command and also the /dev/xxx parameter for the hdparm command. Also edit the MakeImage script to exclude or include particular directories on your system. If you have a fairly standard SuSE 9.2 installation then you might be able to leave it as it is.
  3. Execute the MakeImage script to create a disk image.
  4. Pop a CD into the drive.
  5. If the CD is not blank, execute the DiskErase script.
  6. Execute the DiskWrite script.
After the burn, take a good look at the data and see that it has copied correctly. This also might be a good moment to catch up on personal hygiene with a quick face scrub.

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.

End of article

 

Navigation: (site map) learn linux home pagetechnical articles