DEV Community

Cover image for Linux Server Backup That Actually Restores
Shuvo Halder
Shuvo Halder

Posted on

Linux Server Backup That Actually Restores

Linux Server Backup That Actually Restores

Every engineer has the same story: the backup job ran green for a year, the disk died on a Tuesday, and the restore failed in the first five minutes. The backup was never the problem. The restore was — because nobody had ever actually done one.

A backup you have never restored is not a backup. It is a hope with a cron job attached. This is the backup and restore system I run in production, the one I test every month, and the one I would defend in a DevOps interview.

RTO and RPO: The Two Numbers That Decide Everything

Before you pick a tool, you need two numbers, agreed with whoever owns the service:

  • RPO (Recovery Point Objective) — how much data you can afford to lose. An RPO of 1 hour means backups at least every hour. An RPO of 24 hours means a nightly backup is fine.
  • RTO (Recovery Time Objective) — how fast the service must be back. An RTO of 1 hour means you need a restore procedure that actually completes in under an hour, not one that takes six hours to download from cold storage.

These two numbers decide everything downstream: backup frequency, storage tier, and whether you need hot standby instead of backups at all. A backup is not a high-availability strategy — if your RTO is five minutes, you need replication, not a tarball. Write the RPO and RTO down, get them agreed, and design to them. Most backup failures I have seen were really RTO failures: the data was there, but the restore took four times longer than the business could wait.

The 3-2-1-1-0 Rule

The classic 3-2-1 rule still holds, and the modern extension makes it ransomware-proof:

  • 3 copies of your data: the original plus two backups.
  • 2 different media types: local disk and object storage, for example — so one failure mode cannot take both.
  • 1 copy off-site: a different region, a different provider, or at minimum a different building.
  • 1 copy offline or immutable: object lock, write-once storage, or a detached drive. Ransomware encrypts everything it can reach — an immutable copy is the only one it cannot touch.
  • 0 errors: backups verified with zero errors. A backup job that "mostly worked" is a failed backup.

The last two are the ones teams skip, and they are the ones that matter when it counts. A backup on the same server it protects is not a backup. A backup in the same cloud account as production is one stolen credential away from being deleted along with production.

What to Back Up (and What to Leave Out)

Back up the things you cannot recreate:

  • Application data — databases, uploaded files, user content. This is irreplaceable.
  • Configuration — /etc, application configs, nginx, systemd units, firewall rules. A server rebuilds in minutes if the config is safe; it takes days if it is not.
  • Secrets inventory, not secrets — keep a list of what exists and where the vault is. Never back up plaintext private keys to the same place as everything else.
  • The backup scripts and restore runbook themselves. If the only copy of the restore procedure is on the dead server, you do not have a restore procedure.

Leave out what you can rebuild: package caches, logs older than your retention policy, /tmp, and anything the deployment pipeline recreates. Backing up 200 GB of logs nobody reads is how a restore that should take twenty minutes takes three hours.

For databases, never back up a live database file by copying it. Use the database's own tools, with a consistent snapshot:

# PostgreSQL: a consistent logical dump, compressed, with a timestamp
pg_dump -U appuser -h localhost appdb | gzip > /backup/db/appdb-$(date +%F-%H%M).sql.gz

# MySQL/MariaDB: single transaction for a consistent InnoDB snapshot
mysqldump --single-transaction --quick -u root appdb | gzip > /backup/db/appdb-$(date +%F-%H%M).sql.gz
Enter fullscreen mode Exit fullscreen mode

A copied data/ directory from a running database is a corrupted database with extra steps. Dump it properly, and test that the dump actually imports.

The Tools: restic, borg, and the Boring Ones

You do not need an enterprise backup suite. You need a tool that encrypts, deduplicates, verifies, and has been around long enough to be boring.

restic is my default. Encrypted by design, deduplicated, and it backs up to local disk, SFTP, S3-compatible storage, and more, with the same commands:

# Initialise an encrypted repository (do this once, keep the password in a vault)
restic -r s3:https://s3.example.com/backups/web-01 init

# Nightly backup of the things that matter
restic -r s3:https://s3.example.com/backups/web-01 backup \
  /etc /var/www /home --exclude /var/www/*/cache

# Prove it works: verify the repository, then restore to a scratch directory
restic -r s3:https://s3.example.com/backups/web-01 check
restic -r s3:https://s3.example.com/backups/web-01 restore latest \
  --target /tmp/restore-test --include /etc/nginx
Enter fullscreen mode Exit fullscreen mode

borg is the alternative when the destination is a server you control over SSH: same deduplication and encryption, excellent compression, and append-only mode so a compromised client cannot delete old backups. rsync with --link-dest is the honest fallback — no encryption, no dedup across hosts, but simple, transparent, and impossible to misunderstand. And for whole-disk images, filesystem snapshots (LVM, ZFS, btrfs) give you a consistent point-in-time copy to back up from, which is the only correct way to back up a live filesystem.

Whichever tool you choose, the rule is the same: encrypt before the data leaves the server, and keep the encryption password somewhere the backup itself does not live. An unencrypted backup in object storage is a data breach waiting for a misconfigured bucket.

Automate It, Then Monitor the Automation

A backup that depends on someone remembering to run it will be forgotten the week it matters. Automate with a systemd timer (or cron), and — this is the part everyone skips — monitor the job:

# /etc/systemd/system/backup.service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh

# /etc/systemd/system/backup.timer
[Timer]
OnCalendar=daily 02:00
Persistent=true
Enter fullscreen mode Exit fullscreen mode

Then alert on three things: the job did not run, the job exited non-zero, and the repository check failed. A backup job that fails silently for three weeks is worse than no backup at all, because it gives you confidence you have not earned. Send the job's output somewhere you actually read, and add a weekly "last successful backup is older than 26 hours" alert. If nobody gets paged when backups stop, backups have already stopped.

The Restore Drill: The Only Part That Counts

This is the whole article in one section. Schedule a restore drill, monthly, and treat it like a fire drill — timed, documented, and done by the person who would do it at 3 AM.

  1. Pick a real scenario. "The database server is gone" is a scenario. "Restore a file" is a warm-up.
  2. Restore to a scratch system, never over production. Spin up a throwaway VM or container, restore there, and see what actually comes back.
  3. Verify the data, not just the exit code. restic check proves the repository is intact. It does not prove the application starts. Start the application, log in, open a record, run a query. Checksum a sample of files against the originals.
  4. Time it. If the restore takes 3 hours and your RTO is 1 hour, you have found a design problem, not a drill problem. Fix it now, not during an incident.
  5. Write down what broke. Every drill finds something: a missing config, a forgotten cron job, a secret nobody documented. That list is the most valuable output of the drill.
# A minimal monthly drill script: restore and verify
restic -r s3:https://s3.example.com/backups/web-01 restore latest --target /tmp/drill
diff -r /tmp/drill/etc/nginx /etc/nginx && echo "CONFIG OK"
gunzip -c /backup/db/appdb-latest.sql.gz | psql -U appuser -h drill-db -d appdb_restore -f - \
  && echo "DATABASE RESTORES AND IMPORTS"
Enter fullscreen mode Exit fullscreen mode

If you cannot restore from a backup, you do not have a backup. Say it in the retro, fix it, and drill again next month. The drill is the product; the backup job is just the raw material.

Production Pitfalls Checklist

The failures I actually see, in order of frequency:

  1. Never testing a restore. The number one cause of "we lost everything" stories. The backup ran fine. The restore had never been tried.
  2. Backups on the same server as production. One disk failure, one ransomware event, one rm -rf — and both copies are gone.
  3. No encryption, or the password stored with the backup. An unencrypted backup is a breach. A password in the same bucket as the data is decoration.
  4. Backing up a live database by copying files. Corrupted dumps that only reveal themselves during a restore, when it is too late.
  5. No retention policy. Disk fills up, the backup job starts failing, nobody notices until the incident. Set retention (restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune) and alert on it.
  6. Backing up everything, including the junk. Restore time is dominated by data nobody needs. Exclude caches, logs, and build artifacts.
  7. No monitoring on the backup job. Silent failure for weeks, discovered during the incident it was supposed to save you from.
  8. One cloud account, one region, one credential. A compromised account or a region outage takes production and backups together. Separate accounts, separate regions, object lock on.
  9. Secrets and configs living only on the server. The server dies, the config dies with it, and the rebuild becomes archaeology. Configs belong in version control or a config backup.
  10. Treating backup as high availability. Backups recover data. They do not keep the service up. If the RTO is minutes, you need replication and failover, not a restore job.

What Hiring Managers Actually Ask

If you are targeting remote DevOps or sysadmin roles, this topic comes up because it reveals whether you have ever actually recovered a system:

  • "Walk me through your backup strategy." (Answer with RPO and RTO first, then the tool, then the drill. Anyone can name a tool; the numbers prove you designed it.)
  • "Your database server just died. What do you do?" (Answer in order: confirm the scope, start the restore to a scratch host, verify the data, cut over, then write the incident report. Panic is not a step.)
  • "How do you know your backups work?" (Answer: "I restore them monthly, on a timer, and here is what the last drill found." That answer is worth more than any certification.)
  • "How do you protect backups from ransomware?" (Answer: immutability, offline copies, separate credentials, and a restore test that assumes the attacker already got in.)

The answer they want is never "I set up a cron job". It is the system underneath: what you back up, how fast you can get it back, and the proof that you have done it.

The Monthly Backup Routine

My monthly routine takes thirty minutes:

  1. Run the restore drill on a scratch host — database import, config diff, application login.
  2. Check retention and repository size — is it growing faster than expected, and is pruning working?
  3. Verify the off-site and immutable copies exist and are reachable with separate credentials.
  4. Review backup job failures from the last month — any silent gaps?
  5. Rotate the repository encryption password check — is it still in the vault, and does someone else on the team know where it is?

Do that, and a backup stops being a cron job you hope works and becomes a recovery you have already rehearsed. The best backup story is a boring one: the server died, we restored, nobody noticed.


Your turn: When did you last actually restore from a backup — and did it work the first time? Tell me in the comments.

Top comments (0)