From 92913939658fba8933264a5bc3dab69346aaa669 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 30 Sep 2026 18:20:04 +0000 Subject: [PATCH] Prune short-lived upgrade backups after 2 weeks Each upgrade already writes a SQL dump + webapp/WAR snapshot pair under a shared timestamp for tactical rollback. Add automatic pruning of pairs older than RETENTION_DAYS (14), run right after manager-exec.sh upgrade succeeds so nothing is discarded if the upgrade itself failed. Pruning works from the union of timestamps across sql/webapp/logs so a partial pair (one artifact skipped via -f/-w) is still cleaned up without erroring. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb --- upgrade-cmds.sh | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/upgrade-cmds.sh b/upgrade-cmds.sh index 5dd8265..5382d77 100644 --- a/upgrade-cmds.sh +++ b/upgrade-cmds.sh @@ -1,8 +1,49 @@ +# Short-lived tier: this is the per-upgrade safety net, not the nightly +# backup. Every run pairs a SQL dump and a webapp/WAR snapshot under the +# SAME $timestamp so a revert always restores both together, never one +# without the other. It's deliberately aggressive about pruning: old +# pairs older than RETENTION_DAYS get deleted right after a new upgrade +# succeeds, since these exist for tactical "undo the last upgrade" use, +# not long-term retention - disk size matters less here than it does for +# the nightly tier. backupdir="/home/sportrum/backup" +RETENTION_DAYS=14 timestamp=$(date +'%Y%m%d-%H%M%S') sqlbackup="sportrum-upgrade-backup-${timestamp}.sqldump" webappbackup="sportrum-upgrade-backup-${timestamp}.war" logsbackup="sportrum-log-backup-${timestamp}" + +# Deletes every backup artifact (sql dump, war snapshot, log dir) whose +# embedded timestamp is older than RETENTION_DAYS. Works from the union of +# timestamps found across all three backup dirs - not each dir's own +# listing - so a timestamp only disappears entirely once every artifact +# that shares it is old enough. rm -f/-rf are used throughout since a +# given timestamp may legitimately be missing one artifact (e.g. this +# upgrade was run with -f or -w and skipped a backup), and that's not an +# error here. +prune_upgrade_backups() { + local cutoff_epoch + cutoff_epoch=$(date -d "-${RETENTION_DAYS} days" +%s) + + local timestamps + timestamps=$( { + ls "${backupdir}/sql" 2>/dev/null | sed -n 's/^sportrum-upgrade-backup-\(.*\)\.sqldump$/\1/p' + ls "${backupdir}/webapp" 2>/dev/null | sed -n 's/^sportrum-upgrade-backup-\(.*\)\.war$/\1/p' + ls "${backupdir}/logs" 2>/dev/null | sed -n 's/^sportrum-log-backup-\(.*\)$/\1/p' + } | sort -u ) + + for ts in $timestamps; do + local ts_epoch + ts_epoch=$(date -d "${ts:0:4}-${ts:4:2}-${ts:6:2} ${ts:9:2}:${ts:11:2}:${ts:13:2}" +%s 2>/dev/null) || continue + if [ "$ts_epoch" -lt "$cutoff_epoch" ]; then + echo "Pruning short-lived backup pair from ${ts} (older than ${RETENTION_DAYS} days)..." + rm -f "${backupdir}/sql/sportrum-upgrade-backup-${ts}.sqldump" + rm -f "${backupdir}/webapp/sportrum-upgrade-backup-${ts}.war" + rm -rf "${backupdir}/logs/sportrum-log-backup-${ts}" + fi + done +} + if [ "$backup_sql" == "true" ]; then echo "Backup database to ${sqlbackup}..."; sleep 2 pg_dump sportrum > ${backupdir}/sql/${sqlbackup} @@ -31,3 +72,11 @@ cd /home/sportrum/ echo "Upgrade DB using manager at $(pwd)/manager..."; sleep 2 rm -rf manager ; unzip -q manager.zip ; cd manager bash -x manager-exec.sh upgrade +upgrade_status=$? +cd /home/sportrum/ + +if [ $upgrade_status -eq 0 ]; then + prune_upgrade_backups +else + echo "manager-exec.sh upgrade failed (exit ${upgrade_status}) - skipping backup pruning so nothing is discarded until this is investigated." +fi