#!/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 # # this code is hereby placed in the public domain. # # THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # $Id: date-rotate,v 1.12 2003/12/27 06:27:58 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" logs="$logs /var/log/ipaccounting /var/log/ipacct /var/log/temperature" logs="$logs /home/www/logs/access_log /home/www/logs/error_log /home/www/logs/error_log.electricsheep" logs="$logs /home/www/logs/access_log.not-configured" logs="$logs /var/account/pacct" keep=90 #### 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` sleep 1 /etc/init.d/acct stop /etc/init.d/acct start sleep 1 #### for log in $rotlogs; do #### customize here -- call any post-rotation log watching scripts case "`basename \"$log\"`" in messages.*|secure.*) /etc/messages.parse "$log" ;; esac #### chmod -w -- "$log" bzip2 -- "$log" done exit $result