Understanding rsync

  • rsync – Remote synchronization
  • Remote/Local file synchronization tool
  • Uses the algorithm that minimizes the amount of data copied by moving portions of files that have changed, rather than all files.

rsync with Local System

Example 1) Copy contents of dir1 to dir2

$ rsync -r dir1/ dir2

-r stands for recursive. It will copy contents of dir1 to dir2

NOTE: If there is no “/” after the directory, then it will copy directory, together with its files, into the dir2 (dir2/dir1/content)

Example 2) Copy the contents of dir1 to dir2 (include file’s metadata)

$ rsync -a dir1/ dir2

-a stands for archive. It will copy contents of dir1 to dir2, preserve symbolic links, special and device files, modification times, group, owner, permissions.

Example 3) See what the command would do

$ rsync -anv dir1/ dir2

sending incremental file list

-n flag shoots the blank, it does not do anything, just shows what it would do when executed.

Rsync with Remote System

  • Rsync uses SSH tunnel to transfer files to a remote machine. We must have public/private keys set in order to use rsync. In fact, rsync would attempt to login via SSH by itself. If rsync does not make through, it will ask for SSH login password (if enabled).

Example 1) Send directory from local machine to the remote machine

$ rsync -a /local/path username@remote_host:/remote/path 

Example 2) Send a directory from a remote machine to the local machine

$ rsync -a username@remote_host:/remote/path /local/path 

Rsync useful options

The -z flag will compress files to reduce network transfer.

$ rsync -az /source/path /destination/path 

The -P flag will show progress bar of completion and it allows to resume interrupted transfers (for large files)

$ rsync -azP /source/path /destination/path 

The –delete switch will delete anything in destination folder, and then do the rsync command

$ rsync -a --delete /source/path /destination/path 

Exclude files located in a directory we are syncing

$ rsync   -az   --exclude=regex_files_to_exclude   /source/path   /destination/path 

Leave a Reply