#!/bin/sh #---------------------------------------------------------------------------- # /var/install/bin/rsyslogd-tools-manual-rotate rsyslogd Rotate log manually # # Copyright (c) 2001-2022 Ansgar Puester # # Creation: 08.12.2016 ap # Last Update: $Id$ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- # include eislib . /var/install/include/eislib ASK=/var/install/bin/ask CAT=/bin/cat RM=/bin/rm MKTEMP=/bin/mktemp # include config_shlib . /var/install/bin/config_shlib # set package name package_name=rsyslogd # include default configuration . /etc/config.d/${package_name} #exec 2>/tmp/$(basename ${0})-trace$$.log #set -x #---------------------------------------------------------------------------- # check if numeric value # input : $1 - value # return: 0 - numeric # 1 - no numeric #---------------------------------------------------------------------------- is_numeric () { echo "$1" | grep -q '^[0-9]*$' } # --------------------------------------------------------------------------- # generate manual logrotate config # --------------------------------------------------------------------------- generate_manual_logrotote_config () { #tmpdir=$(mktemp -p /tmp -d XXXXXXXXXXXXX) tmpdir=/tmp/manual_logrotote; mkdir "${tmpdir}" [ "$index" -le 9 ] && act_num='0' act_num="$act_num$index" eval act_action='$RSYSLOGD_RULE_'${index}'_ACTION' eval act_pre='$RSYSLOGD_RULE_'${index}'_ROTATE_PRE_CMD' eval act_post='$RSYSLOGD_RULE_'${index}'_ROTATE_POST_CMD' eval act_int='$RSYSLOGD_RULE_'${index}'_ROTATE_LOG_INTERVAL' eval act_count='$RSYSLOGD_RULE_'${index}'_ROTATE_LOG_COUNT' eval act_rotate_mode='$RSYSLOGD_RULE_'${index}'_ROTATE_MODE' act_char_1=${act_action:0:1} [ "${act_char_1}" = '-' ] && act_action="${act_action:1}" if [ "${act_rotate_mode}" = 'size' ] then mecho --warn "Probably loss of log information when rotating an outchannel logfile" fi if [ -z "${act_post}" ] then mecho --warn "Empty RSYSLOGD_RULE_%_ROTATE_POST_CMD set to '/etc/init.d/rsyslogd --quiet reload'" act_post='/etc/init.d/rsyslogd --quiet reload' fi grep -v '^include' /etc/logrotate.conf > ${tmpdir}/logrotate_manually.conf { echo "#-------------------------------------------------------------------------------" echo "# $ACT_ROTATE_FILE file generated by $pgmname" echo "#" echo "# Do not edit this file, edit $rsyslogdfile" echo "# Creation date: `date`" echo "#-------------------------------------------------------------------------------" echo "$act_action {" echo " $act_int" echo ' missingok' echo " rotate $act_count" echo ' compress' echo ' notifempty' if [ ! "$act_pre" = '' ] then echo ' prerotate' echo " $act_pre" echo ' endscript' fi if [ ! "$act_post" = '' ] then echo ' postrotate' echo " $act_post" echo ' endscript' fi echo '}' } >> ${tmpdir}/logrotate_manually.conf } pgmname=`basename $0` # set variables rsyslogdfile=/etc/config.d/${package_name} clrhome mecho mecho mecho --info "rsyslogd: Rotate logfile manually" # check parameter has_error=0 if [ $# -ne 2 ] then mecho --error "Fatal: to few parameters ($#/2)" has_error=1 fi file=$1 index=$2 if ! is_numeric ${index} then mecho --error "Fatal: parameter 2 is not numeric" has_error=1 fi if [ "${has_error}" = 0 ] then mecho mecho "File to rotate: ${file}" mecho #mecho " Index: ${index}" generate_manual_logrotote_config #/usr/sbin/logrotate -d ${tmpdir}/logrotate_manually.conf /usr/sbin/logrotate -f ${tmpdir}/logrotate_manually.conf RC=$? mecho [ "${RC}" = 0 ] || opt='--warn' mecho ${opt} "Logrotate finished with return code ${RC}" mecho #echo "Halt:" #read halt ${RM} -rf ${tmpdir} fi mecho mecho anykey