1. Overview
This guide explains how to securely copy files from one server to another using rsync over SSH.
Using rsync over SSH is a secure and efficient method for transferring and synchronizing files. It is commonly used for backups, data migration, and scheduled file synchronization.
2. Prerequisites
Before you begin, ensure that:
- SSH access is enabled on both servers.
- rsync is installed on both servers.
- You have root or sudo privileges.
3. Install rsync
If rsync is not already installed, install it on both the source and destination servers.
Debian / Ubuntu
apt install rsync -yCentOS / RHEL / AlmaLinux
yum install rsync -y4. Configure SSH Key-Based Authentication
To avoid entering the SSH password for every rsync operation, configure SSH key-based authentication from the source server to the destination server.
Step 1: Generate an SSH Key Pair
On the source server, run:
ssh-keygen -t rsa -b 4096 -C “rsync-ssh-key”- Press Enter to accept the default location (/root/.ssh/id_rsa).
- Leave the passphrase empty if passwordless authentication is required.
Step 2: Copy the Public Key to the Destination Server
Run the following command:
ssh-copy-id root@destination-vmThis copies the public key (id_rsa.pub) to the destination server and adds it to the ~/.ssh/authorized_keys file.
Step 3: Verify the SSH Connection
Test the SSH login:
ssh root@destination-vm
If the configuration is successful, you should be able to log in without entering a password.
5. Copy Files Using rsync
Use the following command to copy files over SSH:
rsync -avz -e ssh /path/to/source-directory/ root@destination-vm:/path/to/destination-directory/Command Options :
-a
Archive mode. Preserves file permissions, timestamps, and symbolic links.
-v
Displays detailed output during the transfer.
-z
Compresses data during transfer.
-e ssh
Uses SSH as the transport protocol.
6. Example: Copy a Directory
To copy the contents of /var/www/html to /var/www/backup on the destination server:
rsync -avz -e ssh /var/www/html/ root@destination-vm:/var/www/backup/Note: A trailing slash (/) on the source directory copies only the contents of the directory. Without the trailing slash, the entire directory is copied.
7. Perform a Dry Run
To preview the files that will be transferred without making any changes, run:
rsync -avz –dry-run -e ssh /var/www/html/ root@destination-vm:/var/www/backup/8. Automate rsync Using Cron
Open the crontab editor:
crontab -eExample: Run the rsync command every day at 2:00 AM.
0 2 * * * rsync -avz -e ssh /var/www/html/ root@destination-vm:/var/www/backup/ >> /var/log/rsync.log 2>&19. Troubleshooting
Permission Denied
Verify that SSH key-based authentication is configured correctly and that the SSH key permissions are correct.
Connection Refused
Ensure that the SSH service is running on the destination server.
Path Errors
Verify that the source and destination directories exist before running the command.
10. Conclusion
Using rsync with SSH provides a secure and efficient way to transfer files between servers. It is well suited for backups, data migration, and automated file synchronization while preserving file attributes during the transfer.