Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqldump doesn't work in crontab

I'm trying to add a cronjob in the crontab (ubuntu server) that backups the mysql db.

Executing the script in the terminal as root works well, but inserted in the crontab nothing happens. I've tried to run it each minutes but no files appears in the folder /var/db_backups.

(Other cronjobs work well)

Here is the cronjob:

* * * * * mysqldump -u root -pHERE THERE IS MY PASSWORD --all-databases | gzip > /var/db_backups/database_`date +%d%m%y`.sql.gz

what can be the problem?

like image 856
xspecial Avatar asked Nov 01 '13 16:11

xspecial


2 Answers

You need to escape % character with \

mysqldump -u 'username' -p'password' DBNAME > /home/eric/db_backup/liveDB_`date +\%Y\%m\%d_\%H\%M`.sql 
like image 127
The Java Guy Avatar answered Sep 21 '22 04:09

The Java Guy


I was trying the same but I found that dump was created with 0KB. Hence, I got to know about the solution which saved my time.

Command :

0 0 * * * mysqldump -u 'USERNAME' -p'PASSWORD' DATEBASE > /root/liveDB_`date +\%Y\%m\%d_\%H\%M\%S`.sql 

NOTE: 1) You can change the time setting as per your requirement. I have set every day in above command.

2) Make sure you enter your USERNAME, PASSWORD, and DATABASE inside single quote (').

3) Write down above command in Crontab.

I hope this helps someone.

like image 30
Krupal Patel Avatar answered Sep 20 '22 04:09

Krupal Patel