Cron error with using backquotes

后端 未结 2 1385
攒了一身酷
攒了一身酷 2021-01-11 21:36

The following works fine from command line

/usr/bin/mysqldump -uUser -pPass Db_name > /var/www/db_backup/db.`date +%Y%m%d%H%M`.sql 

but

相关标签:
2条回答
  • 2021-01-11 21:54

    Try it with $() instead of backticks. And you probably do need to escape the percent signs since cron converts them to newlines otherwise.

    * 0 * * * /usr/bin/mysqldump -uUser -pPass Db_name > /var/www/db_backup/db.$(date +\%Y\%m\%d\%H\%M).sql
    

    Also, you should store the password in an option file with secure permissions (eg. 600 or 640) instead of passing it on the command line.

    0 讨论(0)
  • 2021-01-11 22:04

    Put your one line script (as shown) into a proper script file and invoke that from cron:

    $ cat /usr/local/bin/db-backup
    #!/bin/sh
    /usr/bin/mysqldump -uUser -pPass Db_name > \
       /var/www/db_backup/db.`date +%Y%m%d%H%M`.sql 
    $ # use RHEL commands to add db-backup to your crontab
    
    0 讨论(0)
提交回复
热议问题