Skip to content

Instantly share code, notes, and snippets.

@steeleforge
Created July 11, 2013 13:45
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 steeleforge/5975569 to your computer and use it in GitHub Desktop.
Save steeleforge/5975569 to your computer and use it in GitHub Desktop.
dispacher_clean.sh is a utility to read a config file that contains human readable rules to control the deletion of files on a batch basis. That makes this file somewhat dangerous. Execute access is required to run this and it should only be run with a user that has read/write access limited to htdocs. A configuration file sample is also in this…
#!/bin/bash
# #
# David Steele - david.steele@acquitygroup.com
# 2013 Acquity Group
#
# The purpose of this script is to be run regularly as part of a cron job and delete cached files that either have become stale or should be regenerated or left deleted because another related asset is newer.
#
# Config expected pattern (ignore brackets)
# "delete files like <file pattern> in <relative directory path> older than <duration or file path>"
##
pattern="/^delete files like .+ in .+ older than \d \w+$/"
function delete_file() {
if [ -f "$1" ]; then
echo "[`date`] - Deleting file: $1"
rm $1
fi
}
function clean_empty_dirs() {
if [ -d "$1" ]; then
find $1 -depth -type d -empty | while read d; do
echo "*** [`date`] - Deleting empty directory: '$d' ***"
rmdir $d
del_dir_count=$((del_dir_count + 1))
done
fi
}
#term,location,#|path,duration
function process_rule() {
IFS=","
params=($r)
if [[ ( -z "${params[0]}" ) || ( -z "${params[1]}" ) || ( -z "${params[2]}" ) ]]; then
echo "*** Error in statement ***"
else
if [ -d "${params[1]}" ]; then
if [[ "${params[2]}" =~ ^[0-9]+$ ]]; then
older_than_ttl ${params[1]} ${params[0]} ${params[2]} ${params[3]}
else
older_than_file ${params[1]} ${params[0]} ${params[2]}
fi
echo "*** [`date`] - CLEANUP: '${params[1]}' ***"
clean_empty_dirs ${params[1]}
fi
fi
unset IFS
}
# - 1 search location
# - 2 file name search term
# - 3 test file
function older_than_file() {
find $1 -depth -type f -name $2 | while read f; do
if [ $f -ot "$3" ]; then
delete_file $f
fi
done
}
# - 1 search location
# - 2 file name search term
# - 3 ttl #
# - 4 ttl literal duration
function older_than_ttl() {
files=()
case $4 in
minute) files=$(find $1 -depth -type f -name $2 -mmin +$3) ;;
minutes) files=$(find $1 -depth -type f -name $2 -mmin +$3) ;;
hour) files=$(find $1 -depth -type f -name $2 -mmin +$(($3*60))) ;;
hours) files=$(find $1 -depth -type f -name $2 -mmin +$(($3*60))) ;;
week) files=$(find $1 -depth -type f -name $2 -mtime +$(($3*7))) ;;
weeks) files=$(find $1 -depth -type f -name $2 -mtime +$(($3*7))) ;;
*) files=$(find $1 -depth -type f -name $2 -mtime +$3) ;; #days||day
esac
echo $files | while read f; do
delete_file $f
done
}
# Entry script that reads/interprets configuration
# for rule processing.
if [ -f "$1" ]; then
while read line
do
rules=$(echo $line | awk '{print $4","$6","$9","$10}')
for r in $rules; do
echo "*** [`date`] - BEGIN: '$line' ***"
process_rule $r
echo "*** [`date`] - END: '$line' ***"
done
done < $1
else
echo "*** Please provide configuration file ***"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment