####################################################################### # age_zip - gzip files in X directory that meet Y criteria # allowing for recursion # J. David Schronce - Mon Jul 16 16:05:33 CDT 2007 # ####################################################################### # age_zip compresses old files from whatever directory you specify. It's # great for compressing those old log files and temp files you might # need later # ####################################################################### LOGFILE=/some/dir/logs/age_zip/age_zip.`date '+%y%m%d-%H%M%S`.log ####################################################################### # Check for parms ####################################################################### if [ $# -lt 3 ] || [ $# -gt 4 ]; then echo "\nUSAGE: $0 [-r|-R] DirectoryName REGEX Age(in days)" echo "-r (or -R) for RECURSION is optional\n" exit fi if [ $# -eq 4 ]; then RECURSE=$1 if [ $RECURSE != "-r" ] && [ $RECURSE != "-R" ]; then echo "\nUSAGE: $0 [-r|-R] DirectoryName REGEX Age(in days)" echo "-r (or -R) for RECURSION is optional\n" exit else PRUNE="" RECURSE="full recursion" fi TARGETDIR=$2 PARM=$3 AGE=$4 fi if [ $# -eq 3 ]; then TARGETDIR=$1 PARM=$2 AGE=$3 PRUNE="-depth 0" RECURSE="no recursion" fi ####################################################################### # Validate TARGETDIR ####################################################################### if [ $TARGETDIR = "/" ]; then echo "\n\"/\" is an invalid starting directory..\n" exit fi if [ -d $TARGETDIR ]; then cd $TARGETDIR 2> /dev/null else echo "\n$TARGETDIR does not exist..\n" exit fi ####################################################################### # Count number of matching files (use 'bc' to strip leading spaces) ####################################################################### FOUND=`find . -name "$PARM" -type f -mtime +$AGE $PRUNE 2> /dev/null | grep -v gz$ | wc -l` FOUND=`echo "$FOUND + 0" | bc` ####################################################################### # Quit if nothing to do ####################################################################### if [ $FOUND -eq 0 ]; then echo "\n$FOUND Files to Compress - exiting...\n" exit fi ####################################################################### # Initialize LOGFILE ####################################################################### echo "Files Compressed: $FOUND" > $LOGFILE echo "Matching $PARM, at least $AGE days old." >> $LOGFILE echo "Starting in: $TARGETDIR with $RECURSE.\n" >> $LOGFILE ####################################################################### # Process files that DO NOT end in 'gz' ####################################################################### for foundfile in `find . -name "$PARM" -type f -mtime +$AGE $PRUNE | grep -v gz$`; do ls -l $foundfile >> $LOGFILE cd `dirname $foundfile` 2> /dev/null gzip -Nv `basename $foundfile` >> $LOGFILE 2>> $LOGFILE cd $TARGETDIR 2> /dev/null if [ -f $foundfile.gz ]; then ls -l $foundfile.gz >> $LOGFILE fi echo "" >> $LOGFILE done