Last active
December 22, 2016 16:21
-
-
Save labbots/fe3ebce678287b6e13aede89e5e730a1 to your computer and use it in GitHub Desktop.
Bash script to clean files in directory based on access time and created time. https://labbots.com/delete-stale-files-by-access-time-bash/
This file contains hidden or 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 | |
#!/bin/bashset -e | |
# Script to cleanup cache files in file system. | |
# This script is used to make sure the cache folder does not exceed the set threshold limit in the server. | |
# The script can be configured as cron job in the server to monitor and delete older files in specified directory. | |
# The script sends out email if the script cannot bring the directory size below the threshold even after deleting | |
# files based on min and max access time and created time set in the script. | |
# | |
# Usage | |
#------ | |
# ./file_cache_clean.sh <directory> <threshold_in_mb> | |
# | |
# Note: Make sure the threshold is set in MB and if not provided it defaults to 100MB | |
# The script is required to be executed as root. | |
# | |
# Caution : Make sure the directory argument is correct. Since the script is executed as root. Any wrong directory input can | |
# corrupt the system. | |
# Incase of failure to minimize to threshold notify | |
EMailTo="[email protected]" | |
DateStamp=$(date '+%Y%m%d_%H%M') | |
# Global varible declarations | |
FOLDER="$1" | |
THRESHOLD=${2:-100} | |
VERBOSE=true | |
MAX_ACCESSTIME_LIMIT=30 | |
MIN_ACCESSTIME_LIMIT=10 | |
MAX_CREATETIME_LIMIT=30 | |
MIN_CREATETIME_LIMIT=10 | |
function log() { | |
error="$1" | |
if [[ "$VERBOSE" = true ]] || [[ "$error" = true ]] ; then | |
echo -e "${1}" | |
fi | |
} | |
# Check whether the script is run as root | |
if [[ $UID -ne 0 ]];then | |
log "Script needs to be run as root.Aborted with no further action." true | |
exit 1 | |
fi | |
# Validation of input | |
if [[ "$FOLDER" == "/" ]];then | |
log "Forbidden." true | |
exit 1 | |
fi | |
if [[ -z "$FOLDER" ]];then | |
log "Folder argument is mandatory." true | |
exit 1 | |
fi | |
if [[ ! -d "$FOLDER" ]];then | |
log "Specified Folder does not exist." true | |
exit 1 | |
else | |
# Resolve relative path or symlinks to actual path | |
FOLDER="$(readlink -f $FOLDER)" | |
fi | |
# End of Validation | |
# Delete all files in directory based on access time | |
function cleanUpDirectoryByAccessTime(){ | |
ACCESSTIME=$1 | |
FOLDERSIZE=$(du -ms $FOLDER | awk '{print $1}') | |
if [[ "$FOLDERSIZE" -gt "$THRESHOLD" ]] ;then | |
find "$FOLDER" -type f -atime +${ACCESSTIME} -delete | |
FOLDERSIZE=$(du -ms $FOLDER | awk '{print $1}') | |
if [[ "$FOLDERSIZE" -gt "$THRESHOLD" ]];then | |
access_dcr=$(($ACCESSTIME-10)) | |
if [[ "$access_dcr" -ge "$MIN_ACCESSTIME_LIMIT" ]];then | |
cleanUpDirectoryByAccessTime $access_dcr | |
fi | |
fi | |
else | |
return 0; | |
fi | |
} | |
# Delete all files in directory based on create time | |
function cleanUpDirectoryByCreatedTime(){ | |
CREATETIME=$1 | |
FOLDERSIZE=$(du -ms $FOLDER | awk '{print $1}') | |
if [[ "$FOLDERSIZE" -gt "$THRESHOLD" ]];then | |
find "$FOLDER" -type f -ctime +${CREATETIME} -delete | |
FOLDERSIZE=$(du -ms $FOLDER | awk '{print $1}') | |
if [[ "$FOLDERSIZE" -gt "$THRESHOLD" ]];then | |
access_dcr=$(($CREATETIME-10)) | |
if [[ "$access_dcr" -ge "$MIN_CREATETIME_LIMIT" ]];then | |
cleanUpDirectoryByCreatedTime $access_dcr | |
fi | |
fi | |
else | |
return 0; | |
fi | |
} | |
log "Starting to process $FOLDER for cleanup by access time." | |
cleanUpDirectoryByAccessTime $MAX_ACCESSTIME_LIMIT | |
log "Starting to process $FOLDER for cleanup by created time." | |
cleanUpDirectoryByCreatedTime $MAX_CREATETIME_LIMIT | |
log "Delete all empty folders." | |
find "$FOLDER" -empty -type d -delete | |
FOLDERSIZE=$(du -ms $FOLDER | awk '{print $1}') | |
if [[ "$FOLDERSIZE" -gt "$THRESHOLD" ]];then | |
echo "folder $FOLDER is above threshold and script cannot clean up the directory.Date: $DateStamp" | mail -s "File cache clearout" ${EMailTo} > /dev/null 2>&1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment