Skip to content

Part J. Backups and restore

A backup that has never been restored is a hope, not a backup. This part has three layers and a monthly restore drill.

Layer What When Kept Where
1. Nightly database dump Everything in the bideward database (hub and research), encrypted Every night 03:15 UTC 14 days on the server /var/lib/bideward-backup/dumps
2. Off-server copy The same encrypted dump Right after layer 1 As long as the Drive folder keeps them (weekly clean-up by you) Your Drive, through the existing Apps Script address
3. The app’s own JSON backup The dashboard’s Backup now, as today As scheduled in the dashboard As today Your Drive
4. Lightsail snapshots The whole disk Before big changes (manual); automatic daily only if the price is acceptable 7 AWS
5. Monthly offline copy The newest encrypted dump, copied by you to your own computer and an external disk Once a month, in the restore drill (J.5) The last 6 Your computer, an external disk

Layer 1 and 2 are what you rely on. The dump is encrypted with a public key; only the private key (kept offline by you) can open it, so a stolen copy is useless, and the server itself cannot read its own backups. Snapshots are billed by size: look at the price Lightsail shows when you open the Snapshots tab before turning on the automatic option. If it matters, use manual snapshots before upgrades and rely on layers 1 and 2.

  1. On your own computer (not the server) make the key pair. Install age (apt install age, brew install age, or the release from the age project’s page), then:

    Terminal window
    age-keygen -o bideward-backup.key

It prints a public key line starting with age1. Copy that line. Store bideward-backup.key offline in at least two places (a password manager’s secure file and a printed copy). Never put it on the server and never in Drive. If it is lost, no backup can be opened.

  1. On the server install age and write the public key:

    Terminal window
    sudo apt install -y age
    sudo install -m 644 /dev/null /etc/bideward/backup.age.pub
    sudo nano /etc/bideward/backup.age.pub

Paste the age1… line only, then save.

  1. A limited user and a database login that can only read:

    Terminal window
    sudo adduser --system --group --no-create-home --shell /usr/sbin/nologin bideward-backup
    sudo install -d -m 700 -o bideward-backup -g bideward-backup /var/lib/bideward-backup /var/lib/bideward-backup/dumps
    echo 'bidewardmap bideward-backup bideward_backup' | sudo tee -a /etc/postgresql/17/main/pg_ident.conf
    sudo systemctl reload postgresql
    psql -U bideward_dba -d bideward -c "create role bideward_backup login;" -c "grant pg_read_all_data to bideward_backup;" -c "grant connect on database bideward to bideward_backup;"
    psql -U bideward_dba -d bideward -c "grant insert on hub.backups to bideward_backup;" -c "grant usage on sequence hub.backups_id_seq to bideward_backup;"

EXPLAIN — what this does: bideward_backup can read every table and sequence, and write only one thing: its own result row in hub.backups (the table is created by migration 0002, so the last command needs F.7 done first; run it then). It cannot change or delete anything else.

  1. The Drive upload settings: sudo install -m 600 -o root -g root /dev/null /etc/bideward/backup.env, then sudo nano it and enter BACKUP_URL=<the Apps Script web-app address> and BACKUP_KEY=<its key> (the same values the dashboard backup uses today; names confirmed in E1). If the dump grows past the Apps Script size limit, the script splits it into parts first; if that is still not enough we choose another free store, and I will tell you before that happens.

  2. Update the Apps Script (once). The nightly dump needs the new version of tools/drive-backup.gs (version 2: a separate “Dumps” folder that is never pruned, an answer to “which version are you”, and a delete that works only for the size-test files). Open your existing Apps Script project at script.google.com, replace the whole code with the file’s content, click Deploy → Manage deployments → the pencil → Version: New version → Deploy. The web-app address stays the same. The backup script refuses to upload to the old version and says so, so nothing can end up in the Settings folder by mistake. Optional: add the script property DUMP_FOLDER_ID (the id of a Drive folder of your choice); without it the dumps go to a “Dumps” folder inside the backup folder.

The files are in the repository (deploy/backup.sh and the timer units, pull request 3 of E.8). What the script does, in order:

  1. pg_dump of the bideward database in compressed custom format to a time-stamped file.
  2. pg_restore -l on the file, which proves the file can be read; the script stops with an error if not.
  3. Encrypts the file with age and the public key, removes the unencrypted file, writes a sha256 line.
  4. Uploads the encrypted file to Drive (the key is passed through curl’s config input, never on the command line).
  5. Deletes dumps older than 14 days from the server (settings KEEP_DAYS, SPLIT_MB).
  6. Writes the result to /var/lib/bideward-backup/last-backup.json (read by bideward-watch) and a row in hub.backups (kind, size, checksum, location, status) so the dashboard can show the last backup time. A failure at any step is written the same way, with the step and the reason, and ends the run with an error (which also triggers the Telegram failure message).

Install and start:

Terminal window
sudo /opt/bideward/hub/deploy/install.sh
sudo systemctl enable --now bideward-backup.timer
sudo systemctl start bideward-backup.service
sudo systemctl status bideward-backup.service --no-pager
sudo ls -l /var/lib/bideward-backup/dumps

VERIFY — you should see: the service finished successfully, one file ending .dump.age with its .sha256, and the new file in your Drive folder. The timer line (systemctl list-timers) shows the next run at 03:15 UTC.

J.3a The Drive upload: size test, splitting and the fallback

Section titled “J.3a The Drive upload: size test, splitting and the fallback”

EXPLAIN — why: Google documents 50 MB per call for what Apps Script can fetch; it does not publish a limit for a request sent into an Apps Script web app. We measure instead of guessing.

  1. After the first dump (J.3), note its size: sudo ls -lh /var/lib/bideward-backup/dumps.
  2. Upload test. Run sudo systemd-run --wait --pipe -p User=bideward-backup -p EnvironmentFile=/etc/bideward/backup.env /usr/local/sbin/bideward-backup --test-upload (the script sends dummy files of 5, 20, 40 and 60 MB, no real data, to the Apps Script address, asks it to delete them again, and prints which sizes were accepted and how long each took; it first checks that the Apps Script is the new version). Write the largest accepted size in your table.
  3. Splitting. A dump larger than 20 MB (setting SPLIT_MB; set it lower if the test shows a smaller largest size) is cut into numbered parts (name.dump.age.part001 and so on). The manifest file (name.manifest) holds the checksum of the whole file on its first line and one line per part. Drive keeps the parts and the manifest. Restore (J.4 C) downloads every part, checks every checksum, joins them, then decrypts.
  4. Fallback. If the Apps Script path is slow or refuses the test sizes, the upload switches to rclone with a Google Drive remote limited to one folder (set up once from your computer with rclone config; its token is kept in /etc/bideward/rclone.conf, root only). Decision rule: stay on Apps Script while every test size passes and the nightly upload finishes in under 10 minutes; otherwise switch.
  5. Alert. The Part K watcher sends a Telegram alert if the nightly upload fails, or if no new file has appeared in Drive within 26 hours.

A. One setting or one bot’s settings. Dashboard → Settings → History or Restore points (C.5). No database tools needed.

B. One table or a few rows from a dump (scratch restore).

  1. Copy the encrypted dump and the sha256 file from Drive (or from /var/lib/bideward-backup/dumps) to your own computer. Decrypt it there:

    Terminal window
    sha256sum -c bideward-20261201T031500Z.dump.age.sha256
    age -d -i bideward-backup.key -o bideward.dump bideward-20261201T031500Z.dump.age
  2. Copy the decrypted file to the server into /var/lib/bideward-backup/ with scp, and make a scratch database:

    Terminal window
    psql -U bideward_dba -d postgres -c "create database bideward_scratch owner bideward_dba;"
    pg_restore -U bideward_dba -d bideward_scratch --no-owner /var/lib/bideward-backup/bideward.dump
  3. Look at the data in bideward_scratch with psql; copy exactly the rows you need into bideward (insert … select through a dump of that one table, or ask me to write the statement). Then remove the scratch database and the decrypted file:

    Terminal window
    psql -U bideward_dba -d postgres -c "drop database bideward_scratch;"
    sudo shred -u /var/lib/bideward-backup/bideward.dump

C. Everything, on a new server (the server is lost).

  1. Create a new Lightsail instance and do Parts A, B and F up to F.6 (the code at the same tag, the same env files; the tunnel token is the same).

  2. Run the migrations (F.7 step 1) so the tables exist, stop the hub if it started, then restore data over them from the decrypted dump:

    Terminal window
    pg_restore -U bideward_dba -d bideward --clean --if-exists --no-owner /path/to/bideward.dump
  3. Start the hub, research and tunnel. The hostname, Cloudflare rules and every loader’s address do not change, so nothing is re-pasted. Loaders reconnect on their own.

  4. Expected loss: everything after the last nightly dump (up to 24 hours of statistics and round log). Money and take-profit records were also written by the bots’ own state mirror (S1), so the bots can resume from their own copy.

D. From a Lightsail snapshot. Create a new instance from the snapshot, then follow I.4 step 3 for Tailscale and the tunnel.

  1. Take the newest Drive dump, decrypt it on your computer, restore it into bideward_scratch as in J.4 B.
  2. Compare row counts with the live database for hub.bots, hub.bot_sessions and hub.kv; the dump’s counts must be equal to or just below the live ones.
  3. Drop the scratch database and delete the decrypted file. Write the date and result in the checklist at the end of this guide. A failed drill is an emergency: tell me the same day.
  4. Monthly offline copy. In the same session download the newest encrypted dump (and its parts, if split) from Drive to your own computer and to an external disk. Keep the last six. The drill always uses the Drive copy, never the server’s own copy, so it proves the off-server path.

← Previous: Part I. Validate with 1 to 3 bots and measure speed · Guide index · Next: Part K. Monitoring and alerts →