linux的crontab调度shell命令

applecarelte 2015-02-03

1、编写shell脚本,添加可执行权限。

     eg: backup_spagobi_db.sh 

#!/bin/sh
#backup spagobi db
#note:
#from 099 to 102 db
#30 23 * * * root run-parts /home/work/scripts/backup_spagobi_db.sh >/dev/null 2>&1

#path
rootpath=~/dbbackup
basepath=$rootpath/logs
logfile=$basepath/backup.log

mkdir $basepath >/dev/null 2>&1

#db-config
romote_host=cp01-qa-junheng2-qa099.cp01.baidu.com
romote_port=3306
romote_user=root
romote_password=******

local_host=localhost
local_port=3306
local_user=root
local_password=******

#backup db
biframe=biframe2
spagobi=spagobi_datainside

#start...
starttime=`date '+%Y-%m-%d %H:%M:%S'`

echo 'backup spagobi db start ...'$starttime 
echo 'backup spagobi db start ...'$starttime >> $logfile

cd $rootpath

curday=`date '+%Y%m%d'`

biframe_filename=${biframe}_$curday
spagobi_filename=${spagobi}_$curday

rm -rf $biframe_filename.sql
rm -rf $spagobi_filename.sql

cd /home/work/local/mysql/bin

echo 'backup '$biframe_filename' start...' >> $logfile

./mysqldump -u$romote_user -p$romote_password -h$romote_host -P$romote_port --no-create-db $biframe  >~/dbbackup/$biframe_filename.sql

./mysql -u$local_user -p$local_password -h$local_host -P$local_port -e "CREATE DATABASE if not exists ${biframe_filename} default charset utf8"

./mysql -u$local_user -p$local_password -h$local_host -P$local_port  $biframe_filename < ~/dbbackup/$biframe_filename.sql

echo 'backup '$biframe_filename' over!' >> $logfile


echo 'backup '$spagobi_filename' start...' >> $logfile

./mysqldump -u$romote_user -p$romote_password -h$romote_host -P$romote_port --no-create-db $spagobi  >~/dbbackup/$spagobi_filename.sql

./mysql -u$local_user -p$local_password -h$local_host -P$local_port -e "CREATE DATABASE if not exists ${spagobi_filename} default charset utf8"

./mysql -u$local_user -p$local_password -h$local_host -P$local_port  $spagobi_filename < ~/dbbackup/$spagobi_filename.sql

echo 'backup '$spagobi_filename' over!' >> $logfile

endtime=`date '+%Y-%m-%d %H:%M:%S'`

echo 'backup all over!'$endtime >> $logfile
echo 'backup all over!'$endtime 

#drop old backup dbs before 7 days 
echo 'drop old backup dbs before 7 days start...'
echo 'drop old backup dbs before 7 days start...' >> $logfile

for((i=7;i<31;i++))
do
    
day=$(date -d -${i}' day' +%Y%m%d)
#echo $day

#biframe_historys=${biframe}_${day}
#spagobi_historys=${spagobi}_${day}

echo 'drop '$biframe_historys
./mysql -u$local_user -p$local_password -h$local_host -P$local_port -e "drop DATABASE if exists ${biframe_historys}"

echo 'drop '$spagobi_historys
./mysql -u$local_user -p$local_password -h$local_host -P$local_port -e "drop DATABASE if exists ${spagobi_historys}"

#echo ''

done

echo 'drop old backup dbs over!'$endtime >> $logfile
echo 'drop old backup dbs over!'$endtime

echo ' -------------------------- '>> $logfile

添加可执行权限 chmod +x  backup_spagobi_db.sh

2、编写调度命令

  用root用户在/etc/cron.allow 添加可调度crontab的用户。(没有这个文件则创建它)

  每个用户提交的crontab调度都存放在/var/spool/cron/crontabs/目录下在与用户名同名的文件里。

  用当前用户(eg:work这个用户)命令添加调度命令:crontab -e

30 23 * * * /home/work/scripts/backup_spagobi_db.sh >/dev/null 2>&1

  解释:在每天的晚上23:30分执行这个shell脚本。

编辑后保存,保存后则该调度命令就会保存在/var/spool/cron/crontabs/work这个用户文件中。可以通过crontab -l来查看调度的命令。

3、让调度资源生效。

      在root用户下重启crontab服务。

      命令:/etc/init.d/crond restart 

即可调度。(注:每次编辑调度命令后大偶要从新启动服务才能生效)

 【附】

crontab格式介绍 : http://blog.itpub.net/24930246/viewspace-1067356/

相关推荐