Hi,
Am trying to write my own version of a log rotate scripts 'coz I don't have the logrotate for other flavors of *nix servers. Probably should try and download the source and re-compile but our SA don't want to do so.
Anyway, just want to know if there is any way to improve on the code below:
while [ ${rotate_max} -gt 0 ];
do
let rotate_next=${rotate_max}
let rotate_prev=${rotate_max}-1
if [[ ! -f ${rotate_file}.${rotate_next} ]] && [[ ${rotate_next} -ne 0 ]] ; then
touch ${rotate_file}.${rotate_next}
fi
if [[ ! -f ${rotate_file}.${rotate_prev} ]] && [[ ${rotate_prev} -ne 0 ]] ; then
touch ${rotate_file}.${rotate_prev}
fi
if [[ ${rotate_max} -eq 1 ]] ; then
echo "cp -p ${rotate_file} ${rotate_file}.${rotate_next}"
else
echo "cp -p ${rotate_file}.${rotate_prev} ${rotate_file}.${rotate_next}"
fi
let rotate_max=${rotate_max}-1
done
Sample output as below:
cp -p alert_db01.log.9 alert_db01.log.10
cp -p alert_db01.log.8 alert_db01.log.9
cp -p alert_db01.log.7 alert_db01.log.8
cp -p alert_db01.log.6 alert_db01.log.7
cp -p alert_db01.log.5 alert_db01.log.6
cp -p alert_db01.log.4 alert_db01.log.5
cp -p alert_db01.log.3 alert_db01.log.4
cp -p alert_db01.log.2 alert_db01.log.3
cp -p alert_db01.log.1 alert_db01.log.2
cp -p alert_db01.log alert_db01.log.1
$ ls -l alert*
-rw-r--r-- 1 newbie01 guest 0 Apr 14 01:25 alert_db01.log
-rw-r--r-- 1 newbie01 guest 0 Apr 14 10:35 alert_db01.log.1
-rw-r--r-- 1 newbie01 guest 0 Apr 14 10:35 alert_db01.log.10
-rw-r--r-- 1 newbie01 guest 0 Apr 14 10:35 alert_db01.log.2
-rw-r--r-- 1 newbie01 guest 0 Apr 14 10:35 alert_db01.log.3
-rw-r--r-- 1 newbie01 guest 0 Apr 14 10:35 alert_db01.log.4
-rw-r--r-- 1 newbie01 guest 0 Apr 14 10:35 alert_db01.log.5
-rw-r--r-- 1 newbie01 guest 0 Apr 14 10:35 alert_db01.log.6
-rw-r--r-- 1 newbie01 guest 0 Apr 14 10:35 alert_db01.log.7
-rw-r--r-- 1 newbie01 guest 0 Apr 14 10:35 alert_db01.log.8
-rw-r--r-- 1 newbie01 guest 0 Apr 14 10:35 alert_db01.log.9
$
rotate_max is a variable that will be passed as an argument or read from a config file. For the sample output above, rotate_max is at 10.
Am currenly doing echo cp at the moment, will change that to the actual cp when am happy with how the script is running.
BTW I have the section of code below 'coz the cp will give an error if the file to copy from does not exist.
if [[ ! -f ${rotate_file}.${rotate_next} ]] && [[ ${rotate_next} -ne 0 ]] ; then
touch ${rotate_file}.${rotate_next}
fi
if [[ ! -f ${rotate_file}.${rotate_prev} ]] && [[ ${rotate_prev} -ne 0 ]] ; then
touch ${rotate_file}.${rotate_prev}
fi
Any feedback much appreciated. Thanks in advance.