Rsync: Sync Two Folders via SSH in Both Directions

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:04

Helpful in rsync is, that also the permissions- and owner information is transfered ("-a" parameter). The "--progress" parameter shows the progress and "--exclude" allows to exclude directories. Be carefully about "--delete". Mind hidden folders like ".git" and ".vagrant" etc.
 

SSH session on local system (developer desktop) to sync files from local system to remote server (stage/live)

rsync -a --progress --delete source/ destination-host:/destination-directory/

Example:

rsync -a --progress --delete www/ --exclude=sites/default/files/ --exclude=.git/ --exclude=.idea/ --exclude=.vagrant/ --exclude=provisioning/ ssh-username@host.com:/srv/www/destination-folder/

From remote server to local system (developer desktop)

Here we assume you are in a ssh session on your stage/live server and you want to sync from your local development environment to stage/live server.

Example:

rsync -chavzP --stats --progress --delete username@host.com:/srv/www/some-folder/ /var/www/some-folder/

explainshell.com 

Detailed explanation for the commands can be found at explainshell.com. The rsync command above is explained here.

Happy syncing.

Rsync without changing permissions

The following command will sync the files and folders. It will also delete. But it will not change the permissions. It can be helpful, if you want to ensure, that your webserver is still able to read and execute the files. Like in a case, where I needed this command.

rsync -a --no-perms --progress --delete ./ user@host:/var/www/some-directory/

Just simulate, without real sync

Use the --dry-run option, to execute the command without any real file transfer. Then this command shows you, what "would" be transferred.

Tags