shell任务定时备份日志脚本

分类:shell编程 阅读:51511 次

log_bak.sh 代码

bin_home="/home//bin"

Folder_DEPLOY=$bin_home"/log"
log_back="/home/logback"


function checkDerectory() {
if [ "`ls -A $Folder_DEPLOY`" = "" ]; then
#do something
echo false
else
#do something
echo true
fi
}
function checkFiles() {
if [ "`ls -A $Folder_DEPLOY`" = "" ]; then
#do something
echo false
else
#do something
echo true
fi
}


dates=$(date -d "2 days ago" +%Y-%m-%d)
files="log."$dates"*"


cd $bin_home


if [ -f $files ]; then
mv $files $Folder_DEPLOY
derecResult=$(checkDerectory)
#echo $derecResult
if $derecResult ; then
#do something
#echo "now tar ..."
cd $Folder_DEPLOY
tarfilename="converter.log."$dates".tar.gz"
tar -czvf $tarfilename *
#echo "now mv ..."
mv $tarfilename $log_back
#echo "rm -rf ..."
rm -rf $files
fi

fi

添加任务计划

查看任务计划

crontab -l

添加任务计划

crontab -e

10 0 * * * /home/bin/log_bak.sh

重启crontab

su root

service crond restart

OK,搞定。