Backup your data with Rsync

September 25, 2005
This is a short guide to using rsync tool to efficiently backup your data.

What is Rsync ?

Rsync is a fast, highly versatile file copying tool.

You can use rsync to copy your data (files and directories) in following situations -

  • Locally - To a different location on the same computer.
  • Between two computers over any remote shell (SSH or RSH).
  • To/from a remote rsync daemon via TCP.

Rsync is widely used for backups and mirroring, and as an improved copy command for everyday use.

It is installed by default on almost all Linux distributions.

You can use rsync to take incremental backup of your data.

Rsync Usage

When using rsync, you need to specify a source and a destination. In its simplest form, the syntax for rsync is -

$ rsync [computer-name-1:]source-dir [computer-name-2:]destination-dir

Here computer-name-1 and computer-name-2 are names of the host and destination computers. If the host and destination are the same computer, you can omit this part.

The first time you run rsync, it will do a full backup of the files and directories. However, the beauty of rsync is, the next time you run the command, it only copies the files and directories that have changed since the previous run. This is called a differential backup.

The following are a couple of examples of using rsync that copies data.

Source folder - /media/documents

Destination folder - /home/backup

remote computer -

remote computer user - ravi

Copy all the data from source to destination

$ rsync -av /media/documents /home/backup

-a - indicates the data is transfered in "archive" mode. This preserves the symbolic links, devices, attributes, permissions, ownerships and so on of the files while transfering them. This is the same as using -rlptgoD.

-v - stands for verbose. It prints on the screen a running commentary of what rsync is doing.

Compress the data to reduce bandwidth usage

$ rsync -avz /media/documents /home/backup

-z - Using this option will reduce the bandwidth used for data transfer by compressing the data prior to its transfer.

Synchronize the data

$ rsync -avz --delete /media/documents /home/backup

--delete - This option ensures that any directory or file that is deleted at the source will be deleted in the destination as well. In other words, it asks rsync to synchronize data between source and destination.

Copy/Sync to a remote location using SSH

$ rsync -avz --delete -e ssh /media/documents

When transfering files to/from a remote computer, it is always advisable to use SSH.

Exclude certain files from backup

Suppose I want to exclude all the hidden folders and hidden files from being backed up, I can do so using --exclude-from=FILE option.

$ rsync -avz --delete --exclude-from=/home/ravi/files-excluded.txt' /media/documents /home/backup

Here the FILE (/home/ravi/files-excluded.txt) contain a list of files or folders (one per line) that rsync should exclude from backup. This can include exclude patterns also.

Blank lines in the file and lines starting with ’;’ or ’#’ are ignored.

In this example, the file (files-excluded.txt) contain two lines -

# File - /home/ravi/files-excluded.txt

How to take incremental backup

To take incremental backup using Rsync, you need to use 3 additional options. They are -

--backup - With this option, preexisting destination files are renamed as each file is transferred or deleted.

--backup-dir=DIR - This option tells rsync to store all backups in the specified directory on the receiving side. And finally,

--suffix=SUFIX - This option allows you to override the default backup suffix used with your own. For example, you can have the current date appended to the file names as the sufix.

Check out the following example of doing an incremental backup of files.

$ rsync -avz --backup --backup-dir=/home/backup-inc --suffix=`date +"%F"` /media/documents /home/backup


Easy automated snapshot style Backups with Linux and Rsync
Automatic backups with Rsync and Anacron