Skip to content

Instantly share code, notes, and snippets.

@NguyenTrungTin
Last active October 29, 2018 08:28
Show Gist options
  • Save NguyenTrungTin/aaee9135e88e68d3aaa6bc1fddeff182 to your computer and use it in GitHub Desktop.
Save NguyenTrungTin/aaee9135e88e68d3aaa6bc1fddeff182 to your computer and use it in GitHub Desktop.
rsync - Backup/Copy

Copy all files that have changed

  1. Option 1: Don't include hidden file
rsync -r --exclude=".*" source/ destination/
  1. Option 2: Include hidden file
rsync -avz source/ destination/

Note the slashes at the end of both paths. Any other syntax may lead to unexpected results!


The advantages of rsync are:

  • After the initial sync, it will then copy only the files that have changed.

  • You can use it over a network, convenient for files in $HOME, especially config files.

How to rsync a single file?

Same server:

rsync -avz /var/www/public_html/source/.htaccess /var/www/public_html/dest/

Different Server:

rsync -avz /var/www/public_html/.htaccess root@<remote-ip>:/var/www/public_html/
  1. How to use rsync to backup a directory without git subdirectory
rsync -avz --exclude='.git/' source/ destination/
  1. Copying files using rsync from remote server to local machine:

From your local machine:

rsync -chavzP --stats user@remote.host:/path/to/copy /path/to/local/storage

From your local machine with a non standard ssh port:

rsync -chavzP -e "ssh -p $portNumber" user@remote.host:/path/to/copy /local/path

Or from the remote host, assuming you really want to work this way and your local machine is listening on SSH:

rsync -chavzP --stats /path/to/copy user@host.remoted.from:/path/to/local/storage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment