Here is the final extended guide with cron job automation and example output logs added:
Includes: SSH Key Authentication · Non-root Usage · Cron Job Automation · Example Output
🔐 SSH Key Authentication Setup
- Generate SSH key (if not created yet):
ssh-keygen -t rsa -b 4096
- Copy the key to the remote server:
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] -p 5200
- Verify login without password:
ssh -p 5200 [email protected]
✅ PUSH (Local ➝ Remote)
Root + password auth:
rsync -avp --progress -e "ssh -p 22" /home/storage [email protected]:/home/vzdump
Non-root + SSH key auth:
rsync -avp --progress -e "ssh -i ~/.ssh/id_rsa -p 5200" /home/storage [email protected]:/home/backupuser/vzdump
✅ PULL (Remote ➝ Local)
Root + password auth:
rsync -avp --progress -e "ssh -p 5200" [email protected]:/home/cpaneluser1/backups/test /home/cpaneluser1
Non-root + SSH key auth:
rsync -avp --progress -e "ssh -i ~/.ssh/id_rsa -p 5200" [email protected]:/home/backupuser/backups/test /home/localuser/backups/
📌 Real-World Example
Transfer from remote backup server to local server using a non-root SSH key:
rsync -avp --progress -e "ssh -i ~/.ssh/id_rsa -p 5200" [email protected]:/home/backupuser/backups/test /home/localuser/backups/
⏱️ Automate with Cron Job
To run a scheduled daily backup at 2:30 AM, edit the crontab:
crontab -e
Add:
30 2 * * * rsync -az --delete -e "ssh -i /home/localuser/.ssh/id_rsa -p 5200" [email protected]:/home/backupuser/backups/ /home/localuser/backups/ >> /home/localuser/logs/rsync-backup.log 2>&1
🔹
--delete
removes files locally that no longer exist remotely (optional).
🔹 Log file will be stored in/home/localuser/logs/rsync-backup.log
.
🧾 Example Output (from manual run)
receiving incremental file list
test/
test/file1.tar.gz
1,024,000 100% 11.50MB/s 0:00:00 (xfr#1, to-chk=2/3)
test/file2.sql
256,000 100% 10.24MB/s 0:00:00 (xfr#2, to-chk=1/3)
sent 123 bytes received 1,280,456 bytes 213,456.67 bytes/sec
total size is 1,280,000 speedup is 1.00
🔒 Best Practices
- Always use non-root users for routine transfers.
- Store SSH keys with 600 permissions:
chmod 600 ~/.ssh/id_rsa
- Set ownership and file permissions post-transfer:
chown -R localuser:localuser /home/localuser/backups/ chmod -R 700 /home/localuser/backups/