定时任务(vi /etc/crontab)
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed * 2 * * * root sh /home/script/erpbackup.sh
执行mysql数据导出与迁移备份文件到远程机
#!/bin/sh #! Encoding UTF-8 #--------------------------------------------------------- # scriptname: erpbackup.sh # version: 1.0 for linux # method: backup mysqldb # author: Winchell.Cao UserName='root' AdminPwd='root' DbName=ucsdb BakDir=/home/backup/ LogFile=/home/backup/mysqlbak.log DATE=`date +%Y%m%d` DumpFile=ucsdb$DATE.sql GZDumpFile=$DumpFile.tgz Server=10.80.248.25 ServerUserName=root ServerUserPwd="password\r" TargetPath=/home/backup #main--------------------------------------------------------- echo $(date +"%y-%m-%d %H:%M:%S") backup begin >> $LogFile cd $BakDir #备份当天文件 mysqldump -u$UserName -p$AdminPwd $DbName > $DumpFile echo $(date +"%y-%m-%d %H:%M:%S") Dump Done >> $LogFile #tar压缩文件 tar czvf $GZDumpFile $DumpFile >> $LogFile #rm -f $DumpFile echo $(date +"%y-%m-%d %H:%M:%S") $GZDumpFile compress done >> $LogFile #上传到远程机器 #exec /home/script/autoexpectl.sh expect <<! spawn scp $GZDumpFile $ServerUserName@$Server:$TargetPath expect "*password:" send $ServerUserPwd expect eof ! echo $(date +"%y-%m-%d %H:%M:%S") scp $GZDumpFile compress done >> $LogFile rm -f $GZDumpFile #删除当前目录下7天前的文件备份文件 #find $BakDir -name "vpndata*" -mtime +7 -exec rm {} \; #echo $(date +"%y-%m-%d %H:%M:%S") delete old file done >> $LogFile #验证结果 ls -al $DumpFile >> $LogFile echo $(date +"%y-%m-%d %H:%M:%S") finish >>$LogFile