The server went down with the data center
The host stopped answering, and there was no backup. Now the database, uploads, and .env go to two S3 buckets: the archive is encrypted, and old copies are deleted.
In October several Yandex data centers stopped after drone attacks. Yandex described it: on 8–11 October the sites in Sasovo, Kaluga, and Vladimir were hit. My server was in one of them. It did not answer on the network, and the disk was out of reach.
The code was in git, so I pulled it onto a new machine. The database, images, and .env lived only on the old disk. There was no backup. I rebuilt the config from memory and pointed DNS at the new server. Whatever was on the old disk is gone.
There is a backup now. This is how it is built.
What gets copied
I do not copy the code. It is in git. I copy the things that leave a site empty after git clone:
- The SQLite database
cms/.tmp/data.db: pages, entries, CMS users. public/uploads: files from the admin. They are not in git..env: keys and settings. Without it the app comes up with different secrets.
The script
#!/usr/bin/env bash
set -euo pipefail
STAMP=$(date +%Y%m%d-%H%M%S)
BUCKET=backups
REMOTES=(s3-a s3-b)
CMS_DIR="$HOME/cms"
KEEP_DAYS=30
TMP_DB="/tmp/data-$STAMP.db"
TMP_TAR="/tmp/cms-$STAMP.tar.gz"
TMP_AGE="/tmp/cms-$STAMP.tar.gz.age"
cleanup() { rm -f "$TMP_DB" "$TMP_TAR" "$TMP_AGE"; }
trap cleanup EXIT
sqlite3 "$CMS_DIR/.tmp/data.db" ".backup '$TMP_DB'"
tar -czf "$TMP_TAR" \
-C "$CMS_DIR" public/uploads .env \
-C /tmp "data-$STAMP.db"
age -r "$AGE_RECIPIENT" -o "$TMP_AGE" "$TMP_TAR"
failed=0
for REMOTE in "${REMOTES[@]}"; do
if rclone copy "$TMP_AGE" "$REMOTE:$BUCKET/backups/" --s3-no-check-bucket \
&& rclone ls "$REMOTE:$BUCKET/backups/" | grep -F "$STAMP" \
&& rclone delete "$REMOTE:$BUCKET/backups/" --min-age "${KEEP_DAYS}d"
then
continue
fi
echo "backup failed: $REMOTE" >&2
failed=1
done
if [[ "$failed" -ne 0 ]]; then
exit 1
fi
if [[ -n "${HC_UUID:-}" ]]; then
curl -fsS --retry 3 "https://hc-ping.com/${HC_UUID}"
fi
echo "OK backup $STAMP"
Almost every line is there to stop a broken or empty backup.
Why each piece is there
set -euo pipefail. The script stops on the first error, before anything is uploaded. Without it, sqlite3 can miss the database while tar and rclone still run, and an empty file lands in the bucket. pipefail is there because of rclone ls | grep: without it, an rclone error disappears. The OK line is at the end. If it is not in the log, the script did not get there.
trap … EXIT. Temporary files are removed on success and if the script dies in the middle. Otherwise /tmp fills up with databases and archives that contain .env.
.backup instead of cp. While you copy, the site is still writing to the database. cp can catch the middle of a write: the file looks whole and then fails to open. .backup asks SQLite itself to make the copy, and that copy stays consistent even while writes are happening.
STAMP in the file name. Each run creates a file with its own name and does not overwrite yesterday’s. The script uses the stamp to check that the file arrived. Because of that, archives pile up. rclone sync does not clean them: sync would make the bucket a copy of the source, the source is one fresh file, and the old backups would be deleted. rclone delete --min-age removes them instead. The same lifetime can be a bucket lifecycle rule if you would rather not delete from the script.
age before the upload. The archive contains .env. A private bucket hides it from other people’s requests, but not from the provider, and not from someone who leaked the bucket keys. The age public key lives on the server, in AGE_RECIPIENT. The private key is not stored on this disk: otherwise encryption is useless when the disk itself disappears. An rclone crypt remote works instead of age.
-C in tar. The archive gets short paths (public/uploads, .env), not absolute ones. On another machine the files land where they should. The database is in /tmp, so it gets a second -C.
rclone copy, not sync. copy only adds today’s file. --s3-no-check-bucket is there because some S3 endpoints error on a bucket check even when the write succeeds.
Two stores and one shared exit code. These are two different S3 endpoints with separate keys. If the first one does not answer, the loop continues and the second one still gets today’s copy. The error is remembered. At the end the script exits with code 1, even if one of the stores accepted the file. grep -F "$STAMP" looks in the bucket for this run’s file.
OK and a ping. HC_UUID is a check on healthchecks.io. The ping goes out only after both stores. Cron runs the script once a day, for example 15 4 * * * /home/deploy/backup-cms.sh. If the ping is missing, an email arrives. The remote config, AGE_RECIPIENT, and HC_UUID live on the server. They do not go into the repository.
How I check a restore
Once a quarter I download an archive somewhere that is not the production server. I decrypt it, unpack it, and ask the database whether it is intact:
rclone copy "s3-b:$BUCKET/backups/cms-$STAMP.tar.gz.age" /tmp/
age -d -i "$HOME/.config/age/key.txt" -o /tmp/cms.tar.gz "/tmp/cms-$STAMP.tar.gz.age"
mkdir -p /tmp/restore
tar -xzf /tmp/cms.tar.gz -C /tmp/restore
sqlite3 "/tmp/restore/data-$STAMP.db" "PRAGMA integrity_check;"
integrity_check should answer ok. Then I check that public/uploads has files and that .env opens. A backup that has never been brought up this way is, to me, an untested backup.