Skip to content

Instantly share code, notes, and snippets.

@twtw
Created August 16, 2020 02:59
Show Gist options
  • Save twtw/2f5807e7ee256a5f3860c5d2e7e75df0 to your computer and use it in GitHub Desktop.
Save twtw/2f5807e7ee256a5f3860c5d2e7e75df0 to your computer and use it in GitHub Desktop.
archive everyday maillog or multi website logs to every file. origin from solaris rotate script.
#! /bin/sh
# in crontab:
# 59 23 * * * /home/user/bin/rotateMailLog.sh
# save one or many logs in one folder to other folder
LOGDIR=/var/log
if test -d $LOGDIR
then
cd $LOGDIR
for LOG in maillog messages.log
do
if test -s $LOG
then
test -f $LOG && mv $LOG /home/LOG/$LOG.`date +%Y%m%d`
cp /dev/null $LOG
chmod 644 $LOG
fi
done
fi
#kill -HUP `cat /var/run/syslogd.pid`
/bin/systemctl restart rsyslog
#! /bin/sh
# 58 23 * * * /home/bin/rotateMultiWebLogs.sh
# https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays[Introduction to Bash arrays | Opensource.com]
# define multi websites folders paths and log names, one by one
LOGDIR=(/var/logs/web1 /var/logs/web2)
LOGNAME=(access.log access.log)
# test parameters
fake_rotate(){
echo DIR: ${LOGDIR[$1]} LOGNAME: ${LOGNAME[$1]}
}
do_rotate(){
if test -d ${LOGDIR[$1]}
then
cd ${LOGDIR[$1]}
if test -s ${LOGNAME[$1]}
then
#test -f $LOG && mv $LOG /home/rtfm/LOG/$LOG.`date +%Y%m%d`
test -f ${LOGNAME[$1]} && mv ${LOGNAME[$1]} ${LOGNAME[$1]}.`date +%Y%m%d`
cp /dev/null ${LOGDIR[$1]}/${LOGNAME[$1]}
chmod 644 ${LOGDIR[$1]}/${LOGNAME[$1]}
fi
else
echo no ${LOGDIR[$1]}
fi
}
for logdir in ${!LOGDIR[@]}
do
do_rotate $logdir
done
#kill -HUP `cat /var/run/nginx.pid`
/bin/systemctl restart nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment