Privacy Policy
Snippets index

  rsync usage examples

Common rsync options:

  • a = archive - means it preserves permissions (owners, groups), times, symbolic links, and devices; also implies recursive
  • v = verbose - means that it prints on the screen what is being copied
  • r = recursive - means it copies directories and sub directories
  • z = compress - compress file data during the transfer
  • P = equivalent to --partial --progress
  • --progress: show progress during transfer
  • --partial: keep partially transferred files
  • --delete: delete extraneous files from dest dirs

Example:

rsync -avz foo:src/bar/ /data/tmp

A trailing slash on the source changes rsync behavior to avoid creating an additional directory level at the destination.

You can think of a trailing / on a source as meaning "copy the contents of this directory" as opposed to "copy the directory by name", but in both cases the attributes of the containing directory are transferred to the containing directory on the destination.

In other words, each of the following commands copies the files in the same way, including their setting of the attributes of /dest/foo:

rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo

Using Rsync filter to include/exclude files

  • use --filter="- pattern" to exclude files
  • use --filter="+ pattern" to include files

Example: retrieve only filenames matching "2019-08-01*":

rsync -a --progress --filter="+ 2019-08-01*" --filter="- *" host:/media/sf_Backups/ .

rsync over ssh simplest example

Copy remote "dir" folder and all subfolders under local "mydir":

$ rsync -avz -e ssh remoteuser@remotehost:/remote/dir /this/mydir

or (same as before):

$ rsync -avz -e ssh remoteuser@remotehost:/remote/dir /this/mydir/

Copy remote "dir" content into target local folder "mydir":

$ rsync -avz -e ssh remoteuser@remotehost:/remote/dir/ /this/mydir/

example which uses a specific port:

$ rsync -rvz -e 'ssh -p 2222' --progress --remove-sent-files ./dir user@host/path

Sync remote folder "test" into local folder "test":

$ rsync -e ssh --delete --progress --partial -avh username@remotehost.com:/home/username/public/media/test/ ./test/

Faster rsync transfer

Reduce encryption with the following option:

rsync -e "ssh -c arcfour" ...

Copy a whole Ubuntu server for backup

rsync -avh / storage1:/home/backups/hostname/ --progress --partial --filter="- /proc" --filter="- /sys" --filter="- /root/.cache" --filter="- ___BIG_FILE_CAN_SAFELY_BE_REMOVED" --filter="- swapfile

Look ma !

sudo rsync -avz --chmod=u+w,og-w -e 'ssh -p 24970 -i /home/master/.ssh/id_rsa' --rsync-path="sudo -u gaia rsync" morlandi@www1.brainstorm.it:/home/gaia/public/media/inspinia /home/gaia/public/media

So many thing happen here:

sudo
we're acting as "master" user, but the target local folder "/home/gaia/public/media" has another owner, so we need "sudo"
rsync -avz
rsync with "archive" and "compress" options
--chmod=u+w,og-w
set the file mask for copied files and folders
-e 'ssh -p 24970 -i /home/master/.ssh/id_rsa'
since target host uses a non-standard port for SSH access, we specify the remote shell to use; we also need to select a specific identity file to match user's "authorized_keys" on remote server
--rsync-path="sudo -u gaia rsync"
we do this since the owner of the source folder is user "gaia", but we're connecting to the remote server with "morlandi" credentials (a sudoer on the remote server); we couldn't use root directly due to "PermitRootLogin=no" in SSH settings
morlandi@www1.brainstorm.it:/home/gaia/public/media/inspinia
the remote source
/home/gaia/public/media
the local destination