Backing Up Your Server¶
Approximate Time: 30 minutes
Prerequisite: Configuring your Server
What to Back Up¶
It isn't important to back up the entire operating system.
If the server needs to be rebuilt, such as when changing providers or in the case of a hardware failure, you'd build the server again and reinstall things like fail2ban and docker as you did.
Better yet, you're using a declarative build and can reinstall everything with a single command.
Presumably your code is checked into git, so backing the code up isn't important either.
The only things that you need to back up are files created on the server (such as user uploads), your databases, and your ~/compose directory if it isn't tracked in git.
Where to Store Backups¶
A commonly cited rule for backups is the 3-2-1 rule: 3 copies of your data, 2 different kinds of storage, 1 offsite.
For a Postgres database this might mean:
- The live data in the database is our first onsite copy.
- Storing a second copy of the data locally, our second onsite copy, useful for local restores.
- Storing a third copy on a different service, useful if we lose access to our server or even Hetzner account. We'll be using object storage for our offsite backups, which is distinct from the local drive based storage on the Hetzner instance. This covers the second kind of storage as well.
Object Storage¶
Most cloud providers offer an "object storage" service. These services allow storing files in a redundant storage service where data loss due to hardware failure is almost impossible. They bill at around pennies per GB/mo and support a common interface based on Amazon S3's original implementation.
Backblaze B2¶
Backblaze B2 is object storage that's great for low cost backups.
Choices?
There are a lot of choices to make: registrar, host, operating systems, various third-party services. It can be overwhelming.
To help, our guides typically make a general recommendation that we think is a safe choice for most people as well as a few alternatives to consider.
Our resource guide also provides other reasonable choices you can consider.
Create a Bucket¶
Visit https://secure.backblaze.com/b2_buckets.htm
Give the bucket a unique name. Something like yoursite-backups should work.
Leave the other settings the same. It is especially important to leave 'Files in Bucket' as private.
Create a Key¶
Visit https://secure.backblaze.com/app_keys.htm
Set a descriptive name for the key, something like yoursite-backups-key.
To increase security, you can restrict the key's access to the bucket you just created.
Leave the rest of the options as their defaults.
This will give a keyID that looks like
00041e5476d1f9a000000000d.
And an applicationKey resembling K0001B0kq99Qa+aWT8rt1pc0W115lo0.
You'll also need your bucket's endpoint, shown on the bucket's details
(e.g. s3.us-west-002.backblazeb2.com).
Save these values in your password manager. It is essential you keep them private.
Warning
If your key is made public, even temporarily, delete the key and create a new one.
Bots are constantly scanning sites like GitHub for cloud credentials. If you expose these credentials for even a few seconds, consider them compromised.
People can not only access your files, but hijack your account for their own purposes.
restic¶
We are going to use a program called restic to manage taking and storing backups.
Then we will create a random password. Either use your password manager or run:
Then create a file ~/.restic-env containing:
export AWS_ACCESS_KEY_ID='<keyID>'
export AWS_SECRET_ACCESS_KEY='<applicationKey>'
export RESTIC_REPOSITORY='s3:https://<endpoint>/<bucket-name>/<server-name>'
export RESTIC_PASSWORD='<random-password>'
Once created, run chmod 600 ~/.restic-env to limit access to the current user.
Save this Password!
This password will be used to encrypt our backups; without it our backups are useless!
Save this password in your password manager right now.
Create the Repository¶
restic needs a repository (an encrypted store inside your bucket) before the first backup.
You'll need to run this once:
You should see a message like created restic repository at s3:https://s3.us-west-002.backblazeb2.com/yoursite-backups/server-name.
To confirm it worked:
An empty list with no errors means the repository exists and your credentials work.
Simple Backup Script¶
mkdir ~/backups then place this script at ~/compose/backup.sh:
#!/bin/sh
set -e
. ~/.restic-env
# back up our Miniflux database to a local file
cd ~/compose
docker compose exec -T db pg_dump -U miniflux -Fc miniflux > ~/backups/miniflux-$(date +%F).dump
# CHANGE: anything that you want to back up should wind up in this ~/backups directory. More databases? Locally created files?
# back up everything in ~/backups and ~/compose to our restic repository
restic backup ~/backups ~/compose
# optional: don't keep backups forever
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
# clean up old backups from disk
find ~/backups -name '*.dump' -mtime +7 -delete
Once created run: chmod 700 ~/compose/backup.sh to make the script executable and only accessible to the current user.
Warning
The key on your server can delete backups, since restic forget --prune needs that permission.
If an attacker gets into your server, they could erase your backups too.
Backblaze's Object Lock can prevent this if it is a concern.
Verifying Backups¶
It is important to verify the backups work as intended.
Before proceeding, you will need to install restic on your laptop. You will also need to create a ~/.restic-env on your laptop. It should be identical to the one you created on the server.
On the server run ~/compose/backup.sh and wait for it to complete successfully.
After it does, on your laptop, run:
. ~/.restic-env
restic snapshots # list backups; newest should be recent
restic check # ensure repository structure is what you expect
restic check --read-data-subset=5% # download and verify 5% of the data
A more complete check would be to restore the backup and inspect the data:
restic restore latest --target /tmp/restore-test
ls /tmp/restore-test/home/ # confirm expected files are present
pg_restore --list <path-to-pgdump> # confirm dump file is readable
Scheduling Regular Backups¶
Now that we know our backups work, we can use cron to schedule regular backups:
crontab -e will open a special file that tells cron what to run when.
If there are existing lines leave them alone, add the new line:
0 3 * * * ~/compose/backup.sh >> ~/backups.log 2>&1
This will tell cron to run your backup script every day at 3am (in the server's timezone), appending all output to the file ~/backups.log. Feel free to change the time or log location for your own purposes.
Confirming cron Is Running¶
After a day or two, revisit the steps to verify your backups to make sure you're seeing continuous updates.
If you aren't seeing what you expect, check journalctl -u cron --since today to see if the job itself ran.
You can also check ~/backups.log to see if any error messages print. If backups are running on their own, you're done for now. It is probably a good idea to set a reminder to check a backup every month or two.