Welcome to HowToShipIt — practical how-to guides for developers: code, AI tools, and servers, explained step by step.

How to Set Up Automated VPS Backups: A Complete Guide (2026)

Your VPS will fail. Not maybe — when. A bad kernel update, a corrupted disk, an rm -rf in the wrong terminal, or your provider losing a rack. If your only plan is “the host takes snapshots,” you don’t have a backup strategy — you have hope.

This guide shows you how to set up automated VPS backups the right way: encrypted, offsite, with database dumps, retention policies, and restore drills you actually run. You’ll pick between two battle-tested tools — restic (for cheap object storage like Backblaze B2) or Borg (for a second server) — and have nightly backups running in about an hour. Everything here is copy-paste ready for a typical Ubuntu 24.04 VPS.

Last verified: 7 October 2026. Versions referenced: restic 0.19.1, Borg 1.4.5.

Why provider snapshots alone won’t save you

Most VPS hosts offer snapshots or automated disk backups, and you should keep them on — they’re great for rolling back a botched update in minutes. But they have blind spots:

  • Same provider, same fate. If your account is compromised, the host has an outage, or you stop paying the bill, your snapshots go with the server.
  • Coverage gaps. OVHcloud, for example, documents that snapshots cover the system disk only — additional attached disks are silently excluded. Your database volume might not be in the snapshot at all.
  • No point-in-time file restores. Snapshots are whole-disk images. Need one deleted config file from last Tuesday? Good luck.

The fix is the old 3-2-1 rule: 3 copies of your data, on 2 different media types, with 1 copy offsite. Your live server plus provider snapshots are copies one and two. This guide builds copy three: automated, encrypted, offsite backups you control.

Step 1: Decide what to back up (and what to skip)

The slowest part of an outage is arguing about what mattered. Write the list down before you touch a tool. On a typical hosting VPS, back up:

  • Web content and app code: /var/www, /home/*/public_html, /srv
  • Server config: /etc/nginx, /etc/apache2, /etc/php, /etc/ssh
  • Certificates: /etc/letsencrypt (or regenerate via ACME later)
  • Databases: logical dumps, never raw /var/lib/mysql copies unless you know exactly what you’re doing
  • Automation and app roots: /opt, your Docker Compose files, crontabs (/etc/cron*), systemd units

And ruthlessly exclude what you can regenerate: node_modules, vendor/, /proc, /sys, /dev, /tmp, caches, and giant logs you rotate elsewhere. A quick sudo du -xhd1 / | sort -h shows you what’s actually eating disk before you commit to backing it up.

Step 2: Pick your backup engine

Three tools dominate Linux VPS backups. Here’s how they actually differ:

rsync restic Borg
Deduplication No Yes Yes (very efficient)
Encryption No (by default) AES-256, built in AES, built in
Compression No zstd lz4, zstd, lzma (tunable)
Cloud / object storage SSH only Native: S3, B2, GCS, Azure… Needs rclone or a mount
Remote model SSH No server agent needed Borg must be on both ends
Install Preinstalled Single static binary Distro package or pip
Ransomware protection No Via backend policies Append-only mode built in

The practical rule: backing up to cheap object storage? Use restic. Backing up to a second server you control? Use Borg (with borgmatic to manage it). rsync is fine for syncing files, but it gives you no snapshots, no dedup, and no encryption — it’s a transfer tool, not a backup system.

This guide walks through restic in full and covers Borg as the alternative path. Pick one and keep going.

Path A: restic to Backblaze B2 (the cheap offsite route)

Backblaze B2 is S3-compatible object storage that costs roughly $6–7 per TB per month as of October 2026, with the first 10 GB free and egress free up to 3× your average stored data. For a small VPS, your backups will cost you pennies or nothing.

A1. Create the bucket and an application key

In your Backblaze dashboard, create a private bucket (name it something like myvps-backups), then create an application key scoped to that bucket with read/write access. Note down three things: the S3 endpoint for your region (shown in the dashboard), the key ID, and the application key itself. You won’t be able to see the key again.

A2. Install restic

sudo apt update && sudo apt install restic
restic version   # expect 0.19.1 (July 2026) or newer

Distro packages can lag, so check the version. If it’s ancient, grab the static binary from the restic GitHub releases page instead — it’s a single file with no dependencies.

A3. Create the repository

Never put credentials in your shell history or in a world-readable file. Create a root-only password file and a root-only env file:

sudo install -d -m 700 /etc/restic
openssl rand -base64 48 | sudo tee /etc/restic/password > /dev/null
sudo chmod 600 /etc/restic/password

sudo tee /etc/restic/backup.env > /dev/null <<'EOF'
RESTIC_REPOSITORY="s3:YOUR_B2_S3_ENDPOINT/myvps-backups"
RESTIC_PASSWORD_FILE="/etc/restic/password"
AWS_ACCESS_KEY_ID="YOUR_KEY_ID"
AWS_SECRET_ACCESS_KEY="YOUR_APPLICATION_KEY"
EOF
sudo chmod 600 /etc/restic/backup.env

Now initialize the encrypted repository:

sudo -i
set -a; source /etc/restic/backup.env; set +a
restic init

You should see created restic repository ... successfully. If the S3 endpoint is wrong, this is where it fails — double-check the endpoint string in your B2 dashboard.

A4. Write the backup script

One script, run by cron, that dumps databases first and then snapshots everything. Save it as /usr/local/sbin/vps-backup.sh:

#!/bin/bash
set -euo pipefail

set -a; source /etc/restic/backup.env; set +a
DUMP_DIR="/var/backups/db"
mkdir -p "$DUMP_DIR"

# --- Database dumps (adjust names/users to yours) ---
# MySQL / MariaDB: --single-transaction avoids locking InnoDB tables
mysqldump --single-transaction --all-databases | gzip > "$DUMP_DIR/mysql.sql.gz"
# PostgreSQL:
# sudo -u postgres pg_dumpall | gzip > "$DUMP_DIR/postgres.sql.gz"

# --- Snapshot files + dumps ---
restic backup \
  --exclude-caches \
  --exclude='/var/www/*/node_modules' \
  --exclude='/var/www/*/vendor' \
  --exclude='/var/cache' \
  --exclude='/tmp' \
  --exclude='*.log' \
  /var/www /etc /home /opt /srv "$DUMP_DIR"

# --- Retention: keep 7 daily, 4 weekly, 6 monthly snapshots ---
restic forget --prune --keep-daily 7 --keep-weekly 4 --keep-monthly 6

echo "[$(date -Is)] backup OK" >> /var/log/vps-backup.log
sudo chmod 700 /usr/local/sbin/vps-backup.sh
sudo /usr/local/sbin/vps-backup.sh   # run it once by hand and watch for errors

That first run uploads everything and takes the longest. Every run after it is incremental — restic’s deduplication means only changed blocks travel over the wire.

A5. Schedule it with cron

Run backups in the small hours, when traffic is lowest, and make cron email you when something breaks:

sudo crontab -e
MAILTO=you@example.com
0 3 * * * /usr/local/sbin/vps-backup.sh

No MAILTO, no alerts, and you’ll discover your backups have been failing for three months on the day you need them. Set it.

A6. Verify the repository weekly

Add a weekly integrity check. Corruption you catch early is a warning; corruption you catch during a restore is a disaster:

0 4 * * 0 /usr/bin/bash -c 'set -a; source /etc/restic/backup.env; set +a; restic check' 

Step 3: The restore drill (do this before you need it)

An untested backup is a rumor. Once your first few nightly runs are green, prove the whole chain works:

sudo -i
set -a; source /etc/restic/backup.env; set +a

# List your snapshots
restic snapshots

# Restore the latest snapshot to a temp dir (never over live data)
restic restore latest --target /tmp/restore-test

# Spot-check a file you care about
diff /tmp/restore-test/etc/nginx/nginx.conf /etc/nginx/nginx.conf && echo "config matches"

Then test a database dump end-to-end on a scratch database — a dump that won’t import is decoration. Put a recurring monthly reminder in your calendar for this drill. Fifteen minutes, once a month, is the cheapest insurance in your stack.

Path B: Borg + borgmatic to a second server

If you’d rather back up to a second VPS or a cheap storage box over SSH (Hetzner Storage Box, another VPS, a home server), Borg is the better fit: it needs Borg installed on both ends but gives you the best deduplication and compression of any option here, plus append-only mode that protects old archives even if the backed-up server is compromised.

# On both servers
sudo apt update && sudo apt install borgbackup
borg --version   # expect 1.4.5 (July 2026) or newer

# On your main VPS: dedicated SSH key for backups
ssh-keygen -t ed25519 -f ~/.ssh/borg_backup_key -N "" -C "borg-backup"
ssh-copy-id -i ~/.ssh/borg_backup_key.pub borgbackup@BACKUP_SERVER

# Initialize the encrypted repo
export BORG_RSH="ssh -i ~/.ssh/borg_backup_key"
export BORG_PASSPHRASE="$(cat /etc/restic/password 2>/dev/null || openssl rand -base64 48)"
borg init --encryption=repokey-blake2 borgbackup@BACKUP_SERVER:/var/borg-repos/myvps

# Keep a copy of the repo key somewhere safe (NOT on the backed-up server)
borg key export borgbackup@BACKUP_SERVER:/var/borg-repos/myvps ~/borg-repo-key.txt

Then let borgmatic manage the schedule, retention, and database hooks via one YAML file at /etc/borgmatic/config.yaml:

repositories:
    - path: "ssh://borgbackup@BACKUP_SERVER/var/borg-repos/myvps"
      label: primary
source_directories:
    - /var/www
    - /etc
    - /home
    - /opt
exclude_patterns:
    - /var/www/*/node_modules
    - /var/www/*/vendor
    - '*.log'
    - /tmp
    - /var/cache
keep_daily: 7
keep_weekly: 4
keep_monthly: 6
# mariadb_databases / postgresql_databases hooks dump your DBs automatically
mariadb_databases:
    - name: all
postgresql_databases:
    - name: all

borgmatic ships with a systemd timer (borgmatic.timer) you can enable, and it validates your config before every run — which is exactly the kind of safety net cron alone doesn’t give you.

7 mistakes that make backups useless

  1. Never testing restores. The number-one backup failure mode. Run the drill from Step 3 monthly.
  2. Backups on the same disk or same account only. Offsite means a different provider or location, or it isn’t offsite.
  3. Secrets in world-readable files. Repo passwords and API keys must be chmod 600 and owned by root. Audit this once: sudo find /etc/restic /usr/local/sbin -perm -o+r should return nothing.
  4. Backing up caches and build artifacts. node_modules and vendor/ inflate every snapshot and slow restores. Exclude them.
  5. No retention policy. Without restic forget --prune (or borgmatic’s keep rules), snapshots grow forever until the job fails on a full disk — silently, at 3 AM.
  6. No alerting. Set MAILTO in cron. A backup job nobody watches is a job that fails quietly.
  7. Dumping live databases without care. Always use mysqldump --single-transaction for InnoDB, and never copy raw /var/lib/mysql files from a running server.

Wrapping up

You now have a choice of two complete paths: restic to Backblaze B2 for a few cents a month of encrypted offsite storage, or Borg + borgmatic to a second server for maximum efficiency and ransomware-resistant append-only archives. Either way, the non-negotiable parts are the same: dump your databases properly, keep one copy truly offsite, prune old snapshots, alert on failures, and run a restore drill every month.

Do it tonight. Your future self — the one staring at a dead server at 2 AM — will thank you.

Further Reading & References

Leave a Comment