# Runs as the sportrum user (see upgrade.sh's "sudo su - sportrum -c"), # with $mode and $tag already set in scope by the caller. # # Short-lived tier: 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. Deliberately aggressive about pruning - old pairs older than # RETENTION_DAYS get deleted right after a successful upgrade, since these # exist for tactical "undo the last upgrade" use, not long-term retention. # # Migrations tier (migrate mode only): a SEPARATE, NEVER-pruned directory # per tag (migrations//) holding a before/after SQL+WAR+logs snapshot # plus the full DB migration transcript. Kept indefinitely on purpose - # these are the record of exactly what a given schema change did, not a # rolling safety net - so they live under their own top-level directory # rather than any naming convention prune_upgrade_backups would need to # know to skip. backupdir="/home/sportrum/backup" RETENTION_DAYS=14 timestamp=$(date +'%Y%m%d-%H%M%S') # Deletes every SHORT-LIVED-TIER backup artifact (sql dump, war snapshot, # log dir) whose embedded timestamp is older than RETENTION_DAYS. Never # touches migrations/ - that lives under a different top-level directory # entirely, not just a different filename pattern, so there's no risk of # this ever pruning something meant to be kept forever. 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. # hotfix-code mode never takes a SQL dump), 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 } # Dumps the DB, zips the current webapp, and copies current logs to the # three given destinations. Shared by the short-lived tier and, for # migrate mode, the migrations// tier - one dump/zip per instant, # written wherever it's needed, rather than dumping the same DB twice # (nothing else can write to it while Tomcat is stopped, so a second dump # would just be redundant load, not a more accurate snapshot). mkdir -p on # the destination directories defensively - sportrum_backups is a fresh # named Docker volume (see sportrum-ansible's docker-compose.yml.j2), so # sql/webapp/logs/migrations don't necessarily already exist on it the way # they did on the old server's plain filesystem. take_snapshot() { local sql_dest="$1" war_dest="$2" logs_dest="$3" mkdir -p "$(dirname "$sql_dest")" "$(dirname "$war_dest")" echo "Backing up database to ${sql_dest}..." pg_dump sportrum > "${sql_dest}" echo "Backing up webapp to ${war_dest}..." ( cd /home/sportrum/webapp && zip -q -r sportrum sportrum && mv sportrum.zip "${war_dest}" ) echo "Backing up logs to ${logs_dest}..." mkdir -p "${logs_dest}" cp /home/sportrum/webapp/sportrum/logs/* "${logs_dest}" } case "$mode" in migrate) migdir="${backupdir}/migrations/${tag}" if [ -e "$migdir" ]; then echo "ERROR: ${migdir} already exists - refusing to overwrite an indefinitely-kept migration record. Re-run with a different tag." >&2 exit 1 fi mkdir -p "$migdir" sql_file="${backupdir}/sql/sportrum-upgrade-backup-${timestamp}.sqldump" war_file="${backupdir}/webapp/sportrum-upgrade-backup-${timestamp}.war" logs_dir="${backupdir}/logs/sportrum-log-backup-${timestamp}" take_snapshot "$sql_file" "$war_file" "$logs_dir" # Same instant as the short-lived-tier backup just taken above, also # preserved indefinitely under the tag - copied, not re-dumped (see # take_snapshot's comment). cp "$sql_file" "${migdir}/before.sqldump" cp "$war_file" "${migdir}/before.war" cp -r "$logs_dir" "${migdir}/before-logs" echo "Installing new sportrum.war and manager.zip..." cd /home/sportrum/webapp rm -rf sportrum/* ; unzip -q sportrum.war -d sportrum chmod -R o+w sportrum/WEB-INF/storage chmod -R o+w sportrum/logs cd /home/sportrum/ rm -rf manager ; unzip -q manager.zip ; cd manager echo "Running DB migration (transcript saved to ${migdir}/upgrade-output.log)..." bash manager-exec.sh upgrade 2>&1 | tee "${migdir}/upgrade-output.log" # A pipe's own exit status is tee's, not manager-exec.sh's - # PIPESTATUS[0] is the first command in the pipe, i.e. the actual # migration. upgrade_status=${PIPESTATUS[0]} cd /home/sportrum/ if [ $upgrade_status -eq 0 ]; then echo "Taking post-migration snapshot..." take_snapshot "${migdir}/after.sqldump" "${migdir}/after.war" "${migdir}/after-logs" prune_upgrade_backups else echo "manager-exec.sh upgrade failed (exit ${upgrade_status}) - skipping the post-migration snapshot and short-lived-tier pruning so nothing is discarded until this is investigated." >&2 exit "$upgrade_status" fi ;; hotfix-code) war_file="${backupdir}/webapp/sportrum-upgrade-backup-${timestamp}.war" logs_dir="${backupdir}/logs/sportrum-log-backup-${timestamp}" mkdir -p "$(dirname "$war_file")" echo "Backing up webapp to ${war_file}..." ( cd /home/sportrum/webapp && zip -q -r sportrum sportrum && mv sportrum.zip "${war_file}" ) echo "Backing up logs to ${logs_dir}..." mkdir -p "${logs_dir}" cp /home/sportrum/webapp/sportrum/logs/* "${logs_dir}" # No SQL backup - hotfix-code is only ever reached after # check-schema-diff.sh has already confirmed there's no pending schema # change, so there's nothing here a SQL dump would protect against # that the short-lived tier's existing history doesn't already cover. echo "Installing new sportrum.war..." cd /home/sportrum/webapp rm -rf sportrum/* ; unzip -q sportrum.war -d sportrum chmod -R o+w sportrum/WEB-INF/storage chmod -R o+w sportrum/logs # No manager-exec.sh upgrade - hotfix-code is specifically for changes # with no DB migration to run. prune_upgrade_backups ;; *) echo "ERROR: unknown mode '$mode'" >&2 exit 1 ;; esac