Using date to add filename to database backups made by cron

I want to maintain multiple dated backups via cron, but I can't figure out how to concatenate the filename in cron.

Currently I am just using 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 not 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 can I concretize this line in cron?

+2


source to share


4 answers


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

      



+13


source


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

      

+3


source


You can define a variable like this:

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

      

and use it in concatenation like this:

...> /var/www/vhosts/mysite.org/dbBackups/"$MYDATE"dbname.sql

      

+2


source


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 the syntax $(some command)

instead of backquotes. Not sure if it will work for cron, but you can try if you prefer this style (one advantage of this style is that they can get up without any problem).

+1


source







All Articles