Pre-requisites:
A system with CentOS Stream 9 installed and running
Access to a terminal or command line
Root or sudo privileges
The commands in this guide should work on any modern Linux distribution.
Rsync, short for remote synchronization, is a Linux-based tool that lets you transfer and synchronize files or folders between remote Linux-based servers. It can be easily integrated with Bash scripts and Cron jobs for automation. For example, you can schedule a script to run daily to backup your files to a remote server for off-site disaster recovery.
First, update your systems package index
dnf update -y
First, find out whether rsync is installed. Run the command below. If you see output, rsync is installed.
which rsync
Note that rsync must be installed on both the source and destination machines.
Installing rsync
dnf -y install rsync
Basic syntax for an rsync command
rsync -[options] source destination
The rsync command below invokes SSH using the target host’s port 2222 as its access point. To invoke archiving, the -a option is used. The -v option indicates verbosity, which displays more information about files being transferred. The -z option compresses the file data during the transfer. The -h option outputs numbers in a human-readable format. To specify the type of protocol to be used, the -e option is used.
The ssh command provides an -i option that specifies the path to the private key to be used for authentication.
This command executes from the source machine, which sends from source to target machine
rsync -avzh -e “ssh port=2222” /usr/bin user@remotehost:/usr/bin
Run the command below to generate a new key using the ssh-keygen tool using the rsa algorithm. The -t option defines the type of algorithm that’s used. The -b option specifies the bit length that is used. For RSA keys, it is recommended that you specify a bit length of 4096.
ssh-keygen -t rsa -b 4096
Next, copy and put the newly created id_rsa.pub public key file in the ~/.ssh/authorized_keys file on your server.
Using rsync with SSH keys to transfer files
This command executes from the source machine, which sends from source to target machine
rsync -avzh -e “ssh -p 22 -i ~/.ssh/id_rsa” dir user@host:/dir
Additional information on the official rsync website: