1

I am trying to create a crontab to do a backup to my database in MySQL, the mysqldump command works fine with no errors as a single command or running bash script, but when I added the bash script to the crontab file it did not execute, below is my code:

#!/bin/bash

mysqldump --column-statistics=0 -h xx.xx.xx.xxx -u wissam -p'xxxxxx' 
--all-databases | gzip > ~/Desktop/DBs/Backup"$(date +"%Y_%m_%d_%I_%M_%p")file.sql.gz" 

the crontab command:

09 9    * * *   root    sh /home/ahmad/Desktop/DBs/mysqlcron.sh 2>&1>> /home/ahmad/Desktop/DBs/MySQLdump.log

which was added to /etc/crontab, after that I tried to restart the service but did not work for me, I also tried these solutions 1, 2 and 3 and still did not working.

Any help, please?

1
  • To capture stdout and stderr to the same log file, you need to reverse the order of redirections: >> /home/ahmad/Desktop/DBs/MySQLdump.log 2>&1 Apr 14, 2020 at 8:47

2 Answers 2

1

Crontab needs absolute paths, so besides the recommendations of Rinzwind, use either

09 9    * * *   root    /bin/bash /home/ahmad/Desktop/DBs/mysqlcron.sh 2>&1>> /home/ahmad/Desktop/DBs/MySQLdump.log

or

09 9    * * *   root    /home/ahmad/Desktop/DBs/mysqlcron.sh 2>&1>> /home/ahmad/Desktop/DBs/MySQLdump.log
2
  • should I add the absolute path of mysqldump ?
    – Ahmad
    Apr 14, 2020 at 9:18
  • Not completely sure about mysqldump(and gzip by the way) but sure about ~/Desktop/DBs/Backup. Replace the ~ by the desired directory. Apr 14, 2020 at 9:57
0

Please use absolute paths.

mysqldump

and

~/

need a directory. "~" is NEVER going to get expanded to your user.

Besides that:

-h xx.xx.xx.xxx -u wissam -p'xxxxxx' 

is very bad as it will echo host and password to the process list. Use ...

--defaults-extra-file=.mysql

and put at least the password in that file.

3
  • I changed the path here gzip > ~/Desktop/DBs/Backup to absolute path but did not work too
    – Ahmad
    Apr 14, 2020 at 6:36
  • and why are you ignoring "mysqldump"?
    – Rinzwind
    Apr 14, 2020 at 6:44
  • I am not ignoring it, but I think I understood your answer in a wrong way besides all solutions and command on the internet did not mention the path of mysqldump , won I will change it as you saying
    – Ahmad
    Apr 14, 2020 at 6:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .