我在将日期添加到备份文件的文件名时遇到了问题。以下是 Cron 命令(用于测试):
*/2 * * * tar -zcf /var/backups/www-(back tick)date +%Y%m%d(back tick).tgz /var/www/
它应该每 2 分钟运行一次并创建一个名为 www-20120212.tgz 的备份文件,但是出现了问题。
答案1
‘man crontab’说:
The sixth field of a line in a crontab file is a string that
is executed by the shell at the specified times. A percent
character in this field (unless escaped by \) is translated
to a NEWLINE character
答案2
您需要用 来转义该%
字符\
,因为 cron 会对它做出不同的解释:
*/2 * * * tar -zcf /var/backups/www-`date +\%Y\%m\%d`.tgz /var/www/