Backup and restore
Each self-hosted stack has its own backup, schedule and settings. A backup runs once a day, so at most one working day is lost in an incident. Restoring to an arbitrary point in time is not supported.
What a backup contains
Each backup is a folder named after the UTC time (for example 20261106T033000Z):
| Part | Contents |
|---|---|
| Three database dumps | tmspec (specs), tmspec_auth (accounts and sign-in), tmspec_ai. Each is a consistent snapshot; the three are taken one after another, not at the same instant |
| The sign-in service's signing keys | Losing them signs everyone out and breaks the live tokens |
| The stack's environment file | Passwords and session keys, which a new machine needs |
| Manifest | The stack name, the image tag that was running, the Postgres version |
SHA256SUMS | Checksums of all the parts above; a restore checks it first |
Files have mode 0600 in a 0700 folder. Each dump is read back after it is made, so a truncated file makes the backup fail instead of silently breaking. A failed run leaves no half-finished backup. TMSPEC_BACKUP_KEEP (default 14) is the number of backups kept, on the machine and off it.
Set up the schedule
The schedule is a systemd timer, not a container, so it catches up after the machine was off, reports failures through systemd's tools, and runs as root, which puts it out of reach of the deploy account. By default it runs every day at 03:30, delayed by at most 15 minutes.
Install the script and units once, from the release's source folder (the script docker/backup.sh and the two units in docker/systemd/):
sudo install -m 0755 docker/backup.sh /usr/local/sbin/tmspec-backup
sudo install -m 0644 docker/systemd/tmspec-backup@.service docker/systemd/tmspec-backup@.timer /etc/systemd/system/
sudo install -d -m 0700 /etc/tmspec/backup
sudo systemctl daemon-reload
Then give each stack a settings file /etc/tmspec/backup/<name>.env, pointing at the environment file and the state folder set at install time:
TMSPEC_ENV_FILE=/etc/tmspec/env.prod
TMSPEC_STACK=tmspec
TMSPEC_DEPLOY_STATE_DIR=/var/lib/tmspec/deploy
TMSPEC_BACKUP_DIR=/var/backups/tmspec/main
Run it once, then turn on the timer:
sudo systemctl start tmspec-backup@main.service
sudo systemctl enable --now tmspec-backup@main.timer
The name after @ is the name of the settings file (main in the example). Run the script's install command again when a release changes backup.sh: nothing updates it automatically when you upgrade.
Off-machine copy
A backup on the same disk does not survive losing that disk. A stack with real data needs a copy elsewhere. The off-machine copy is off until you set TMSPEC_BACKUP_REMOTE.
- Any storage
rclonesupports works (S3, SFTP...). The rclone configuration is at/etc/tmspec/rclone.conf(0600); the server only needsgpg. - Each off-machine copy is a file
<UTC time>.tar.gpg, encrypted with gpg (AES256) before it is uploaded, so the keys and passwords inside never leave the machine in clear. - Set
TMSPEC_BACKUP_PASSPHRASE_FILEto a file holding the passphrase.
TMSPEC_BACKUP_REMOTE=offsite:tmspec-backups/main
TMSPEC_BACKUP_PASSPHRASE_FILE=/etc/tmspec/backup/passphrase
Keep the passphrase somewhere other than the machine, for example in a password manager. Without it, the copy cannot be read; and if it is only on the machine, it is lost together with the machine.
Knowing when the schedule has died
Each fully successful run, the off-machine copy included, writes the time to a file. The status command prints that time and exits with code 1 when it is older than TMSPEC_BACKUP_MAX_AGE_HOURS (default 26) or missing:
sudo sh -c 'set -a; . /etc/tmspec/backup/main.env; exec tmspec-backup status'
systemctl list-timers 'tmspec-backup@*'
journalctl -u tmspec-backup@main.service -n 30
Feed status into a monitoring tool. If the off-machine copy fails, the backup stays on the machine, the run reports failure and the time of the last success does not move.
Restore
A restore runs as root, from the release source's docker/ folder, with the same three variables as deploy.sh: TMSPEC_ENV_FILE (the environment file), TMSPEC_DEPLOY_STATE_DIR (the state folder) and TMSPEC_COLORS_DIR (the colors folder, by default colors/ next to the script). Because it runs as root, pass them through sudo env TMSPEC_ENV_FILE=... ./restore.sh .... It takes a backup folder or a .tar.gpg file.
./restore.sh /var/backups/tmspec/main/20261106T033000Z
./restore.sh --force /var/backups/tmspec/main/20261106T033000Z
TMSPEC_BACKUP_PASSPHRASE_FILE=/root/passphrase ./restore.sh --tag <tag> /root/20261106T033000Z.tar.gpg
The order:
take the lock → check SHA256SUMS → infrastructure up → refuse if the database already holds data (unless --force)
→ check that every image of the release is present or can be pulled
→ take both colors down → drop the databases → prepare again → load each dump → replace the signing keys
→ start a color → wait until healthy
- Nothing is changed before the image check, so a wrong tag or a wrong passphrase leaves the stack running. From the drop step onwards the stack is off until the end; if a later step fails, run the restore again.
- Tag. The default is the tag of the running color, then the tag in the backup. A tag newer than the backup is allowed, because the migrations bring the schema up; an older one is not, because migrations only go forward.
--tagchooses a tag (the registry keeps only a few of the most recent tags). - Color. The running color is the one started, because the proxy points at it.
--colorcan be chosen only on a machine with no running color. - Exit codes. 0 done, 1 error, 2 wrong usage, 3 an upgrade or restore holds the lock, 4 the database has data and there is no
--force.
--force deletes the stack's existing data and replaces it with the backup; do not use it on a stack that is really running unless you are sure.
Restore on a new machine
- Set up the machine as for a new installation: Docker, the release, the colors and the proxy (Install and the first owner, steps 0 to 2).
- Copy the environment file taken from the backup back to the
TMSPEC_ENV_FILEpath. The session keys and the role passwords must be the same as the backed-up stack's. - Run
restore.shwith--tag. - Give the state folder back to the account used to deploy, because the restore ran as root.
- Run
deploy.sh deployonce so the proxy points at a color. Until then nothing from outside reaches the restored stack. With nginx or Traefik,deploy.shswitches only Caddy: point the three upstreams at the running color by hand, as in Upgrade.
Keep the sign-in service's name unchanged when moving machines. A new name is a new issuer, and everyone's identity (the issuer together with the account id) would have to be updated too.
Test a restore
A backup that has never been restored is not certain to be usable. Try a restore into a throwaway stack (its own stack name, ports and network range) before you need it, and try again after every change to how you back up or restore. What to look at after the test: every service healthy, the tables' row counts equal, sign-in works through the web and the CLI, and tokens issued before the backup are still accepted.