| | 1 | This script was written as a result of the space that metche backups were taking on wiki:PuffinServer, see ticket:531. |
| | 2 | |
| | 3 | {{{ |
| | 4 | #!/bin/bash |
| | 5 | |
| | 6 | # This script can find or delete files in $DIR older than $DAYS |
| | 7 | # which are not symlinks and are not linked to by symlinks in |
| | 8 | # $DIR, it has been written to be run via cron to prevent metche |
| | 9 | # using too much disk space. |
| | 10 | |
| | 11 | # Number of days worth of files to save |
| | 12 | DAYS=1 |
| | 13 | |
| | 14 | # Directory where the files are |
| | 15 | DIR="/var/lib/metche" |
| | 16 | |
| | 17 | # check that the script is being run by root |
| | 18 | if [[ "$(id -u)" != "0" ]] ; then |
| | 19 | echo "You must run $0 as root or via sudo" |
| | 20 | exit 2 |
| | 21 | fi |
| | 22 | |
| | 23 | # Check that $DIR exists |
| | 24 | if [[ ! -d $DIR ]]; then |
| | 25 | echo "The directory $DIR doesn't exist, please edit the DIR variable in $0 and try again" |
| | 26 | exit 2 |
| | 27 | fi |
| | 28 | |
| | 29 | # Symlinks in $DIR |
| | 30 | SYMLINKS=$(find $DIR -maxdepth 1 -type l) |
| | 31 | |
| | 32 | # Destinations of the symlinks | seperated |
| | 33 | for symlink in $SYMLINKS; do |
| | 34 | # Get the destination of the symlink |
| | 35 | DESTPATH=$(readlink $symlink) |
| | 36 | # Get the symlink filename |
| | 37 | DESTFILE=$(basename $DESTPATH) |
| | 38 | # If $SYMDEST doesn't exist then create it using the value of $DEST |
| | 39 | # and if it does exist then add $DEST to it |
| | 40 | if [[ ! $SYMDESTS ]]; then |
| | 41 | SYMDESTS="$DESTFILE" |
| | 42 | else |
| | 43 | SYMDESTS="$SYMDESTS|$DESTFILE" |
| | 44 | fi |
| | 45 | done |
| | 46 | |
| | 47 | # Change the space seperated list of symlinks to a | seperated one |
| | 48 | for symlink in $SYMLINKS; do |
| | 49 | LINK=$(basename $symlink) |
| | 50 | # If $LINKS doesn't exist then create it using the value of $LINK |
| | 51 | # and if it does exist then add $LINK to it |
| | 52 | if [[ ! $LINKS ]]; then |
| | 53 | LINKS="$LINK" |
| | 54 | else |
| | 55 | LINKS="$LINKS|$LINK" |
| | 56 | fi |
| | 57 | done |
| | 58 | |
| | 59 | # The symlinks and their destinations which we want to keep |
| | 60 | KEEPFILES=$(echo "$LINKS|$SYMDESTS") |
| | 61 | |
| | 62 | # Files older than $DAYS in $DIR |
| | 63 | OLDFILES=$(find $DIR -mtime +$DAYS -type f) |
| | 64 | |
| | 65 | # Delete files which are older than $DAYS and which |
| | 66 | # are not listed in $KEEPFILES |
| | 67 | if [[ $OLDFILES ]]; then |
| | 68 | # Loop through the $OLDFILES |
| | 69 | for file in $OLDFILES; do |
| | 70 | # Remove the $DIR from $file |
| | 71 | FILE=$(basename $file) |
| | 72 | # $MATCH only has a value if $FILE is not in $KEEPFILES |
| | 73 | MATCH=$(echo $FILE | egrep -v "^($KEEPFILES)$") |
| | 74 | if [[ $MATCH ]]; then |
| | 75 | # Check if there is a -d on the command line |
| | 76 | if [[ $1 == "-d" ]]; then |
| | 77 | # Delete the files if it's not in the $KEEPFILES list |
| | 78 | echo "Deleting $DIR/$FILE" |
| | 79 | rm $DIR/$FILE |
| | 80 | else |
| | 81 | echo "Deletable file older than $DAYS days: $DIR/$FILE" |
| | 82 | fi |
| | 83 | fi |
| | 84 | done |
| | 85 | else |
| | 86 | echo "No deletable files older that $DAYS days found in $DIR" |
| | 87 | fi |
| | 88 | |
| | 89 | # Check if there is a -d on the command line |
| | 90 | if [[ $1 != "-d" ]]; then |
| | 91 | # We are not going to delete anything |
| | 92 | echo "To remove deletable files run: $0 -d" |
| | 93 | fi |
| | 94 | }}} |