Gentoo Archives: gentoo-user

From: Alan McKinnon <alan.mckinnon@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] OT crontab not understood
Date: Mon, 09 Jan 2012 12:11:34
Message-Id: 20120109141018.07a7fdb3@khamul.example.con
In Reply to: Re: [gentoo-user] OT crontab not understood by Daniel Troeder
1 On Mon, 09 Jan 2012 14:02:23 +0100
2 Daniel Troeder <daniel@×××××××××.com> wrote:
3
4 > On 09.01.2012 12:33, Alan McKinnon wrote:
5 > > On Mon, 09 Jan 2012 13:10:50 +0100
6 > > Daniel Troeder <daniel@×××××××××.com> wrote:
7 > >
8 > >> Hi :)
9 > >>
10 > >> It seems I don't understand something about cron(tab). Can someone
11 > >> help me pls:
12 > >>
13 > >> I want to run flexbackup with the following backup plan:
14 > >> * monthly full
15 > >> * weekly diff
16 > >> * daily incr
17 > >>
18 > >> So I have installed sys-process/vixie-cron-4.1-r12 (and
19 > >> virtual/cron-0 and sys-process/cronbase-0.3.3).
20 > >>
21 > >> My crontab (created with "crontab -e") contains:
22 > >>
23 > >> 00 03 2-31 * 1-6 /usr/bin/flexbackup -set root -level
24 > >> incremental 00 03 2-31 * 0 /usr/bin/flexbackup -set root
25 > >> -level differential 00 03 1 * * /usr/bin/flexbackup -set
26 > >> root -level full
27 > >>
28 > >> The problem I'm facing is, that incr and diff are executed each day
29 > >> _both_ at the same time (which flexbackup luckily handles well).
30 > >>
31 > >> From my understanding the 2nd line (diff) should only be run on
32 > >> sundays, and the 1st line (inc) should not run sundays.
33 > >>
34 > >>
35 > >> Can someone please explain me what I'm doing wrong?
36 > >
37 > > You are combining fields 3 and 5, those two work funny.
38 > >
39 > > Unlike the other datetime specs, they are not ANDed, they are ORed.
40 > >
41 > > Taking the first one, you obviously want the cron to run at 3 am
42 > > between the 2nd and 31st of the month AND if the day is Mon-Sat.
43 > >
44 > > What it is doing is running at 3am every day between the 2nd and
45 > > 31st and also every day Mon-Sat (even if that is the 1st of the
46 > > month).
47 > >
48 > > Vixie cron does not directly allow you to do what you want. It's
49 > > designed to run things periodically on a set schedule and doesn't do
50 > > "except" very well.
51 > >
52 > > A better approach would be to fire off a wrapper script every day at
53 > > 3am. This script will then check for date, time and day of week and
54 > > launch the app with the appropriate options.
55
56
57
58 > Thank you for the explanation!
59 > Unfortunately that it's ORd :( ....
60
61 It does make sense in a way :-) The man page says:
62
63 "Commands are executed by cron(8) when the minute, hour, and month of year fields
64 match the current time, and when at least one of the two day fields
65 (day of month, or day of week) match the current time."
66
67 If it didn't work as it does, a cron that specified both day fields
68 would equate to something like "on the 1st of a month, but only if it's
69 a Sunday" which is rare. The intention is that you use either the date
70 field or the day of week, but not both
71
72 >
73 > So I wrote this:
74 >
75 > ### /etc/cron.daily/run_flexbackup ###
76 >
77 > #!/bin/bash
78 >
79 > DOM=$(date +%d)
80 > DOW=$(date +%w)
81 >
82 > function run_backup() {
83 > # do some stuff
84 > /usr/bin/flexbackup -set root -level $1
85 > # do more stuff
86 > }
87 >
88 > if [ $DOM = 1 ]; then
89 > run_backup full
90 > else
91 > if [ $DOW = 0 ]; then
92 > run_backup differential
93 > else
94 > run_backup incremental
95 > fi
96 > fi
97
98 Yup, that's the better way. And it comes with the benefit that the cron
99 is now simple and the wrapper script documents how and what it runs.
100 Overall, a better solution i think.
101
102
103 --
104 Alan McKinnnon
105 alan.mckinnon@×××××.com