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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tj4SF2BGFxgoRYrCAzNpDb
This commit is contained in:
parent
aef964fc4d
commit
9291393965
1 changed files with 49 additions and 0 deletions
|
|
@ -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"
|
backupdir="/home/sportrum/backup"
|
||||||
|
RETENTION_DAYS=14
|
||||||
timestamp=$(date +'%Y%m%d-%H%M%S')
|
timestamp=$(date +'%Y%m%d-%H%M%S')
|
||||||
sqlbackup="sportrum-upgrade-backup-${timestamp}.sqldump"
|
sqlbackup="sportrum-upgrade-backup-${timestamp}.sqldump"
|
||||||
webappbackup="sportrum-upgrade-backup-${timestamp}.war"
|
webappbackup="sportrum-upgrade-backup-${timestamp}.war"
|
||||||
logsbackup="sportrum-log-backup-${timestamp}"
|
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
|
if [ "$backup_sql" == "true" ]; then
|
||||||
echo "Backup database to ${sqlbackup}..."; sleep 2
|
echo "Backup database to ${sqlbackup}..."; sleep 2
|
||||||
pg_dump sportrum > ${backupdir}/sql/${sqlbackup}
|
pg_dump sportrum > ${backupdir}/sql/${sqlbackup}
|
||||||
|
|
@ -31,3 +72,11 @@ cd /home/sportrum/
|
||||||
echo "Upgrade DB using manager at $(pwd)/manager..."; sleep 2
|
echo "Upgrade DB using manager at $(pwd)/manager..."; sleep 2
|
||||||
rm -rf manager ; unzip -q manager.zip ; cd manager
|
rm -rf manager ; unzip -q manager.zip ; cd manager
|
||||||
bash -x manager-exec.sh upgrade
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue