Skip to content

Commit

Permalink
Use wal-g delete retain to clean up old backups
Browse files Browse the repository at this point in the history
  • Loading branch information
simonklb committed Jun 20, 2024
1 parent a4f11b0 commit 30b4d13
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion postgres-appliance/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ FROM $BASE_IMAGE as dependencies-builder

ARG DEMO

ENV WALG_VERSION=v2.0.1
ENV WALG_VERSION=v3.0.0

COPY build_scripts/dependencies.sh /builddeps/

Expand Down
40 changes: 21 additions & 19 deletions postgres-appliance/scripts/postgres_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,31 @@ else
POOL_SIZE=(--pool-size "$POOL_SIZE")
fi

BEFORE=""
LEFT=0

NOW=$(date +%s -u)
readonly NOW
while read -r name last_modified rest; do
last_modified=$(date +%s -ud "$last_modified")
if [ $(((NOW-last_modified)/86400)) -ge $DAYS_TO_RETAIN ]; then
if [ -z "$BEFORE" ] || [ "$last_modified" -gt "$BEFORE_TIME" ]; then
BEFORE_TIME=$last_modified
BEFORE=$name

if [[ "$USE_WALG_BACKUP" == "true" ]]; then
AFTER=$((NOW-(DAYS_TO_RETAIN*86400)))
$WAL_E delete --use-sentinel-time --confirm retain FIND_FULL "$DAYS_TO_RETAIN" --after "$(date --iso-8601=seconds -d @"${AFTER}")"
else
BEFORE=""
LEFT=0

while read -r name last_modified rest; do
last_modified=$(date +%s -ud "$last_modified")
if [ $(((NOW-last_modified)/86400)) -ge $DAYS_TO_RETAIN ]; then
if [ -z "$BEFORE" ] || [ "$last_modified" -gt "$BEFORE_TIME" ]; then
BEFORE_TIME=$last_modified
BEFORE=$name
fi
else
# count how many backups will remain after we remove everything up to certain date
((LEFT=LEFT+1))
fi
else
# count how many backups will remain after we remove everything up to certain date
((LEFT=LEFT+1))
fi
done < <($WAL_E backup-list 2> /dev/null | sed '0,/^name\s*\(last_\)\?modified\s*/d')
done < <($WAL_E backup-list 2> /dev/null | sed '0,/^name\s*\(last_\)\?modified\s*/d')

# we want keep at least N backups even if the number of days exceeded
if [ -n "$BEFORE" ] && [ $LEFT -ge $DAYS_TO_RETAIN ]; then
if [[ "$USE_WALG_BACKUP" == "true" ]]; then
$WAL_E delete before FIND_FULL "$BEFORE" --confirm
else
# we want keep at least N backups even if the number of days exceeded
if [ -n "$BEFORE" ] && [ $LEFT -ge $DAYS_TO_RETAIN ]; then
$WAL_E delete --confirm before "$BEFORE"
fi
fi
Expand Down

0 comments on commit 30b4d13

Please sign in to comment.