How to Set Up Automated VPS Backups (rsync, cron & Off-Site Storage)

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

Browse more articles in Server Security & Hardening.

  • backup, disaster recovery, rsync, cron, vps security
  • 0 Користувачі, які знайшли це корисним
Ця відповідь Вам допомогла?

Схожі статті

SSH Hardening: Change the Port, Disable Root Login & Use SSH Keys (Ubuntu & Debian)

SSH is the front door to your VPS — and by default it listens on a well-known port, often...

How to Install and Configure Fail2Ban on Ubuntu & Debian (Complete Guide)

Fail2Ban monitors your server's log files and automatically blocks (bans) IP addresses that show...

How to Configure UFW Firewall on a Linux VPS (Ubuntu & Debian)

UFW (Uncomplicated Firewall) is the standard firewall front-end on Ubuntu and Debian. A correctly...

How to Enable Two-Factor Authentication (2FA) for SSH on a Linux VPS

Two-Factor Authentication (2FA) adds a second layer of protection to SSH: even if your password...

VPS Security Checklist for Beginners: 12 Essential Steps

Every new VPS is deployed with default settings that are convenient but not secure. This...