Programming

rsync

What is rsync?

Andrew Tridgell and Paul Mackerras wrote rsync. The first open talk was in 1996. The command compares two folders. It skips a file that did not change. For a file that did change, it can send only the different part. That is why the second copy of a large tree is shorter than the first. When it goes to a far machine, SSH often carries the path. rsync is not SSH itself. It only carries the file over it. The slash at the end of the line changes the meaning. A slash after the folder name copies what is inside. Leaving it off also takes the folder itself.

What is rsync used for?

rsync is for a backup and for keeping two folders the same. It is so you do not copy the whole disk every time.

  • Taking a site's files to another server
  • Writing only what changed into the backup each night
  • Comparing two folders
  • Continuing a large file's half-finished copy

What can you do with rsync?

With rsync you keep a folder close to another place. It does not run the program inside the file. It only carries the bytes. --delete removes the extra file on the far side. Running that on the wrong folder takes the files there away. Look first with the dry-run flag. If a password is written inside the command, it can stay in the history.

Is rsync hard to learn?

Copying one folder is easy. The difficulty is the slash and the delete. Whether the slash is at the end or not writes to another place. When --delete runs on the wrong target, bringing it back is hard. If the far machine's path or user is wrong, nothing goes. A clock difference sometimes counts a fresh file as old. That is why the first look is without delete and with a dry run.

Advantages and limits

Advantage: the second copy is short, a file that did not change does not come again, and it can go to a far server. Limit: a wrong delete is hard to undo. rsync is not cp and it is not SSH.

Basic ideas

The source is where the file comes from. The target is where it goes. The difference is the part that is not already the same.

  • A file that did not change is skipped
  • A slash changes the folder's meaning
  • --dry-run only shows
  • --delete removes the extra file on the target

A short example

This command keeps the inside of one folder close to another folder. It does not delete. It only writes to the path you show.

rsync -a source/ backup/

Common questions

  • Are rsync and cp the same? No. cp copies the file as it is. rsync tries to skip what did not change and to send the different part.
  • Are rsync and SSH the same? No. SSH enters a far machine. rsync carries the file, and SSH can carry the path.
  • Does the slash matter? Yes. source/ takes what is inside. source also takes the folder itself.