sportrum-deploy/upgrade-cmds.sh

83 lines
3.4 KiB
Bash
Raw Normal View History

# 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}
else
echo "Backup of database skipped!"
fi
if [ "$backup_war" == "true" ]; then
echo "Backup webapp to ${webappbackup}..."; sleep 2
cd /home/sportrum/webapp
zip -q -r sportrum sportrum
mv sportrum.zip ${backupdir}/webapp/${webappbackup}
else
echo "Backup webapp skipped!"
fi
cd /home/sportrum/webapp
echo "Keep logs in ${backupdir}/logs/${logbackup}..."; sleep 1
mkdir -p ${backupdir}/logs/${logsbackup}
cp sportrum/logs/* ${backupdir}/logs/${logsbackup}
echo "Remove webapp at $(pwd) and unzip new war..."; sleep 2
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/
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