Backing Up Files with "rsync" (for Linux users)

"rsync" is a simple but powerful backup utility in Linux. Although the traditional archive tool "tar" can make a backup, "rsync" supports more functions, for example, saving the backup on a remote machine. For incremental backups, rsync provides a better method than "tar".

How to Use "rsync"

The basic usage is:

rsync -a src/ dest/

This command will copy all the files and directories in the "src" to the "dest" directory. In fact, it is equivalent to "cp -a src/ dest/". But if there has been many files in "dest" and there are only a few differences between "src" and "dest". "rsync" runs much faster. It makes "rsync" more suitable for creating incremental backups.

Notice: For most of software, the trailing slash after a directory name isn't important, but it is not for rsync. Let's see two example (suppose there is a file "foo" in the "src" directory):

rsync -a src dest

This will produce dest/src/foo. However, if I add the slash after the directories:

rsync -a src/ dest/

This will produce dest/foo. As you see, rsync doesn't care of the trailing slash after the destination directory. But it is different between "src" and "src/". So you must know about it.

Option: --delete

"rsync" can make a completely same backup from the source directory. However, if I used "rsync" before and just now I deleted a file from the source directory, "rsync -a src/ dest/" won't delete the file from the destination (backup) directory. The option "--delete" can solve the problem. For example:

rsync -a --delete src/ dest/

It will check all the files and directores in "dest" and if rsync finds a file or a directory in the "dest" which is not it the "src" directory, rsync will delete it. In this way, rsync keeps the "src" and the "dest" completely same.

Use "cron" to execute rsync on time

"cron" is a time-based utility for executing some commands automatically. It can run some programmes at a specific time. If you are lazy enough, you can add "rsync" into the cron configure file: /etc/crontab.

Use a text editor to open /etc/crontab and add the following line into it:

20 4 * * * root rsync -a --delete src/ dest/

When the time is 04:20, cron will run "rsync -a --delete src/ dest/" to make a backup and delete the unused files. By the way, "cron" is a very useful utility in Linux/Unix operating system. If you want to learn more, you can read its manual page.

Tags: