#! /bin/sh # /etc/logbackup.d/sysklogd # logbackup fragment for system logs # # This script allows rotation and backup of all system logs, on systems # where /var/log is mounted as tmpfs. # # It is called by /etc/init.d/logbackup: # - at system startup to retreive backed-up logs to /var/log # - on a daily basis to rotate and back-up the logs on the hard-drive # - at system halt or reboot to back-up the logs before the # tmpfs is unmounted # # This script handles all sysklogd-managed logs and login-related logs # (wtmp, btmp, and lastlog). # # This script was written by François Févotte, but is largely adapted # from Debian's default /etc/cron.daily/sysklogd file, which was # originally written by Martin Schulze . NAME="syslog" DIR_LOG="/var/log" DIR_BAK="/var/log.real" LOGS=`syslogd-listfiles --all` LOGS="$LOGS $DIR_LOG/wtmp $DIR_LOG/btmp " [ -r /etc/logbackup.conf ] && . /etc/logbackup.conf if [ "$LOGBACKUP_INCLUDED" != "" ]; then echo $LOGS $DIR_LOG/lastlog | xargs -n 1 echo >> $LOGBACKUP_INCLUDED fi test -x /usr/sbin/syslogd-listfiles || exit 0 test -x /sbin/syslogd || exit 0 test -f /usr/share/sysklogd/dummy || exit 0 set -e do_retrieve() { logger -i -t "logbackup" "($NAME) Retrieve backed-up logs" cd $DIR_LOG for LOG in $LOGS; do LOGNAME=`basename $LOG` [ -r $DIR_BAK/$LOGNAME.0 ] && cp -a $DIR_BAK/$LOGNAME.* $DIR_LOG done # If there exists /var/log/lastlog, this is the newest version if [ ! -r $DIR_LOG/lastlog ]; then # retrieve the backed-up version if possible if [ -r $DIR_BAK/lastlog ]; then cp -a $DIR_BAK/lastlog $DIR_LOG fi fi # Ensure login-related files exist and have the right permissions # and ownership touch $DIR_LOG/lastlog $DIR_LOG/wtmp $DIR_LOG/btmp chmod 0644 $DIR_LOG/lastlog $DIR_LOG/wtmp chmod 0640 $DIR_LOG/btmp chown root.utmp $DIR_LOG/lastlog $DIR_LOG/wtmp $DIR_LOG/btmp return 0 } do_backup() { logger -i -t "logbackup" "($NAME) Back-up logs" cd $DIR_LOG for LOG in $LOGS; do LOGNAME=`basename $LOG` [ -r $DIR_LOG/$LOGNAME.0 ] && cp -a $DIR_LOG/$LOGNAME.* $DIR_BAK done [ -r $DIR_LOG/lastlog ] && cp -a $DIR_LOG/lastlog $DIR_BAK return 0 } do_rotate() { logger -i -t "logbackup" "($NAME) Rotate logs" cd $DIR_LOG for LOG in $LOGS; do if [ -s $LOG ]; then savelog -g adm -m 640 -u root -c 7 $LOG >/dev/null fi done for LOG in `syslogd-listfiles --auth`; do if [ -f $LOG ]; then chown root:adm $LOG chmod o-rwx $LOG fi done /usr/sbin/logrotate --force /etc/logrotate.d/login /etc/init.d/sysklogd reload-or-restart > /dev/null return 0 } case "$1" in retrieve) do_retrieve ;; backup) do_retrieve do_rotate do_backup ;; *) echo "Usage: $SCRIPTNAME {retrieve|backup}" >&2 exit 3 ;; esac