Securing VPS/Servers Backups

VPS Backup Strategy: Snapshots, Off-Site Copies and a Restic Setup

What a provider snapshot does and does not protect against, what to back up on a VPS, and a complete encrypted off-site backup with restic, including retention and a restore test.

A backup plan for a VPS has to survive three different failures: you delete or break something, the server is compromised, or the provider loses the machine or closes the account. A provider snapshot handles the first and little else. This guide sets out what to back up, where to keep it, and a working setup with restic that covers all three.

Commands are given for Ubuntu 26.04/24.04 and Debian 13 (apt) and for AlmaLinux and Rocky Linux 9 and 10 (dnf). They are standard tools from each distribution; this guide is a reference and was not run on a test server for this article.

Snapshot or backup?

Provider snapshotFile-level backup
What it isAn image of the whole disk at one momentCopies of chosen files and database dumps
Where it livesWith the same provider, often the same data centreWherever you send it
RestoresThe whole server, quicklyOne file, one database or everything
Survives an account or provider failureNoYes, if stored elsewhere
HistoryUsually one or a few copiesAs many versions as you keep
Best forA safety net before upgrades and risky changesDaily protection and long-term history

Use both. Take a snapshot before every major change, and run file-level backups every day.

The rules

  • 3-2-1: three copies of the data, on two different systems, one of them with a different provider.
  • Decide how much you can lose. A daily backup means up to 24 hours of lost orders, posts or uploads. If that is too much, back up the database more often.
  • Decide how long recovery may take. Restoring 50 GB over the network to a new server takes hours. Know the number before you need it.
  • Encrypt before the data leaves the server. Backups contain everything an attacker wants: password hashes, customer data, API keys.
  • The server must not be able to destroy its own backups. If an intruder with root on the VPS can delete the repository, ransomware can too. Use storage that supports append-only access or object locking, or pull backups from a separate machine.
  • A backup is proven only by a restore.

What to back up

WhatWhere it usually isNote
Website files and uploads/var/www, /home/*/Exclude caches and node_modules
DatabasesDump files you createNever copy the live data directory
Configuration/etcWeb server, PHP, database, firewall, cron
Certificates/etc/letsencryptCan be reissued, but saves time
Docker volumes and compose files/var/lib/docker/volumes, your project foldersStop or dump stateful containers first
Mail/var/vmail or similarIf the server handles mail
Package listdpkg --get-selections or rpm -qa outputLets you rebuild the same software set

Dump databases first

Copying the files of a running database can give a backup that will not start. Create a consistent dump and back that up.

# MariaDB / MySQL (InnoDB tables, no locking)
mysqldump --single-transaction --routines --triggers --events --databases shop blog | gzip > /var/backups/db/mysql-$(date +%F).sql.gz

# PostgreSQL, one database
sudo -u postgres pg_dump -Fc shop > /var/backups/db/shop-$(date +%F).dump

On MariaDB the command is also available as mariadb-dump. Keep a few days of dumps locally and delete older ones, or the dump folder will fill the disk.

A working setup with restic

restic is an open-source backup program that encrypts, deduplicates and keeps versions. It can write to another server over SFTP, to S3-compatible object storage, to Backblaze B2 and to many other targets through rclone.

sudo apt install restic       # Ubuntu, Debian
sudo dnf install restic       # AlmaLinux, Rocky Linux (EPEL repository)

1. Store the settings. Create /root/.restic-env, readable by root only (chmod 600):

export RESTIC_REPOSITORY="sftp:backup@backup.example.com:/srv/restic/web1"
export RESTIC_PASSWORD_FILE="/root/.restic-password"

Put a long random password in /root/.restic-password and keep a copy in your password manager. Without it the backups cannot be read by anyone, including you.

2. Create the repository (once):

source /root/.restic-env
restic init

3. The backup script. Save as /usr/local/bin/backup.sh and make it executable:

#!/usr/bin/env bash
set -euo pipefail
source /root/.restic-env

mkdir -p /var/backups/db
mysqldump --single-transaction --routines --triggers --events --databases shop blog | gzip > /var/backups/db/mysql.sql.gz

restic backup /etc /var/www /var/backups/db --exclude-caches --exclude '/var/www/*/cache'
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune

The forget line keeps seven daily, four weekly and six monthly versions and removes the rest. Adjust database names, paths and retention to your site.

4. Schedule it. As root, crontab -e:

15 3 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

5. Check it.

source /root/.restic-env
restic snapshots          # list versions
restic check              # verify repository structure

Know when it stops working

Backup jobs fail quietly: a full target, an expired key, a changed password. Have the script report success to a monitoring service that alerts you when the report does not arrive (a "dead man's switch" check), or at least look at restic snapshots in your weekly routine. The newest snapshot should never be more than a day old.

Test a restore

source /root/.restic-env
restic restore latest --target /tmp/restore-test
ls /tmp/restore-test/var/www
gunzip -c /tmp/restore-test/var/backups/db/mysql.sql.gz | head -20

That proves the files can be read. Twice a year, do the full exercise: create a new VPS, install the stack, restore files and import the database, and load the site. Time it and write down the steps. That document is what you will want in a real emergency.

Where to keep the off-site copy

  • Object storage (S3-compatible services, Backblaze B2): pay per gigabyte, supports object locking against deletion.
  • A storage box or second VPS with another provider: a fixed monthly price, reached over SFTP.
  • Your own machine or NAS: no monthly cost. It should pull the backups, so that the server holds no credentials to it.
  • Consumer cloud drives through rclone: workable for small sites; check the provider's limits on automated use.

Whichever you choose, use credentials that belong only to that server, so that a compromise of one machine does not expose the backups of others.

Frequently asked questions

Is a provider snapshot enough?

No. It protects against your own mistakes and fails together with the provider or the account. Keep at least one copy elsewhere.

How often should I back up a VPS?

Daily for most sites. Dump the database more often if losing a day of orders or content would hurt.

restic or Borg?

Both encrypt and deduplicate. restic talks to object storage directly; BorgBackup works over SSH to a server that also runs Borg. Pick the one that matches where you will store the data. The older Duplicity guide on this site shows a third option.

How long should I keep backups?

Long enough to go back before a problem you have not noticed yet. A hacked site is often discovered weeks later, which is why the example keeps monthly versions for half a year.

Backups are one of the six recurring jobs in our VPS management guide, and the restore test is on the maintenance checklist.

Share story:

Continue reading other guides on How VPS.