System Backup

Mike Chavoustie [chavoumd |at| clarkson.edu]
Clarkson Open Source Institute

Why do we care?

Backing up your system is an important part of computer maintenance. On a personal computer, one puts alot of time into setting up the system to be configured just the right way. In the server world, the day-to-day use of customers and users depends on the reliability and stability of the system. Should something go wrong, you as a system administrator don't want to lose anything you've already spent your time setting up.

There are many key utilities that one needs to be familiar with in order to be effective in this venture. Several will be outlined below, but this is by no measure an exhaustive list.

THE Command

tar is a simple command that is very similar to zip or rar. It is used to bundle files together into one archive to make for easy transfer and storage. This is the bread and butter of backup.

Syntax: Options:

In a system backup setting, you want to make sure to use options cpf. This will create an archive in a file, while maintaining the permissions already set to the individual files being added. This makes things easier in restoring, as all the file permissions will already be correct.

Example:

That line will create a new backup called stuff.tar, backing up /home/me/docs and /home/me/pics with all the permissions intact.

Example:

This line will extract stuff.tar preserving all permissions into the current directory, denoted by the ..

One thing to be careful of are the z and j options. They will shrink the size of your backed up tarball, but if one bit of the file gets corrupted, then you lose the entire backup. When making important backups, it is a good idea not to use these options.

Databases, anyone?

Now that we know how to backup our data, we need to know what to backup. In many server applications, databases are used that contain sensitive data that needs to be saved in the case of a crash. Many websites now use mysql or postgres databases to store information. The database is already stored as a file, but it is usually most useful to use the utilities that the database system provides. For this tutorial, we will demonstrate using mysql.

mysqldump is the utitlity provided with any stock mysql installation. You are able to use it to 'dump' and backup the tables and information in your databases.

Syntax: Options:

All of the aforementioned options are helpful when backing up your databses in any backup environment.

Example:

This will backup all databases, complete with insert statements and all mysql add options into the file mystuff.sql. The file redirection (>) is necessary because mysqldump defaults its output to standard out, aka. your screen.

If we wanted to reinsert our backed up databases from mystuff.sql, the command would look something like this:

Example:

That command would insert all of the databses stored in mystuff.sql into our current mysql instance.

Look Ma, No Hands!

Administrators are by their very nature lazy. You don't want to have to spend the majority of your time doing all of your backups by hand. Automation is very easy on a linux system, and now we'll discuss ways to make your machine back itself up.

crontab is a method by which your computer can schedule different tasks to be run. The crontab stores the time and date that a particular program should be executed. The crontab file is usually located in /etc/crontab. In order to edit the current crontab, use the following:

Syntax: Options: Example:

The line above will edit the user backup's crontab. This will open up the default command line editor and allow you to edit the file. The structure of a crontab file is below.

Syntax: Options:

Using * tells crontab to use all values for that field (i.e. * in the day field means every day). Lists (0,1,4,5) and ranges (0-4,6-23) can be used as well. Another helpful tool is called stepping. Using /, you can divide time into sections. Instead of using 0,15,30,45, you can use */15.

Example:

This will run the command /home/clarkbw/public_html/pcosi/rawdog-1.8/rawdog -wu as the user clarkbw every 30 minutes, every day.

If you want to be notified of when a particular cronjob is run, you can insert these lines to have the system email you when it is completed.

Example:

Each time the crontab is run, an email will be sent to backups@cosi.clarkson.edu, only if there is any output produced by the command.

Dude, Where's My Car?

Often times you don't want to save a backup of your computer on the same computer. If the hard drive dies, you're in the soup even if you have a backup stored. If you want to securely transfer your backup file to another computer, you can use the scp command.

Syntax: Options: Example:

This line will log into mycomp.com as user me and copy /home/me/cool.avi to my current directory. In order to use scp, the remote machine needs to have ssh enabled. You will be prompted for username's password using this command.

Lots of Birds with One Stone

Using all of the commands we have gone over, we can do a pretty good job of backing up a system. However, the crontab only takes one line as a command to run. What if we wanted to bundle all these commands into one file? You can, using shell scripting. For this example, we will be using bash, a very standard and popular shell used in *nix environments.

Scripting using bash is very easy. You simply place all the commands you want to run in order, as you would type them in a command prompt, into a text file. Then, running the file will run those commands in order doing exactly as you typed.

Example:

#!/bin/bash

mysqldump --all-databases --complete-insert --all > mystuff.sql
tar -cvf mybackup.tar mystuff.sql /home/me/stuff/
scp mybackup.tar me@othercomp.com:/home/me

This simple script will backup my mysql database, tar up my database and my stuff directory, and the copy that file to my other computer. The #!/bin/bash is necessary at the top of the file in order for the shell to understand the instructions.

In order to run the script above, it needs to have execute permissions. The following line will accomplish this. (Assume the file is named backup.sh.)

Example:

Then, the following line will run the script.

Example:

Presto!

You're now pretty well equipped to do a backup of your current linux system. Make sure to use man [command name] for any of the commands decribed above if you're looking for more options available. Special thanks to Kyle Smith for some quick cheatsheets to make this easier.