Repository navigation
Reduce DB backup costs: monthly dumps + expire dumps older than 1 year - #24
Conversation
Reduces the number of full and partial DB dumps written to S3 from both environments to save on storage costs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
Adds S3 lifecycle rules to the full and partial DB dump buckets of both environments so old dumps are deleted automatically. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
Removes the extra daily dumps from the anyway DB dump buckets, keeping the earliest dump of every month and everything from the last 30 days. Dry run by default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
Manually triggered, dry run by default. Uses the same Vault login and AWS user as the Reboot EC2 workflow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
Review fixes: - group dumps by series (date prefix stripped, partial + partial_schema together) so a testing_ or other series can't cause a month of real dumps to be deleted - only keep a date if all its parts exist and are non-empty; warn when a month has no complete dump - report bucket versioning status and per-series month counts - reject --keep-recent-days below 1, also in the workflow - pass delete batches via a file and log which batch failed - don't allow two cleanup workflow runs at once Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
- when no dump in a month is complete, keep the most complete non-empty one, and keep the whole month if every dump is empty - treat dumps under 1 KB as empty (a gzipped empty file isn't 0 bytes) - group incomplete-month warnings into one line per series - show the AWS CLI error message when a call fails - add a lifecycle rule removing expired delete markers on versioned buckets - cron comment: the schedule runs in server time Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
DB dumps are moving from daily to monthly (data-for-change/dfc-iac#24), so a dump dated today usually won't exist. When DBRESTORE_FILE_NAME is not set, list the bucket and restore the newest date that has both the partial and the partial_schema dump, each at least 1 KB. If none is found, warn and skip the restore, like the missing-credentials case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
The anyway and dfc2 anyway workflows deployed to the production servers on a push to any branch that touched the app folders, so pushing a PR branch deployed it before review. Restrict the push trigger to main; manual runs (workflow_dispatch) are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
|
What happened
What changed on the servers: only the backup schedule
Impact
Fix: commit 8f36f2d in this PR restricts both workflows' Generated by Claude Code |
DB dumps moved from daily to monthly (data-for-change/dfc-iac#24), so a dump dated today usually won't exist. When DBRESTORE_FILE_NAME is not set, list the bucket and restore the newest date that has both the partial and the partial_schema dump, each at least 1 KB. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
|
@tkalir @ziv17: how to bring backups back to daily, if we decide monthly isn't enough. While this PR is NOT merged (fastest, no code change)
That only rewrites the cron entry. No containers restart and no images change, since everything else on the servers already matches After this PR is merged
Middle ground: weekly backups, A single extra backup right now, without changing the schedule (e.g. to fill the Nov 29–30 gap): bin/apps-dfc2.py compose anyway run --rm db-backup-cronjobbin/apps.py compose anyway run --rm db-backup-cronjobWhat still works if we go back to daily
Generated by Claude Code |
|
@tkalir @ziv17: correction to my earlier comment. The monthly schedule probably won't stay until Dec 1. Why it will revert
What this means
Generated by Claude Code |
ziv17
left a comment
There was a problem hiding this comment.
Good initiative. I think monthly backups with last 30 days suffice.
|
@atalyaalon , we still need you ;) |
- job-level if: github.ref == 'refs/heads/main', so "Run workflow" on another branch can't deploy it either - concurrency group per server, so two pushes in a row don't run deploy_app at the same time Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
- dfc2-anyway-db-dumps-reader: add the dfc-anyway-cbs bucket access that was granted by hand, and ignore its hand-added tags, so a terraform apply no longer removes them - thin-db-dumps workflow: 60 minute timeout, don't persist the GitHub token in the checkout Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV
Related PRs
main(merged). This PR adds stronger guards on top: change 4.Why
Both servers (
dfcanddfc2) dump the Anyway DB to S3 every day (a full dump and a partial dump), and the dumps are never deleted. That adds about 1,460 files a year, and the S3 bill keeps growing. This PR cuts how often dumps are made, cleans up the daily dumps already there, and caps how long dumps are kept.Changes
1. DB backups run monthly instead of daily
Files:
apps/anyway/compose.yaml,apps-dfc2/anyway/compose.yamldb-backup-cronjobschedule:10 0 * * *(daily) →10 0 1 * *(1st of each month at 00:10, server time). This one job writes both the full and partial dumps.When it takes effect: on merge. The deploy workflows redeploy both servers and rewrite
/etc/cron.d/anyway-db-backup-cronjob.Trade-off: if the DB is lost, up to a month of data could be lost with it (it was up to a day before). One failed monthly run also means no backup that month. The backup monitor in #25 alerts on that.
2. Delete DB dumps older than 1 year
File:
modules/anyway/aws_s3_buckets.tfAdds an
aws_s3_bucket_lifecycle_configurationto these 4 buckets:dfc-anyway-full-db-dumpsdfc-anyway-partial-db-dumpsdfc2-anyway-full-db-dumpsdfc2-anyway-partial-db-dumpsRule 1,
expire-old-db-dumps:Rule 2,
remove-expired-delete-markers: if versioning is on, delete markers left with no versions are removed. AWS doesn't allow this in the same rule asexpiration.days.When it takes effect: not on merge. Someone must run a targeted
terraform apply: see Rollout. Expiry then runs within ~24–48 hours, and it's permanent.3. One-time clean-up: keep only one dump per month
Files:
bin/thin_db_dumps.py(new),.github/workflows/thin-db-dumps.yml(new)How it decides what to keep:
anyway.pgdump.gz,anyway_partial.pgdump.gz(together with its_partial_schemafile) and e.g.testing_anyway_partial.pgdump.gzare each handled separately, so one series can never cause another series' dumps to be deleted.It never touches:
YYYY-MM-DD_keep_recent_days, minimum 1)Because of the 30-day window, a run today keeps all the daily dumps from the last 30 days. Run it again in about a month to get down to one dump per month there too, or set a smaller
keep_recent_days.What it prints: each bucket's versioning status, the months kept per series, files and GB to keep or delete, warnings, and the AWS error message if a call fails. The workflow doesn't list individual file names; run the script locally with
--verbosefor that.How to run it: Actions → Thin DB dumps → Run workflow, on
main.aws_prod_app_user, admin) as Reboot EC2.Tested against a fake AWS CLI, not real AWS. Covered:
testing_series4. Deploy guards on the anyway deploy workflows
Files:
.github/workflows/anyway.yml,.github/workflows/anyway-dfc2.ymlbranches: [main]on the push trigger: the same change as Only deploy the anyway apps from main (stop deploys from any branch push) #27, already merged, so it's identical and conflict-free.if: github.ref == 'refs/heads/main': a manual Run workflow on another branch now skips the deploy; the run shows green but does nothing.concurrencyper server: two pushes in a row can no longer rundeploy_appat the same time. That caused the two failed dfc2 runs on 2026-11-27.main(anyway, anyway-etl, safety-data-client, anyway-reports) aren't affected.5. Keep the manual AWS access in Terraform
File:
modules/anyway/aws_db_dumps_users.tfThe problem:
dfc2-anyway-db-dumps-readerwas given two things by hand in AWS:dfc-anyway-cbsbucketBefore this change, any
terraform applywould have removed both.The change:
dfc-anyway-cbsbucket ARN to the policy, exactly as it is in AWSlifecycle { ignore_changes = [tags, tags_all] }, so the hand-added tag is kept. It isn't copied into code, because the tag's name is an access key ID and this repo is public.The CI plan now shows no change for this user.
Rollout
terraform version). CI uses 1.2.9 and no lock file is committed. A newer Terraform can rewrite the shared state in a format CI can't read.mainautomatically. Don't delete the branch any other way, which can close Free uptime + DB backup monitoring, remove paid Route53 health checks #25.main, with delete unchecked. Check the sizes, versioning and warnings.apps/anyway/compose.yamlrefers to2024-01-24_anyway.pgdump.NoSuchLifecycleConfigurationmeans there are no existing rules, which is safe.terraform init "-backend-config=$(bin/get_backend_config.py)"·t·erraform a·pply -target='module.anyway.aws_s3_bucket_lifecycle_configuration.db_dumps'4 to add, 0 to change, 0 to destroy, plus the usual "resource targeting" warnings. If it shows anything else, answerno.terraform apply. The full plan also contains 3 DKIM Route53 records (*._domainkey.anyway.co.il) that differ from the code: a trailing dot and TTL 1800 vs 300. That's harmless, but unrelated, so decide on it separately.terraform planshould show only those 3 DKIM changes. Some provider 4.x versions may also show a lasting diff onfilter {}.Result
Once the second clean-up run is done, each bucket has about 13 dumps per series: one a month for a year, plus the last 30 days. Before, it was one a day, forever.
Estimated cost saving
This is an estimate: I don't have the real bucket sizes. It uses S3 Standard in eu-central-1 at about $0.0245 per GB-month. Upload requests and data coming into S3 cost close to nothing.
Let D = GB written per day by one server (full + partial gzipped dumps). There are 2 servers.
Example, D = 5 GB/day per server:
For the real sizes, see the Thin DB dumps dry run, or check each bucket:
aws s3 ls s3://dfc-anyway-full-db-dumps --recursive --summarize --human-readable | tail -2Reviews
Four review rounds by separate agents, all read-only, the last one by a different model. All findings are fixed, and the last round confirmed the code is ready. Its description corrections are applied above.
Not in this PR (follow-ups)
--no-deployin the squash commit title. Otherwise anyway's deploy workflow builds new images and redeploys dfc2.dfcserver: #28.🤖 Generated with Claude Code
https://claude.ai/code/session_01XtN7GhfRRihvBTHsWxGZoV