Linux rsync Command: Copy and Back Up

rsync copies files and folders, locally or to another computer. After the first copy it only sends what has changed, which makes it ideal for backups and for keeping folders in sync.

Copying folders

rsync -av documents/ backup/
Copies the contents of documents into backup. -a keeps permissions and dates, and -v lists each file.
rsync -av documents backup/
Without the slash after documents, the folder itself is copied, giving backup/documents.
rsync -av --progress bigfile.iso backup/
Shows the progress of each file, which is useful for large files.

Watch the trailing slash. documents/ means "the contents of documents". documents without a slash means "the documents folder itself".

Mirroring and dry runs

rsync -avn --delete documents/ backup/
A dry run (-n): shows what would be copied and deleted without changing anything.
rsync -av --delete documents/ backup/
Makes backup an exact copy of documents, deleting files from backup that are no longer in documents.
rsync -av --exclude="*.tmp" --exclude="cache/" website/ backup/
Leaves out temporary files and the cache folder.

Always do a dry run with --delete. If you mix up the source and destination, --delete removes the wrong files.

Copying to another computer

rsync uses ssh to copy to and from other computers.

rsync -avz documents/ [email protected]:/backup/documents/
Copies to a server. -z compresses the data on the way, which helps on slow connections.
rsync -avz [email protected]:/var/www/ website-copy/
Copies from a server to this computer.

Quick reference

OptionMeaning
-aArchive: keep permissions, dates and links
-vList each file
-zCompress during transfer
-nDry run: show what would happen
--deleteRemove files not in the source
--excludeLeave out matching files
--progressShow progress for each file