Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement