linux

Rsync helpers and common usage

Reading time of 425 words
2 minutes
Reading time of 425 words ~ 2 minutes


If you find this content useful, consider buying me a coffee
Or help me out by engaging with any advertisers that you find interesting

rsync is a widely used tool for synchronisation so to keep copies of a file on multiple computers the same. Because of its flexibility it has become the defacto standard on Linux and other similar systems. While newer protocols and tools such as Dropbox and BitTorrent Sync overlap with and improve on some of rsync’s capability. Rsync is still relevant today both for syncing and for other function such as local directory duplication and large file copying.

Rsync

Common arguments

-v verbose

-h humanise the output by implementing binary multipliers instead of byte values etc.

-P display progress and allow resumption of interrupted transfers

-n do a dry run for testing

--stats display statistics on the file transfer (useful to combine with -n)

-r recursive to include all sub-directories and their content

--remove-source-files move files instead of copying

-a archive mode which preserves permissions, symlinks, etc.

-z compress the data over transfer, the following file types will not be compressed 7z ace avi bz2 deb gpg gz iso jpeg jpg lz lzma lzo mov mp3 mp4 ogg png rar rpm rzip tbz tgz tlz txz xz z zip

-skip-compress=gz/jpg/mp[34]/7z/bz2 will compress all data over transfer except those file that match these extensions, mp[34] is short for mp3/mp4

--exclude='/directory' --exclude='file' --exclude='*pattern*' exclude directory names, filenames or pattern matches

--include='/directory' --include='file' --include='*pattern*' include directory names, filenames or pattern matches

--max-size='50M' only copy files less than 50 MB

--bwlimit=50 cap bandwidth usage to 50 KiB

--timeout=60 permit a transfer timeout of 60 seconds before rsync will exit

-u update mode which skips all files on the destination that are newer than the source

Copy a large, local file

rsync -hvP archive.tar.gz /directory/to/store/copy

Copy a local directory and its content

rsync -hvrP /directory/to/copy/* /directory/to/store/copy

Copy a local directory to a remote server via SSH.

-e ssh use a secure shell (SSH) connection

rsync -ahvzPe ssh /directory/to/copy/* [email protected]:/directory/to/store/copy

Or switch to copy a remote directory locally.

rsync -ahvzPe ssh [email protected]:/directory/to/store/copy /directory/to/copy/*

Copy the source code of website

The --includes and --excludes arguments must be in the correct order

--include="*/" includes all subdirectories

--include="*.html" --include="*.css" --include="*.js" --include="*.php" includes all files matching these file extensions

--exclude="*" exclude all other files that don’t match the previous includes

-m prunes empty directories from the copy

rsync -avhPrm --include="*/" --include="*.html" --include="*.css" --include="*.js" --include="*.php"  --exclude="*" /var/www/* /var/backups/www.src/

Copy a website with some exceptions

-R use relative path names for --includes and --excludes

rsync -avhPrR --exclude=".*" --exclude="/downloads" --exclude="/setup" /var/www/* /var/backups/www.src/

Learn more.

Written by Ben Garrett

If you find this content useful, consider buying me a coffee
Or help me out by engaging with any advertisers that you find interesting