Skip to main content

How to Back Up a Website: What to Copy, the 3-2-1 Rule and Testing the Restore

In this article

To back up a website you copy two things: the database, which holds the orders, accounts and content, and the uploaded files. Those are the images and documents people have added since launch. The code itself should already live in a git repository, so it does not need a backup job of its own. Keep three copies of the data, on two different kinds of storage, with one of them off-site: the 3-2-1 rule. Automate the copy so it runs every night, and restore it somewhere every month to prove it works.

That last step is the one that separates a backup from a hope. A copy that nobody has turned back into a working site is an untested assumption. The first real test is the worst moment to find out the dump is from the wrong database or the encryption key was only ever on the server that died.

This guide lists what a site is made of, explains the 3-2-1 rule precisely. It then walks through an automated nightly backup for a Django site, and sets out a restore test you can run in under an hour.

What a website is made of

People say "back up the website" as if it were one file. It is at least seven things, and they live in different places and change at different speeds.

PartWhere it livesChangesHow to protect it
Code and templatesGit repository (GitHub, GitLab)Every deployAlready versioned; tag releases, keep a second remote
DatabasePostgreSQL on the server or a managed instanceEvery order, sign-up and editNightly dump, shipped off-site; hourly or continuous for a busy shop
Uploaded mediaA media folder or an object storage bucketWhenever someone uploadsSync to a second bucket with versioning on
Configuration and secretsEnvironment file, settingsRarelyStored in a password manager, never only on the server
Server configurationNginx, systemd, cron filesRarelyKept in the repository or a deploy script that recreates them
DNS recordsRegistrar or CloudflareRarelyExport once and after every change
TLS certificatesServerEvery 60 to 90 daysNo backup needed; reissue from Let's Encrypt

The database and the media are the two that cannot be recreated from anywhere else, so they are what "the backup" means in practice. The rest is about being able to rebuild the server quickly, which is the subject of the disaster recovery plan template.

The 3-2-1 rule, precisely

The rule is short enough to remember and specific enough to audit against.

  • Three copies, counting the live data. So the live database plus two backups, not the live database plus one.
  • Two kinds of storage. The server's own disk and an object storage bucket count as two. Two folders on the same disk do not, because the disk is the thing that fails.
  • One off-site. A different provider, or at least a different region and a different account. The "different account" part matters more than it used to. If a stolen hosting password can delete the server and its backups in the same session, the backup was never really off-site.

A sensible modern addition is that one copy should be immutable for a period. Object storage from AWS, Backblaze, Hetzner and others can lock a version. It then cannot be deleted or overwritten for a set number of days, even by the account owner. That is the defence against ransomware that encrypts the live site and then reaches for the backups.

Setting up an automated nightly backup

These steps assume a Django site with PostgreSQL on a VPS. The same shape works on a managed platform, where the platform's own database snapshots become one of the copies rather than the only one.

1. Dump the database

Use pg_dump in custom format, which compresses the output and lets pg_restore rebuild tables selectively. Run it as a user with read access only.

pg_dump -Fc -h localhost -U backup shopdb > /var/backups/shopdb-$(date +%F).dump

Dumps made with one PostgreSQL major version restore cleanly to the same or a newer version, not an older one. So note the version in the file name if you run more than one.

2. Copy the uploaded media

If uploads live on the server, sync the media folder to a bucket with rclone or the provider's command line tool. If uploads already live in a bucket, turn on versioning there and replicate it to a second bucket in another account. Versioning means an accidental delete or an overwritten image can be undone without touching the nightly job.

3. Encrypt before it leaves the server

A database dump contains every customer's name, address and order. Encrypt it with age or GPG before upload, and keep the decryption key in the password manager and with whoever is responsible for recovery. A key that exists only on the server protects the backup from everyone, including you.

4. Ship it off-site with a retention rule

Upload to a bucket in a different account or provider from the hosting. Set a lifecycle rule so the bucket keeps every nightly copy for a month and one copy a month for a year, then deletes the rest. Keeping everything forever is not a policy; it is a bill and a data protection problem.

5. Schedule it and watch it

Run the script from a systemd timer or cron in the small hours, and have the last line of the script ping a dead-man's-switch monitor such as Healthchecks.io. A monitor that expects a ping and alerts when it does not arrive catches the failure mode that matters: the job quietly stopped running three weeks ago. Backup scripts that email on failure never email about the night they did not run at all.

set -euo pipefail
DAY=$(date +%F)
pg_dump -Fc -U backup shopdb | age -r "$BACKUP_PUBKEY" > /tmp/shopdb-$DAY.dump.age
rclone copy /tmp/shopdb-$DAY.dump.age offsite:shop-backups/db/
rclone sync /srv/shop/media offsite:shop-backups/media/
rm /tmp/shopdb-$DAY.dump.age
curl -fsS -m 10 https://hc-ping.com/your-check-id > /dev/null

If your site is on a platform you do not control

The job is harder when you cannot reach the database. Shopify backs up its own infrastructure but offers no store-level restore to merchants. The useful backup is therefore a scheduled export of products, customers and orders as CSV plus a download of the theme. WordPress hosts usually offer snapshots; add a database export you control, because a snapshot held by the host disappears with the account. Wix does not let you export the site at all, which is worth knowing before you commit to it.

Whatever the platform, the test in the next section still applies. A CSV you have never tried to import is in the same category as a dump you have never restored.

Testing the restore

A restore test once a month, on a staging server rather than the live one, takes under an hour once the steps are written down.

  1. Create a fresh database and restore last night's dump into it with pg_restore. Note how long it takes; that number is the first input to your recovery time objective.
  2. Sync the media from the off-site bucket to the staging server.
  3. Point a staging copy of the site at the restored database and open it in a browser.
  4. Check the three things that prove the data is real: the most recent order or enquiry is present, an admin login works, and an image uploaded yesterday loads.
  5. Record the date, the duration and anything that went wrong.

The things a restore test finds are rarely dramatic and always worth finding early.

  • A dump made with a newer PostgreSQL than staging runs.
  • A database extension missing from the new server.
  • A media bucket the staging server has no permission to read.
  • A secret key nobody wrote down.

The same pg_dump and pg_restore routine is the backbone of moving a Django database between servers, so practising it on backups pays off twice.

Once a year, go further and rebuild the whole server from nothing using only the repository, the deploy script and the backups. That is the test that catches the undocumented package someone installed by hand years ago and never mentioned.

Retention and personal data

Backups contain personal data, so they are covered by UK GDPR like everything else. Decide how long you keep them, write it into the retention schedule in your privacy policy, and make sure the lifecycle rule enforces it.

When someone asks for their data to be erased, the ICO's guidance accepts that the copy in a backup will be removed when that backup expires. The condition is that you do not restore it back into use. The point is to have a cycle, not to keep every copy indefinitely. Access to the backup bucket should be limited to the people who would actually run a restore, which is one of the items on the website security checklist.

Questions people ask

How often should I back up my website?

Nightly is the minimum for any site that changes. A shop or booking site should dump the database more often, or use PostgreSQL's continuous archiving so that the most you can lose is a few minutes of orders. Media can be synced nightly because it is rarely overwritten.

Is my hosting provider's backup enough?

It is one copy, held by the same company, in the same account, and usually on the same infrastructure. Treat it as the convenient copy for a quick rollback. Keep your own off-site copy for the day the account is suspended, the provider has an outage or the login is compromised.

Where should website backups be stored?

In object storage with a different provider or account from the hosting, in a UK or EU region, encrypted before upload. Versioning or object locking should be on so a deleted copy can be recovered.

How long should I keep website backups?

A month of nightly copies and a year of monthly ones covers almost every business, including the slow-burning problems such as a corruption nobody noticed for six weeks. Longer only if a regulator or contract requires it.

How we handle this on our own builds

Every site on our maintenance plan gets a nightly encrypted database dump and media sync to off-site storage. We restore it for real on a schedule rather than trusting the green tick. A restore we have timed is the only honest answer to "how long would we be down". The monthly note to the client records when the last restore ran.

If you look after your own site, the one thing to do this week is restore last night's backup to a scratch server and open it in a browser. Either it works, in which case you can sleep, or it does not, in which case you have found out at a time of your choosing.