Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append current date to the filename via Cron?

Tags:

cron

cron-task

I've created a Cron task at my webhost to daily backup my database and I would like it to append the current date to the filename.

My Cron job looks like this

mysqldump -u username -pPassword db_name > www/db_backup/db_backup+date%d%m%y.sql 

But the file I get is this: db_backup+date no file extension or date.

I've also tried this command

mysqldump -u username -pPassword db_name > www/db_backup/db_backup_'date +%d%m%y'.sql  

but that doesn't even give an file output.

What is the right syntax for getting the date appended to my file??

like image 649
Emil Devantie Brockdorff Avatar asked Feb 02 '12 10:02

Emil Devantie Brockdorff


People also ask

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .

How do I create a FileName from a date in Linux?

You should use double quotes and need to evaluate date +"%F" using command substitution. Double quote helps you create a single file where some options of date command would include a space. For example, touch test_$(date) will create multiple files, where as touch "test_$(date)" won't.

What is Dev Null 2 &1 in crontab?

/dev/null is a special filesystem object that discards everything written into it. Redirecting a stream into it means hiding your program's output. The 2>&1 part means "redirect the error stream into the output stream", so when you redirect the output stream, error stream gets redirected as well.

Can I use tilde in crontab?

1 Answer. Show activity on this post. You should try using $HOME rather than ~ as tilde expansion is not reliable in a cron job.


1 Answers

* * * * * echo "hello" > /tmp/helloFile_$(date +\%Y\%m\%d\%H\%M\%S).txt 

You just need to escape the percent signs.

Other date formats: http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/

like image 148
ethanneff Avatar answered Oct 16 '22 20:10

ethanneff