2009年1月22日 星期四

cron job for zip last 7 days files

1. set schedule for run cronjob every hr
crontab -e

0 */1 * * * (. archive.sh > archiveJob.log 2>&1)


2. prepare a job for backup and zip the file last 7 days ( not include today ! )

archive.sh

#!/bin/sh

logpath=/usr/local/jboss/server/default/log
fname=`echo a b c d e f g h`
bday=7

cd $logpath

echo -e "\n$logpath\n"

for (( i=$bday;i>=1;i=i-1 ))
do
ddate=`/bin/date +%Y-%m-%d -d -"$i"day`

if [ -e "$logpath"/server.log."$ddate" ]; then
gzip server.log."$ddate"
mv server.log."$ddate".gz archive/
echo server.log."$ddate" moved.
fi

for f in $fname
do
if [ -e "$logpath"/server.log."$ddate$f" ]; then
gzip server.log."$ddate$f"
mv server.log."$ddate$f".gz archive/
echo server.log."$ddate$f" moved.
fi
done

done

#cd /usr/local/jboss/bin
#sh run.sh &

echo -e "
----------------------------------------
Server Log Backup and Archive Finished.
----------------------------------------
"

沒有留言: