Created
January 7, 2015 10:55
-
-
Save AD7six/1aab280f9530bfd2aeb8 to your computer and use it in GitHub Desktop.
Script to ensure a folder used for caching always has x% free space. Generated with new_script http://linuxcommand.org/script_library.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################################################################ | |
# | |
# Delete least used files | |
# | |
################################################################################ | |
PROGNAME=${0##*/} | |
PROGDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
VERSION="0.1" | |
DRYRUN=0 | |
error_exit() { | |
echo -e "${PROGNAME}: ${1:-"Unknown Error"}" >&2 | |
exit 1 | |
} | |
graceful_exit() { | |
exit | |
} | |
usage() { | |
echo -e "Usage: $PROGNAME [-h|--help|-n|--dryrun] directory" | |
} | |
help_message() { | |
cat <<- _EOF_ | |
$PROGNAME ver. $VERSION | |
Find least-accessed files, and delete them | |
Will ensure that the relevant disk is less than 70% | |
full | |
$(usage) | |
Example: | |
$PROGNAME /some/path/ | |
Drive /dev/sdb1 has xxx free bytes | |
need yyy bytes available | |
Finding files to delete ... | |
Deleting the following files: | |
/some/path/foo.zip | |
/some/path/bar.zip | |
Options: | |
-h, --help Display this help message and exit. | |
-n, --dryrun Simulate only | |
_EOF_ | |
return | |
} | |
function driveSize { | |
stats=`df -P $DIR | awk 'NR>1{print}'` | |
DRIVE=`echo $stats | awk '{print $1}'` | |
DRIVESIZE=`echo $stats | awk '{print $2}'` | |
DRIVEUSED=`echo $stats | awk '{print $3}'` | |
TARGETUSED=`expr $DRIVESIZE \* 7 / 10` | |
DRIVEFREE=`expr $DRIVESIZE - $DRIVEUSED` | |
TARGETFREE=`expr $DRIVESIZE \* 3 / 10` | |
BYTESTODELETE=`expr $DRIVEUSED - $TARGETUSED` | |
} | |
function findToDelete { | |
FILEPATH=`tempfile` | |
# find | |
# %A+ - Last access time YYYY-MM-DD:HH:MM:SS.X | |
# %p - filename | |
# %s - Size in bytes | |
find "$DIR" -printf "%A+::%p::%s\n" \ | |
| sort > "$FILEPATH.raw" | |
cat "$FILEPATH.raw" | awk -v todelete="$BYTESTODELETE" -F "::" ' | |
BEGIN { deleted=0; } | |
{ | |
deleted += $3; | |
if (deleted < todelete) { print $2; } | |
} | |
' > "$FILEPATH.processed" | |
} | |
function main { | |
driveSize; | |
echo "Drive $DRIVE has $DRIVEFREE free bytes" | |
echo " need $TARGETFREE bytes available" | |
if [ $DRIVEFREE -lt $TARGETFREE ]; | |
then | |
echo "Finding files to delete ..." | |
findToDelete | |
if [ $DRYRUN == 1 ]; | |
then | |
echo "The following files would be deleted:" | |
echo "" | |
cat "$FILEPATH.processed" | |
else | |
echo "Deleting the following files:" | |
echo "" | |
cat "$FILEPATH.processed" | tee | rm | |
fi | |
else | |
echo "No action required at this time" | |
fi | |
} | |
# Parse command-line | |
while [[ -n $1 ]]; do | |
case $1 in | |
-n | --dryrun) | |
DRYRUN=1;; | |
-h | --help) | |
help_message; graceful_exit ;; | |
-* | --*) | |
usage | |
error_exit "Unknown option $1" ;; | |
*) | |
DIR=$1;; | |
esac | |
shift | |
done | |
if [ "$DIR" == "" ]; | |
then | |
usage; | |
graceful_exit; | |
fi | |
main $DIR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment