To back up a VPS properly, use two layers. Turn on your provider's snapshots or backups so you can roll back the whole server quickly, and also send your databases and important files, encrypted, to storage at a different company. Automate both, keep a sensible number of old copies, and restore from them on a schedule to prove they work.
Commands below are for Ubuntu 24.04.
Provider backups vs your own backups: what is the difference?
Most VPS providers offer snapshots, automatic backups, or both. They are useful, but not a complete plan.
| Provider snapshots and backups | Your own file and database backups | |
|---|---|---|
| What is copied | The whole disk image | Only what you choose: dumps, uploads, config |
| Where it lives | Same provider, same account | Anywhere you send it, ideally another company |
| Restore speed | Fast for the whole server | Slower, but you can restore one file or one table |
| Database safety | A copy of a running database may need recovery on restore | A proper dump is consistent by design |
| Protects against provider or account loss | No | Yes, if stored elsewhere |
Two details matter. First, provider backups often skip attached volumes. On Hetzner Cloud, for example, neither backups nor snapshots include them, as covered in our new VPS security checklist. Second, if your provider account is compromised, suspended or closed, backups in that same account go with it.
So keep provider backups for quick rollbacks, and add your own off-site backups for everything you cannot afford to lose.
What is the 3-2-1 rule?
The 3-2-1 rule is the standard starting point. CISA describes it as keeping:
- 3 copies of important files,
- on 2 different types of storage,
- with 1 copy stored off-site.
For a single VPS, a practical version is: the live data on the server, a provider backup or snapshot, and an encrypted copy in object storage at a different company. That gives you three copies, two kinds of storage and one off-site.
What should you back up?
Not the operating system, which you can reinstall in minutes. Back up what you cannot recreate:
- Databases, as dumps (see below), not as raw data files.
- User uploads and generated files, such as
/var/www/app/uploadsor Docker volumes holding data. - Configuration:
/etc(Nginx, systemd units, cron), your app's.envor config files, and yourcompose.yamlif you use Docker. - Anything not in Git. If code only exists on the server, back it up and fix that.
Write the list down. It doubles as your rebuild checklist. Back up as often as the data you can afford to lose: if a day of orders is too much, nightly is not enough.
How do you back up PostgreSQL and MySQL?
Copying a running database's files can give you a broken copy. Use the database's own dump tool, which produces a consistent snapshot.
For PostgreSQL, the pg_dump documentation says it "makes consistent exports even if the database is being used concurrently" and does not block readers or writers. The custom format (-Fc) is compressed and restores with pg_restore. pg_dump covers one database, so also dump roles with pg_dumpall.
sudo mkdir -p /var/backups/db
sudo -u postgres pg_dump -Fc appdb > /var/backups/db/appdb.dump
sudo -u postgres pg_dumpall --globals-only > /var/backups/db/globals.sql
For MySQL or MariaDB with InnoDB tables, use --single-transaction. The MySQL manual explains it dumps a consistent state "without blocking any applications", but only InnoDB tables are consistent, and other connections should avoid schema changes while it runs.
sudo mysqldump --single-transaction --quick --routines appdb > /var/backups/db/appdb.sql
Dumps suit small and medium databases. MySQL's manual warns that dumps do not scale well to large ones, and PostgreSQL's notes that pg_dump alone is generally not the right choice for regular backups of production databases. Once restores take hours, look at physical backups and point-in-time recovery.
How do you send backups off-site with restic?
restic is a backup tool that deduplicates, encrypts and sends data to many destinations, including any S3-compatible object storage. According to its design documentation, all data is encrypted with AES-256 and authenticated with Poly1305-AES before it leaves the server, so the storage company cannot read it.
Install it from Ubuntu's archive:
sudo apt install restic
Ubuntu 24.04 ships restic 0.16.4, while the current upstream release is 0.19.1. The packaged version works for the commands below. If you want newer features, use an official release binary.
Create a bucket at an object storage provider, ideally at a different company from your VPS. Then put the credentials in a root-only file:
sudo install -m 600 /dev/null /etc/restic.env
sudo nano /etc/restic.env
RESTIC_REPOSITORY=s3:https://s3.example-provider.com/my-vps-backups
RESTIC_PASSWORD_FILE=/etc/restic.password
AWS_ACCESS_KEY_ID=your-key-id
AWS_SECRET_ACCESS_KEY=your-secret
Create a long random password, store it in /etc/restic.password with mode 600, and also store a copy somewhere off the server, such as a password manager. Without it, the backups cannot be decrypted by anyone, including you.
Initialise the repository once, then run a first backup:
sudo bash -c 'set -a; . /etc/restic.env; restic init'
sudo bash -c 'set -a; . /etc/restic.env; restic backup /var/backups/db /etc /var/www/app/uploads'
restic's S3 documentation lists the repository formats for Amazon S3 and other S3-compatible services.
BorgBackup is a solid alternative, with similar deduplication and encryption. It works best when the destination is a server you reach over SSH, such as a storage box. The Borg project lists 1.4 as the stable series and says 2.0 is still in testing, so use 1.4 for production.
How long should you keep backups?
Keep enough history to recover from problems you notice late, like a bad data import found two weeks on. A common pattern is daily copies for a week, weekly for a month and monthly for a year. In restic, forget removes old snapshots and --prune frees the space, as the restic docs explain:
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune
One more protection: the credentials on the server can usually delete backups too, so an attacker who takes the server could wipe them. If your storage provider supports bucket versioning or object lock, consider it, or use credentials that cannot delete.
How do you automate backups with systemd timers?
Put the steps in one script, /usr/local/bin/vps-backup:
#!/usr/bin/env bash
set -euo pipefail
set -a; . /etc/restic.env; set +a
sudo -u postgres pg_dump -Fc appdb > /var/backups/db/appdb.dump
sudo -u postgres pg_dumpall --globals-only > /var/backups/db/globals.sql
restic backup /var/backups/db /etc /var/www/app/uploads
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune
restic check --read-data-subset=5%
Make it executable with sudo chmod 700 /usr/local/bin/vps-backup. The last line reads a random 5% of the stored pack files each run, which over time catches corruption that a plain restic check would miss, as described in the restic repository docs.
Then create /etc/systemd/system/vps-backup.service:
[Unit]
Description=Nightly VPS backup
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/vps-backup
And /etc/systemd/system/vps-backup.timer:
[Unit]
Description=Run VPS backup nightly
[Timer]
OnCalendar=*-*-* 02:30:00
RandomizedDelaySec=15m
Persistent=true
[Install]
WantedBy=timers.target
Enable it and check the next run:
sudo systemctl daemon-reload
sudo systemctl enable --now vps-backup.timer
systemctl list-timers vps-backup.timer
journalctl -u vps-backup.service
Persistent=true makes a missed run happen after the server comes back up. A cron line such as 30 2 * * * root /usr/local/bin/vps-backup in /etc/cron.d/ also works, but systemd keeps the logs in the journal for you.
Finally, make failures visible. Backups that silently stopped weeks ago are a classic trap, so use a heartbeat monitor that alerts you when the job does not check in.
How do you test a restore?
A backup is only proven when you have restored from it. CISA's guidance also stresses scheduled recovery tests. Do this once a quarter, on a fresh server, not on production. Install restic and PostgreSQL there, copy over /etc/restic.env and the password file, then run as root (sudo -i):
set -a; . /etc/restic.env; set +a
restic snapshots
restic restore latest --target /tmp/restore-test
sudo -u postgres createdb restore_check
sudo -u postgres pg_restore -d restore_check /tmp/restore-test/var/backups/db/appdb.dump
Then check row counts in key tables, a recent record and a few uploaded files. Time the whole process: that is how long a real recovery takes.
Frequently asked questions
Are my provider's automatic backups enough?
They are a good first layer, but they live in the same account as your server and may skip attached volumes. Add at least one encrypted copy at a different company, so a provider or account problem cannot take everything at once.
Should I back up Docker containers?
Back up the data, not the containers. Images can be rebuilt from your Dockerfile and registry. Back up named volumes or bind-mounted data folders, your compose.yaml and your .env file, and dump databases with their own tools.
What if I lose the restic password?
The data cannot be recovered. That is the point of the encryption. Keep the password in a password manager or a sealed record outside the server, and make sure a second person can reach it.
Getting backups in place
Turn on provider backups today, then add nightly database dumps and an encrypted off-site copy with restic, run by a systemd timer. Put a restore test in the calendar. For the rest of a server's first-hour setup, see how to secure a new VPS.
If you would rather have backups built, monitored and restore-tested for you, our DevOps service does exactly that.

