rsync 101


2015-06-13 · 2 min read

rsync stands for remote sync, it’s an utility to synchronize (or copy) the files and directories from one location to another. rsync uses incremental « update » - an algorithm that minimizes the amount of data copied by only transfering the differeneces between sets of files across the network connection.

COPY/SYNC LOCALLY

rsync -azhP dir1/ dir2
  • -a option - it stands for « archive » i.e. it syncs recursively and preserves symbolic links, special and device files, modification times, group, owner, and permissions.
  • -z option - it adds compression
  • -P option - it combines the options --progress and --partial
  • -h option - it outputs numbers in a human-readable format

COPY/SYNC TO A REMOTE LOCATION

Pushing the contents of dir1 to a dir2 on a remote machine remote1.

rsync -a ~/dir1 remote1:dir2

Pulling the contents of dir2 on a remote machine remote1 to a local directory dir1.

rsync -a remote1:dir2 dir1

KEEP DIRECTORIES SYNCHRONIZED

In order to keep two directories synchronized, it's also necessary to delete files from the destination directory once they are removed from the source. This is not rsync default behaviour though, rsync does not delete anything from the destination directory. --delete option can be used to overwrite this and allow full directories synchronization.

rsync -a --delete source destination

OTHER OPTIONS

  • -n option - it reports the actions it would have taken but rsync does not transfer any files
  • --max-size option - it tells rsync not to transfer files that are greater than a specific size
  • --exclude option - it tells rsync to exclude specified files or directories (comma-separated list) located inside a directory being synchronized

SYNC OVER A SPECIFIC SSH PORT

rsync -azP -e 'ssh -p <port number> ' source destination