jerrygarciuh
jerrygarciuh

Reputation: 22028

Using date to append filename to database backups made by cron

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?

Upvotes: 2

Views: 10586

Answers (4)

ZeroCool
ZeroCool

Reputation: 1500

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

Upvotes: 2

lutz
lutz

Reputation:

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

Upvotes: 3

hlovdal
hlovdal

Reputation: 28268

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).

Upvotes: 1

none
none

Reputation:

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

Upvotes: 13

Related Questions