Debian Codegraph @

debian/run-rsnapshot

raw
1 #!/usr/bin/ruby -w
2
3 # This program checks how long ago rsnapshot was last run. Based on
4 # that time, a suitable rsnapshot command line is launched. This
5 # program is intended to be run from (ana)cron.
6 #
7 # This program is in the public domain, do with it whatever you want.
8 #
9 # Author: [email protected]
10
11 require 'time'
12 require 'fileutils'
13
14 RSNAPSHOT_CONF = "/etc/rsnapshot.conf"
15
16 SYNTAX = "Syntax: run-rsnapshot [selftest]"
17
18 # Rsnapshot's definition of hourly is "once every 4 hours". Yes,
19 # really. Look it up.
20 HOURS_PER_HOURLY = 4
21
22 # Find the snapshot root from /etc/rsnapshot.conf
23 def snapshot_root
24 File.new(RSNAPSHOT_CONF).each_line do |line|
25 line.chomp!
26 if line =~ /^snapshot_root\s+(.*)$/
27 return $1
28 end
29 end
30 return nil
31 end
32
33 # Finds the number of files that must be in each interval
34 def interval_sizes
35 sizes = {}
36 File.new(RSNAPSHOT_CONF).each_line do |line|
37 line.chomp!
38 if line =~ /^interval\s+([a-z]+)\s+([0-9]+)$/
39 name = $1
40 number = $2.to_i
41 sizes[name] = number
42 end
43 end
44 return sizes
45 end
46
47 # An interval like "daily" or "monthly"
48 class Interval
49 attr_reader :name
50 attr_reader :amount
51 attr_reader :parent
52
53 # Either just the name or all parameters must be non-nil.
54 # frequency_hours - how many time units are there between each generation?
55 # amount - how many generations are there?
56 # parent - from where do I get my updates?
57 def initialize(name, snapshot_root, frequency_hours = nil, amount = nil, parent = nil)
58 @name = name
59 @snapshot_root = snapshot_root
60
61 if frequency_hours != nil
62 raise "Must specify all parameters for #{name}" unless (amount && parent)
63 end
64
65 @frequency_hours = frequency_hours
66 @amount = amount
67 @parent = parent
68 end
69
70 # Return current time of day. In a method by itself to be
71 # overridable by the unit tests.
72 def now
73 return Time.now
74 end
75
76 # Return age in hours of the newest or oldest snapshot
77 # kind - :newest or :oldest
78 def age_hours(kind)
79 raise "Kind must be :newest or :oldest" unless [:newest, :oldest].include? kind
80
81 # Being parentless means that we represent the contents of the
82 # actual file system, and as such we're always entirely up to
83 # date.
84 return 0 unless @parent
85
86 # No files means no "hours since"
87 return nil if snapshots.size == 0
88
89 best = nil
90 snapshots.each do |snapshot|
91 mtime = File.mtime(snapshot)
92 best = mtime unless best
93 if kind == :newest
94 best = mtime if mtime > best
95 elsif kind == :oldest
96 best = mtime if mtime < best
97 end
98 end
99
100 age = ((now - best) / 3600.0).round
101 return age
102 end
103
104 def snapshots
105 return Dir["#{@snapshot_root}/#{@name}*"].sort
106 end
107
108 def [](snapshot)
109 return snapshots[snapshot]
110 end
111
112 def size
113 return snapshots.size
114 end
115
116 def empty?
117 return size == 0
118 end
119
120 # Is this the original file source rather than a snapshot?
121 def origin?
122 return @parent == nil
123 end
124
125 # Am I ready to have my snapshot taken?
126 def complete?
127 # The original file system source is always complete.
128 return true if origin?
129
130 return size >= @amount
131 end
132
133 # Do I feel like I'm out of date?
134 def want_update?
135 # The original file system source never wants any updates from us.
136 return false if origin?
137
138 return true unless complete?
139 return true if age_hours(:newest) >= @frequency_hours
140 return false
141 end
142
143 # Would trying to update this interval get me back in shape?
144 def can_update?
145 # We can never update the original file system source
146 return false if origin?
147
148 return false unless @parent.complete?
149 return true if empty?
150
151 improvement_hours = age_hours(:newest) - @parent.age_hours(:oldest)
152 return improvement_hours >= @frequency_hours
153 end
154
155 def to_s
156 return "#{@name} interval"
157 end
158 end
159
160 # Given some intervals, the Evaluator suggests the correct kind of
161 # rsnapshot job
162 class Evaluator
163 def initialize(intervals)
164 @intervals = intervals
165 end
166
167 def suggest_update_interval
168 @intervals.each do |interval|
169 return interval if (interval.want_update? && interval.can_update?)
170 end
171 return nil
172 end
173 end
174
175 # This is the program's main piece of code
176 def run_rsnapshot
177 # Set up the intervals
178 origin = Interval.new("origin", snapshot_root)
179 hourly =
180 Interval.new("hourly", snapshot_root, HOURS_PER_HOURLY, interval_sizes["hourly"], origin)
181 daily =
182 Interval.new("daily", snapshot_root, 24, interval_sizes["daily"], hourly)
183 weekly =
184 Interval.new("weekly", snapshot_root, 7 * 24, interval_sizes["weekly"], daily)
185 monthly =
186 Interval.new("monthly", snapshot_root, 28 * 24, interval_sizes["monthly"], weekly)
187
188 # Find out what interval to update (if any)
189 evaluator = Evaluator.new([monthly, weekly, daily, hourly, origin])
190 update_interval = evaluator.suggest_update_interval
191
192 if update_interval
193 # Launch rsnapshot with the given interval
194 system("nice rsnapshot #{update_interval.name}")
195 return $?.exitstatus
196 else
197 return 0
198 end
199 end
200
201 if ARGV == []
202 exit run_rsnapshot
203 elsif ARGV == [ "selftest" ]
204 require 'test/unit/autorunner'
205 require 'tempfile'
206
207 class MockEvaluator < Evaluator
208 def initialize(time_source, root_dir, intervals)
209 super(intervals)
210
211 @time_source = time_source
212 @root_dir = root_dir
213 end
214
215 # Simulate calling rsnapshot
216 def update_interval(interval_name)
217 basename = @root_dir + "/" + interval_name
218 interval = @intervals.find { |interval| interval.name == interval_name }
219 parent_interval = interval.parent
220
221 # Increase the numerical suffix of all my files
222 interval.snapshots.reverse.each do |snapshot|
223 snapshot =~ /\.([0-9]+)$/
224 number = $1.to_i
225 number += 1
226 new_name = "#{basename}.#{number}"
227 File.rename(snapshot, new_name)
228 end
229
230 # Rotate my parent's last file into my file 0
231 if parent_interval.origin?
232 # Simulate taking a file system snapshot
233 destination = "#{basename}.0"
234 File.open(destination, 'w').close
235 File.utime(@time_source.now, @time_source.now, destination)
236 else
237 if parent_interval.empty?
238 raise "#{interval_name}'s parent #{parent_interval.name} has no files in it"
239 end
240 File.rename(parent_interval[-1], "#{basename}.0")
241 end
242
243 # If my last file has too high a number, remove it
244 last_file = interval[-1]
245 last_file =~ /\.([0-9]+)$/
246 number = $1.to_i
247 File.delete(last_file) if number >= interval.amount
248 end
249 end
250
251 # An interval taking its time from an artificial time source rather
252 # than from the system clock.
253 class MockInterval < Interval
254 def initialize(time_source,
255 name, snapshot_root, frequency_hours = nil, amount = nil, parent = nil)
256 super(name, snapshot_root, frequency_hours, amount, parent)
257 @time_source = time_source
258 end
259
260 def now
261 return @time_source.now
262 end
263 end
264
265 class TestInterval < Test::Unit::TestCase
266 # This is our simulated clock
267 attr_accessor :now
268
269 def initialize(foo)
270 super(foo)
271 @now = Time.now
272 end
273
274 # Execute a block with a temporary directory that will be nuked
275 # after the block is run
276 def with_tempdir(&block)
277 tempdir = Tempfile.new("run-rsnapshot-selftest")
278 path = tempdir.path
279 tempdir.close!
280 tempdir = path
281 begin
282 FileUtils.rm_f tempdir
283 Dir.mkdir(tempdir)
284 block.call(tempdir)
285 ensure
286 FileUtils.rm_rf tempdir
287 end
288 end
289
290 # Simulate calling rsnapshot several times while the clock is
291 # ticking, and verify that we're getting the suggestions we
292 # expect.
293 def test_interval
294 with_tempdir do |tempdir|
295 origin = MockInterval.new(self, "origin", tempdir)
296 daily = MockInterval.new(self, "daily", tempdir, 24, 7, origin)
297 weekly = MockInterval.new(self, "weekly", tempdir, 7 * 24, 4, daily)
298 monthly = MockInterval.new(self, "monthly", tempdir, 28 * 24, 6, weekly)
299
300 evaluator = MockEvaluator.new(self, tempdir, [monthly, weekly, daily, origin])
301 10.times do |month_number|
302 4.times do |week_number|
303 7.times do |day_number|
304 update_interval = evaluator.suggest_update_interval
305 assert_equal("daily", update_interval ? update_interval.name : "<nil>",
306 "Daily: m#{month_number}, w#{week_number}, d#{day_number}")
307 evaluator.update_interval(update_interval.name)
308 @now += 12 * 60 * 60
309
310 update_interval = evaluator.suggest_update_interval
311 assert_not_equal("daily", update_interval && update_interval.name,
312 "Not daily twice per day: m#{month_number}, w#{week_number}, d#{day_number}")
313 # Don't call any interval we got, we're just looking at
314 # what interval we got here
315 @now += 12 * 60 * 60
316 end
317 update_interval = evaluator.suggest_update_interval
318 assert_equal("weekly", update_interval ? update_interval.name : "<nil>",
319 "Weekly: m#{month_number}, w#{week_number}")
320 evaluator.update_interval(update_interval.name)
321 @now += 24 * 60 * 60
322 end
323 update_interval = evaluator.suggest_update_interval
324 assert_equal("monthly", update_interval ? update_interval.name : "<nil>",
325 "Monthly: m#{month_number}")
326 evaluator.update_interval(update_interval.name)
327 @now += 24 * 60 * 60
328 end
329 end
330 end
331 end
332 else
333 STDERR.puts SYNTAX
334 exit 1
335 end