-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_crashplan.rb
76 lines (67 loc) · 2.9 KB
/
backup_crashplan.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/ruby
require 'time'
## Configuration variables
too_old_threshold = (86400 * 3)
LOG_FILE = "/Library/Logs/CrashPlan/backup_files.log.0"
LOG_FILE2 = "/Library/Logs/CrashPlan/backup_files.log"
## Declaration of variables
last_successful_time = Time.at(0)
last_completion_time = Time.at(0)
last_failure_time = Time.at(-1)
## Creates an array of "events" from the log file
if File.exist?(LOG_FILE)
events=File.open(LOG_FILE, 'r').readlines
elsif File.exist?(LOG_FILE2)
events=File.open(LOG_FILE2, 'r').readlines
else
puts "The file \"#{LOG_FILE}\" does not exist."
exit 1001
end
## Validates and parses date/time strings in the format of "%m/%d/%y %I:%M%p".
def parse_time(datetime_string)
if datetime_string =~ /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}([AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/
month = datetime_string.split(/[\s,\/]/)[0]
day = datetime_string.split(/[\s,\/]/)[1]
year = datetime_string.split(/[\s,\/]/)[2]
time_string = datetime_string.split(/[\s,\/]/)[3]
actual_time = Time.parse("#{year}-#{month}-#{day} #{time_string}")
elsif datetime_string == ""
actual_time = Time.at(0)
else
puts "Error parsing log. \"#{datetime_string}\" is not the proper date format."
exit 1001
end
return actual_time
end
## Parses the log line by line to determine completion, success, failures
events.each do |event|
if event[0,1] == "W"
last_failure_time = parse_time(event[2..17])
end
if (event[0,1] == "I" and event.include? "Completed backup to CrashPlan")
if last_completion_time > last_failure_time
last_successful_time = parse_time(event[2..17])
end
last_completion_time = parse_time(event[2..17])
end
end
## Determines success/failure based on completion, success, and failure times
if last_completion_time == Time.at(0)
puts "Backup has never completed."
exit 1001
elsif last_successful_time == Time.at(0)
puts "Backup has never been successful. Last completion: #{last_completion_time.strftime("%F, %T")}."
exit 1001
elsif (Time.now - last_completion_time) > too_old_threshold
puts "Last backup is old. Last completion: #{last_completion_time.strftime("%F, %T")}."
exit 1001
elsif (Time.now - last_successful_time) > too_old_threshold
puts "Last successful backup is old. Last success: #{last_successful_time.strftime("%F, %T")}."
exit 1001
elsif last_completion_time > last_successful_time
puts "Last backup had errors. Last success: #{last_successful_time.strftime("%F, %T")}."
exit 0
else
puts "Last backup was successful. Last success: #{last_successful_time.strftime("%F, %T")}."
exit 0
end