Commit 8759073d authored by Maciej Lipinski's avatar Maciej Lipinski

[issue #20] updated log_rotate script to index rotated files with number, not date.

Using date was not a good ideal (i.e. format "file_name-date")
because the date on the switch can change due to PTP synchronization.

Now, files are indexed with number
parent 3bf0de56
#!/bin/sh #!/bin/bash
############################################################# #############################################################
# Maciej Lipinski @CERN # Maciej Lipinski @CERN
...@@ -27,6 +27,8 @@ fi ...@@ -27,6 +27,8 @@ fi
# Set number of rotated logs, fron input or default # Set number of rotated logs, fron input or default
if [ -z $2 ]; then if [ -z $2 ]; then
log_numb=10 log_numb=10
elif [ $2 -lt 2 ]; then
log_numb=2
else else
log_numb=$2 log_numb=$2
fi fi
...@@ -39,35 +41,24 @@ if [ ! -f "$log_file" ]; then ...@@ -39,35 +41,24 @@ if [ ! -f "$log_file" ]; then
exit 1; exit 1;
fi fi
# Move the log file from rsyslogd into the rotate file # Rotate old syslog files, indexed by numbers (higher values
# The date is an integer so that it is easy to recongize # for older files). Numbers are used, not the date, because
# the oldest file. # the date can change, e.g. as a result of PTP synchronization.
# In this process, we discard files with max-specified number
# (ie.e. $log_num).
i=$log_numb;
while [ $i -gt 1 ]; do
printf -v new_rotate_file "%s-%02d" $log_file $i;
i=$(($i - 1))
printf -v old_rotate_file "%s-%02d" $log_file $i;
echo "move $old_rotate_file to $new_rotate_file"
# Discard any errors when using mv, errors happen at the begining
# when there is no rotated files.
mv -f $old_rotate_file $new_rotate_file 2> /dev/null
done
new_rotate_file=${log_file}-$(date +%Y%m%d%H%M%S) # move the syslog file as to the rotated files with the lowest
# index because it is the newest
new_rotate_file=${log_file}-01
mv -f $log_file $new_rotate_file mv -f $log_file $new_rotate_file
echo "Move $log_file to $new_rotate_file" echo "Move $log_file to $new_rotate_file"
# Remove excess rotate file. Just in case, there are
# more than a single excess rotate file, remove excess
# files until happy with its number.
# Print the expected and current number of rotate files (just for info)
log_cnt=$(/bin/ls ${log_file}-* | /usr/bin/wc -l)
echo "Number of rotate files is $log_cnt and should be max $log_numb"
# Remove any excess files
while [ 1 ]; do
# Check the number of rotate files.
log_cnt=$(/bin/ls ${log_file}-* | /usr/bin/wc -l)
if [ $log_cnt -gt $log_numb ]; then
# Remove the oldest file if there are too many rotate files.
oldest_rotate_file=$(/bin/ls ${log_file}-* | /usr/bin/head -1)
echo "Remove oldest file: $oldest_rotate_file"
rm $oldest_rotate_file
else
break
fi
done
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment