SSH - rsync


In some cases it’s needed to synchronize your local data with data on remote server. In the case that your application is not providing any native way for case like this and you need to simply synchronize folders on your local and remote server. It’s possible to use “Rsync over SSH”.

In this way it’s possible to:

  • Backup your File System
  • Create copy of your local data to remote server
  • Or just keep synchronized data between multiple servers.

Configure SSH Accounts

In this case we’ll use SSH protocol for connecting to remote server. According to this a user account accessible with SSH protocol is need.

Please keep in mind that mentioned account need to have access to the data that will be synchronized

Example - Create user on Server

[root@SSH_Server ~]# useradd user                                     # Create User account

[root@SSH_Server ~]# passwd user                                      # Set password
Changing password for user user.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Example - Create user on Client

[root@SSH_Client ~]# useradd user                                          # Create User accout

[root@SSH_Client ~]# passwd user                                           # Set password
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

[user@SSH_Client ~]$ ssh-keygen                                            # Create SSH keys
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Created directory '/home/user/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/
The key fingerprint is:
a1:44:a4:45:6c:64:2c:88:29:bd:1b:53:51:d1:81:c7 user@SSH_Client
The key's randomart image is:
+--[ RSA 2048]----+
| + ..OX=..       |
|+ o o== E        |
|.  o.o...        |
|  +  . . .       |
|   +  . S        |
|  .              |
|   |
|   |
|   |

[user@SSH_Client ~]$ echo $SSH_AUTH_SOCK                              # Just check if your SSH Agent isn't running and if not please start him

[user@SSH_Client ~]$ ssh-agent $SHELL                                 # Start SSH Agent as this will make our life easier

[user@SSH_Client ~]$ ssh-add ~/.ssh/id_rsa                            # Load SSH keys in to SSH Agent
Identity added: /home/user/.ssh/id_rsa (/home/user/.ssh/id_rsa)

[user@SSH_Client ~]$ ssh-add -L                                       # Check if the SSH Key is started
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsu4IjsrBsH7BE550nB8FBMVhKxPu3JLybTgENmFgpMkIwL7t9RVbdbMuVuh602F4reTqzDLaYixCaW9XFUFpdwDo5q7AaEZcnrj5HtwavftmpctGT9+XHqKsIPK4RCuCNTeM1i/SIhFZT0Ki0aCVm+CTiOe3lniBEPwNvMnYA9Wl9RCyUDkwQcdBXN+opgBtQT4eeK4wlWE75BfKqSw0rclPv9TD22vRivV3xQ9CxARciZcGSQGOXy5e3hlWe3doOjt8a6sZDV9U2kkWuwmPN49FVG2sEhzJPgYMGEDNrQWYeWLVvJRjEuDIjID21pIq4vK69jW+dhfJRd/K2hWtOQ== /home/user/.ssh/id_rsa

[user@SSH_Client ~]$ ssh-copy-id user@SSH_Server                                 # Copy your SSH Key to SSH Server
The authenticity of host 'SSH_Server (SSH_Server)' can't be established.
RSA key fingerprint is 3f:cc:2d:47:df:ba:91:36:9e:a8:3c:cd:e4:a7:71:5d.
Are you sure you want to continue connecting (yes/no)? yes                       # Acept the SSH Server Key
Warning: Permanently added 'SSH_Server' (RSA) to the list of known hosts.
user@SSH_Server's password:                                                      # Use Password to log to SSH Server
Now try logging into the machine, with "ssh 'user@SSH_Server'", and check in:


to make sure we haven't added extra keys that you weren't expecting.

Create Example Directories & Files

For this example we’ll create in home dir of “user” small directory and file structure to present the synchronization.

Example - Create directory and file examples on Client Site

[user@SSH_Client ~]$ cd ~                                 # Go to home dir of user
[user@SSH_Client ~]$ mkdir source_dir                     # This is the main dir that we'll sinc to SSH Server
[user@SSH_Client ~]$ mkdir source_dir/a                   # Jsut another dirs
[user@SSH_Client ~]$ mkdir source_dir/b
[user@SSH_Client ~]$ mkdir source_dir/c
[user@SSH_Client ~]$ echo a> ./source_dir/a/a.txt         # Jsut some text files
[user@SSH_Client ~]$ echo b> ./source_dir/b/b.txt
[user@SSH_Client ~]$ echo c> ./source_dir/c/c.txt

Example - Create destination directory on Server Site

[user@SSH_Server ]$ cd ~
[user@SSH_Server ~]$ mkdir destination_dir

Push data from SSH Clinet to SSH Server

To push the data from SSH Client to SSH Server please run this command

[user@SSH_Client ~]$ rsync -avz -e "ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress /home/user/source_dir user@SSH_Server:~/destination_dir/
sending incremental file list
source_dir/a/a.txt           2 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/7)
source_dir/b/b.txt           2 100%    1.95kB/s    0:00:00 (xfer#2, to-check=1/7)
source_dir/c/c.txt           2 100%    1.95kB/s    0:00:00 (xfer#3, to-check=0/7)

sent 295 bytes  received 85 bytes  253.33 bytes/sec
total size is 6  speedup is 0.02

Validation - of trasfered data

drwxrwxr-x 3 user user 4096 Oct 26 21:47 destination_dir
[user@SSH_Server ~]$ find ./destination_dir/
QR Code
QR Code wiki:infrastructure_tools:ssh:ssh-rsync (generated for current page)