Skip to content

Commit

Permalink
Add script to purge backup iles
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanMarie-PM authored and asteel-gsa committed Oct 11, 2023
1 parent bda7e9a commit cd1b638
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions bin/ops/purge_backups.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
set -e


echo "This script works only in dev amd production spaces as we take backups only in these spaces"
echo "It relies on naming conventions used in br_nackup.sh"
echo "It assumes that the S3 bucket named 'backups' has been bound"

cfspace=$(cf target |tail -1|cut -d':' -f2|xargs)
echo "Space: ${cfspace}"

if [[ "$cfspace" != "dev" && "$cfspace" != "prodiction" ]]; then
echo "Error: Backups may only be purged fromr dev or production"
exit 1
fi
prefix=$(date '+%Y-%m-%d-%H%M')
export PATH=/home/vcap/deps/0/apt/usr/lib/postgresql/15/bin:$PATH
python manage.py fac_s3 backups --ls | awk '{print $1}' | grep "db\-backup" | sort -r > "$prefix"_backup_list.txt

# retain 2 PR backups, 7 CRON backups and 2 non-std backups
grep "\_PR\_" "$prefix"_backup_list.txt | tail +3 > "$prefix"_purge_list.txt
grep "\_CRON\_" "$prefix"_backup_list.txt | tail +8 >> "$prefix"_purge_list.txt
grep -v -e "\_PR\_" -e "\_CRON\_" "$prefix"_backup_list.txt | tail +3 >> "$prefix"_purge_list.txt

echo "Purging backups ..."
while read line; do
db_backup_name="$line"
echo "DB" "$db_backup_name"
media_backup_name=$(echo "$line" | sed -e 's/-db-backup.psql.bin/-media-backup.tar/g')
echo "MEDIA" "$media_backup_name"
python manage.py fac_s3 backups --rm $db_backup_name
python manage.py fac_s3 backups --rm $media_backup_name
done < "$prefix"_purge_list.txt

rm "$prefix"_backup_list.txt "$prefix"_purge_list.txt

0 comments on commit cd1b638

Please sign in to comment.