在cron作业中使用$(日期“+%Y-%m-%d_%H:% m:%S”)有什么问题?

时间:2021-10-04 01:08:17

I am trying to sumbit a MySQL backup script through cpanel cron jobs panel, but I am receiving errors that I don't understand. I have tried the command on my Ubuntu

我试图通过cpanel cron jobs面板添加MySQL备份脚本,但是我收到了一些我不理解的错误。我在我的Ubuntu上试过这个命令

mysqldump -u(USERNAME) -p(PASSWORD) --all-databases | gzip > /home/MYHOMEDIR/myDBBckups/full_backup$(date "+%Y-%m-%d_%H:%M:%S").sql.gz

The error I am facing is:

我所面临的错误是:

---------- Email message ----------

From: Cron Daemon <root@MYSERVER>

Date: Thu, Jul 11, 2013 at 9:37 PM

Subject: Cron <MYUSERNAME@MYSERVER> mysqldump -u(USERNAME) -p(PASSWORD) --all-databases | 
gzip > /home/MYHOMEDIR/myDBBckups/full_backup$(date "+

To: MYEMAIL@gmail.com


/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'

/bin/sh: -c: line 1: syntax error: unexpected end of file 

---------- End of Email message ----------

When I remove the date command everything works fine but I don't want to override my backup every time.

当我删除date命令时,一切正常,但是我不想每次都重写我的备份。

With help of Colleagues, i fixed it like this:

在同事的帮助下,我把它修复成这样:

mysqldump -u(USERNAME) -p(PASSWORD) --all-databases | gzip > /home/MYHOMEDIR/myDBBckups/full_backup$(date "+\%Y-\%m-\%d_\%H:\%M:\%S").sql.gz

many thanks.

多谢。

2 个解决方案

#1


2  

The % character in the command portion of a cron job is translated to a newline character. Either use \% to denote a % character, or put the date command into an external script that you invoke from cron.

cron作业命令部分中的%字符被转换为换行字符。或者使用\%来表示一个%字符,或者将date命令放到从cron调用的外部脚本中。

#2


1  

Cron interprets the % character.

Cron解释%字符。

Put the line in a file create_backup.sh and put the command bash /path/to/create_backup.sh in your crontab instead.

将该行放到一个文件create_backup中。sh并放置命令bash /path/to/create_backup。换上你的crontab。

#1


2  

The % character in the command portion of a cron job is translated to a newline character. Either use \% to denote a % character, or put the date command into an external script that you invoke from cron.

cron作业命令部分中的%字符被转换为换行字符。或者使用\%来表示一个%字符,或者将date命令放到从cron调用的外部脚本中。

#2


1  

Cron interprets the % character.

Cron解释%字符。

Put the line in a file create_backup.sh and put the command bash /path/to/create_backup.sh in your crontab instead.

将该行放到一个文件create_backup中。sh并放置命令bash /path/to/create_backup。换上你的crontab。