- Change language to:
The Logrotate:
We rotate our logs daily, so we needed to write a generic script that provided us with these functions. The script below gives us those options, so it works for us.
#!/usr/local/bin/ksh
##################################################################
# in sorted order:
# 1. Version History
# 2. Dependencies
# 3. Usefull Info
# 4. Variables
# 5. Functions
# 6. Actual Script
# 7. Creditlines
##################################################################
#
# 1. Version History
#
# date who? what?
# 2002/08/29 Remko Lodder Initial Version
#
#################################################################
#
# 2. Dependencies
#
# date who? what dependencies?
# 2002/08/29 Remko Lodder No dependencies at this moment (standalone script)
#
################################################################
#
# 3. Usefull Info
#
# This script provides logrotation on a relative easy way.
# It was designed for the elvandar.org domain from Remko Lodder
# With a few modifications one can also do logtransfers with the
# same structure buildup by template.ksh
#
###############################################################
#
# 4. Variables
#
#LOGFILE HANDLING
LOG="/var/log/messages"
BACKUPDIR="/etc"
TAR="/usr/bin/tar"
GZIP="/usr/bin/gzip"
DIRDATE=`date "+%Y%m"`
DATE=`date "+%Y-%m-%d"`
TARGET="/backup/logfiles/$DIRDATE"
RotTarget="/backup/logfiles/$DIRDATE/"
SCRIPT=`basename $0`
LOGFILE="/var/logs/$SCRIPT.log"
ERRORFILE="/var/logs/$SCRIPT.errors"
NAMELOG=`echo $LOG | sed ’s/\//\ /g’ | awk ‘ { print $3 } ‘`
UID=$LOGNAME
##############################################################
#
# 5. Functions
#
### Function message, write message with script name and time in the logfile,
### This code is from Rick Rosbag!
### $* is a special variable which will print the rest of the options
### (like message bla ($* then is bla))
function message
{
echo $SCRIPT: `date +%Y-%d-%m@%H:%M` hour: $*
}
### Function trap_errors, trap errors in the script so we know what went wront,
### This code is from Rick Rosbag!
### $? is korn shell’s standard error return variable, (like exit 1 makes $? 1)
function trap_errors
{
ERROR=$?
if [[ "$ERROR" -ne "0" ]];
then
message Whoops, something went wrong in $SCRIPT. It reported errors.
message The error code was $ERROR.
message The linenumber that created this error is $1
message Whoops, something went wrong in $SCRIPT. It reported errors.
message The error code was $ERROR >> $ERRORFILE
message The linenumber that created this error is $1 >> $ERRORFILE
fi
}
### Function check directory’s, look if the $DIRDATE directory exists in $TARGET
### When this directory does not exist, it will be created,
### since otherwise the logfiles cannot be stored on the disk
function does_dir_exist
{
if [[ -d "$TARGET" ]];
then
message dir already exists
else
mkdir -p $TARGET
message $TARGET created
fi
}
### Function run_rotate, rotate given logfiles
### no extra info at this moment
function run_rotate
{
message rotating $LOG
cp $LOG $RotTarget/$NAMELOG.$DATE
gzip $RotTarget/$NAMELOG.$DATE
:> $LOG
message rotated and zipped at $RotTarget/$NAMELOG.$DATE
message placing a logger command into the new syslog file
logger rotated by $SCRIPT on $DATE
}
### Function chmod_target ,chmod’s $TARGET to 700 for user logfiles
### No other info needed at this moment
function chmod_target
{
chown -R logfiles:logfiles /backup/logfiles/
message /var/logs was chowned to logfiles:logfiles
chmod -R 700 /backup/logfiles
message /backup/logfiles was chmodded to 700
}
### Function restart syslogd, restart syslog from out a function
### No other info needed at this moment
function restart_syslogd
{
message Restarting Syslogd
kill -HUP `cat /var/run/syslog.pid `
message Syslogd Restarted
}
### function: do backup, backups SQL databases and /etc
function do_backup
{
message Creating a running backup of the current /etc
message and /usr/local/etc directories
message using $TAR to tar the directories and after that
message use $GZIP to compress them
$TAR -cf $RotTarget/etc-standard.$DATE.tar $BACKUPDIR
message Tarred the etc directories now gzipping them
$GZIP $RotTarget/etc-standard.$DATE.tar
message backing up the DB
mysqldump -u example_user -pexample_password exampledb \
>> /backup/logfiles/$DIRDATE/example.$DATE.sql
gzip /backup/logfiless/$DIRDATE/example.$DATE.sql
message Backup Completed! /etc is now safe in
message $RotTarget/etc and SQL backup complete
}
#############################################################
#
# 6. Actual script
#
### Rick Rosbag Code
### exec >> $LOGFILE, write logging to $LOGFILE
exec >> $LOGFILE
exec 2>&1
message —————————————————-
trap ‘trap_errors $LINENO’ ERR
message Logrotate starting
logger "starting $SCRIPT issued by $UID"
message Rotating Logs
message Checking wether dir exists
does_dir_exist
message Really Rotating
run_rotate
message Restarting syslog
restart_syslogd
do_backup
chmod_target
logger "$SCRIPT finished, issued by $UID"
message End of Script
message —————————————————
############################################################
#
# 7. Creditlines
#
# logrotate.ksh idea / structure / code Rick Rosbag
# logrotate.ksh initially written by Remko Lodder (c) 2002.

