How to Set Up Automatic VPS Backups with rsync and cron

If you are running a VPS, backups are not optional. One mistake, crash, or hack can wipe out everything in seconds.
Most beginners either:
- Don’t take backups at all
- Rely only on hosting provider snapshots
- Or manually copy files (which is unreliable)
The right way is to automate backups using reliable tools.
In this guide, you’ll learn how to set up automatic VPS backups using rsync and cron — a powerful, lightweight, and production-ready solution used by system administrators.
Why Automatic VPS Backups Are Important
Without backups, you risk:
- Data loss
- Website downtime
- Security incidents
- Failed updates
With automation:
👉 Your data is backed up regularly without manual work.
What is rsync?
rsync is a fast file synchronization tool used to copy files efficiently between locations.
Key benefits:
- Incremental backups (only changes copied)
- Fast and efficient
- Works over SSH
- Widely used in production
What is cron?
cron is a scheduler in Linux used to run tasks automatically at specific intervals.
Example:
- Daily backups
- Weekly cleanup
- Hourly sync
Backup Strategy Overview
We will:
- Create backup directory
- Write rsync command
- Create backup script
- Schedule using cron
- Test and verify
Step 1: Prepare Backup Location
You can back up to:
- Another VPS
- External storage
- Same server (not ideal but acceptable)
Create directory:
mkdir -p /backup
Step 2: Basic rsync Command
Example:
rsync -avz /var/www /backup/
Explanation:
-a→ archive mode-v→ verbose-z→ compression
Step 3: Remote Backup (Recommended)
Backup to another server:
rsync -avz /var/www user@backup-server:/backup/
Step 4: Set Up SSH Key (Passwordless)
Generate key:
ssh-keygen
Copy:
ssh-copy-id user@backup-server
Step 5: Create Backup Script
Create file:
nano backup.sh
Paste:
#!/bin/bash
DATE=$(date +%F)
rsync -avz /var/www user@backup-server:/backup/$DATE
Make executable:
chmod +x backup.sh
Step 6: Test Backup
Run:
./backup.sh
Verify files copied.
Step 7: Automate with cron
Open cron:
crontab -e
Add:
0 2 * * * /root/backup.sh
👉 Runs daily at 2 AM
Step 8: Keep Only Recent Backups
Add cleanup:
find /backup/* -mtime +7 -delete
Step 9: Backup Databases
MySQL backup:
mysqldump -u root -p dbname > db.sql
Add to script.
Step 10: Secure Backups
- Use SSH
- Restrict access
- Encrypt backups if needed
Best Practices
- Store backups offsite
- Test restore regularly
- Keep multiple copies
- Monitor backup logs
Common Mistakes
- Not testing backups
- Storing backups on same server only
- Forgetting database backups
Real-World Insight
Most data loss happens not because backups are missing — but because:
👉 backups are not tested
FAQs
How often should I back up?
Daily for most servers.
Is rsync better than snapshots?
Yes for flexibility.
Final Thoughts
Setting up automatic VPS backups with rsync and cron is one of the smartest things you can do for your server. It’s simple, powerful, and reliable.
