#!/bin/sh # rdiff-backup doesn't handle log.n -> log.n+1 renaming very well... # this script does log rotation by using .YYYYMMDD instead. # # - dean gaudet # Copyright (c) 2005 Dean Gaudet # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. # $Id: date-rotate,v 1.25 2005/04/27 01:18:17 dean Exp $ # the logs are listed up here... below you'll find the # restart commands, and some log parsing commands. search for "customize" # and edit appropriately. # WARNING: this script uses the append-only attribute, which can confuse # tools which expect to be able to rename/truncate/etc. a log file (for # example package management tools sometimes want to create/destroy logs). #### customize here logs="/var/log/messages /var/log/secure /var/log/maillog /var/log/cron" logs="$logs /var/log/ipaccounting /var/log/ipacct /var/log/temperature" logs="$logs /var/log/apache/access_log /var/log/apache/error_log" logs="$logs /var/log/apache/access_log.not-configured" logs="$logs /var/log/apache/access_log.catzooks" logs="$logs /var/account/pacct" logs="$logs /var/log/sheep/access_log /var/log/sheep/error_log /var/log/sheep/fsd/fsd_log" logs="$logs /var/log/webmail/access_log /var/log/webmail/error_log" keep=180 #### ext=`date +%Y%m%d` ext_long=`date +%Y%m%d_%H%M%S` result=0 safe_rename () { chattr -a "$1" && link -- "$1" "$2" && unlink -- "$1" } create_new () { touch -- "$1" \ && chown --reference="$2" -- "$1" \ && chmod --reference="$2" -- "$1" \ && chattr +a "$1" } unlink_old () { find "`dirname \"$1\"`" -regex "$1.[0-9_]+\(.gz\|.bz2\)?" -printf '%T@ %p\0' \ | sort -z -k 1,1nr \ | perl -n0e "++\$i; (\$f) = m#^\\d+ (.*)\$#; if (\$i > $keep) {unlink(\$f); }" } rotlogs='' for log in $logs; do if safe_rename "$log" "$log.$ext"; then create_new "$log" "$log.$ext" unlink_old "$log" rotlogs="$rotlogs $log.$ext" elif safe_rename "$log" "$log.$ext_long"; then create_new "$log" "$log.$ext_long" unlink_old "$log" rotlogs="$rotlogs $log.$ext_long" else echo "error rotating $log" 1>&2 result=1 fi done #### customize here -- HUP/restart daemons whose logs are rotated # restart lots of stuff kill -HUP `cat /var/run/syslogd.pid` sleep 1 kill -USR1 `cat /var/run/apache.pid` `cat /var/run/sheep.pid` `cat /var/run/webmail.pid` sleep 2 /etc/init.d/acct stop /etc/init.d/acct start sleep 1 su sheep /home/sheep/docroot/v2.6/etc/rc.fsd reload #### for log in $rotlogs; do #### customize here -- call any post-rotation log watching scripts case "`basename \"$log\"`" in messages.*|secure.*) /root/lib/messages.parse "$log" ;; maillog.*) (cat /var/log/maillog.incomplete $log \ | /root/lib/smtpd-sessions /var/log/maillog.incomplete.new /dev/null \ && mv /var/log/maillog.incomplete.new /var/log/maillog.incomplete) \ 2>&1 | mail -s "twinlark smtp report $ext" root ;; esac #### chmod -w -- "$log" gzip -9 -- "$log" done exit $result