1

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?

AReddy
  • 3,172
  • 5
  • 36
  • 76
Poomse8
  • 13

2 Answers2

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
MacMartin
  • 2,924
  • I will test this asap, i'll get back to this in 1h – Poomse8 Dec 19 '16 at 14:02
  • This was idd correct. The + is fine but the % needs escaping. Thank you for your time! http://www.ducea.com/2008/11/12/using-the-character-in-crontab-entries/ – Poomse8 Dec 19 '16 at 15:06
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.

flowtron
  • 356
  • 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