#!/usr/bin/env bash # # On the server, next to the compose file, or with its directory as $1. One # snapshot of the database, taken through the running container, kept # alongside the others. # # bash backup.sh # into $APP_DATA/backups # 0 4 * * * bash /srv/swag/backup.sh >> /srv/swag/backup.log 2>&1 # # `VACUUM INTO` rather than copying the file: SQLite is in WAL mode, so the # .db on disk is missing whatever is still in the -wal, and `cp` of a live # database is a coin toss. VACUUM INTO takes a read lock, writes one # self-contained and compacted file, and never blocks a writer for long enough # to matter on a database of this size. # # The database is the one the app's own config names, asked of the app inside # the container, and it is opened read-only, so a path that names no file is # an error rather than a new empty database and a snapshot of nothing. # # SERVICE is the compose service (deploy.sh says why it is here); APP_DATA is # renamed with the other APP_ variables, and `from app.config` with the package. SERVICE=app # Tables every database this app writes has: the same set as REQUIRED_TABLES in # import_database.py, which a test holds this line to. A snapshot without one # of them is deleted and the run fails. # Ours: `roles` is app/storage/schema.py's first table, where the boilerplate has `ticks`. TABLES="jobs roles" set -euo pipefail # A snapshot is the whole database, so it is owner only whatever the caller's # umask is. This covers the folder made below; the umask inside the container # (set in the snippet) covers the .db, and gzip gives the .gz the mode of the # file it compresses. umask 077 cd "${1:-$(dirname "$(readlink -f "$0")")}" # Same source of truth as the compose file and deploy.sh. DATA=$(sed -n 's/^[[:space:]]*APP_DATA=\([^#]*\).*/\1/p' .env | tail -1 | xargs || true) DATA=${DATA:-./data} KEEP=${KEEP:-14} stamp=$(date +%Y%m%d-%H%M%S) mkdir -p "$DATA/backups" # shellcheck disable=SC2086 # TABLES is a list of words on purpose docker compose exec -T "$SERVICE" python - "$SERVICE-$stamp" $TABLES <<'PY' import os, sqlite3, sys from contextlib import closing from pathlib import Path from app.config import load os.umask(0o077) name, required = sys.argv[1], set(sys.argv[2:]) database = load().database.absolute() snapshot = Path("/data/backups") / f"{name}.db" with closing(sqlite3.connect(f"{database.as_uri()}?mode=ro", uri=True)) as db: db.execute("VACUUM INTO ?", (str(snapshot),)) with closing(sqlite3.connect(f"{snapshot.as_uri()}?mode=ro", uri=True)) as db: tables = {row[0] for row in db.execute("SELECT name FROM sqlite_master WHERE type='table'")} missing = sorted(required - tables) if missing: snapshot.unlink() sys.exit(f"{database} has no table {', '.join(missing)}: not this app's database") print(f"snapshot of {database}") PY gzip -f "$DATA/backups/$SERVICE-$stamp.db" echo "$DATA/backups/$SERVICE-$stamp.db.gz $(du -h "$DATA/backups/$SERVICE-$stamp.db.gz" | cut -f1)" # Oldest first, everything past KEEP goes. Named by timestamp, so sorting the # names sorts them by age. ls -1 "$DATA"/backups/"$SERVICE"-*.db.gz 2>/dev/null \ | sort \ | head -n -"$KEEP" \ | while read -r old; do rm -f "$old"; echo "removed $old"; done