Выбрать главу

You will need sufficient disk space to hold the compressed image before it is written to the optical disc. 

Then use the -z option to growisofs :

# growisofs -Z /dev/dvd -RJz /tmp/zftree /system-*

Putting this all together into a script, and mailing the results to the email alias backup-alert , we get this:

#!/bin/bash

#

# backup-dvd :: backup selected directories to a compressed DVD

#

# List of the directories to be backed up

DIRLIST

= "

/etc /home "

# Create timestamp file

(

rm -f /system-*

touch /system-$(hostname)

# Make directory for compressed backup tree

rm -rf /tmp/zftree 2>/dev/null

mkdir /tmp/zftree

RESULT=0

for DIR in $DIRLIST

do

 mkzftree $DIR /tmp/zftree${DIR}

 RESULT=$(( $? + $RESULT ))

done

if [ "$RESULT" -eq 0 ]

then

 # Burn the DVD

 growisofs -Z /dev/dvd -RJz /tmp/zftree /system-*

 # Eject the disc

 eject

else

 echo "Skipping burn: file compression failed."

fi

# Delete the zftree

rm -rf /tmp/zftree 2>/dev/null

) 2>&1|mail -s "Backup Log $(hostname)" backup-alert

Edit the DIRLIST line so that it contains a list of the directories to be backed up, separated by spaces.

Save this file as /usr/local/bin/backup-dvd and then make it executable:

# chmod u+rx /usr/local/bin/backup-dvd

And be sure to create an email alias for the backup-alert user in the file /etc/aliases :

backup-alert: chris frank

To produce a backup, execute this script:

# backup-dvd  

But it's a better idea to configure the system to run this script automatically every night (see Lab 6.4, "Scheduling Tasks ").

6.3.1.10. Backing up to tape

To back up directories to tape, use the tape archiver ( tar ):

# tar -cf /dev/st0 /system-* /etc /home

tar: Removing leading \Q/' from member names

tar: Removing leading \Q/' from hard link targets

In this command, /dev/st0 is the first tape drive, and /etc and /home are the directories being backed up.

To perform a compressed backup, add the z (for gzip compression) or j (for bzip2 compression) option:

# tar -czf /dev/st0 /system-* /etc /home

tar: Removing leading \Q/' from member names

tar: Removing leading \Q/' from hard link targets

Here is a script that will perform a tape backup:

#!/bin/bash

#

# backup-tape :: backup selected directories to a compressed tape

#

# List of the directories to be backed up

DIRLIST="

/etc /home "

# Create timestamp file

(

rm -f /system-*

touch /system-$(hostname)

# Produce the tape

tar -czf /dev/st0 /system-* $DIRLIST

# Eject the tape if possible

mt -f /dev/st0 eject

) 2>&1|mail -s "Backup Log $(hostname)" backup-alert

Save this script as /usr/local/bin/backup-tape .

Like the backup-dvd script, this script will send an email report to the email alias backup-alert . To include a list of files in the email report, add the -v option to the tar command:

tar -cz v f /dev/st0 /system-* $DIRLIST

To produce a backup tape, run the script from the command line:

# backup-tape

It's best to run this script automatically every night (see Lab 6.4, "Scheduling Tasks ").

6.3.1.11. Restoring files from backups

When restoring from tape, it's a good idea to restore to a location other than the original file location to ensure that critical data is not accidentally overwritten. These commands will perform a full restore of a tape to the directory /tmp/restore :

# mkdir /tmp/restore

# cd /tmp/restore

# tar xvzf /dev/st0

To restore only certain files, specify the filenames as arguments to tar :

# tar xvzf /dev/st0 home/chris/

If the file specified is a directory, all of the files and subdirectories in that directory will be restored.

Restoring from disc is easy: just copy the files that you want to the location that you want. You can do this graphically, or you can restore all of the files on the disc:

# mkdir /tmp/restore

# cd /tmp/restore

# cp -r /media/CDROM/* .

6.3.1.12. Viewing the table of contents and verifying a backup

To verify that a tape backup is readable, use tar's t option to view a table of contents of the tape:

# tar tvzf /dev/st0

-rw-r--r-- root/root 0 2006-07-01 01:34:24 system-bluesky.fedorabook.com

drwxr-xr-x root/root 0 2005-09-23 15:01:38 etc/gconf/

drwxr-xr-x root/root 0 2005-03-02 11:59:15 etc/gconf/gconf.xml.mandatory/

drwxr-xr-x root/root 0 2005-08-29 00:53:34 etc/gconf/1/