Doing a Simple Backup with rsync

Cheap hard disk space, fast networks, and some really neat new tools have given Linux users some nice backup alternatives to the old reliable removable media (such as tapes and CDs). To back up your personal data or the data from a small office computer, the examples in this section provide fairly simple ways of creating usable backups of your data.

To do this procedure, you need to have hard disk space on a computer that is at least slightly larger than the hard disk you are backing up. That hard disk space could be on:

  • A different partition — By backing up to a separate disk partition, you are protected in case the partition you are backing up becomes corrupted. However, you are not protected if your hard disk goes bad.

  • A different hard disk — Backing up to a separate hard disk can protect from a corrupted disk, but won’t help you if your computer is hit by lightning, a flood, or other acts of God.

  • A different computer — By backing up over the network, you can back up to another computer that is as far away from the souce of your data as makes you feel comfortable. You can back up to the computer down the hall or one across the country.

To do the actual backup, the procedure uses the rsync command. The rsync command is like a remote copy command (similar to rcp) on steroids. In essence, rsync lets you copy files from one location to another. However, it also has some nice extra features that let you:

  • Only transfer differences — If you transfer a file that was transferred during an earlier backup, rsync uses a checksum-search algorithm to determine the differences between the old file and the new one. Then it only sends the data needed to account for the differences between the two files.

  • Transfer data securely — rsync combines with ssh (or another remote shell) to encrypt the data, so it can travel securely across a network.

  • Maintain ownership — The transferred files can keep their same permissions, ownership, and group designations. (Because ownership is based on numeric UID and GID, matching user and group accounts must be set up between machines for the files to appear to be owned by the same users and groups after the files are copied.)

The following sections show examples of the rsync command at work.

Backing up files locally

The first example shows a simple backup of a user’s personal files. Here I’m copying the /home/chris directory (including all its files and subdirectories) to another directory on the local computer. That directory (/mnt/backup/homes) could be on a separate partition (see Chapter 2 for creating separate partitions), hard disk (see Chapter 10 to add a hard disk), or a remote NFS file system (see Chapter 18 to mount an NFS file system):

 # rsync -av /home/chris /mnt/backup/homes/  

Notice that there's no trailing slash after /home/chris (so it's not /home/chris/). Without that trailing slash, rsync will copy files from that directory to a target directory named chris (/mnt/backup/homes/chris). With a trailing slash, all files from /home/chris/ are copied directly to the homes directory (/mnt/backup/homes/).

In this example, the entire contents of the /home/chris directory structure are added to the /mnt/backup/homes/chris directory. All files, subdirectories, links, devices and other file types are copied. By using the archive option (-a), all ownership, permissions, and creation times are maintained on the copied files.

If /mnt/backup/homes is on a separate disk, you now have your entire /home/chris directory copied in two places on the same machine. If the /mnt/backup/homes directory is an NFS shared directory (with write permission on), the files are now backed up to another machine.

Since the example is a backup of my personal files that don’t change too often, after a few days of changes to the files, I might want to run the exact same command again:

 # rsync -av /home/chris /mnt/backup/homes/ 

This time any new files are copied to the target directory and the changes to any files I modified are applied to the original backup files. Any files I deleted from my home directory will still be in the target directory (rsync doesn’t remove deleted files unless you specifically tell it to). The result is, again, a complete copy of the /home/chris directory at the moment the rsync command is run, plus any files that have been deleted from any /home/chris directories.


If you want files that were deleted from the sending directory to be likewise deleted from the target directory, you can add the --delete option to rsync.

Backing up files remotely

While the previous example was a quick, informal backup method, with more critical data, you want to make sure that the data are being backed up to another computer and that the backup is done at regular intervals. This can be accomplished by using rsync in concert with ssh and cron.

Having ssh as the transport mechanism ensures that data will be encrypted when it is transferred. Also, because the ssh service (sshd) is enabled by default on many Fedora and Red Hat Linux systems, you need only a user name and password to the remote system to do the backup. (As long as you can use ssh to connect to the remote machine and rsync is installed remotely, you can use the rsync command to transfer files there.) Here’s an example:

 # rsync -azv -e ssh /home/chris duck:/mnt/backup/homes/  root@duck's password: ******* building file list ... done  

Here, I identify the remote computer (named duck in this case) by putting it before the remote directory name, separated with a colon. I use some different options as well. To the archive (-a) and verbose (-v) options, I add the -z option to compress the data (making it more efficient to transfer). I also use the -e ssh option to have rsync use an ssh remote shell to transfer data. The password prompt you see is the ssh login prompt.

You can repeat this command each time you want to back up your files. However, the more efficient way to do this is to set up this command to run as a cron job so that the backups happen automatically at set intervals.

To have rsync run automatically, you can’t have it prompt you for a password. To have the rsync command run without prompting for a password, follow this procedure:

  1. Set up ssh to do no-password logins for the user who is going to perform the backup (see the section “Using ssh, scp and sftp without passwords” in Chapter 14 for information on how to do this).

  2. Decide how often you want the backup to run. For example, if you want to run the rsync command once each day, as root user you could create a file called /etc/cron.daily/mybackup.

  3. Set permissions to be executable:

     # chmod 755 /etc/cron.daily/mybackup  
  4. Add the command line to the mybackup file that you want to use:

    rsync -azv -e ssh /home/chris chris@duck:/mnt/backup/homes/ 

Notice that I added the user name chris as the log-in user name on the remote computer (duck). For you, this will be the name of the person for which you set up a no-password login in step 1.

At this point, a backup will be done once each day to the machine specified.


With the simple backup command just shown, you can build on more complex features. In particular, you might want to think about building in a snapshot feature. Snapshots allow you to go back to a particular date and time to restore a backed-up file. Mike Rubel has an excellent procedure for doing rsync snapshots at his entitled "Easy Automated Snapshot-Style Backups with Linux and Rsync."

Red Hat Fedora Linux 3 Bible
Red Hat Fedora Linux 3 Bible
ISBN: 0764578723
EAN: 2147483647
Year: 2005
Pages: 286 © 2008-2017.
If you may any questions please contact us: