-1

I'm trying to automate a nightly backup with the following cron jobs (IPs and password redacted. of course):

[root@katexic ktxc]# crontab -l
30 3 * * * /bin/tar -czf /backup/ktxc/ktxc-`date '+%m%d%y'`.tar.gz /var/www/ktxc.to;scp -i /root/.ssh/id_rsa /backup/ktxc/ktxc-`date '+%m%d%y'`.tar.gz backup@111.22.33.44:/home/backup/backups
45 3 * * * /bin/tar -czf /backup/katexic/katexic-`date '+%m%d%y'`.tar.gz /var/www/katexic.com;scp -i /root/.ssh/id_rsa /backup/katexic/katexic-`date '+%m%d%y'`.tar.gz backup@111.22.33.44:/home/backup/backups
00 4 * * * /usr/bin/mysqldump -u ktxc_yourls -pxxxpasswordxxx ktxc_yourls | gzip > /backup/ktxc/ktxc-mysql-`date +\%m\%d\%y`.sql.gz;scp -i /root/.ssh/id_rsa /backup/ktxc/ktxc-mysql-`date '+%m%d%y'`.sql.gz backup@111.22.33.44:/home/backup/backups

These work fine from the command line, but not when the cron daemon tries to run them. In the /var/log/cron I see messages like:

Jun 10 03:30:01 katexic CROND[7437]: (root) CMD (/bin/tar -czf /backup/ktxc/ktxc-`date '+)
Jun 10 03:45:01 katexic CROND[7445]: (root) CMD (/bin/tar -czf /backup/katexic/katexic-`date '+)

I assume that there's some problem with the + sign in my commands? How do I fix these so they run properly as cron jobs?

This isn't solved by the troubleshooting in Why is my crontab not working, and how can I troubleshoot it? because that doesn't, afaict, cover special characters and the like and the error in the log is the same as cron sends to my email.

Chris
  • 101
  • 4

1 Answers1

1

You need to escape special characters with backslash so try with + and \%. And, also other special characters too (if any). The other option is to put these long commands in simple shell script, add executable permission bit to those scripts and reference them in your crontab.

Samar
  • 1,036
  • 8
  • 6
  • Thanks. Creating a shell script and calling that took care of the problem. I found various items documenting the need to escape the % sign, but not the + sign. I assume there are more special characters that need to be considered! – Chris Jun 11 '15 at 01:08