bash MySQLDump 通过 shell 脚本没有被执行,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18386110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:21:59  来源:igfitidea点击:

MySQLDump via shell script not being executed, why?

mysqllinuxbashshellcentos

提问by Kismet Agbasi

Hello all,

大家好,

I trust you're well as you read this request for assistance. I am not very experienced in the Linux world, but all the same I'm striving to learn as quickly as possible. So please pardon any obvious ignorance on my part and assist if possible.

我相信您在阅读此帮助请求时一切都好。我在 Linux 世界中的经验不是很丰富,但我一直在努力尽快学习。因此,请原谅我的任何明显的无知,并在可能的情况下提供帮助。

I am running a ClearOS v5.2 Enterprise server (based on the Centos linux distro) and I'm using the following command in a shell script to backup my MySQL database.

我正在运行 ClearOS v5.2 Enterprise 服务器(基于 Centos linux 发行版)并且我在 shell 脚本中使用以下命令来备份我的 MySQL 数据库。

mysqldump -u root -pMYPASSWORD --all-databases > /mnt/shares/flexshares/backup/MySQL/mysql-backup-`date +%Y-%m-%d-%H-%M`.sql

I use a cron job to call this script @ 1:15 AM daily. Now, I can see in the cron log that the job is executed at that time and the script is called, but the command doesn't execute (log entries below). I say this because I don't see the sql file being created in the /mnt/shares/flexshares/backup/MySQLdirectory.

我每天凌晨 1:15 使用 cron 作业调用此脚本。现在,我可以在 cron 日志中看到当时执行了作业并调用了脚本,但是命令没有执行(下面的日志条目)。我这样说是因为我在/mnt/shares/flexshares/backup/MySQL目录中没有看到正在创建的sql 文件。

Aug 21 01:15:01 server1 crond[27842]: (root) CMD (/root/my_scripts/backup-mysql.sh)
Aug 22 01:15:01 server1 crond[9031]: (root) CMD (/root/my_scripts/backup-mysql.sh)

Strangely enough, if I run the same command from the CLI it executes without any problems and the MySQL database is dumped to the target directory (/mnt/shares/flexshares/backup/MySQL).

奇怪的是,如果我从 CLI 运行相同的命令,它会毫无问题地执行,并且 MySQL 数据库被转储到目标目录 ( /mnt/shares/flexshares/backup/MySQL)。

What am I missing or what have I included in the script that's causing it not to run? Here's my shell script ("backup-mysql.sh"):

我错过了什么或者我在脚本中包含了什么导致它无法运行?这是我的 shell 脚本(“backup-mysql.sh”):

#!/bin/bash
# FULL BACKUP OF MySQL DATABASE
mysqldump -u root -pMYPASSWORD --all-databases > /mnt/shares/flexshares/backup/MySQL/mysql-backup-`date +%Y-%m-%d-%H-%M`.sql

I am very grateful for any help I can get, thanks.

我非常感谢我能得到的任何帮助,谢谢。

v/r

比/转

Kismet

基斯梅特

回答by rakib_

I think your commands are not executed properly from script. Can you try the following -

我认为您的命令没有从脚本中正确执行。你可以尝试以下 -

         #!/bin/sh
         DUMPFILE=mysql-backup-`date +%Y-%m-%d-%H-%M`.sql
         `mysqldump -u root -pMYPASSWORD --all-databases > $DUMPFILE`

回答by Narayan

I feel there is no problem with the script.

感觉剧本没有问题。

Please check if you are provided with enough execute permission and your cron entry is proper.

请检查是否为您提供了足够的执行权限以及您的 cron 条目是否正确。

Try executing the script on your own and see if the DB file is generating or not.

尝试自己执行脚本,看看数据库文件是否正在生成。

回答by Kismet Agbasi

I really appreciate the assistance. Based on @Narayan's comment that the problem may be at the cron job - I did some more testing with the cron job to see where my problem was. Well, it wasn't long before I got an access denied when I tried to run the script directly from the CLI by doing ./backup-mysql.sh.

我非常感谢您的帮助。根据@Narayan的评论,问题可能出在 cron 作业上 - 我对 cron 作业进行了更多测试,以查看我的问题出在哪里。好吧,不久之后,当我尝试通过执行./backup-mysql.sh直接从 CLI 运行脚本时,访问被拒绝。

This prompted me to check the permissions of the script file, and ironically it wasn't executable. I believe this was why the script wasn't running from the cron job. I changed the permissions and tested and all is working fine.

这促使我检查脚本文件的权限,具有讽刺意味的是它是不可执行的。我相信这就是脚本没有从 cron 作业运行的原因。我更改了权限并进行了测试,一切正常。

I guess my lesson of the day in the linux world, CHECK PERMISSIONS ALWAYS! Thanks anyway for your assistance, I really appreciate it.

我想我今天在 linux 世界的教训是,始终检查权限!无论如何,感谢您的帮助,我真的很感激。