通过 Cron 检查 shell 脚本运行的上一步
--- 文件 makebackup.sh
#!/bin/bash
DATE='date'
mysqldump --all-databases | gzip -9 > /backup/temp_db.gz
tar -Pcf /backup/temp_ftp.tar /public_html/
tar -Pcf /backup/temp_backup.tar /home/temp_db.gz /backup/temp_ftp.tar
sleep 60 && /backup/upload.sh $DATE
--- 文件 upload.sh
#!/usr/bin/expect -f
# connect via scp
spawn scp /backup/temp_backup.tar [email protected]:/home/backup_$argv.tar
#######################
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "mypassword\r"
}
}
interact
为什么这不起作用,我不想使用睡眠,我需要知道最后一个 tar 何时结束并执行文件 upload.sh。相反,它总是在最后一个 tar 文件启动后立即执行。
&&即使我删除 sleep 60 也不会执行任何操作
--- file makebackup.sh
#!/bin/bash
DATE='date'
mysqldump --all-databases | gzip -9 > /backup/temp_db.gz
tar -Pcf /backup/temp_ftp.tar /public_html/
tar -Pcf /backup/temp_backup.tar /home/temp_db.gz /backup/temp_ftp.tar
sleep 60 && /backup/upload.sh $DATE
--- file upload.sh
#!/usr/bin/expect -f
# connect via scp
spawn scp /backup/temp_backup.tar [email protected]:/home/backup_$argv.tar
#######################
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "mypassword\r"
}
}
interact
Why this does not work, i don't want to use sleep i need to know when last tar is over and execute file upload.sh. Instead it always executes as soon as last tar file starts.
&& does not do anything even if i remove sleep 60
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所说的“相反,它总是在最后一个 tar 文件启动后立即执行”,通常这意味着有一个“&”在该行的末尾或者你确定 tar 真的有效吗?您正在查看早期创建的旧 tar.gz 吗?确保它是一个大小正确的新 tar 文件。
编辑 我并不是说您必须删除文件,只是 dbl 检查放入最终 tar 中的内容是否有意义。
您是否正在检查最终 tar cmd 的输入文件的大小? (/home/temp_db.gz /backup/temp_ftp.tar)? 编辑 我的意思是,未压缩的 tar 文件 (temp_ftp.tar) 应该略大于它包含的所有文件的大小总和。如果您知道组成 temp_ftp.tar 的文件有 1 Meg,并且该文件为 1.1 Meg,那很好,如果是 0.5 Meg,那就不好了。 (还可以考虑对整个内容进行 gzip 压缩,以减少到远程主机的传输时间)。你的压缩数据库文件,很难说,大概是有效的,如果文件大小约为 25 字节,那么这表明创建文件时出现错误。
否则你所说的真的似乎是不可能的。这是这些事情之一,或者是其他事情在搅乱事情。
调试最后一个 tar 花费多长时间的一种方法是将命令包装在两个日期命令中,即
另外,根据您的标题“检查上一步”,我添加了从 tar 捕获返回代码并打印值。
再次强调一下,在 Linux shell 脚本中,没有办法(带有“&”字符的后台作业除外)让一个命令在前一个命令完成之前开始执行。
编辑文件的所有权和权限可能会搞砸文件的所有权和权限。使用 \
确认您的用户 ID 拥有这些文件并且您可以写入这些文件。如果您愿意,请编辑您的帖子以包含该信息。
另外,您的标题是“cron”:您是否捕获了该脚本的所有可能输出以帮助调试情况?使用 makebackup.sh 顶部附近的
set -vx
打开 shell 调试。将调试输出添加到 tar cmd '-v'。捕获整个过程的 cron 输出,例如
并确保没有找到任何错误消息。
( Crontab 示例,分钟小时日星期一(星期几,0-6 或 *),更改日期/时间以满足您的测试需求)
您的期望脚本使用 '\r',您不想使用 '\n'在Unix/linux环境下。如果您是基于 Windows 的服务器,那么您需要 '\r\n' 。
编辑预期脚本是否有效,您是否满意地证明文件正在被复制,它们在备份站点上的大小是否相同,日期是否更改?
如果您希望有一天备份能够拯救您的系统,您必须更好地了解整个过程应该如何工作以及它是否按预期工作。根据您的情况和备用计算机的可用性,您应该安排一次恢复备份的测试,看看它们是否真的有效。当您使用 -P 来保留完整路径信息时,您确实需要小心,不要用旧文件覆盖您的工作系统。
总结一下我的建议,仔细检查一切。
我希望这有帮助。
As you say 'Instead it always executes as soon as last tar file starts', normally that means there is an '&' at the end of the line OR are you sure the tar is really working? Are you looking at an old tar.gz that was created early on? Make sure it is a new tar file that is correct size.
Edit I'm not saying you have to delete files, just dbl-check that what is being put into the final tar makes sense.
Are you checking the sizes of input files to your final tar cmd? (/home/temp_db.gz /backup/temp_ftp.tar)? Edit By this I mean, that an uncompressed tar file (temp_ftp.tar) should be just slightly larger than the sum of sizes of all files it contains. If you know that you have 1 Meg of files that compose temp_ftp.tar, and the file is 1.1 Meg, that is good, if it is .5 Meg, then that is bad. (Also consider gziping the whole thing to reduce transmission time to your remote host). Your compressed db file, hard to say, presumably that is working, if the file size is something like 25 bytes, then that indicates an error in creating the file.
Otherwise what you are saying really seems impossible. It is one of these things, or something else is bollixing things up.
One way to debug how long the last tar is taking is to wrap the command in two date commands, i.e.
Also, per your title, 'check previous step', I've added capturing the return code from tar and printing the value.
Again, to reinforce what you know already, in a linux shell script, there is no way (excepting a background job with the '&' char) for one command to start executing before the previous one has completed.
EDIT ownership and permissions on your files might be screwing things up is ownership and permissions on your files. use \
to confirm that your userID owns the files and and that you can write to them. If you want to, edit your posting to include that information.
Also, your headline says 'cron' : are you capturing all of the possible output of this script to help debug the situation? Turn on shell debugging with
set -vx
near the top of makebackup.sh. Add debugging output to your tar cmd '-v'.Capture the cron output of your whole process like
And be sure you don't find any error messages.
( Crontab sample, min hr day mon (day-of-week, 0-6 or *) , change date/time to meet your testing needs)
Your expect script uses '\r', don't you want '\n' in the Unix/linux environment. If you're a Windows based server, then you want '\r\n' .
Edit does the expect script work, have you proved to your satisifaction that files are being copied, are they the same size on the backup site, does the date change?
If you expect backups to save your systems someday, you have to develop a better understanding of how the whole process should work and if it is working as expected. Depending on your situation and availability of alternate computers, you should schedule a test of your restoring your backups to see if they will really work. As you're using -P to preserve full-path info, you'll really need to be careful not to overwrite your working system with old files.
To summarize my advise, double-check everything.
I hope this helps.