Sub topic 1
What a server backup must achieve
A backup is a copy of your data that you can use to get a service running again after something goes wrong. Many servers have copies that nobody has ever restored from. This chapter sets the goals a backup has to meet before you pick a tool. The later chapters build restic and Borg setups against these goals.
Prerequisites
This chapter assumes you can:
- Work on the Linux command line and read simple shell scripts.
- Log in over SSH with keys.
- Read a systemd unit file and check file ownership and permissions.
- Say roughly what lives under
/etc,/homeand/var.
You don't need any restic or Borg experience yet.
What you are protecting against
A backup protects against more than a failed disk. Each failure below needs a different property from your backups.
| Failure | Example | What the backup must provide |
|---|---|---|
| Hardware loss | Disk dies, host is deleted by the provider | A copy on different hardware |
| Human error | `rm -rf` in the wrong directory, bad config edit | Older versions, not just the latest state |
| Software corruption | A buggy migration damages a database | Older versions from before the damage |
| Ransomware or intruder | Attacker encrypts or deletes files, including backups reachable from the server | A copy the compromised server cannot overwrite or delete |
| Site loss | Fire, flood, account suspension | A copy in another location or under another account |
Redundancy is not backup. RAID and replicated disks protect against a disk failing. They copy a deletion or a corruption to every disk within seconds. A snapshot on the same storage as the data fails together with it. A backup needs history and separation from the thing it protects.
The 3-2-1 rule
The 3-2-1 rule is a checklist for separation:
- Keep 3 copies of your data: the live data plus two backups.
- Store them on 2 different kinds of storage or systems, so one shared fault cannot destroy both.
- Keep 1 copy off site, away from the server's location and ideally outside its administrative control.
For a typical VPS this could mean:
- The live data on the server.
- A restic or Borg repository on a backup host you run, reached over SSH.
- A second repository in object storage at a different provider.
Many teams add a fourth point: at least one copy should be unreachable by the server's own credentials, or append-only. If an intruder gets root on the server, they can use whatever credentials the server holds. A repository that the server can write to but not delete from limits the damage. Later chapters cover how to set that up.
A common mistake is counting a second copy that shares a failure with the first. A backup written to another directory on the same disk is not a second copy. A repository on a second volume that the same cloud account controls is a weaker copy than one under a separate account.
Recovery point objective and recovery time objective
Two numbers turn "we have backups" into a requirement you can check.
- Recovery point objective (RPO) is how much recent data you can afford to lose. It is measured backwards from the failure. An RPO of 24 hours means that after a failure you may lose up to a day of changes. The backup interval sets a ceiling on this. Daily backups mean a worst-case loss of just over 24 hours, plus however long the backup run takes.
- Recovery time objective (RTO) is how long the service can stay down before the damage becomes unacceptable. It covers everything between the failure and a working service: noticing the problem, provisioning a server, installing software, restoring data, and checking that it works.
RPO and RTO pull in different directions on cost. A one-hour RPO needs hourly backups, more storage traffic and more load on the server. A short RTO needs a tested procedure, a fast restore path, and often a standby server. Ask the owner of each service how much data loss and downtime is acceptable. Then write the numbers down. Most "we need real-time backup" statements become "a day is fine for the wiki, fifteen minutes for the orders database" once someone asks.
Different data on one server can have different targets:
| Data | Example RPO | Example RTO | Consequence for the design |
|---|---|---|---|
| Configuration in `/etc` | 24 hours | 4 hours | Daily file backup is enough |
| Web content in `/var/www` | 24 hours | 4 hours | Daily file backup is enough |
| Orders database | 15 minutes | 1 hour | Needs more than daily dumps, such as WAL archiving or replication |
| User uploads in `/home` | 24 hours | 8 hours | Daily backup, restored after the database |
Snapshot-style tools like restic and Borg can run frequently, but they are best at periodic backups. If your database RPO is measured in minutes, treat these tools as one layer and add database-level replication or log archiving. The chapter on databases returns to this.
Deciding what to back up
Back up what you cannot recreate. Rebuild everything else from automation. A useful sort:
- Irreplaceable state: databases, uploaded files, user home directories, mail, application data directories.
- Hard-to-recreate configuration:
/etc, TLS certificates and account keys, crontabs, systemd units you wrote, firewall rules. - Replaceable: the operating system packages, container images, build artifacts, caches. Record what was installed instead, with a package list or a configuration management repository.
Sizing the data early tells you how much storage and transfer time to plan for. This command reports the size of each candidate path. The -x flag stops du from crossing into other file systems, and -s prints one total per path.
sudo du -xsh /etc /home /var/www /var/lib 2>/dev/nullIf you also need a list of what packages the server depends on, save it alongside the data. On Debian or Ubuntu, dpkg --get-selections produces one. On Fedora or RHEL, rpm -qa does.
What a backup must do
Putting this together, a server backup must meet these requirements:
- Meet the RPO. It runs often enough, and finishes quickly enough, that the newest recoverable copy is never older than the target.
- Keep history. It retains several past versions so you can recover from a mistake you notice days later.
- Be separate. At least one copy is off site and cannot be changed by a compromised server.
- Be confidential. Backups contain password hashes, keys and private data. They must be encrypted before they leave the server, and the key must be protected.
- Be consistent. Files captured while an application is writing them can be unusable. Databases need a dump or snapshot method that gives a coherent state.
- Be restorable within the RTO. You have practiced the restore, you know how long it takes, and the instructions do not live only on the server that failed.
- Report failure. If a run fails or stops happening, someone finds out the same day, not at restore time.
The first six describe the backup itself. The seventh is the one most often skipped, and it is the cause of many silent failures: a full disk, an expired credential, or a timer that was never enabled.
A quick self-audit
Before choosing a tool, answer these questions for your server in writing:
- What are the RPO and RTO for each kind of data?
- Where is each of the three copies, and which of them would survive losing the server's credentials?
- Who knows where the repository passphrase is, and is a copy stored outside the server?
- If the server vanished now, what is the first command you would run to get a replacement serving traffic?
- How would you learn tomorrow that tonight's backup did not run?
If you cannot answer one, that is the first gap to close. The rest of this tutorial turns each requirement into a working configuration: choosing between restic and Borg, setting up and checking repositories, retention, scheduling, alerts and recovery drills.
Exercise
A server runs a web application with a PostgreSQL database. The owner says the site can be down for at most 2 hours and can lose at most 30 minutes of orders. Backups run once a day at 02:00 to a second disk in the same machine. Name the RPO and RTO, and give two ways the current setup fails the 3-2-1 rule or the stated goals.
Show answer
The RPO is 30 minutes and the RTO is 2 hours. Daily backups give a worst-case data loss of about 24 hours, so they miss the RPO. The backup also sits on the same machine as the live data, so a host loss, deletion or intruder takes both copies. That is one location, not an off-site copy, and it provides only 2 of the 3 copies, which are the live data and one local backup. Fixes include more frequent database backups or log archiving, plus a repository on another host or provider.
Exercise
Why does RAID mirroring not replace backups? Give one failure it handles and one it does not.
Show answer
RAID mirroring keeps the array running when a single disk fails, so it handles hardware failure of one disk. It does not handle accidental deletion, corruption or ransomware, because it copies those changes to every disk immediately. It also keeps no history and does not protect against loss of the whole machine.