views:

366

answers:

4

Hello,

The title says it all. I want to maintain multiple dated backups via cron but I can't seem to figure out how to concatenate the file name in cron.

Currently I just use the following:

/bin/mv /var/www/vhosts/mysite.org/dbBackups/today.sql /var/www/vhosts/mysite.org/dbBackups/yesterday.sql

/usr/bin/mysqldump --add-drop-table -u dbname -pmypass dbname > /var/www/vhosts/mysite.org/dbBackups/today.sql

What I tried was no good:

/usr/bin/mysqldump/mysqldump --add-drop-table -u dbname -pmypass dbname > '/var/www/vhosts/mysite.org/dbBackups/' . date +%Y%m%d . dbname.sql

So how do I concantenate that string in cron?

TIA

JG

+3  A: 
/usr/bin/mysqldump/mysqldump --add-drop-table -u dbname -pmypass dbname > "/var/www/vhosts/mysite.org/dbBackups/"`date +%Y%m%d`dbname.sql
none
Hm, looks odd. Could you please format this in a code block?
I'm sorry, first post here, just fixed it.
none
Thanks! Works like a charm!
jerrygarciuh
+1  A: 

Put the date command in back-quotes and remove the spaces:

/usr/bin/mysqldump/mysqldump --add-drop-table -u dbname -pmypass dbname > /var/www/vhosts/mysite.org/dbBackups/`date +%Y%m%d`dbname.sql

In (at least) bash you can also use $(some command) syntax instead of back-quotes. Not sure if it will work for cron, but you might try if you prefer that style instead (one benefit of that style is that they can nest without problems).

hlovdal
+1  A: 

You can use the $(...) syntax.

/usr/bin/mysqldump/mysqldump --add-drop-table -u dbname -pmypass dbname >
/var/www/vhosts/mysite.org/dbBackups/$(date +%Y%m%d)dbname.sql
+1  A: 

You may want to define a variable like this:

 export MYDATE="$(date '+%Y-%m-%d')"

and use it concatenated like this:

...> /var/www/vhosts/mysite.org/dbBackups/"$MYDATE"dbname.sql
ZeroCool
That is especially useful if you work with more than one file. I often make backups for multiple databases, for example.