๐ Overview
This guide walks you through securely copying files from a local folder to a remote server using rsync over SSH โ including setting up SSH key-based authentication.
โ Prerequisites
- SSH access to the remote server
rsyncinstalled (dnf install rsyncoryum install rsync)sshandssh-keygenavailable on your local system- Firewall allows outbound connections on SSH port (default 22)
๐ Step 1: Set Up SSH Key Authentication
1.1 Generate SSH Key (on the local server)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- Press Enter to accept the default file path (
~/.ssh/id_rsa) - Choose whether or not to set a passphrase
1.2 Copy SSH Public Key to Remote Server
ssh-copy-id -p 22 user123@example-backup-server.com
- Replace
user123andexample-backup-server.comwith your details - You’ll be prompted to enter the remote user’s password once
Alternatively, manually append the public key to the remote file:
cat ~/.ssh/id_rsa.pub | ssh -p 22 user123@example-backup-server.com 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && chmod 700 ~/.ssh'
๐ ๏ธ Step 2: Run Rsync Command
Basic Rsync
rsync -e 'ssh -p22' -ahvP /home/exampleuser/public_html/wp-content/uploads user123@example-backup-server.com:./backups
Now that SSH keys are in place, rsync can run without prompting for a password.
๐ก Optional Rsync Enhancements
๐งน Delete Remote Files Not Present Locally
rsync -e 'ssh -p22' -ahvP --delete /home/exampleuser/public_html/wp-content/uploads user123@example-backup-server.com:./backups
๐ Limit Bandwidth Usage (e.g. 1MB/s)
rsync -e 'ssh -p22' -ahvP --bwlimit=1024 /home/exampleuser/public_html/wp-content/uploads user123@example-backup-server.com:./backups
โฐ Step 3: Automate with Cron
Open the cron editor:
crontab -e
Add this to run rsync daily at 3:00 AM:
0 3 * * * rsync -e 'ssh -p22' -ahvP /home/exampleuser/public_html/wp-content/uploads user123@example-backup-server.com:./backups
๐งฐ Troubleshooting
| Error | Solution |
|---|---|
Permission denied | Check file permissions or SSH key setup |
Host key verification failed | Manually SSH into the remote server to accept fingerprint |
Connection timed out | Verify firewall and SSH port availability |
No such file or directory | Double-check source and destination folder paths |
โ Done!
You now have a secure, passwordless method for syncing local data to a remote server using rsync with SSH keys โ ideal for automated backups or file transfers.
