Append current date to the filename via Cron?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 05:50:18

问题


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??


回答1:


You should use `` instead of '' around the command you want to execute to generate the current date for your filename.




回答2:


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

You just need to escape the parentheses.

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




回答3:


I need to create a new log file every time command is executed. So every day I should have a log like this /home/me/Logs/power_20151230.log The crontab line that I use is this:

00 8 * * * /home/me/power.py ON >> /home/me/Logs/power\_`date +20\%y\%m\%d`

Note the underscore character must be escaped too.




回答4:


You must escape the format and use evaluation

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


来源:https://stackoverflow.com/questions/9110663/append-current-date-to-the-filename-via-cron

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!