1

Using cron to backup mysql to a folder everyday. So tried to schedule cron with below code in linux server. related post

0 0 * * * <username> mysqldump -u<mysql username> -p<mysql password> <database name> > /home/<username>/Db_BackUp/expense_$( date +"\℅Y_\℅m_\℅d" ).sql

above cron is working but generated file name is like

expense_\.Y_\.m_\.d.sql

I need cron to create filename as expense_2016_04_05.sql

how to do this in cron?

related answer from SO Post

addition info about cron restart SO Post

Community
  • 1
  • 1
Syed Nizamudeen
  • 440
  • 3
  • 7
  • 25

1 Answers1

4

Since cron is not a shell, it doesn't execute items in $() wrapper within the cron line in the way a shell does. However some crons like Vixie Cron will allow you to define variables before your command:

YMD=/bin/date +%Y_%m_%d
0 0 * * * /path/to/command > /home/username/Db_BackUp/expense_$($YMD).sql

If that doesn't work, you might have to write a helper script and run it via cron instead. For example:

mysqldump_helper.sh

#!/bin/bash
YMD=`/bin/date +%Y_%m_%d`
/path/to/command > /home/username/Db_BackUp/expense_$YMD.sql

crontab

* * * * * /path/to/mysqldump_helper.sh
David White
  • 1,763
  • 2
  • 16
  • 27