Section 8.1. Backups


8.1. Backups

Backing up the files on your servers is vitally importantbut, of course, you already know this. Since no-one actually likes performing backups, they need to be as easy as possible to do: ideally, they should occur automatically. Traditionally, Linux servers were backed up using a custom set of shell scripts that were created by the system administrator; everyone rolled their own private backup solution and tweaked it repeatedly for best results. Linux has come a long way since those days, and more automated tools are now available. However, it's still useful to be able to put together your own scripts for backing up a LAMP server, because this approach gives you a very fine degree of control.

8.1.1. Simple Backups

The simplest way of performing a backup is to write the files to a CD or DVD, and store the backup away. Your backup strategy can be as complex as you likeand will probably need to be fairly intricate if you're integrating your new LAMP server into a larger enterprise setupbut it's better to have a simple backup process that you actually carry out than a complex strategy that never gets off the ground. Below is a simple script that can be run on a regular basis to back up your selected files to a CD.[1] The first time it runs, you'll select the files that you want to back up; thereafter, running the script (daily) will simply back up those files to a new CD. The easier it is to create the backup, the more likely it is that you'll do it.

[1] This script is also available as part of this book's code archive.

simple-cd-backup.sh

 #!/bin/bash # Back up files to CD, simply. BACKUPDIR="$HOME/.simple-cd-backup/FilesToBackUp" ISO=/tmp/CD-Backup-$(date -Iseconds).iso DISCNAME="Backup data, $(date +%c)" if [ ! -f /proc/sys/dev/cdrom/info ]; then   zenity --title "Simple CD Backup" --error --error-text \       "Couldn't find a CD burner."   exit fi BURNERINDEX=$(grep "Can write CD-R:" /proc/sys/dev/cdrom/info | \     python -c "import sys; s=sys.stdin.readline().split(); \     print s.count('1') and s.index('1')-1") if [ $BURNERINDEX == '0' ]; then   zenity --title "Simple CD Backup" --error --error-text \       "Couldn't find a CD burner."   exit fi CDDEVICE=$(grep "drive name:" /proc/sys/dev/cdrom/info | \     python -c "import sys; s=sys.stdin.readline().split(); \     print s[$BURNERINDEX]") mkdir -p "$BACKUPDIR" >/dev/null 2>&1  nautilus --no-desktop --browser \     "$HOME/.simple-cd-backup/FilesToBackUp" zenity --title "Simple CD Backup" --question --question-text \     "Burn files linked in the directory?" if [ $? == 1 ]; then exit; fi mkisofs -f -l -o $ISO -A "$DISCNAME" "$BACKUPDIR" | \     zenity --progress-text="Creating backup" \     --title "Simple CD Backup" --progress --pulsate auto-close cdrecord -v speed=4 dev=/dev/$CDDEVICE $ISO | \     zenity  --progress-text="Burning CD" \     --title "Simple CD Backup" --progress --pulsate auto-close rm $ISO zenity --info --infotext "Backup burned to CD." 

Save this script as ~/Desktop/simple-cd-backup.sh, then set it to be executable: either right-click on the file and set the appropriate permissions, or enter chmod +x ~/Desktop/simple-cd-backup.sh at the command line.

You can double-click on this file to see the Run or Display? dialog depicted in Figure 8-1. Click Run to run the script as-is; click Display to open the file in a text editor.

Figure 8-1. The Run or Display? dialog.


If you run the script, it will create the directory ~/.simple-cd-backup/FilesToBackUp, open that directory in Nautilus, and, finally, pop up the confirmation window shown in Figure 8-2.

Figure 8-2. The windows displayed by the simple-cd-backup script.


Move the confirmation dialog to the side (but don't click Cancel) and create symlinks to the files that you want to back up into this directory. It's vital that you link files into the directory: don't copy them. Sometimes, you can right-click on a file to create a link, but this option won't be available if you don't have permission to write to that file's directory. In these cases, use the ln -s command. For example, if you want to back up the /etc directory, which contains all your system configuration, you'd type the following:

[kermit@swinetrek ~]$ ln -s /etc \ > ~/.simple-cd-backup/FilesToBackUp/etc [kermit@swinetrek ~]$ 

Alternately, if you have a three-button mouse , you could follow this procedure:

  • Double-click the Computer icon on the desktop, or select Places > Computer.

  • Double-click Filesystem, to show the root directory.

  • Use the middle button to drag the etc folder into the FilesToBackUp folder.

  • Click Link here on the menu.

Once you've established links to every file you intend to back up in the FilesToBackUp folder, close the FilesToBackUp window. Insert a blank CD and, where you see Burn files linked in the directory? in the dialog, click OK. The files you linked will be added to a CD image, and this image will be burned to the CD. Dialogs will appear, reporting the progress of these two steps, so you'll need to click OK when the process is complete.

The next time you run a backup (say, the following day), the links you created will still be there. This means that, to run a daily backup, you can simply click simple-cd-backup, then click OK at the Burn files linked in the directory? prompt: it will burn your files to the backup CD.

8.1.2.1. Some Explanation

Let me explain how the script works in a bit more detail. You don't need to know this information to use the script, but this is a good example of how a short shell script can act like a powerful application, requiring only a small coding effort. This explanation may also help you to debug the script if it doesn't quite work as intended on your system.

The bit of the script that actually does the work is the last couple of lines, which use mkisofs (an abbreviation of "make ISO filesystem") and cdrecord. The rest of the script focuses on setting up the backups in a user-friendly way.

First, simple-cd-backup.sh checks that the system has a CD burner by using a little Python to parse the file /proc/sys/dev/cdrom/info. This file lists all the CD-ROM drives on the system and provides an indication of the capabilities of each. On my machine, it looks like this:

 drive name:             hdc drive speed:            24 drive # of slots:       1 Can close tray:         1 Can open tray:          1 Can lock tray:          1 Can change speed:       1 Can select disk:        0 Can read multisession:  1 Can read MCN:           1 Reports media changed:  1 Can play audio:         1 Can write CD-R:         1 Can write CD-RW:        1 Can read DVD:           1 Can write DVD-R:        1 Can write DVD-RAM:      0 Can read MRW:           1 Can write MRW:          1 Can write RAM:          1 

The file above lists one CD drive, called "hdc," which can be addressed as /dev/hdc. The presence of more CD drives would add columns to the file. Our script uses a single line of Python to look into this file for the line "Can write CD-R" followed by a 1. If it doesn't find such a column, it displays an error (using zenity , which is discussed below) and exits.

The FilesToBackUp window is a Nautilus window that shows the directory ~/.simple-cd-backup/FilesToBackUp, which the script creates (if it doesn't already exist). To view it, run Nautilus directly using this command:

[kermit@swinetrek ~]$ nautilus --no-desktop --browser \ > "$HOME/.simple-cd-backup/FilesToBackUp"

The script makes extensive use of zenity , a command which is used to pop up various types of GNOME alert boxes and dialog windows from shell scripts . For example, the script uses the following code to display a dialog that contains OK and Cancel buttons:

[kermit@swinetrek ~]$ zenity --title "Simple CD Backup" \ > --question --question-text "Burn files linked in the directory?"

This displays the dialog shown in Figure 8-3.

Once you've confirmed that you'd like to burn the CD, the real work begins. The script uses mkisofs to create an ISO CD image that contains all the files linked in the directory. It does so using the following mkisofs command:

simple-cd-backup.sh (excerpt)

 mkisofs -f -l -o $ISO -A "$DISCNAME" "$BACKUPDIR" 

Figure 8-3. A question dialog displayed using zenity.


$ISO has previously been defined as the name under which the generated ISO file should be saved; $DISCNAME identifies a useful name for the disk (containing the date); $BACKUPDIR is the ~/.simple-cd-backup/FilesToBackUp directory. The options -f and -l control minor aspects of the way mkisofs works: -f means "follow symbolic links in the directory", and -l saves the files with full names (rather than abbreviated Windows 95 FILENA~1.EXT names).

Once the CD image has been created, the cdrecord command is used to burn it to the disc:

simple-cd-backup.sh (excerpt)

 cdrecord -v speed=4 dev=/dev/$CDDEVICE $ISO 

This simply burns the ISO image ($ISO) to the specified CDROM drive (/dev/$CDDEVICE, which we determined earlier) at four-speed (a low speed like this reduces the risk of errors on some drives).

Finally, the created ISO image is deleted with rm, and the script uses zenity again to inform you that it's finished.

8.1.2. Enterprise Backup Solutions

Linux is very well supplied with "enterprise level" backup tools. A description of how to create a backup strategy in a multi-operating-system, multi-server environment would constitute a book in itself, so here I'll simply provide some pointers for further reading.

If you're looking to implement an enterprise level backup strategy from scratch, you should investigate Bacula or AMANDA. Both these tools are capable of being the centerpiece of a full enterprise backup strategy, and both are open-source. A number of commercial tools that may already be in use in your enterprise environment are also available on Linux. Veritas Backup, a commonly used tool in industry, can run on and back up Linux servers as well as Windows; another powerful commercial tool is Arkeia.




Run Your Own Web Server Using Linux & Apache
Run Your Own Web Server Using Linux & Apache
ISBN: 0975240226
EAN: 2147483647
Year: 2006
Pages: 92

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net