我使用的是 CentOS 5.6。下面的代码可能有一些拼写错误,因为出于安全原因我需要将真实信息更改为虚假信息:
备份文件
#!/bin/bash
set -vx
rm -v /server/temp_db.gz
rm -v /server/temp_ftp.tar
rm -v /server/temp_backup.tar
mysqldump --all-databases | gzip -9 > /server/temp_db.gz
echo "$(date +%H:%M:%S) - finished DB backup"
tar -Pcf /server/temp_ftp.tar /server/public_html/
echo "$(date +%H:%M:%S) - finished FTP backup"
tar -Pcf /server/temp_backup.tar /server/temp_db.gz /server/temp_ftp.tar
DATE=`date +%A-%b-%d-%Y_%H-%M-%S`
sleep 120
/myfolder/upload.sh $DATE
上传.sh
#!/usr/bin/expect -f
# connect via scp
spawn scp /backup/temp_backup.tar [email protected]:/server/backup.tar
#######################
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "mypassword\r"
}
}
interact
我创建了 2 个文件(如上):
cd /myfolder/
chmod +x backup.sh
chmod +x upload.sh
./backup.sh
一切正常。创建时间temp_backup.tar
仅比上一个时间戳晚2秒,手动运行命令通常需要10-20秒:
rm -v /server/temp_db.gz
+ rm -v /server/temp_db.gz
removed `/server/temp_db.gz'
rm -v /server/temp_ftp.tar
+ rm -v /server/temp_ftp.tar
rm: cannot remove `/server/temp_ftp.tar': No such file or directory
rm -v /server/temp_backup.tar
+ rm -v /server/temp_backup.tar
rm: cannot remove `/server/temp_backup.tar': No such file or directory
mysqldump --all-databases | gzip -9 > /server/temp_db.gz
+ mysqldump --all-databases
+ gzip -9
echo "$(date +%H:%M:%S) - finished DB backup"
date +%H:%M:%S
++ date +%H:%M:%S
+ echo '20:50:16 - finished DB backup'
20:50:16 - finished DB backup
tar -Pcf /server/temp_ftp.tar /server/public_html/
+ tar -Pcf /server/temp_ftp.tar /server/public_html/
echo "$(date +%H:%M:%S) - finished FTP backup"
date +%H:%M:%S
++ date +%H:%M:%S
+ echo '20:50:19 - finished FTP backup'
20:50:19 - finished FTP backup
tar -Pcf /server/temp_backup.tar /server/temp_db.gz /server/temp_ftp.tar
+ tar -Pcf /server/temp_backup.tar /server/temp_db.gz /server/temp_ftp.tar
DATE=`date +%A-%b-%d-%Y_%H-%M-%S`
date +%A-%b-%d-%Y_%H-%M-%S
++ date +%A-%b-%d-%Y_%H-%M-%S
+ DATE=Friday-Dec-16-2011_20-50-24
sleep 120
+ sleep 120
/myfolder/upload.sh $DATE
+ /myfolder/upload.sh Friday-Dec-16-2011_20-50-42
spawn scp /server/temp_backup.tar [email protected]:/backup/backup_Friday-Dec-16-2011_20-50-42.tar
Password:
temp_backup.tar 100% 516MB 23.5MB/s 00:22
现在我设置了一个 cron 任务:
* */10 * * * /myfolder/backup.sh 2>&1 > /myfolder/backup.log
我检查了 cron 任务是由 运行的root
,与手动运行相同,文件的所有者是root:root
并且两个文件都有权限-rwxr-xr-x
,但由于某种原因它仍然不起作用。再次注意创建之前和之后的时间戳backup_temp.tar
——仅相差 2 秒:
rm -v /server/temp_db.gz
+ rm -v /server/temp_db.gz
removed `/server/temp_db.gz'
rm -v /server/temp_ftp.tar
+ rm -v /server/temp_ftp.tar
removed `/server/temp_ftp.tar'
rm -v /server/temp_backup.tar
+ rm -v /server/temp_backup.tar
removed `/server/temp_backup.tar'
mysqldump --all-databases | gzip -9 > /server/temp_db.gz
+ mysqldump --all-databases
+ gzip -9
echo "$(date +%H:%M:%S) - finished DB backup"
date +%H:%M:%S
++ date +%H:%M:%S
+ echo '20:41:05 - finished DB backup'
20:41:05 - finished DB backup
tar -Pcf /server/temp_ftp.tar /server//public_html/
+ tar -Pcf /server/temp_ftp.tar /server/public_html/
echo "$(date +%H:%M:%S) - finished FTP backup"
date +%H:%M:%S
++ date +%H:%M:%S
+ echo '20:41:07 - finished FTP backup'
20:41:07 - finished FTP backup
tar -Pcf /server/temp_backup.tar /server/temp_db.gz /server/temp_ftp.tar
+ tar -Pcf /server/temp_backup.tar /server/temp_db.gz /server/temp_ftp.tar
DATE=`date +%A-%b-%d-%Y_%H-%M-%S`
date +%A-%b-%d-%Y_%H-%M-%S
++ date +%A-%b-%d-%Y_%H-%M-%S
+ DATE=Friday-Dec-16-2011_20-41-09
sleep 120
+ sleep 120
/myfolder/upload.sh $DATE
+ /myfolder/upload.sh Friday-Dec-16-2011_20-39-08
spawn scp /server/temp_backup.tar [email protected]:/backup/backup_Friday-Dec-16-2011_20-39-08.tar^M
Password:
答案1
检查你的路径!创建 cron 作业时的一个常见错误是假设 cron 运行时的路径与用户运行文件时的路径相同。 mysqldump 和expect 可能需要给出完整路径,而不是相对路径。