Skip to content

Commit

Permalink
refactor the clean backups script
Browse files Browse the repository at this point in the history
  • Loading branch information
edulix committed Nov 5, 2023
1 parent 14cf658 commit 51a8c43
Showing 1 changed file with 26 additions and 47 deletions.
73 changes: 26 additions & 47 deletions templates/clean_old_postgres_backups.sh
Original file line number Diff line number Diff line change
@@ -1,78 +1,57 @@
#!/bin/bash

# This file is part of deployment-tool.
# Copyright (C) 2017 Sequent Tech Inc <[email protected]>
# Copyright (C) 2017 Sequent Tech Inc <[email protected]>

# deployment-tool is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License.

# deployment-tool is distributed in the hope that it will be useful,
# deployment-tool is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public License
# along with deployment-tool. If not, see <http://www.gnu.org/licenses/>.
# along with deployment-tool. If not, see <http://www.gnu.org/licenses/>.

# Automatically exit bash on any error inside the exit
set -e

BACKUP_DIR={{ config.postgres_backups.folder }}
KEEP_DAYS={{ config.postgres_backups.base_backups.keep_days }}
DATE_NOW=`date +%s`
DATE_NOW=$(date +%s)

if [ "$KEEP_DAYS" -eq "0" ]; then
echo "keeping backups indefinitely"
exit 0
fi

DATE_OLDEST=`echo "$DATE_NOW - (24*3600*$KEEP_DAYS)" | bc`
DATE_OLDEST=$(echo "$DATE_NOW - (24*3600*$KEEP_DAYS)" | bc)

LIST_BASE=`ls $BACKUP_DIR/base`
LIST_DUMP=`ls $BACKUP_DIR/dump`
LIST_WAL=`ls $BACKUP_DIR/wal`
delete_old_files() {
local directory=$1
local list=$2

for FNAME in $LIST_BASE
do
RAW_DATE=`echo $FNAME | sed 's/\([[:digit:]]*\)_\([[:digit:]]*\)_\([[:digit:]]*\)_\([[:digit:]]*\)_\([[:digit:]]*\)_\([[:digit:]]*\)/20\3-\2-\1 \4:\5:\6/'`
if [ "$RAW_DATE" == "$FNAME" ]; then
continue
fi
FDATE=`date --date="$RAW_DATE" +%s`
for FNAME in $list; do
# Get file's last modification time in seconds since the epoch
local FDATE=$(date -r "$directory/$FNAME" +%s)

# check date and remove if it's too old
if [ "$DATE_OLDEST" -gt "$FDATE" ]; then
echo "deleting $BACKUP_DIR/base/$FNAME"
rm -Rf $BACKUP_DIR/base/$FNAME
fi
done
# check date and remove if it's too old
if [ "$DATE_OLDEST" -gt "$FDATE" ]; then
local full_path="$directory/$FNAME"
echo "deleting $full_path"
rm -rf "$full_path"
fi
done
}

for FNAME in $LIST_DUMP
do
RAW_DATE=`echo $FNAME | sed 's/dump_\([[:digit:]]*\)_\([[:digit:]]*\)_\([[:digit:]]*\)_\([[:digit:]]*\)_\([[:digit:]]*\)_\([[:digit:]]*\)\.gz/20\3-\2-\1 \4:\5:\6/'`
if [ "$RAW_DATE" == "$FNAME" ]; then
continue
fi
FDATE=`date --date="$RAW_DATE" +%s`
LIST_BASE=$(ls "$BACKUP_DIR/base")
LIST_DUMP=$(ls "$BACKUP_DIR/dump")
LIST_WAL=$(ls "$BACKUP_DIR/wal")

# check date and remove if it's too old
if [ "$DATE_OLDEST" -gt "$FDATE" ]; then
echo "deleting $BACKUP_DIR/base/$FNAME"
rm -Rf $BACKUP_DIR/base/$FNAME
fi
done

for FNAME in $LIST_WAL
do
# Get file's last modification time in seconds since the epoch
FDATE=`date -r "$BACKUP_DIR/wal/$FNAME" +%s`

# check date and remove if it's too old
if [ "$DATE_OLDEST" -gt "$FDATE" ]; then
echo "deleting $BACKUP_DIR/wal/$FNAME"
rm -f $BACKUP_DIR/wal/$FNAME
fi
done
delete_old_files "$BACKUP_DIR/base" "$LIST_BASE"
delete_old_files "$BACKUP_DIR/dump" "$LIST_DUMP"
delete_old_files "$BACKUP_DIR/wal" "$LIST_WAL"

exit 0

0 comments on commit 51a8c43

Please sign in to comment.