• Aucun résultat trouvé

Backing Up Your System

Dans le document Setting Up LAMP: (Page 123-127)

There are literally hundreds of methods for backing up your filesystem. You can find many of them located on the Web, including free backup scripts for you to download and use that will provide an abundance of choices for your system. Each method will use different libraries that you might be required to download and install. You can even find complete GUI backup sys-tems and schedulers for the X Window interface as well.

4337Book.fm Page 101 Saturday, June 19, 2004 5:24 PM

This section will cover the actual creation of the backup files in a tarball. From there you can choose to copy that tarball onto your choice of media. Multiple books have been written on backup systems for Linux, and if you require an extensive recovery system, we recommend that you purchase a book to provide you with an extended knowledge of backup systems. This sec-tion is intended to give you only a basic understanding of why you need a solid backup system in place and how to set up a simple program.

Remember that however you choose to back up your data, you need to test, test, and retest your methodologies before applying them. You should use a test system to run your scripts and scheduled jobs and then check the media to make sure all the intended files have been success-fully backed up. Without testing, you might think you have the security of redundancy but in reality you could be left empty-handed if disaster strikes, which of course will be when you are least prepared and least expect it.

It is a good idea to automate your backup systems with a series of complete backups and incremental backups. A complete backup stores all the system information for a complete recov-ery, whereas an incremental backup stores only the information that has changed since the last backup. It is highly recommended that your incremental backup store all changes since your last complete backup so that you do not need to restore by using four or five backups. Instead you would restore by using only your last good complete backup—and if it has been more than 24 hours since the complete backup, you would use the most recent incremental backup.

To automate your backup system, you can choose to use the crontab file located in the /etc directory. This file contains information on scripts, processes, and commands that are run on a schedule. You system will automatically parse this file looking for any entries that need to be run and will fire off the appropriate command. It is usually standard practice to create (or download) your own backup script and have cron fire off that one script. This will prevent your crontab file from becoming messy with too many lines of code. Your command line for backing up files can become rather large before you know it. For now, let’s cd into your home directory and deposit the scripts you are about to create there. First, create a repository for your backup files as tem-porary storage. Move to the root directory and type the following:

mkdir backups

You will now have a new directory named backups. With that done, let’s look at the tar com-mand you’ll use for backing up your system. Go ahead and use the comcom-mand:

pico full_backup

You will then be brought to a new black file. In this file you will place your tar command, which will look like this:

tar -zcvpf /backups/full_backup-`date '+%Y%m%d'`.tar.gz \ --directory / \

--exclude=*.iso \ --exclude=proc \

103

An analysis of the command will show that you will be creating a backup of the / directory with a name that starts with backup- and then contains the current date in a YYMMDD format and finishes off the filename with the appropriate .tar.gz extension, denoting it as a gzipped tarball. You will also notice the --exclude options. These enable you to specify files (for exam-ple, the .iso files) and directories that you do not wish to include in your backup. Finally, you will notice the last line contains a single dot. This tells tar to include all other files and direc-tories not expressly listed in your exclude arguments.

Now, if you were to keep executing this script every week, you would end up with a lot of backup files that could be taking up a ton of space. So you are going to add another line into your script that will delete any files older than 30 days. To do that, type the following:

find /backups/full_backup-* -mtime +30 -exec rm -rf {} \;

This tells the find command to run the rm -rf command for each file it finds in the /backups directory—in effect, wiping out your old backups. After this line, you might wish to add your commands for sending the completed file to a specific device such as a tape or CD-RW drive.

If you would like to send this file to another computer on your network as a backup server, you should create a script for the crontab on your backup server to reach each of your other servers that are being backed up. This is done for security reasons so that a malicious user who gains access to your web server would not have access to your backup server as well. Barring that, everything is set up properly.

After you have added your line to move your file to the appropriate device/location, go ahead and save and exit the file. Then open another new file with pico that will be responsible for cre-ating the incremental backups. Let’s call it incremental_backup. With that file open, let’s use the following for your script:

4337Book.fm Page 103 Saturday, June 19, 2004 5:24 PM

This will create a tarball that contains only files that are newer than the last full backup (on Sunday) as your incremental backup. Next you’ll use the same command as the full backup script to delete any older files, but this time you want to delete only incremental files that are older than one week. So use the following command:

find /backups/incremental_backup-* -mtime +14 -exec rm -rf {} \;

Here you see that the -mtime flag now has a +14 argument for the number of days and that you are now finding files that begin with incremental. These are the two areas that have changed for your new script. After that has been added, add the same command you used in your full backup script to transfer the directory or single file to your device or location of choice. With that fin-ished, you will be ready to add your cron job into the system to automatically run these scripts when required.

To run your newly created scripts automatically, you’ll be using the /etc/crontab file. You can edit the crontab file and have your changes automatically take effect by using the crontab -e command. If you are logged in as root, then you will be taken to the crontab file in vi. Once you exit and write the changes, the crontab will automatically make its changes. Try that now and take a look at the format. Here you have six parameters per line:

minute This is an integer value from 0 to 59 representing the time at which to run this script.

hour This is also an integer representing the hour at which to run this script or command.

It should be a value from 0 to 23.

dayOfMonth The dayOfMonth value should be from 1–31 for the day of the month the script or command specified should be run.

monthOfYear This is a value from 1–12 representing the month of the year.

dayOfWeek dayOfWeek should be a value from 0–7, which represents Sunday through Mon-day, with the 7 representing Sunday once again.

shellCommand At last, this indicates the full path to the script or command that should be run when the preceding requirements are met.

Using the preceding format, you can determine that the line for your full_backup script should read as follows:

0 2 * * 0 /root/full_backup

This will cause your full backup script to run weekly at 2:00 A.M. every Sunday. Notice that the asterisks denote a null value for that argument. For the incremental script, you will add a line to run the backup every night at 2:00 A.M. except for Sunday. To do this, let’s use the following:

0 2 * * 1-6 /root/incremental_backup

105

Linux Administration Checklist

Now your crontab is ready to be saved. After it is saved, you can exit. Congratulations—you have successfully set up a basic backup system for your Linux server.

Dans le document Setting Up LAMP: (Page 123-127)