๐ 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
rsync
installed (dnf install rsync
oryum install rsync
)ssh
andssh-keygen
available 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 "[email protected]"
- 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 [email protected]
- Replace
user123
andexample-backup-server.com
with 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 [email protected] '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 [email protected]:./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 [email protected]:./backups
๐ Limit Bandwidth Usage (e.g. 1MB/s)
rsync -e 'ssh -p22' -ahvP --bwlimit=1024 /home/exampleuser/public_html/wp-content/uploads [email protected]:./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 [email protected]:./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.