Home Blog

Encrypted offsite backups that verify themselves

I had been running a password vault and a Git mirror forge on a VPS without backups. Everything lived on a single 40 GB volume. A disk failure or a bad migration could have taken out the only copy.

I had spent time securing the services but had not made a recovery plan. Backups were overdue.

I ended up with two nightly jobs, using different backup methods for the databases and the Git repositories. Both check their output before uploading it.

Encrypt before it leaves the machine

The vault's items are already encrypted client-side. But its database also contains the master password hash, which is offline-crackable. That should not sit in third-party storage in the clear, no matter how much you trust the provider.

I used age with a keypair, so the jobs do not need an interactive passphrase:

sudo age-keygen -o /etc/backup/age-identity.key
sudo chmod 600 /etc/backup/age-identity.key
sudo sh -c 'age-keygen -y /etc/backup/age-identity.key > /etc/backup/age-recipient.txt'

The decryption key lives on the VPS so the jobs can verify their archives unattended. This protects the copies at the storage provider; it does not protect against someone who compromises the VPS and can already read the live databases.

Separate backups for databases and repositories

The vault database is a few hundred kilobytes. The git mirror tree is 1.7 GB across 71 repositories, of which roughly 200 files change on any given day.

Uploading the full 1.7 GB tree every night would repeat a lot of unchanged data, while updating a single encrypted archive incrementally is awkward. I split the jobs accordingly:

rclone sync keeps a mirror without retaining older versions. If a repository is deleted locally, it is deleted remotely on the next run. I accept that for these upstream mirrors. For point-in-time recovery, I would need --backup-dir or a snapshotting tool.

Use the database's own backup command

sqlite3 "$LIVE_DB" ".backup '$STAGING/db.sqlite3'"

Both databases run in WAL mode. When I checked, a live 247 KB -wal file sat beside the main database. Copying only the main file can miss committed transactions held in the WAL. SQLite’s .backup command produces a consistent copy while the service keeps running.

The same care applies on the way back: when restoring over a live database, delete the stale -wal and -shm files, because they belong to the database you just replaced.

Every run restores its own output

Each job decrypts and unpacks the archive it just created, then checks the restored database before uploading:

age -d -i "$IDENTITY" "$ARCHIVE" | tar xzf - -C "$VERIFY" \
  || die "verification failed: could not decrypt and untar"

INTEG=$(sqlite3 "$VERIFY/db.sqlite3" 'PRAGMA integrity_check;')
[ "$INTEG" = "ok" ] || die "verification failed: integrity_check said '$INTEG'"

COUNT=$(sqlite3 "$VERIFY/db.sqlite3" 'SELECT COUNT(*) FROM items;')
[ "$COUNT" = "$LIVE_COUNT" ] || die "verification failed: $COUNT in backup vs $LIVE_COUNT live"

The checks confirm that the archive decrypts, that the restored database passes its integrity check, and that its row count matches the live database. If any check fails, the job exits non-zero before uploading.

After uploading, the job also checks that the remote object exists:

LOCAL=$(stat -c%s "$ARCHIVE")
REMOTE=$(rclone size "$REMOTE_PATH/$(basename "$ARCHIVE")" --json \
         | sed -n 's/.*"bytes":\([0-9]*\).*/\1/p')
[ "$REMOTE" = "$LOCAL" ] || die "size mismatch (local $LOCAL, remote ${REMOTE:-none})"

The job rejects snapshots below a minimum expected size:

[ "$COUNT" -gt 0 ] || die "snapshot has zero items, refusing to ship it"

This catches empty or incomplete output before it can replace a usable remote copy.

Two rclone details that cost me time

rclone config create hangs on OAuth backends. Supplying a token on the command line does not stop it trying to run the interactive browser flow, and with stdin not a terminal it blocks forever. Write the config file by hand:

[remote]
type = <provider>
hostname = <region-specific api host>
token = {"access_token":"...","token_type":"bearer","expiry":"0001-01-01T00:00:00Z"}

Providers with no token expiry can be wrapped like that from a bare access token.

Region matters and is not auto-detected when you supply a token by hand. rclone normally learns the correct API host during its own OAuth dance. Skip that and it uses the default host, which fails to authenticate for accounts in another region, with an error that looks exactly like a bad token. The provider's docs for the backend usually say which option to set.

rclone obscure is reversible obfuscation. Anyone who can read rclone.conf can recover the crypt password. As with the local age key, this setup protects the remote copy rather than the running host.

And prefer rclone sync --checksum for a git tree. The default mtime-and-size comparison misses repacks that rewrite a file to the same size, and consumer storage often has coarse mtime granularity.

The circular dependency

The recovery key needs a copy that can be read without restoring the backup first.

A password manager is a convenient place to keep the age key, unless that same password manager is inside the encrypted backup. In that case, recovery would require the very key I was trying to recover.

I nearly made a less obvious version of that mistake. My key is in pass on my laptop, with a copy pushed to a separate Git host. Those copies are independent of the VPS. But the forge also mirrors that pass repository, and its cloud backup is encrypted with the key stored inside it. That cloud copy cannot help me recover the key.

For recovery, I need to:

  1. Identify which copies of the key can be recovered independently of the encrypted backup. A copy inside that backup cannot serve as a recovery key.
  2. Keep one copy on a medium that has no dependency on any of it. An age key is 184 bytes over three lines. It fits on a piece of paper.

I also need a recovery plan for the hardware token that holds the GPG key. Losing it and the laptop together would otherwise make the password store unreadable, including the age key inside it.

The running setup

Two systemd timers, half an hour apart so they do not contend for two vCPUs and one uplink, both Persistent=true so a missed run catches up after a reboot. Local retention of 14 days, remote 90, with the git tree mirrored rather than dated. First full push of 1.7 GB took about 17 minutes; nightly runs move the couple of hundred files that actually changed.

One gap remains: the scripts in /usr/local/bin and the configuration in /etc are not backed up. I have full copies of both scripts in my notes, but rebuilding from those would take more work than restoring the files. Adding the configuration to the backups is the next task.