1

This question already has an answer here:

I am making a GFFS backup script for a school assignment but I've encountered some issues with it. It works like this:

/etc/backup/backup.sh PERIOD NUMBER 

I have added the following lines in cron:

# m h  dom mon dow   command
# Backup for fileserver:
#daily: 5 times/week
0   23   *   *    1-5   /etc/backup/backup.sh daily $(date -d "-1 day" +%w)
#weekly: 5 times/month
10  23   *   *    7     /etc/backup/backup.sh weekly $((($(date +%-d)-1)/7+1))
#monthly: 12 times/year
20  23   1   *    *     /etc/backup/backup.sh monthly $(date -d "-1 day" +%m)
#yearly: each year
0   3    1   1    *     /etc/backup/backup.sh yearly $(date -d "-1 day" +%Y)

The calculations at the end is to know what previous backup to override. This works perfect when triggered manually but when triggered by cron it does something weird. i'm talking about the weekly backup entry. the calculation is supposed to give me the week number in the current month. i did 'grep CRON /var/log/syslog' and found this line:

Dec 19 14:33:01 BE-SV-04 CRON[5445]: (root) CMD (/etc/backup/backup.sh weekly $((($(date +)

It appears as if cron is not executing the calculation correctly. Any help?

marked as duplicate by derobert, GAD3R, countermode, Jeff Schaller, kenorb Dec 19 '16 at 15:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    with all the "what shell", "what to escape" confusion going on .. I'd suggest to simply call a script "run_backup NAME" from CRON to execute the "backup.sh NAME DATE"; which you can make run in BASH and which it'd be simpler to debug instead of waiting for the next week to end ;-) – flowtron Dec 19 '16 at 14:13
  • 5
    Possibly related: How can I execute date inside of a cron tab job? – steeldriver Dec 19 '16 at 14:13
1

I think you have to escape the "%"- signs

so this:

0   23   *   *    1-5   /etc/backup/backup.sh daily $(date -d "-1 day" +\%w)

... should work. I dont know which have to be escaped, it think + and %, please try out. *when I did it in cron I used the uglyer backtick-syntax for command execution and had to escapte them, too, like: *

0  1 * * * something >> bla\`date \+\%Y_\%m_\%d\`.log
0

I'm not sure which shell the cronjob runs in, but it would seem it can't parse the calculation; you might need to use bc.

  • I've tried adding adding sudo -u root bash -c to it to force it running in bash, same result – Poomse8 Dec 19 '16 at 14:09
  • @Poomse8 - which version of Debian are you running? @flowtron - possibly Dash; I suspect you're right about bc (I don't have a Debian box handy right now, or I'd try and be more helpful!) – John N Dec 19 '16 at 14:14
  • @Poomse8, try running the command (/etc/backup/backup.sh weekly $((($(date +%-d)-1)/7+1))) in dash, my suspicion is you'll see an error - if so it'll help us narrow down the problem. – John N Dec 19 '16 at 14:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.