Skip to content

Instantly share code, notes, and snippets.

@patmandenver
Created July 19, 2015 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patmandenver/c739a7a66339b596a678 to your computer and use it in GitHub Desktop.
Save patmandenver/c739a7a66339b596a678 to your computer and use it in GitHub Desktop.
Bash Script to purge old Database files
#!/bin/bash
#
# Script to purge old database backups
# Run via cron job
#
#########################################
#########################################
#
# Config section
#
#########################################
BACKUP_DIR="/data/db-nightly-backups"
DAYS_TO_KEEP=15
#########################################
#
# Delete Old Datbases older than $DAYS_TO_KEEP
#
#########################################
# for testing you can switch mtime for days to mmin for minutes
NUM_FILES_REMOVED=`find $BACKUP_DIR -maxdepth 1 -mtime +$DAYS_TO_KEEP -name "*.database.*" | wc -l`
# for testing you can switch mtime for days to mmin for minutes
if ! find $BACKUP_DIR -maxdepth 1 -mtime +$DAYS_TO_KEEP -name "*.database.*" | xargs -I{} rm -rf {};
then
echo "DB purge failed. Exiting." 1>&2
exit 3
fi
#########################################
#
# purge Complete now notify!
#
#########################################
echo ""
echo "================================"
echo ""
echo " DATABASE purge Complete"
echo ""
echo " Databases Removed : $NUM_FILES_REMOVED"
echo ""
echo " Only keeping $DAYS_TO_KEEP DAYS"
echo "================================"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment