No KYC VPS
TutorialsNov 14, 20268 min readBy the No KYC VPS team

How to Back Up a VPS: Snapshots, rsync and Restic Explained

A practical VPS backup guide: the 3-2-1 rule, provider snapshots, file-level backups with rsync and restic, and how to test that you can really restore.

A VPS can fail in dull ways: a bad update, a deleted directory, a full disk, a compromised account. A backup you have never restored is only a hope, so this guide covers both making and testing them.

Use the 3-2-1 rule

  • 3 copies of the data (the live copy and two backups)
  • 2 different storage locations
  • 1 copy off the server and, ideally, in another country

Layer 1: snapshots

A snapshot captures the whole disk at a moment in time. It is the fastest way to roll back after a failed upgrade. Take one before every major change. Snapshots live with the same provider, so they do not replace an off-site copy. See VPS backups and snapshots for how they work here.

Layer 2: file-level backups with restic

Restic makes encrypted, deduplicated backups to a remote store. Install it and initialise a repository on storage you control:

apt install restic
export RESTIC_REPOSITORY=sftp:backup@your-storage:/srv/restic
export RESTIC_PASSWORD_FILE=/root/.restic-pass
restic init
restic backup /etc /var/www /home --exclude-caches
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune

Run it from cron or a systemd timer every night. Keep the password somewhere other than the server, or you cannot restore after losing it.

Layer 3: simple rsync copies

For a plain copy of a directory to another machine:

rsync -az --delete -e ssh /var/www/ backup@your-storage:/srv/www-copy/

Rsync is fast but keeps only the latest state, so a deleted file is deleted on the copy too. Use restic when you need history.

Do not forget databases

Copying live database files can give you a corrupt backup. Dump first:

mysqldump --single-transaction --all-databases | gzip > /var/backups/db-$(date +%F).sql.gz
pg_dumpall | gzip > /var/backups/pg-$(date +%F).sql.gz

Test the restore

  1. Once a month, restore a backup to a spare VPS or a temporary directory.
  2. Check that the site starts and the database opens.
  3. Write down how long it took, so you know your real recovery time.

Frequently asked questions

How often should I back up a VPS?

Daily for anything that changes often, such as a database or a shop. Weekly is enough for a static site. Always take a snapshot before a major change.

Are snapshots the same as backups?

No. A snapshot lives with the same provider and usually the same storage, so it protects against a bad update but not against losing the provider account. Keep an off-site copy too.

What is the best tool for VPS backups?

Restic is a good default: it encrypts, deduplicates and supports many storage targets. Rsync is simpler but keeps no history.

How do I know my backup works?

Restore it. Once a month restore to a spare server or temporary folder and check that the site and database start.