Help Running your server Care and troubleshooting
Back up your server
Decide what to keep, back it up every night with restic to S3-compatible storage, and prove you can restore it.
A backup is a copy of your data somewhere else, that you've tested you can get back. We don't back up servers for you yet (it's coming), so this guide sets up nightly backups you run yourself.
What to back up#
The system reinstalls in minutes. What you can't get back is this:
| What | Where | Why |
|---|---|---|
| Settings | /etc | Web server, SSH, firewall and the rest |
| Your files | /home, /root | Users' files and your scripts |
| Apps and websites | /opt, /var/www | With Docker, each app's folder and its data |
| Databases | A dump, in /var/backups/db | A running database's files may not open; a dump always does |
Set up restic#
restic encrypts backups before they leave the server, stores only what changed, and keeps a history you can restore from by date, in any S3-compatible bucket or on another server over SFTP.
Ubuntu / Debian
sudo apt install -y resticAlmaLinux / Rocky
sudo dnf install -y epel-release && sudo dnf install -y resticCreate a bucket with your storage provider, and an access key for it. You need the endpoint, the bucket name, the key ID and the secret.
Make a folder for restic's settings, readable only by root:
sudo install -d -m 700 /etc/resticMake a long random password that encrypts the backups:
sudo sh -c 'openssl rand -base64 32 > /etc/restic/password && chmod 600 /etc/restic/password'Save the storage details, with your own in place of the examples:
sudo tee /etc/restic/env <<'EOF'
RESTIC_REPOSITORY=s3:https://s3.example.com/web-01-backups
RESTIC_PASSWORD_FILE=/etc/restic/password
AWS_ACCESS_KEY_ID=replace-with-your-key-id
AWS_SECRET_ACCESS_KEY=replace-with-your-secret
EOFsudo chmod 600 /etc/restic/envThe AWS_ names work for any S3-compatible storage. For SFTP instead, set RESTIC_REPOSITORY=sftp:[email protected]:/backups/web-01, leave out the two keys, and make sure the server can log in there with an SSH key.
Now become root, load the settings, and create the repository:
sudo -iset -a; . /etc/restic/env; set +arestic initThe backup script#
This dumps your databases, backs up the folders that exist, and keeps one snapshot a day for a week, a week for a month, and a month for six months:
sudo tee /usr/local/bin/backup <<'EOF'
#!/bin/sh
set -eu
set -a; . /etc/restic/env; set +a
# Database dumps go here first. Uncomment the one you use.
mkdir -p /var/backups/db
# runuser -u postgres -- pg_dumpall > /var/backups/db/postgres.sql
# mysqldump --all-databases --single-transaction > /var/backups/db/mysql.sql
paths=""
for p in /etc /home /root /opt /var/www /var/backups/db; do
[ -e "$p" ] && paths="$paths $p"
done
restic backup --exclude-caches $paths
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
EOFsudo chmod 700 /usr/local/bin/backupAdd any other data folders to the for line, then run it once by hand:
sudo /usr/local/bin/backupThe first run sends everything. After that, only changes go.
Run it every night#
A systemd timer runs it at 3:30am, give or take half an hour, or when the server is back if it was off.
sudo tee /etc/systemd/system/backup.service <<'EOF'
[Unit]
Description=Back up the server with restic
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup
EOFsudo tee /etc/systemd/system/backup.timer <<'EOF'
[Unit]
Description=Back up the server every night
[Timer]
OnCalendar=*-*-* 03:30:00
RandomizedDelaySec=30m
Persistent=true
[Install]
WantedBy=timers.target
EOFsudo systemctl daemon-reload && sudo systemctl enable --now backup.timerSee when it runs next, and how the last run went:
systemctl list-timers backup.timersudo journalctl -u backup -n 50 --no-pagerRestore something#
A backup you've never restored from is a hope. Try it now, and every few months.
As root, with the settings loaded (sudo -i, then the set -a line above), list your snapshots:
restic snapshotsSee what's in the latest one:
restic ls latest /etc/caddyRestore a folder into /tmp/restore, so you can look before copying anything back:
restic restore latest --target /tmp/restore --include /etc/caddyThe files land under /tmp/restore/etc/caddy. To restore from an older date, use a snapshot's ID from the list instead of latest.
Every so often, check the repository itself is healthy:
restic checkSimpler: a copy on another machine#
rsync can copy your folders to another always-on Linux machine. It keeps only the latest copy, so a mistake is copied over too the next night. Better than nothing; restic is better.
rsync -aAX --delete /etc /home /opt /var/www [email protected]:/backups/web-01/See Move files to and from your server for how rsync works.
Still stuck? Open a ticket and say which guide you followed.