E-Mail:
Get our new Windows 7 eBook (PDF) for $7 with 70+ Tips. Download Now!

The House is on Fire!

  • No Related Post

The House Is On Fire!
Crisis Management in Linux

Last week we began a series on managing crises in Linux. What to do when your system won’t boot, or you’ve had some non-catastrophic failure? We talked about the importance of the changelog to this crisis management plan, and began a discussion on the importance of backups, specifically creating backups with the tar command. Today, we’ll look at a clean way to add backups to CD-R using a combination of cron and shell scripting. We’ll follow this up tomorrow with an entry in /etc/crontab that will run the script without user intervention.

Here’s a great shell script that will write your critical system files to a CD-R. This script is a slight modification of a script written by noeld at rootprompt.org. When executed by crond, this script will tar and zip system files to a file in your home directory, create an iso image using both the system.tgz file and other complete directories, and will write all this critical data to disk.

First, let’s save and look at the script. Open your favorite text editor and create a file called CDBackup.sh in your home /bin directory:

[tony@server tony]$ cd ~/bin
[tony@server bin]$ vi CDBackup.sh

Now, add the following to the file:

#!/bin/sh

cd /

# Set the values that will be used to name the backup file
DATE=`/bin/date +%b-%d-%y`
HOST=`hostname`

# Introduce yourself to the user
echo “Starting Backup”
/bin/date
date >/home/tony/backup/$HOST-$DATE

# Create a tar file from the /etc/ and /usr/local/bin/ directories
# and write an index
tar -czvf /home/tony/backup/system.tgz \
    ./etc \
    ./usr/local/bin \
      >/home/tony/backup/system.txt

# Give the user some feedback - if they’re watching
echo “Building CD Image”
/bin/date

# Create the iso image (backup.iso) that will be written to the CD-R
# This writes the system.tgz just created plus all the files in the /home/ directory.
mkisofs -r -f -o /home/tony/backup/backup.iso \
    /home \
    /home/tony/backup/system.tgz \
    /home/tony/backup/$HOST-$DATE \
    /home/tony/backup/system.txt

# Talk to the user again, just to be polite
echo “Burning CD”
/bin/date

# Burn the CD-R using cdrecord
cdrecord -v speed=2 dev=0,1,0 -data /home/tony/backup/backup.iso

# Clean up nicely after yourself, removing the iso image
# after writing the disc.
rm /home/tony/backup/system.*

# Say goodbye to the user
echo “Done”
/bin/date
eject cdrom

Bear in mind that you’ll need to make some modifications to this script to suit your own system. For example, my backups are kept in the /home/tony/backup/ directory - yours aren’t likely to be. You’ll need to replace all the references to /home/tony/backup/ with the actual path for your backups. You may also need to change the “dev” parameter in the cdrecord command. To see the information specific to your CD-R, run the command cdrecord -scanbus as root.

Because this script looks only at the content of several directories rather than the creation or modification time of the files themselves, this backup method creates complete backups of those directories. This is as opposed to the incremental backup method we talked about last week. This script can also be modified accordingly to create incremental backups.

Tomorrow, we’ll add an entry to the crontab file to completely automate this CD-R backup routine. Automation makes it much more likely that you’ll have these important backups at your disposal when a Linux crisis arises.

What Do You Think?

 
35 queries / 0.425 seconds.