From 25bdf0cf8f3c558c646b1b7f90b9ece5dfd3c618 Mon Sep 17 00:00:00 2001 From: Aaro Koivukangas Date: Wed, 30 Sep 2026 13:54:50 +0300 Subject: [PATCH] [Scripts] Add dump cleaner so that images don't accumulate over time too much --- anonyymioodi/README.md | 19 +++--- anonyymioodi/lib/common.sh | 2 + anonyymioodi/manage_test_dbs.sh | 18 +++-- anonyymioodi/prune_s3_dumps.sh | 113 ++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 13 deletions(-) create mode 100755 anonyymioodi/prune_s3_dumps.sh diff --git a/anonyymioodi/README.md b/anonyymioodi/README.md index 91c1e22b5..5da590520 100644 --- a/anonyymioodi/README.md +++ b/anonyymioodi/README.md @@ -14,16 +14,17 @@ The quick guide should be enough for most purposes: updating schema, data, or th ./manage_test_dbs.sh release all # dump, upload and push images for every database ``` -Actions are `download`, `dump`, `upload`, `push`, `release` (dump + upload + push) and `rebuild` (download + push). Each one is a thin call to a script in this folder, which can also be used on its own: +Actions are `download`, `dump`, `upload` (upload + prune), `push`, `prune`, `release` (dump + upload + prune + push) and `rebuild` (download + push). Each one is a thin call to a script in this folder, which can also be used on its own: -| Script | What it does | -| -------------------------- | ----------------------------------------------------------------------------------------------------- | -| `dump_local_db.sh` | `pg_dump`s the databases from the running containers into `.databasedumps/` | -| `download_dump_from_s3.sh` | Downloads the newest dump of each database from s3 (`-b ` to pick dumps from a single branch) | -| `upload_dump_to_s3.sh` | Uploads the dumps in `.databasedumps/test` to s3 as `--.sql` | -| `build_and_push_image.sh` | Builds database images from the current dumps and pushes them to the Toska registry | -| `create_s3_bucket.sh` | Creates the s3 bucket if it doesn't exist yet, run automatically before an upload | -| `lib/common.sh` | Shared configuration (dump directories, s3 bucket, registry, database names) | +| Script | What it does | +| -------------------------- | ---------------------------------------------------------------------------------------------------------------------- | +| `dump_local_db.sh` | `pg_dump`s the databases from the running containers into `.databasedumps/` | +| `download_dump_from_s3.sh` | Downloads the newest dump of each database from s3 (`-b ` to pick dumps from a single branch) | +| `upload_dump_to_s3.sh` | Uploads the dumps in `.databasedumps/test` to s3 as `--.sql` | +| `prune_s3_dumps.sh` | Deletes dumps older than 30 days from s3, always keeping the newest `master` dump of each database (`-d `, `-n`) | +| `build_and_push_image.sh` | Builds database images from the current dumps and pushes them to the Toska registry | +| `create_s3_bucket.sh` | Creates the s3 bucket if it doesn't exist yet, run automatically before an upload | +| `lib/common.sh` | Shared configuration (dump directories, s3 bucket, registry, database names) | Dumps end up in `.databasedumps/test/.sql`, which is where `db.Dockerfile` reads them from when an image is built. The s3 commands read their credentials from the `ACCESS_KEY` and `SECRET_KEY` environment variables (e.g. in CI). If neither is set, they fall back to `~/.s3cfg`, which can be found on the toska/dokumentaatio repo. diff --git a/anonyymioodi/lib/common.sh b/anonyymioodi/lib/common.sh index d31e10a59..960dfbca8 100644 --- a/anonyymioodi/lib/common.sh +++ b/anonyymioodi/lib/common.sh @@ -22,6 +22,8 @@ S3_HOST="s3.datacloud.helsinki.fi" S3_BUCKET="s3://oodikone" # Dumps live under a prefix of their own inside the bucket S3_TARGET="$S3_BUCKET/test-dbs" +# Dumps older than this are pruned from s3, except for the newest dump of each database +S3_DUMP_RETENTION_DAYS=30 REGISTRY="registry-toska.ext.ocp-prod-0.k8s.it.helsinki.fi" diff --git a/anonyymioodi/manage_test_dbs.sh b/anonyymioodi/manage_test_dbs.sh index 36fbbb8a6..19d3d791f 100755 --- a/anonyymioodi/manage_test_dbs.sh +++ b/anonyymioodi/manage_test_dbs.sh @@ -16,7 +16,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck source=lib/common.sh source "$SCRIPT_DIR"/lib/common.sh -ACTIONS=("download" "dump" "upload" "push" "release" "rebuild") +ACTIONS=("download" "dump" "upload" "push" "prune" "release" "rebuild") usage() { cat >&2 <&2 <] ... | all + +Deletes the dumps in $S3_TARGET older than the given number of days +(default $S3_DUMP_RETENTION_DAYS). The newest $MASTER_BRANCH dump of each database is +always kept, or the newest dump of any branch if the database has no $MASTER_BRANCH dumps. + +Databases: ${DATABASES[*]} + +Options: + -n, --dry-run Print the dumps that would be deleted instead of deleting them + -d, --days Delete dumps older than this many days + -h, --help Show this help +EOF +} + +dry_run=false +days="$S3_DUMP_RETENTION_DAYS" +arguments=() + +while [[ $# -gt 0 ]]; do + case "$1" in + -n | --dry-run) + dry_run=true + shift + ;; + -d | --days) + days="${2-}" + [[ "$days" =~ ^[0-9]+$ ]] || { + usage + die "Expected a number of days for $1, got: $days" + } + shift 2 + ;; + -h | --help) + usage + exit 0 + ;; + -*) + usage + die "Unknown option: $1" + ;; + *) + arguments+=("$1") + shift + ;; + esac +done + +if [[ ${#arguments[@]} -eq 0 ]]; then + usage + die "No database given" +fi + +prune_database() { + local database="$1" + local deleted=0 + + # s3cmd ls prints "