Skip to content

Commit

Permalink
Added minimum file requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
aSouchereau committed Oct 23, 2023
1 parent 48a3f8d commit e985706
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions scripts/cleanup.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
echo "Cleaning up expired backups"

# Set default backup minimums
MIN_DAILY_BACKUPS="${MIN_DAILY_BACKUPS:-$DAILY_RETENTION}"
MIN_WEEKLY_BACKUPS="${MIN_WEEKLY_BACKUPS:-$WEEKLY_RETENTION}"
MIN_MONTHLY_BACKUPS="${MIN_MONTHLY_BACKUPS:-$MONTHLY_RETENTION}"

# Convert retention values from days to seconds (originals defined in docker-compose/docker run)
# Convert retention values from days to seconds so it can be easily compared to file timestamps
DAILY_RETENTION=$(($DAILY_RETENTION * 86400))
WEEKLY_RETENTION=$(($WEEKLY_RETENTION * 604800))
MONTHLY_RETENTION=$(($MONTHLY_RETENTION * 2678400))



# Set current date to fixed past date so I dont have to keep updating list of filenames for dev environments
if [ "$APP_ENV" = "dev" ]; then
NOW=1697457600 # 2023-10-16 1200hr GMT
Expand All @@ -18,12 +24,12 @@ fi
function cleanup() {
cd "${OUTPUT_DIR}"
retentionCutoff=$(date -u -d "@$((${NOW} - ${1} ))" +%s) # get epoch time for cutoff date of provided retention period
echo "${retentionCutoff}"
for file in *-*-*_vw-data.tar; do
excessFiles=$(find "$OUTPUT_DIR" -maxdepth 1 -type f -name '*-*-*_vw-data.tar' -exec basename {} \; | sort -r | tail -n +$((${2} + 1)))

for file in $excessFiles; do
fdate=$(echo $file | cut -d'_' -f1) # extract timestamp from filename
echo "fdate: ${fdate}"
fsec=$(date +%s --date=${fdate}) # convert filename timestamp into unix timestamp
echo "fsec: ${fsec}"

if [[ $fsec -lt $retentionCutoff ]]; then # remove file if timestamp older than retention cutoff
echo "rm $file"
fi
Expand All @@ -33,13 +39,13 @@ function cleanup() {

case "$BACKUP_TYPE" in
daily)
cleanup "$DAILY_RETENTION"
cleanup "$DAILY_RETENTION" "$MIN_DAILY_BACKUPS"
;;
weekly)
cleanup "$WEEKLY_RETENTION"
cleanup "$WEEKLY_RETENTION" "$MIN_WEEKLY_BACKUPS"
;;
monthly)
cleanup "$MONTHLY_RETENTION"
cleanup "$MONTHLY_RETENTION" "$MIN_MONTHLY_BACKUPS"
;;
*)
echo "Invalid backup type. Skipping cleanup"
Expand Down

0 comments on commit e985706

Please sign in to comment.