Created
July 16, 2015 21:30
-
-
Save adyrcz/e5cb7acf29507e5f6c22 to your computer and use it in GitHub Desktop.
Logrotate Script
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 | |
# rotates webserver log files | |
# Version 1.0 | |
# Andy Dyrcz | |
# Files to rotate are in the format: | |
# | |
# FILEPREFIX.ENVIRONMENT.DATE | |
# Get PATHS and SITENAME from control script name | |
# get absolute path to program folder | |
ctl=`readlink -f "$0"` | |
# get ServerRoot folder containing folders: bin/, conf/, log/, www/ | |
SERVERROOT=${ctl%/*/*} | |
LOGDIR=$SERVERROOT/logs | |
HOST=`hostname -s` | |
DOMAIN=`hostname -d` | |
TYPE=$(echo $HOSTNAME | cut -c5) | |
# Get the ENV var set from the hostname. | |
p="prod" | |
q="qa" | |
d="dev" | |
u="uat" | |
eval "ENV=\$$TYPE" | |
# prefixes for files to rotate | |
FILEPREFIXES=(access error upstream) | |
# backup with timestamp, clear working file, compress after 3 days, delete after 10 days | |
DATE=`date +%Y%m%d.%H%M` | |
#COMPRESS="+3" | |
#REMOVE="+10" | |
COMPRESS="+3" | |
REMOVE="+60" | |
ARCH=`uname` | |
if [ $ARCH == "Darwin" ]; then | |
XARGS_REPL_ARG="-I {}" | |
else | |
XARGS_REPL_ARG="-i" | |
fi | |
# Run compress and removes after all the sites files have been rotated. | |
for fp in ${FILEPREFIXES[*]}; do | |
# copy/clear current log | |
while read file; do | |
# echo "cp $file $file.$DATE" | |
cp $file $file.$DATE | |
# echo "> $file" | |
> $file | |
done < <( find $LOGDIR/ -type f -name "$fp.$ENV.$DATE" ) | |
# do archive maintenance | |
# echo "find $LOGDIR/ -type f ! -name \"*.gz\" -mtime $COMPRESS -name \"$fp.$HOST.$ENV.*\" -exec gzip -f {} \; &>/dev/null" | |
find $LOGDIR/ -type f ! -name "*.gz" -mtime $COMPRESS -name "$fp.$HOST.$ENV.*" -exec gzip -f {} \; &>/dev/null | |
# echo "find $LOGDIR/ -type f -mtime $REMOVE -exec rm -f {} \; &>/dev/null" | |
find $LOGDIR/ -type f -mtime $REMOVE -exec rm -f {} \; &>/dev/null | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment