Home‎ > ‎Backup‎ > ‎

Rsync server to local using SSH and .pem key

Rsync uses left to right -> from to syntax, so if you want to copy something from X to Y, X will be on the left followed by Y. From is on the left, to is on the right.

$ sudo rsync -options /X/ /Y

Notice trailing slash after X, on my system it works without it, but it is used in documentation so you should use it.

From server to local machine over SSH

I am using -i key.pem option, you may jus just ssh, if you want to enter password instead of using key file.

Using .pem authentication file (required on Amazon AWS EC2 after fresh install)

$ sudo rsync --delete -azvv -e "ssh -i ec2.pem" ubuntu@12.34.55.66:/var/www/example.com/ /home/workspace/example.com/backup

or without .pem key authentication file, you will be prompted for password.

$ sudo rsync --delete -azvv -e ssh ubuntu@12.34.55.66:/var/www/example.com/ /home/workspace/example.com/backup

When connecting using SSH, enter sudo rsync -options username@hostname:/remote/directory/on/server/to/backup/files/from /home/directory/on/local/machine/to/backup/files/to, beware that after username@hostname: there is no space, it directly continues with directory path username@hostname:/var/www.expample.com.

Option vv is very verbose, z uses compression and a is archiving mode that preserves permissions and other stuff, use man rsync if not sure, but these are the best options to backup for newbies. 

From local machine to server over SSH

$ sudo rsync --delete -azvv -e ssh /home/workspace/example.com/backup ubuntu@12.34.55.66:/var/www/example.com/ 

Using paths with spaces

Using file names with spaces, remember to use "path with spaces.txt", or using single quotes you have to escape the space '/path/with\ spaces/'.

Testing your syntax

Use option --dry-run  and - vv to test your syntax first, everything will be output on the screen, but nothing will really happen.

$ sudo rsync --dry-run --delete -azvv -e ssh /home/workspace/example.com/backup ubuntu@12.34.55.66:/var/www/example.com/

Troubles with key path, rsync prompting for password when should not

If using rsync with sudo, it looks for key file in /root/.ssh/config not in /home/user/.ssh/config, so be sure to copy or link this file to correct location, otherwise ssh and scp will be working fine while rsync will prompt for password.