This script was written as a result of the space that metche backups were taking on wiki:PuffinServer, see ticket:531. This script is also deployed on wiki:PenguinServer. {{{ #!sh #!/bin/bash # This script can find or delete files in $DIR older than $DAYS # which are not symlinks and are not linked to by symlinks in # $DIR, it has been written to be run via cron to prevent metche # using too much disk space. # Number of days worth of files to save DAYS=1 # Directory where the files are DIR="/var/lib/metche" # check that the script is being run by root if [[ "$(id -u)" != "0" ]] ; then echo "You must run $0 as root or via sudo" exit 2 fi # Check that $DIR exists if [[ ! -d $DIR ]]; then echo "The directory $DIR doesn't exist, please edit the DIR variable in $0 and try again" exit 2 fi # Symlinks in $DIR SYMLINKS=$(find $DIR -maxdepth 1 -type l) # Destinations of the symlinks | separated for symlink in $SYMLINKS; do # Get the destination of the symlink DESTPATH=$(readlink $symlink) # Get the symlink filename DESTFILE=$(basename $DESTPATH) # If $SYMDEST doesn't exist then create it using the value of $DEST # and if it does exist then add $DEST to it if [[ ! $SYMDESTS ]]; then SYMDESTS="$DESTFILE" else SYMDESTS="$SYMDESTS|$DESTFILE" fi done # Change the space separated list of symlinks to a | separated one for symlink in $SYMLINKS; do LINK=$(basename $symlink) # If $LINKS doesn't exist then create it using the value of $LINK # and if it does exist then add $LINK to it if [[ ! $LINKS ]]; then LINKS="$LINK" else LINKS="$LINKS|$LINK" fi done # The symlinks and their destinations which we want to keep KEEPFILES=$(echo "$LINKS|$SYMDESTS") # Files older than $DAYS in $DIR OLDFILES=$(find $DIR -mtime +$DAYS -type f) # Delete files which are older than $DAYS and which # are not listed in $KEEPFILES if [[ $OLDFILES ]]; then # Loop through the $OLDFILES for file in $OLDFILES; do # Remove the $DIR from $file FILE=$(basename $file) # $MATCH only has a value if $FILE is not in $KEEPFILES MATCH=$(echo $FILE | egrep -v "^($KEEPFILES)$") if [[ $MATCH ]]; then # Check if there is a -d on the command line if [[ $1 == "-d" ]]; then # Delete the files if it's not in the $KEEPFILES list echo "Deleting $DIR/$FILE" rm $DIR/$FILE else echo "Deletable file older than $DAYS days: $DIR/$FILE" fi fi done else echo "No deletable files older that $DAYS days found in $DIR" fi # Check if there is a -d on the command line if [[ $1 != "-d" ]]; then # We are not going to delete anything echo "To remove deletable files run: $0 -d" fi }}}