π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[infra] Backup DB by promoting snapshot
Manav Rathi2bd74b306b31
diff --git a/infra/copycat-db/README.md b/infra/copycat-db/README.mdindex 266bd4e6e3..38a1d6d859 100644--- a/infra/copycat-db/README.md+++ b/infra/copycat-db/README.md@@ -55,6 +55,13 @@ on a shell prompt in the container (using `./test.sh sh`)The UUID of the Scalway RDB instance that we wish to backup. If this is missing,then the Docker image falls back to using `pg_dump` (as outlined next).+##### SCW_RDB_USE_SNAPSHOT_BACKUP++Optional. Set this to `true` to make copycat-db restore the latest existing+Scaleway snapshot into a temporary single-node instance named+`copycat-db-temp`, take the backup from that temporary instance, and delete it+after the backup has been downloaded. See the **Snapshot Mode** section below for more details.+##### PGUSER, PGPASSWORD, PGHOSTNot needed in production when taking a backup (since we use the Scaleway CLI to@@ -102,6 +109,28 @@ remote that the crypt remote wraps.The service logs to its standard out/error. The systemd unit is configured toroute these to `/var/logs/copycat-db.log`.+## Snapshot Mode++By default, copycat-db keeps the old behaviour and backs up the instance+specified by `SCW_RDB_INSTANCE_ID` directly.++If `SCW_RDB_USE_SNAPSHOT_BACKUP=true`, it instead:++1. Finds the latest existing Scaleway snapshot for `SCW_RDB_INSTANCE_ID`.+2. Restores it into a temporary non-HA instance named `copycat-db-temp`.+3. Takes the backup from that restored instance.+4. Deletes that temporary instance after the backup has been downloaded.++To trigger this path in production, set `SCW_RDB_USE_SNAPSHOT_BACKUP=true` in+`/root/copycat-db.env` and then start the service or let the timer run it.++This path is intentionally conservative about cleanup. If something fails before+the final explicit delete, the script leaves the temporary instance behind+instead of attempting automatic cleanup during error handling. This is done to+avoid accidental deletions if the script ends up in an unexpected state.++If that happens, clean it up manually in the Scaleway dashboard by deleting the DB instance named `copycat-db-temp`.+## Local testingThe provided `test.sh` script can be used to do a smoke test for building anddiff --git a/infra/copycat-db/copycat-db.sample.env b/infra/copycat-db/copycat-db.sample.envindex ba557714e0..4db152d969 100644--- a/infra/copycat-db/copycat-db.sample.env+++ b/infra/copycat-db/copycat-db.sample.env@@ -1,5 +1,6 @@SCW_CONFIG_PATH=/var/config/scw/copycat-db-config.yamlSCW_RDB_INSTANCE_ID=+SCW_RDB_USE_SNAPSHOT_BACKUP=RCLONE_CONFIG=/var/config/rclone/copycat-db-rclone.confRCLONE_DESTINATION=db-backup-crypt:PGUSER=diff --git a/infra/copycat-db/src/backup.sh b/infra/copycat-db/src/backup.shindex 41b3e811c1..d2b29680ee 100755--- a/infra/copycat-db/src/backup.sh+++ b/infra/copycat-db/src/backup.sh@@ -1,6 +1,7 @@#!/bin/bashset -o errexit+set -o pipefailset -o xtraceNOWS="$(date +%s)"@@ -23,6 +24,40 @@ then# pg_dump for creating the backup.pg_dump -Fc ente_db > $BACKUP_FILEelse+ BACKUP_INSTANCE_ID=$SCW_RDB_INSTANCE_ID+ DELETE_INSTANCE_ID=++ if test "$SCW_RDB_USE_SNAPSHOT_BACKUP" = "true"+ then+ LATEST_SNAPSHOT_ID=$(+ scw rdb snapshot list instance-id="$SCW_RDB_INSTANCE_ID" order-by=created_at_desc -o json \+ | jq -r 'map(select((.status // "" | ascii_downcase) == "ready"))[0].id'+ )++ if test -z "$LATEST_SNAPSHOT_ID" || test "$LATEST_SNAPSHOT_ID" = "null"+ then+ echo "copycat-db: no ready snapshot found for instance: $SCW_RDB_INSTANCE_ID"+ exit 1+ fi++ BACKUP_INSTANCE_ID=$(+ scw rdb snapshot restore "$LATEST_SNAPSHOT_ID" \+ instance-name=copycat-db-temp \+ is-ha-cluster=false \+ -o json \+ | jq -r '.id'+ )++ if test -z "$BACKUP_INSTANCE_ID" || test "$BACKUP_INSTANCE_ID" = "null"+ then+ echo "copycat-db: failed to restore snapshot: $LATEST_SNAPSHOT_ID"+ exit 1+ fi++ scw rdb instance wait "$BACKUP_INSTANCE_ID" timeout=12h+ DELETE_INSTANCE_ID=$BACKUP_INSTANCE_ID+ fi+# We need to export a backup first after creating it, before it can be# downloaded.#@@ -33,12 +68,37 @@ else## To avoid this we need to add a custom wait here ourselves instead of using# the convenience `--wait` flag for the export command provided by Scaleway.- BACKUP_ID=$(scw rdb backup create instance-id=$SCW_RDB_INSTANCE_ID \+ BACKUP_ID=$(scw rdb backup create instance-id="$BACKUP_INSTANCE_ID" \name=$BACKUP_NAME expires-at=$EXPIRY \database-name=ente_db -o json | jq -r '.id')scw rdb backup wait $BACKUP_ID timeout=12hscw rdb backup download output=$BACKUP_FILE \$(scw rdb backup export $BACKUP_ID --wait -o json | jq -r '.id')++ if test "$SCW_RDB_USE_SNAPSHOT_BACKUP" = "true"+ then+ if test -z "$DELETE_INSTANCE_ID"+ then+ echo "copycat-db: refusing to delete without an explicit snapshot restore instance id"+ exit 1+ fi++ if test "$DELETE_INSTANCE_ID" = "$SCW_RDB_INSTANCE_ID"+ then+ echo "copycat-db: refusing to delete production instance"+ exit 1+ fi++ DELETE_INSTANCE_NAME=$(scw rdb instance get "$DELETE_INSTANCE_ID" -o json | jq -r '.name')++ if test "$DELETE_INSTANCE_NAME" != "copycat-db-temp"+ then+ echo "copycat-db: refusing to delete unexpected instance: $DELETE_INSTANCE_ID ($DELETE_INSTANCE_NAME)"+ exit 1+ fi++ scw rdb instance delete "$DELETE_INSTANCE_ID"+ fifirclone copy --log-level INFO $BACKUP_FILE $RCLONE_DESTINATION