Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Update copycat-db snapshot pg_dump flow

Manav Rathia92d718cea98
diff --git a/infra/copycat-db/Dockerfile b/infra/copycat-db/Dockerfile
index 9d58c09fa8..86482c165f 100644
--- a/infra/copycat-db/Dockerfile
+++ b/infra/copycat-db/Dockerfile
@@ -1,25 +1,27 @@
FROM ubuntu:latest
-RUN apt-get update && apt-get install -y curl gnupg
-RUN apt-get install -y tini
-
-# Install Postgres client (needed for restores, and for local testing)
+RUN apt-get update && apt-get install -y \
+ ca-certificates \
+ curl \
+ jq \
+ postgresql-common \
+ tini \
+ unzip \
+ zstd
+
+# Install Postgres client 15 (needed for restores, and for local testing)
# https://www.postgresql.org/download/linux/ubuntu/
-#
-# We don't have specific dependencies on Postgres, so just use the latest.
-RUN apt-get update && apt-get -y install postgresql-client
+RUN /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y
+RUN apt-get update && apt-get install -y postgresql-client-15
# Install SCW CLI
# Latest release: https://github.com/scaleway/scaleway-cli/releases/latest
RUN \
- export VERSION="2.34.0" && \
+ export VERSION="2.55.0" && \
curl -o /usr/local/bin/scw -L "https://github.com/scaleway/scaleway-cli/releases/download/v${VERSION}/scaleway-cli_${VERSION}_linux_amd64" && \
chmod +x /usr/local/bin/scw
-RUN apt-get install -y jq
-
# Install rclone
-RUN apt-get install -y unzip
RUN curl https://rclone.org/install.sh | bash
COPY src /
diff --git a/infra/copycat-db/README.md b/infra/copycat-db/README.md
index 3135c94386..dd69b25587 100644
--- a/infra/copycat-db/README.md
+++ b/infra/copycat-db/README.md
@@ -35,8 +35,34 @@ The name copycat-db is a riff on "copycat", which is what we call our museum
instance that does the object replication. This one replicates the DB, so,
copycat-db.
+## Backup Mode
+
+Copycat DB requires an explicit `BACKUP_MODE`:
+
+- `scw-export`: take a Scaleway-managed backup directly from `SCW_RDB_INSTANCE_ID`
+- `snapshot-scw-export`: restore the latest snapshot into `copycat-db-temp`, take
+ a Scaleway-managed backup from that temporary instance, then delete it
+- `snapshot-pgdump`: restore the latest snapshot into `copycat-db-temp`, attach
+ it to `SCW_PRIVATE_NETWORK_ID`, run `pg_dump` over the private endpoint,
+ package the dump, then delete the temporary instance
+
+To use a snapshot-backed mode in production, set `BACKUP_MODE` accordingly 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`.
+
## Required environment variables
+##### BACKUP_MODE
+
+Required. See the **Backup Mode** section above for the available values and
+their behaviour.
+
##### SCW_CONFIG_PATH
Path to the `config.yaml` used by Scaleway CLI.
@@ -52,23 +78,25 @@ on a shell prompt in the container (using `./test.sh sh`)
##### SCW_RDB_INSTANCE_ID
-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).
+The UUID of the Scalway RDB instance that we wish to backup.
+
+##### SCW_PRIVATE_NETWORK_ID
-##### SCW_RDB_USE_SNAPSHOT_BACKUP
+Required when `BACKUP_MODE=snapshot-pgdump`.
-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.
+This is the UUID of the Private Network that copycat-db should attach to the
+temporary restored Database Instance before running `pg_dump`.
-##### PGUSER, PGPASSWORD, PGHOST
+The compute instance running copycat-db must also be attached to this Private
+Network.
+
+##### PGUSER, PGPASSWORD
Not needed in production when taking a backup (since we use the Scaleway CLI to
take backups in production).
-These are used when testing a backup using `pg_dump`, and when restoring
-backups.
+These are used when taking a `snapshot-pgdump` backup, and when restoring
+backups. The host and port are resolved by the scripts.
##### RCLONE_CONFIG
@@ -109,28 +137,6 @@ remote that the crypt remote wraps.
The service logs to its standard out/error. The systemd unit is configured to
route 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 testing
The provided `test.sh` script can be used to do a smoke test for building and
diff --git a/infra/copycat-db/copycat-db.sample.env b/infra/copycat-db/copycat-db.sample.env
index 4db152d969..0a54849609 100644
--- a/infra/copycat-db/copycat-db.sample.env
+++ b/infra/copycat-db/copycat-db.sample.env
@@ -1,9 +1,8 @@
+BACKUP_MODE=
SCW_CONFIG_PATH=/var/config/scw/copycat-db-config.yaml
SCW_RDB_INSTANCE_ID=
-SCW_RDB_USE_SNAPSHOT_BACKUP=
-RCLONE_CONFIG=/var/config/rclone/copycat-db-rclone.conf
-RCLONE_DESTINATION=db-backup-crypt:
+SCW_PRIVATE_NETWORK_ID=
PGUSER=
PGPASSWORD=
-PGHOST=host.docker.internal
-PGPORT=
+RCLONE_CONFIG=/var/config/rclone/copycat-db-rclone.conf
+RCLONE_DESTINATION=db-backup-crypt:
diff --git a/infra/copycat-db/src/backup.sh b/infra/copycat-db/src/backup.sh
index decf8be364..9261069f81 100755
--- a/infra/copycat-db/src/backup.sh
+++ b/infra/copycat-db/src/backup.sh
@@ -5,10 +5,38 @@ set -o pipefail
set -o xtrace
NOWS="$(date +%s)"
-BACKUP_FILE="db-$NOWS.custom"
-# Scaleway backup names cannot contain dots
-BACKUP_NAME="db-$NOWS-custom"
+if test -z "$SCW_RDB_INSTANCE_ID"
+then
+ echo "copycat-db: SCW_RDB_INSTANCE_ID must be set"
+ exit 1
+fi
+
+if test "$BACKUP_MODE" = "scw-export" || test "$BACKUP_MODE" = "snapshot-scw-export"
+then
+ BACKUP_FILE="db-$NOWS.custom"
+
+ # Scaleway backup names cannot contain dots
+ BACKUP_NAME="db-$NOWS-custom"
+elif test "$BACKUP_MODE" = "snapshot-pgdump"
+then
+ if test "${#PGUSER}" -eq 0 || test "${#PGPASSWORD}" -eq 0
+ then
+ echo "copycat-db: PGUSER and PGPASSWORD must be set for snapshot-pgdump"
+ exit 1
+ fi
+
+ if test -z "$SCW_PRIVATE_NETWORK_ID"
+ then
+ echo "copycat-db: SCW_PRIVATE_NETWORK_ID must be set for snapshot-pgdump"
+ exit 1
+ fi
+
+ BACKUP_FILE="db-$NOWS.dumpdir.tar.zst"
+else
+ echo "copycat-db: BACKUP_MODE must be one of scw-export, snapshot-scw-export, snapshot-pgdump"
+ exit 1
+fi
# Calculate an expiry time 1 month from now
EXPIRYS="$(( 30 * 24 * 60 * 60 + $NOWS ))"
@@ -17,61 +45,43 @@ EXPIRYS="$(( 30 * 24 * 60 * 60 + $NOWS ))"
# Note that GNU date uses "-d" and an "@" to pass an epoch (macOS uses "-r").
EXPIRY="$(date -Iseconds --utc --date "@$EXPIRYS")"
-if test -z "$SCW_RDB_INSTANCE_ID"
+BACKUP_INSTANCE_ID=$SCW_RDB_INSTANCE_ID
+DELETE_INSTANCE_ID=
+
+if test "$BACKUP_MODE" = "snapshot-scw-export" || test "$BACKUP_MODE" = "snapshot-pgdump"
then
- # A required SCW related environment variable hasn't been specified. This is
- # expected when running the script locally for testing. Fallback to using
- # pg_dump for creating the backup.
- pg_dump -Fc ente_db > $BACKUP_FILE
-else
- BACKUP_INSTANCE_ID=$SCW_RDB_INSTANCE_ID
- DELETE_INSTANCE_ID=
+ 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 "$SCW_RDB_USE_SNAPSHOT_BACKUP" = "true"
+ if test -z "$LATEST_SNAPSHOT_ID" || test "$LATEST_SNAPSHOT_ID" = "null"
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
-
- PUBLIC_ENDPOINT_ID="$(
- scw rdb endpoint list "$BACKUP_INSTANCE_ID" -o json \
- | jq -er '
- map(select(.load_balancer != null))
- | if length == 1 then .[0].id
- else error("copycat-db: expected exactly one public endpoint on temp instance")
- end
- '
- )"
-
- scw rdb endpoint delete "$PUBLIC_ENDPOINT_ID" instance-id="$BACKUP_INSTANCE_ID"
- scw rdb instance wait "$BACKUP_INSTANCE_ID" timeout=12h
-
- DELETE_INSTANCE_ID=$BACKUP_INSTANCE_ID
+ 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
+
+if test "$BACKUP_MODE" = "scw-export" || test "$BACKUP_MODE" = "snapshot-scw-export"
+then
# We need to export a backup first after creating it, before it can be
# downloaded.
#
@@ -88,31 +98,71 @@ else
scw rdb backup wait $BACKUP_ID timeout=18h
scw rdb backup download output=$BACKUP_FILE \
$(scw rdb backup export $BACKUP_ID --wait -o json | jq -r '.id')
+else
+ PUBLIC_ENDPOINT_ID="$(
+ scw rdb endpoint list "$BACKUP_INSTANCE_ID" -o json \
+ | jq -er '
+ map(select(.load_balancer != null))
+ | if length == 1 then .[0].id
+ else error("copycat-db: expected exactly one public endpoint on temp instance")
+ end
+ '
+ )"
+
+ scw rdb endpoint delete "$PUBLIC_ENDPOINT_ID" instance-id="$BACKUP_INSTANCE_ID"
+ scw rdb instance wait "$BACKUP_INSTANCE_ID" timeout=12h
+
+ scw rdb endpoint create "$BACKUP_INSTANCE_ID" \
+ private-network.private-network-id="$SCW_PRIVATE_NETWORK_ID"
+ scw rdb instance wait "$BACKUP_INSTANCE_ID" timeout=12h
+
+ PRIVATE_ENDPOINT_IP="$(
+ scw rdb endpoint list "$BACKUP_INSTANCE_ID" -o json \
+ | jq -er '
+ map(select(.private_network != null))
+ | if length == 1 then .[0].ip
+ else error("copycat-db: expected exactly one private endpoint on temp instance")
+ end
+ '
+ )"
+
+ PRIVATE_ENDPOINT_PORT="$(
+ scw rdb endpoint list "$BACKUP_INSTANCE_ID" -o json \
+ | jq -er '
+ map(select(.private_network != null))
+ | if length == 1 then .[0].port
+ else error("copycat-db: expected exactly one private endpoint on temp instance")
+ end
+ '
+ )"
+
+ PGHOST="$PRIVATE_ENDPOINT_IP" PGPORT="$PRIVATE_ENDPOINT_PORT" PGSSLMODE=require \
+ pg_dump -Fd -j 8 --compress=0 --file "db-$NOWS.dumpdir" ente_db
+fi
- if test "$SCW_RDB_USE_SNAPSHOT_BACKUP" = "true"
+if test -n "$DELETE_INSTANCE_ID"
+then
+ 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
- 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"
+ echo "copycat-db: refusing to delete unexpected instance: $DELETE_INSTANCE_ID ($DELETE_INSTANCE_NAME)"
+ exit 1
fi
+
+ scw rdb instance delete "$DELETE_INSTANCE_ID"
+fi
+
+if test "$BACKUP_MODE" = "snapshot-pgdump"
+then
+ tar -cf - "db-$NOWS.dumpdir" | zstd -T0 -q > "$BACKUP_FILE"
+ rm -rf "db-$NOWS.dumpdir"
fi
rclone copy --log-level INFO $BACKUP_FILE $RCLONE_DESTINATION
diff --git a/infra/copycat-db/src/restore.sh b/infra/copycat-db/src/restore.sh
index 6fa1a21a2f..2c5910f47c 100755
--- a/infra/copycat-db/src/restore.sh
+++ b/infra/copycat-db/src/restore.sh
@@ -1,22 +1,39 @@
#!/bin/bash
set -o errexit
+set -o pipefail
set -o xtrace
if test -z "$1"; then
# Find the name of the latest backup.
#
# The backup file name contains the epoch, so we can just sort.
- BACKUP_FILE=$(rclone lsf --include 'db-*.custom' --files-only $RCLONE_DESTINATION | sort | tail -1)
+ BACKUP_FILE=$(
+ rclone lsf --files-only $RCLONE_DESTINATION \
+ | awk '/^db-.*(\.custom|\.dumpdir\.tar\.zst)$/' \
+ | sort \
+ | tail -1
+ )
else
# If a CLI argument is provided, use that as the name of the backup file to
# restore.
BACKUP_FILE="$1"
fi
+if test -z "$BACKUP_FILE"; then
+ echo "copycat-db: no backup found"
+ exit 1
+fi
+
# Download it
rclone copy --log-level INFO "${RCLONE_DESTINATION}${BACKUP_FILE}" .
+# Unpack directory-format backups so pg_restore can read BACKUP_FILE directly.
+if test "${BACKUP_FILE%.custom}" = "$BACKUP_FILE"; then
+ zstd -dc "$BACKUP_FILE" | tar -xf -
+ BACKUP_FILE="${BACKUP_FILE%.tar.zst}"
+fi
+
# Restore from it
#
# This create a database named rdb on Postgres - this is only used for the