This commit is contained in:
oliver
2026-06-22 06:38:27 -03:00
parent 43d2a16ac4
commit af2fe940e7
8 changed files with 114 additions and 20 deletions
+46 -8
View File
@@ -13,7 +13,7 @@ source /4server/sbin/helpers
get_contract_info
log "UUID: $UUID"
log "Backup slots: $BACKUP_SLOTS"
log "Retention: daily 14d, weekly 12w"
log "DB host: $POSTGRES_HOST"
# Build paths
@@ -48,13 +48,51 @@ doas docker exec "$UUID" chown odoo:odoo -R /mnt/backup
doas ln -sf "../$FILENAME" "$BB_DIR/current.zip"
log "bb/current.zip -> ../$FILENAME"
# Remove old backups beyond the configured slot count
# Process substitution (< <(...)) keeps the loop in the main shell so
# set -e / exit codes propagate correctly — unlike a pipe subshell.
while IFS= read -r old_file; do
log "Deleting old backup: $old_file"
doas rm -f "$old_file"
done < <(ls -t "$BACKUP_DIR"/[0-9]*.zip 2>/dev/null | tail -n +$((BACKUP_SLOTS + 3)))
# --- Backup retention ---
# Policy:
# - Keep all backups from the last 14 days (daily tier)
# - For backups older than 14 days but within 3 months (12 weeks),
# keep one per ISO week (the newest in each week)
# - Delete everything else
BACKUP_DAYS_DAILY=14
BACKUP_WEEKS_WEEKLY=12 # ~3 months
# Map of kept files (absolute path -> 1)
declare -A kept_files
# Track which ISO weeks already have a kept weekly backup
declare -A weekly_kept
now_ts=$(date +%s)
while IFS= read -r file; do
filename=$(basename "$file")
filedate="${filename:0:8}" # YYYYMMDD from filename
# BusyBox date (Alpine) requires YYYY-MM-DD; compact YYYYMMDD is rejected
filedate_fmt="${filedate:0:4}-${filedate:4:2}-${filedate:6:2}"
file_ts=$(date -d "$filedate_fmt" +%s 2>/dev/null) || continue
age_days=$(( (now_ts - file_ts) / 86400 ))
if [ "$age_days" -le "$BACKUP_DAYS_DAILY" ]; then
# --- Daily tier: keep everything within the last 14 days ---
kept_files["$file"]=1
elif [ "$age_days" -le $((BACKUP_DAYS_DAILY + BACKUP_WEEKS_WEEKLY * 7)) ]; then
# --- Weekly tier: keep 1 per ISO week ---
week_key=$(date -d "$filedate_fmt" +%G%V) # ISO year + week number
if [ -z "${weekly_kept[$week_key]-}" ]; then
weekly_kept[$week_key]=1
kept_files["$file"]=1
fi
fi
done < <(ls -t "$BACKUP_DIR"/[0-9]*.zip 2>/dev/null)
# Delete any backup not in the keep map
for file in "$BACKUP_DIR"/[0-9]*.zip; do
[ -f "$file" ] || continue
if [ -z "${kept_files[$file]-}" ]; then
log "Deleting old backup: $file"
doas rm -f "$file"
fi
done
log "Backup finished successfully ."