A firewall and hardened SSH configuration protect against intrusion, but nothing protects against accidental deletion, failed updates, or hardware failure except a working backup. This guide sets up automated, scheduled backups using rsync and cron, with an off-site copy for real disaster recovery.
Prerequisites
- Ubuntu or Debian VPS
- Root or sudo access
- A second location to store backups (another server, object storage, or your local machine)
Step 1 — Decide What to Back Up
At minimum, back up:
- Application files (e.g.
/var/www) - Databases (via
mysqldump/pg_dump, not raw data files) - Configuration files (Nginx/Apache configs, cron jobs, SSH keys if custom)
Step 2 — Create a Database Backup Script
sudo nano /usr/local/bin/backup-db.sh
#!/bin/bash
TIMESTAMP=$(date +%F)
BACKUP_DIR="/var/backups/mysql"
mkdir -p "$BACKUP_DIR"
mysqldump -u root --all-databases | gzip > "$BACKUP_DIR/all-databases-$TIMESTAMP.sql.gz"
find "$BACKUP_DIR" -type f -mtime +7 -delete
sudo chmod +x /usr/local/bin/backup-db.sh
Step 3 — Create a File Backup Script
sudo nano /usr/local/bin/backup-files.sh
#!/bin/bash
rsync -az --delete /var/www/ /var/backups/www/
sudo chmod +x /usr/local/bin/backup-files.sh
Step 4 — Sync Backups Off-Site
Local backups protect against application mistakes, but not against the VPS itself failing. Sync to a remote destination via rsync over SSH:
rsync -az -e "ssh -p 2222" /var/backups/ user@backup-server:/backups/vps1/
For unattended execution, set up SSH key authentication (no password) from this server to the backup destination.
Step 5 — Schedule Everything With Cron
sudo crontab -e
0 2 * * * /usr/local/bin/backup-db.sh
15 2 * * * /usr/local/bin/backup-files.sh
30 2 * * * rsync -az -e "ssh -p 2222" /var/backups/ user@backup-server:/backups/vps1/
Step 6 — Test Restoring (This Step Is Not Optional)
A backup that has never been restored is not a verified backup. Test regularly:
gunzip -c all-databases-2026-08-01.sql.gz | mysql -u root
Common Errors
Cron job silently fails — cron runs with a minimal environment; use absolute paths for all commands and redirect output to a log for debugging: >> /var/log/backup.log 2>&1.
Disk fills up from old backups — confirm the retention/cleanup command (find ... -mtime +7 -delete) is actually running.
Best Practices
- Follow the 3-2-1 rule: 3 copies, 2 different media/locations, 1 off-site
- Encrypt backups containing sensitive data before transferring them off-site
- Test restores quarterly, not just backup creation
- Also check whether your VPS provider offers snapshot functionality as an additional, faster recovery option
FAQ
Are provider snapshots enough on their own?
Snapshots are fast and convenient for full-server recovery, but they typically live on the same infrastructure as your VPS — pair them with off-site file/database backups for true disaster recovery.
How long should I keep backups?
A common baseline is 7 daily, 4 weekly, and 3-6 monthly backups, adjusted to your compliance and storage requirements.
Continue Reading
- VPS Security Checklist for Beginners
- How to Install MySQL/MariaDB on a Linux VPS
- How to Monitor Auth Logs and Detect Intrusion Attempts
Browse more articles in Server Security & Hardening.
