Rsync is a tool that we can used for synchronize and transfer data between servers. The command can be used over SSH which encrypts the connection. It also provides options which can be used such as archive mode, backup mode, data compression during the transfer etc.
rsync utility must be installed on both the destination and the source systems. If it is not installed you can install it using your distribution’s package manager:
Ubuntu & Debian :
sudo apt install rsync
CentOS and Fedora:
sudo yum install rsync
Step 1- Setup public SSH keys
On our origin server, we will generate public SSH keys with no password:
ssh-keygen -f ~/.ssh/id_rsa -q -P "" cat ~/.ssh/id_rsa.pub
This is our public SSH key that can be placed on other hosts to give us access:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLVDBIpdpfePg/a6h8au1HTKPPrg8wuTrjdh0QFVPpTI4KHctf6/FGg1NOgM++hrDlbrDVStKn/b3Mu65//tuvY5SG9sR4vrINCSQF++a+YRTGU6Sn4ltKpyj3usHERvBndtFXoDxsYKRCtPfgm1BGTBpoSl2A7lrwnmVSg+u11FOa1xSZ393aaBFDSeX8GlJf1SojWYIAbE25Xe3z5L232vZ5acC2PJkvKctzvUttJCP91gbNe5FSwDolE44diYbNYqEtvq2Jt8x45YzgFSVKf6ffnPwnUDwhtvc2f317TKx9l2Eq4aWqXTOMiPFA5ZRM/CF0IJCqeXG6s+qVfRjB [email protected]
Step 2-Copy SSH keys to destination server
Copy the SSH key to your clipboard and login to your destination server.
Place the SSH key into your ~/.ssh/authorized_keys file:
If your SSH folder does not exist, create it manually:
mkdir ~/.ssh chmod 0700 ~/.ssh touch ~/.ssh/authorized_keys chmod 0644 ~/.ssh/authorized_keys
The SSH options are useful to keep Rsync quiet and not prompting everytime you connect to a new server.
Step 3-Rsync the files or folders
For example the origin server is 220.127.116.11 and the destination is 18.104.22.168.
Copy (or synchronize) a file (/example_folder/a_file.txt) from origin to destination :
rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress /example_folder/a_file.txt [email protected]:/
Copy (or synchronize) a folder content (/example_folder/a_file.txt) from origin to destination :
rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress /example_folder/ [email protected]:/destination_folder
You may have noticed that there is a trailing slash (/) at the end of the first argument in the sync folder commands. This is necessary to mean “the contents of
example_folder“. The alternative, without the trailing slash, would place
example_folder, including the directory, within