Append current date to the filename via Cron?

后端 未结 4 1464
陌清茗
陌清茗 2020-12-09 07:12

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

相关标签:
4条回答
  • 2020-12-09 08:11
    * * * * * 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/

    0 讨论(0)
  • 2020-12-09 08:14

    You must escape the format and use evaluation

    mysqldump -u username -pPassword db_name > www/db_backup/db_backup_`date +\%d\%m\%y`.sql
    
    0 讨论(0)
  • 2020-12-09 08:16

    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.

    0 讨论(0)
  • 2020-12-09 08:18

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

    0 讨论(0)
提交回复
热议问题