Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.