I'm trying to create a cron job for database backup.
我正在尝试为数据库备份创建一个cron作业。
This is what I have so far:
这是我到目前为止:
mysqldump.sh
mysqldump.sh
mysqldump -u root -ptest --all-databases | gzip > "/db-backup/backup/backup-$(date)" 2> dump.log
echo "Finished mysqldump $(date)" >> dump.log
Cron job:
Cron工作:
32 18 * * * /db-backup/mysqldump.sh
The problem I am having is the job is not executing through cron or when I am not in the directory.
我遇到的问题是作业不是通过cron执行,或者当我不在目录中时。
Can someone please advise. Are my paths incorrect?
有人可以建议。我的路径不正确吗?
Also, the following line I'm not sure will output errors to the dump.log:
另外,我不确定以下行会将错误输出到dump.log:
mysqldump -u root -ptest --all-databases | gzip > "/db-backup/backup/backup-$(date)" 2> dump.log
What worked:
什么有效:
mysqldump -u root -ptest --all-databases | gzip > "../db-backup/backup/backup-$(date).sql.gz" 2> ../db-backup/dump.log
echo "Finished mysqldump $(date)" >> ../db-backup/dump.log
1 个解决方案
#1
6
There are a couple of things you can check, though more information is always more helpful (permissions and location of file, entire file contents, etc).
您可以检查几件事,但更多信息总是更有帮助(文件的权限和位置,整个文件内容等)。
- It can never hurt to preface the
mysqldump.sh
file with the Shebang syntax for your environment. I would venture to guess#!/bin/bash
would be sufficient. - 使用适用于您的环境的Shebang语法为mysqldump.sh文件添加前言绝不会有任何损害。我冒昧地猜测#!/ bin / bash就足够了。
- Instead of
mysqldump -u ....
use the absolute path/usr/bin/mysqldump
(or where ever it is on your system). Absolute paths are always a good idea in any form of scripting since it's difficult to say if the user has the same environment as you do. - 而不是mysqldump -u ....使用绝对路径/ usr / bin / mysqldump(或者它在你的系统上的任何地方)。绝对路径在任何形式的脚本编写中总是一个好主意,因为很难说用户是否拥有与您相同的环境。
As for storing the errors in dump.log, I don't believe your syntax is correct. I'm fairly sure you're piping the errors from gzip
into dump.log, not the errors from mysqldump
. This seems like a fairly common question which arrives at the answer of mysqldump $PARAMS | gzip -c dump-$(date)
至于在dump.log中存储错误,我不相信你的语法是正确的。我很确定你将错误从gzip传递到dump.log,而不是来自mysqldump的错误。这似乎是一个相当普遍的问题,它来自mysqldump $ PARAMS |的答案gzip -c dump - $(日期)
#1
6
There are a couple of things you can check, though more information is always more helpful (permissions and location of file, entire file contents, etc).
您可以检查几件事,但更多信息总是更有帮助(文件的权限和位置,整个文件内容等)。
- It can never hurt to preface the
mysqldump.sh
file with the Shebang syntax for your environment. I would venture to guess#!/bin/bash
would be sufficient. - 使用适用于您的环境的Shebang语法为mysqldump.sh文件添加前言绝不会有任何损害。我冒昧地猜测#!/ bin / bash就足够了。
- Instead of
mysqldump -u ....
use the absolute path/usr/bin/mysqldump
(or where ever it is on your system). Absolute paths are always a good idea in any form of scripting since it's difficult to say if the user has the same environment as you do. - 而不是mysqldump -u ....使用绝对路径/ usr / bin / mysqldump(或者它在你的系统上的任何地方)。绝对路径在任何形式的脚本编写中总是一个好主意,因为很难说用户是否拥有与您相同的环境。
As for storing the errors in dump.log, I don't believe your syntax is correct. I'm fairly sure you're piping the errors from gzip
into dump.log, not the errors from mysqldump
. This seems like a fairly common question which arrives at the answer of mysqldump $PARAMS | gzip -c dump-$(date)
至于在dump.log中存储错误,我不相信你的语法是正确的。我很确定你将错误从gzip传递到dump.log,而不是来自mysqldump的错误。这似乎是一个相当普遍的问题,它来自mysqldump $ PARAMS |的答案gzip -c dump - $(日期)