Servers & VPS

VPS Backups: How to Automate Server Backups Properly

Learn how to set up automated VPS backups using native Linux tools to protect files and databases without relying solely on your hosting provider.

Closeup of many cables with blue wires plugged in modern switch with similar adapters on blurred background in modern studio

An automated VPS backup is a scheduled copy of your files and databases that runs without manual intervention. Setting it up properly is the difference between recovering from a failure in minutes and losing weeks of work.

This guide shows you how to automate backups on Linux using tools already available on your server: rsync, mysqldump, and cron.

1. Why you can't rely on your provider alone

Most VPS providers offer automatic snapshots, but they come with significant limitations:

  • Snapshots typically run once per day at most.
  • Retention history is short (7–14 days on most plans).
  • Restoring a snapshot can undo recent unrelated changes.
  • An account misconfiguration or provider failure can delete snapshots and data simultaneously.

The gold standard is the 3-2-1 rule: three copies of your data, on two different media, one offsite. Your own backup script is the first step toward meeting it.

2. What you need to back up

Before writing a single line of script, identify exactly which data is critical:

Data type Typical location Recommended tool
Website files /var/www/html or /home/user/public_html rsync
MySQL/MariaDB databases Managed by the engine, not filesystem mysqldump
Server configuration /etc/nginx, /etc/apache2, /etc/php rsync or tar
SSL certificates /etc/letsencrypt or /etc/ssl rsync
Email /var/mail or /home/user/Maildir rsync

Document these paths before proceeding. A backup script that copies the wrong directory creates a false sense of security.

3. Basic backup script with rsync and mysqldump

The following script creates a compressed local copy of files and databases. Save it to /usr/local/bin/backup-vps.sh and make it executable with chmod +x.

#!/bin/bash
# backup-vps.sh — local backup of website files and databases

DATE=$(date +%Y-%m-%d)
BACKUP_DIR="/var/backups/vps/$DATE"
WEB_SRC="/var/www"
DB_USER="backupuser"
DB_PASS="secure_password"
RETENTION_DAYS=14

mkdir -p "$BACKUP_DIR/web" "$BACKUP_DIR/db" "$BACKUP_DIR/config"

# 1. Website files
rsync -az --delete "$WEB_SRC/" "$BACKUP_DIR/web/"

# 2. Databases (one at a time)
for DB in $(mysql -u"$DB_USER" -p"$DB_PASS" -e "SHOW DATABASES;" 2>/dev/null \
    | grep -Ev "^(Database|information_schema|performance_schema|mysql|sys)$"); do
  mysqldump -u"$DB_USER" -p"$DB_PASS" \
    --single-transaction --routines --triggers "$DB" \
    | gzip > "$BACKUP_DIR/db/${DB}_${DATE}.sql.gz"
done

# 3. Server configuration
rsync -az /etc/nginx/ "$BACKUP_DIR/config/nginx/" 2>/dev/null
rsync -az /etc/apache2/ "$BACKUP_DIR/config/apache2/" 2>/dev/null
rsync -az /etc/php/ "$BACKUP_DIR/config/php/" 2>/dev/null

# 4. Remove backups older than RETENTION_DAYS
find /var/backups/vps -maxdepth 1 -type d -mtime +"$RETENTION_DAYS" -exec rm -rf {} +

echo "Backup complete: $BACKUP_DIR"

Security note: create a dedicated database user with minimal privileges for backups only (SELECT, LOCK TABLES, SHOW DATABASES). Never use the MySQL root account in scripts.

4. Automation with cron

Once the script works manually, configure cron to run it automatically. Edit the crontab for the user that will run the backup:

crontab -e

Add a line to run the backup daily at 03:00 AM (lowest traffic period):

# Daily VPS backup at 3 AM, log to file
0 3 * * * /usr/local/bin/backup-vps.sh >> /var/log/backup-vps.log 2>&1

Verify the log is being created the next day. A silently failing cron job is worse than having no cron at all.

For more server maintenance strategies, explore our VPS servers section covering everything from initial setup to advanced security.

5. Sending backups to a remote destination (offsite)

A local backup protects against configuration errors or accidental deletion, but not against a physical server failure or provider outage. You need a remote destination.

Option A: remote server via rsync over SSH

# Add to your script, after creating the local backup:
REMOTE_USER="backupuser"
REMOTE_HOST="remote-server.com"
REMOTE_DIR="/backups/my-vps/$DATE"

rsync -az -e "ssh -i /root/.ssh/id_backup" \
  "$BACKUP_DIR/" \
  "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/"

Generate a dedicated SSH key for backups (ssh-keygen -t ed25519 -f /root/.ssh/id_backup -N "") and add only the public key to the remote server with restricted permissions.

Option B: S3-compatible object storage

Services like Backblaze B2, Wasabi, or Amazon S3 offer cheap, virtually unlimited storage. Use rclone to upload backups:

# Configure rclone (once):
rclone config

# Upload backup to bucket:
rclone copy "$BACKUP_DIR/" remote:my-backup-bucket/$DATE/ --transfers=4

Rclone supports client-side encryption: no sensitive data reaches the bucket unencrypted.

Key takeaways

  • Provider snapshots are a complement to your strategy, not a substitute.
  • Back up website files, databases, and server configuration separately.
  • Use a dedicated database user with minimal privileges for backup operations only.
  • Automate with cron and check the logs at least once a week.
  • Store at least one copy at a remote destination (3-2-1 rule).
  • Test restoration periodically — a backup you have never restored is not a backup.

If you'd rather have professionals handle this, elenlace.com sets up and monitors your backups as part of our managed VPS service — get in touch today.

FAQ

How often should I run backups on my VPS?

For most websites, a daily backup is sufficient. If your application generates data frequently (active ecommerce, forum, CRM), consider running database backups every 4–6 hours.

How much storage space do I need for backups?

It depends on your data size and desired retention. As a rough guide, if your site occupies 5 GB, a compressed backup typically weighs 1–2 GB. With 14-day retention you need roughly 15–30 GB for local backups.

Does mysqldump lock the database during backup?

With the --single-transaction flag (for InnoDB tables), mysqldump takes a consistent snapshot without blocking writes. MyISAM tables do require a brief lock, which is another reason to migrate to InnoDB.

How do I verify my backups are working correctly?

Review the cron log regularly. Additionally, once a month restore a backup in a test environment and confirm the site works. That test is the only reliable way to know your backup is valid.

Prefer it done for you? El Enlace handles hosting and professional web development.

Compare providers

Other providers and guides worth comparing:

← All