Skip to content

Instantly share code, notes, and snippets.

@abuxton
Forked from bengarrett/rsync.md
Last active November 6, 2020 19:16
Show Gist options
  • Save abuxton/5b8489277bf65d684b0efe433469f740 to your computer and use it in GitHub Desktop.
Save abuxton/5b8489277bf65d684b0efe433469f740 to your computer and use it in GitHub Desktop.
Rsync - Helpers and common usage.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment