How to copy files and sync files between Linux or Mac computers with rsync

Written by
Date: 2011-02-20 16:30:35 00:00


Introduction

From Wikipedia:

rsync is a software application for Unix and Windows systems which synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate. An important feature of rsync not found in most similar programs/protocols is that the mirroring takes place with only one transmission in each direction. rsync can copy or display directory contents and copy files, optionally using compression and recursion.

In daemon mode, rsync listens on the default TCP port of 873, serving files in the native rsync protocol or via a remote shell such as RSH or SSH. In the latter case, the rsync client executable must be installed on both the local and the remote host.

I use rsync a lot specially to backup my important files between two computers, but also to keep my local slackware repository up to date.

rsync is great to keep folders and files synchronized between two computers or in the same computer, you can backup all your important data to an external disk using rsync.

rsync uses

Copy files with rsync

From local to remote computer

rsync --progress --partial -avz /folder/to/copy/ user@remote.server:/remote/folder

This will copy all files in /folder/to/copy/ to /remote/folder in the remote server, the folder copy itself will not be created in the remote computer, and only its contents will be copied, if you want the folder itself to also be created and then its contents copied inside the newly created copy folder, use this command.

rsync --progress --partial -avz /folder/to/copy user@remote.server:/remote/folder

Note the trailing slash after copy that makes the difference. The rest of options are:

  • progress: will show the percentage of the file copied
  • partial: tells rsync to keep the partial file which should make a subsequent transfer of the rest of the file much faster.
  • a: Archive, It is a quick way of saying you want recursion and want to preserve almost everything.
  • v: Verbose
  • z: Compress the files so less bandwidth is needed, and the files are copied faster.

From remote to local computer

rsync --progress --partial -avz user@remote.server:/folder/to/copy/ /local/folder

If you want to copy specific files, include it in the path.

rsync --progress --partial -v /path/to/file/file.ext user@remote.server:/remote/folder/

**From one folder to another folder, in the same local computer

rsync -av /source/folder /destination/folder

Synchronize two folders with rsync

If you want to keep two folders in sync rsync is also your best option.

From local to remote computer

rsync -avz --delete /source/folder user@remote.server:/destination/folder

From remote to local computer

rsync -avz --delete user@remote.server:/source/folder /destination/folder

From one folder to another on the same server

rsync -av --delete /source/folder /destination/folder

The last one is great to backup your files to an external drive, this what I usually do:

rsync -av --delete /home/guillermo/ /media/disk/backup-guillermo/

Then I can unplug the external disk, until the next rsync backup. The --delete option will delete the files in the destination folder, that have been deleted in the source folder, that way the two folders will always be in sync.

One last tip: If you need to limit bandwidth used from in a rsync backup connection use --bwlimit=KBPS and tell rsync how much bandwidth in Kbytes pers second can be used.