SERVER BACKUP STRATEGY

The failure modes you are defending against, the 3-2-1 rule on one rack, restic to a second server, and a restore you have actually tested.

A backup strategy that survives bad days

 

A server backup strategy that survives bad days

A server backup strategy is the plan for getting your data back after something goes wrong, and on a rented dedicated server it is entirely your job. The hardware, the RAID array and the datacenter partner keep the machine running, but the copies that let you recover from a deletion, a bad deploy or ransomware are yours to make and yours to test.

This guide builds one end to end on Ubuntu 24.04 LTS: the failure modes you are actually defending against, the 3-2-1 backup rule translated to a single rack, restic backup on Linux as the workhorse, database dumps done right, full automation, and the restore drill that proves any of it works. Every command is copy-and-run.

Three ways a server loses data

Data does not disappear in one way, and no single defence covers all of them. Sort the threats before you choose tools.

Hardware failure is a disk dying. RAID covers this: a mirrored or parity array keeps running through a dead drive, and our RAID guide explains which level survives what. RAID is the only layer here that keeps the service up during the failure, but it protects against exactly one thing.

Human and software failure is the big one: an accidental rm, a migration that drops the wrong table, a bad deploy, or ransomware that encrypts everything it can reach. RAID faithfully mirrors all of it onto both drives. A snapshot on the same box helps only until the box, or the attacker, takes it too. Only a separate backup copy brings the data back.

Datacenter-level failure is rare but total: fire, flood, or simply losing access to the rack. The datacenter partner designs against it, but the only thing on your side of that line is a copy that lives somewhere else.

Two rules that save people

RAID is not a backup, and a snapshot on the same server is not a backup. Both vanish with the machine. A backup is a separate copy that you have restored from at least once.

The 3-2-1 rule on a rented server

The 3-2-1 backup rule is the one worth memorising: three copies of your data, on two different media, with one copy offsite. It predates the cloud and still decides whether a bad day is an inconvenience or a closure.

On a rented dedicated server the words need translating, because all your hardware lives in one rack. Here is what each part means when you do not own the building.

Three copies

The live data on your server, plus two backups. One backup is not enough: if it is mid-write or itself corrupt when you need it, you want a second to fall back to.

Two different media

Your server's disks are the first medium. The second is different hardware in a different failure domain: another server's drives, S3-compatible object storage, or a NAS in your office. Not a second folder on the same box.

One copy offsite

At least one copy in a different building. A second dedicated server in another datacenter region, object storage in a different location, or your own office NAS. This is the copy that survives losing the whole rack.

Deciding what to back up

Backing up everything is slow, expensive and usually pointless. Split your machine into two piles: state you cannot recreate, and everything you can rebuild from a script or a git repository.

State is what you protect: databases, user uploads, application config, /etc, TLS material, and the data inside your Docker volumes. Rebuildable means the operating system, installed packages and your application code, all of which come back from a fresh install and a git clone. Back up state religiously; back up the rebuildable parts only when restoring them is genuinely faster than reinstalling.

  1. List your databases

    Every PostgreSQL, MySQL or MariaDB instance on the box. These need logical dumps, not file copies, which the next section covers.

  2. Find the user-generated files

    Uploads, avatars, generated documents, anything a user created that is not in git. Write down the paths.

  3. Capture the configuration

    /etc for system config, plus application config and environment files. Small, high value, and the part people forget until a restore.

  4. Locate your Docker volume data

    Named volumes hold database and app state. Know where they live (/var/lib/docker/volumes) or bind them under a path you already back up.

  5. Write down the rebuild path

    For each service, one line on how you reinstall it. If that line exists, you can safely exclude the binaries and back up only the state.

restic: install and point it at a second server

restic is a single Go binary that does encrypted, deduplicated, incremental backups to many backends. It is the right default for a Linux server: fast, well tested, and the repository is encrypted before anything leaves the machine. Ubuntu 24.04 ships a recent version in its repositories.

Install restic and confirm the version.

shell
sudo apt-get update
sudo apt-get install -y restic
restic version

restic protects the repository with a password. Generate a strong one, store it in a file the backup job can read, then copy it into your password manager as well. Lose this password and the backups are unrecoverable, by design.

shell
umask 077
head -c 30 /dev/urandom | base64 | sudo tee /root/.restic-password > /dev/null
sudo chmod 600 /root/.restic-password

Point restic at a second server over SFTP. First create an SSH key here and add its public half to a dedicated backup user on the target host, the same key-based login from the getting-your-server-online guide. Then store the repository location and password path in one env file and initialise the repo. Replace backup.example.net and the paths with yours.

shell
sudo mkdir -p /etc/restic
sudo tee /etc/restic/env > /dev/null <<'ENVFILE'
export RESTIC_REPOSITORY="sftp:backup@backup.example.net:/srv/restic/web01"
export RESTIC_PASSWORD_FILE="/root/.restic-password"
ENVFILE
sudo chmod 600 /etc/restic/env
source /etc/restic/env
restic init

Object storage is the common alternative and often the simpler offsite: any S3-compatible bucket works. Swap the repository line, add the credentials, then run the same restic init. Substitute your endpoint, bucket and keys.

/etc/restic/env
export RESTIC_REPOSITORY="s3:s3.eu-central-1.amazonaws.com/my-backup-bucket/web01"
export RESTIC_PASSWORD_FILE="/root/.restic-password"
export AWS_ACCESS_KEY_ID="YOUR_ACCESS_KEY"
export AWS_SECRET_ACCESS_KEY="YOUR_SECRET_KEY"

Now take the first backup. An excludes file keeps the noise out: caches, temporary files, the live Docker layer store (back up your volume data explicitly instead). Create it, then run the backup of your state paths.

shell
sudo tee /etc/restic/excludes.txt > /dev/null <<'EXCL'
/var/cache
/var/tmp
/tmp
/var/lib/docker/overlay2
/home/*/.cache
*.log
EXCL

source /etc/restic/env
restic backup /etc /srv /home /var/www \
  --exclude-file=/etc/restic/excludes.txt \
  --tag nightly

Databases need dumps, not file copies

Never back up a live database by copying its data directory. The files change while you copy them, so you capture a torn, half-written state that may not restore at all. A logical dump asks the database engine for a consistent snapshot instead, which is why the dump goes into a directory that restic then picks up.

Dump into a dedicated folder, here /srv/backup/db, that sits inside your restic include paths. PostgreSQL first: one custom-format dump per database plus the global roles.

PostgreSQL: dump the globals and one database. pg_dump reads a consistent snapshot without locking writers.

shell
sudo mkdir -p /srv/backup/db
sudo -u postgres pg_dumpall --globals-only > /srv/backup/db/pg-globals.sql
sudo -u postgres pg_dump -Fc mydb > /srv/backup/db/pg-mydb.dump

MySQL or MariaDB: use --single-transaction so the dump is consistent without locking the whole server.

shell
mysqldump --single-transaction --quick --routines --triggers \
  --all-databases | gzip > /srv/backup/db/mysql-all.sql.gz

For a large, busy database where a nightly dump is too slow or you need point-in-time recovery, a WAL-based tool such as pgBackRest is the heavier, purpose-built option. Our production database server guide covers that setup. For most servers, a nightly dump plus restic is plenty.

Automating the nightly backup

Tie the pieces together in one script: dump the databases, run the restic backup, enforce retention, then tell a monitor you succeeded. The set -euo pipefail line matters here, because it aborts the script the moment any step fails, so a broken dump can never report success.

The backup script. Save it, make it executable, and set PING_URL to your own monitor check.

/usr/local/sbin/backup.sh
#!/usr/bin/env bash
set -euo pipefail

source /etc/restic/env
BACKUP_DB=/srv/backup/db
PING_URL="https://hc-ping.com/YOUR_CHECK_UUID"

# 1. Dump the databases into a backed-up directory
mkdir -p "$BACKUP_DB"
sudo -u postgres pg_dumpall --globals-only > "$BACKUP_DB/pg-globals.sql"
for db in $(sudo -u postgres psql -tAc \
    "SELECT datname FROM pg_database WHERE datistemplate = false"); do
  sudo -u postgres pg_dump -Fc "$db" > "$BACKUP_DB/pg-$db.dump"
done

# 2. Back up state to the restic repository
restic backup /etc /srv /home /var/www \
  --exclude-file=/etc/restic/excludes.txt \
  --tag nightly

# 3. Enforce retention and reclaim space
restic forget --prune \
  --keep-daily 7 --keep-weekly 4 --keep-monthly 6

# 4. Tell the monitor we finished cleanly
curl -fsS -m 10 --retry 3 "$PING_URL" > /dev/null

Run it on a schedule with systemd rather than cron, so failures land in the journal and the timer catches up after downtime. The service:

/etc/systemd/system/backup.service
[Unit]
Description=Nightly restic backup
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/backup.sh

The timer: nightly at 02:30 with a randomised delay, catching up if the machine was off.

/etc/systemd/system/backup.timer
[Unit]
Description=Run the nightly backup

[Timer]
OnCalendar=*-*-* 02:30:00
RandomizedDelaySec=15m
Persistent=true

[Install]
WantedBy=timers.target

Make the script executable, enable the timer, and confirm the next run is scheduled.

shell
sudo chmod +x /usr/local/sbin/backup.sh
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
sudo systemctl list-timers backup.timer

The final curl is the trick that turns silence into an alert. Register a check with a healthchecks-style service (self-hosted, or the hosted hc-ping.com), give it your schedule plus a grace period, and it will expect a ping every night. If the dump fails, the backup errors, or the machine is simply down, the ping never arrives and the monitor emails you. A backup job you never hear from is the dangerous kind: with this in place, no news is bad news, not good news.

The restore drill

An untested backup is a hope, not a backup. The only proof is a restore, so make it a habit rather than a panic move. Put a recurring entry in your calendar: once a month, restore something.

Start light. List the snapshots, then restore a subset into a scratch directory and compare it against the live copy.

List snapshots, then restore one path from the latest into a scratch directory.

shell
source /etc/restic/env
restic snapshots
restic restore latest --target /srv/restore-test --include /etc/nginx

Restoring a database is a separate step: restic gives you back the dump file, and you replay it into a fresh database.

shell
restic restore latest --target /srv/restore-test --include /srv/backup/db
sudo -u postgres createdb mydb_restore
sudo -u postgres pg_restore -d mydb_restore \
  /srv/restore-test/srv/backup/db/pg-mydb.dump

The test that actually matters is the full one: can you bring the service back on a machine that is not the original. Once a quarter, rehearse it end to end.

Rehearsing a full restore

This is the drill that tells you your real recovery time. Assume the production server is gone and rebuild the service from nothing but your backups.

  1. Get a clean machine

    A spare server or a local VM running the same Ubuntu 24.04. Do not reuse anything from the original.

  2. Bring your credentials from offline

    Install restic, then supply the repository location and password from your password manager, not from the dead server.

  3. Restore the state

    Run restic restore latest into place: /etc, uploads, application config, and the database dumps.

  4. Rebuild the rest

    Reinstall packages and redeploy your code from git, then drop the restored config back on top.

  5. Replay the databases

    Load the dumps into a fresh database instance and check the row counts look right.

  6. Prove it serves

    Point a test hostname at the machine and confirm the application actually works, not just that the files exist.

  7. Write down the time

    How long that took is your real recovery time. Now you can plan around a true number instead of a guess.

Protecting the backups themselves

Backups hold your most sensitive data in one place, and a compromised server will try to destroy its own history. Two defences carry the weight.

Encryption is already handled: restic encrypts every repository client-side before upload, so nobody with access to the backup host or the bucket can read your data without the password. That is also why keeping the password offline is not optional. Store it in your password manager and, for a company, a printed copy in a safe. It is the one credential that must survive the loss of every server.

The second defence stops a hacked server from wiping its own backups. Start by limiting the SSH key the backup uses so it can only connect from this server's IP.

On the backup host, prefix the backup key in the backup user's authorized_keys with restrictions. Replace the IP with your server's address.

~backup/.ssh/authorized_keys
restrict,from="203.0.113.10" ssh-ed25519 AAAAC3Nza...key... web01-backup

That reduces the blast radius but does not make the repository append-only: over SFTP the key can still delete snapshots. For real append-only protection, do not rely on SFTP. Run restic's rest-server with --append-only on the backup host, so the nightly job can add snapshots but never remove them, or use an S3-compatible bucket whose access key has no delete permission and object lock switched on. Keep the one full-access credential that is allowed to prune on the backup host or an offline admin machine, never on the server being backed up. Then a root compromise can encrypt the live server but cannot reach back and erase last week's clean copy.

Retention, dedup and cost

The retention policy in the script keeps 7 daily, 4 weekly and 6 monthly snapshots: up to seventeen restore points reaching back half a year. That sounds like seventeen full copies. It is not, because restic deduplicates at the block level, so an unchanged file costs nothing after its first snapshot. Real usage is roughly one full copy plus the sum of what actually changed each day.

7
daily snapshots kept
4
weekly snapshots kept
6
monthly snapshots kept
1
offsite copy, minimum

Size the backup target at roughly your live data footprint plus 30-50% for history and churn, more if a database rewrites a lot of pages each night. Check the real numbers with restic itself: restic stats latest shows the size of a restore, and the raw-data mode shows what the repository physically stores after deduplication.

For the offsite copy, a second dedicated server with many large drive bays in RAID 6 is a cheap, roomy restic target that sits in a different rack from your production box. Our backup and storage servers are built for exactly this. Object storage works too, and you can run both: SFTP to a storage server for fast local restores, plus an S3 bucket in another region for the truly bad day.

What each layer protects against

The layers are not alternatives, they are a stack. Each one covers a gap the others leave open.

RAID Local snapshot Nightly restic Offsite copy
Disk failure Covers, stays up No Recovers Recovers
Deletion or bad deploy No Covers Covers Covers
Ransomware No Only if untouched Covers if append-only Covers
Datacenter-level event No No No Covers
Time to recover None, no downtime Seconds Minutes to hours Hours

The short version

RAID keeps you running through a dead disk and nothing else. Backups cover the mistakes and the attacks; only an offsite copy covers losing the whole rack. Build the stack in order: mirror the disks, dump your databases, run restic nightly to a second server, keep one append-only copy somewhere else, and restore from it every month. The strategy is not the backup you took, it is the restore you have tested.

Continue reading

Need help choosing?

Our experts can help you find the perfect server for your needs.

Contact us »