Skip to content
STAGE33R

Guides · Incident response and recovery · 6 steps · reviewed

How to back up a Linux server with restic and prove that the restore works

A backup you have never restored is a hope, not a backup. restic makes encrypted, deduplicated backups to local disk or object storage, and a regular restore test turns the hope into a fact.

  1. Create the repository

    Use storage that is not attached to this server in a way ransomware can reach. Keep the password safe elsewhere.

    export RESTIC_REPOSITORY=s3:s3.amazonaws.com/your-bucket/server1
    export RESTIC_PASSWORD_FILE=/root/.restic-pass
    restic init
  2. Dump databases to files first

    Back up consistent dumps, not live data directories.

    mkdir -p /var/backups/db
    mysqldump --all-databases --single-transaction > /var/backups/db/mysql.sql
    sudo -u postgres pg_dumpall > /var/backups/db/postgres.sql
  3. Back up files and dumps

    Choose the folders that hold your data and configuration.

    restic backup /var/backups/db /etc /home /var/www --exclude-caches
  4. Keep a sensible history and check integrity

    Prune keeps a daily, weekly and monthly series.

    restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
    restic check
  5. Restore to a spare folder and open the files

    This is the test. Restore, then check a file and import a dump into a scratch database.

    restic restore latest --target /tmp/restore-test
    ls -la /tmp/restore-test/var/backups/db
  6. Schedule it and be told when it fails

    A systemd timer or cron job, and a monitor that alerts on failure.

Common mistakes

  • Keeping the only copy on the same server.
  • Never testing a restore until the day you need it.
  • Backing up the live database files, which can be inconsistent.

How to undo it

Restoring is the rollback for most other changes. Keep at least one copy that the server cannot delete.

Sources

Commands are for Debian and Ubuntu unless noted. Test on a copy first and keep a backup: you are responsible for changes on your own systems.