summaryrefslogtreecommitdiffstats
path: root/modules/tick/files/plugins/tarsnap
blob: 0352cdbdb898ffbdbf1cb976b5ebc4185dfd1904 (plain) (blame)
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
#!/usr/bin/env ruby

require 'date'

def parse_archive(archive)
  # tozt-daily-2018-12-31_03:34:13-homedoypass
  m = archive.match(/
    \A
    (?<host>[a-z]+) # tozt
    -
    (?<period>[a-z]+) # daily
    -
    (?<year>[0-9]+) # 2018
    -
    (?<month>[0-9]+) # 12
    -
    (?<day>[0-9]+) # 31
    _
    (?<hour>[0-9]+) # 03
    :
    (?<minute>[0-9]+) # 34
    :
    (?<second>[0-9]+) # 13
    -
    (?<name>[a-z_]+) # homedoypass
    \z
  /x)
  raise "failed to parse archive name '#{archive}'" if m.nil?

  parsed = m.names.zip(m.captures).to_h
  parsed["time"] = Time.new(
    m["year"],
    m["month"],
    m["day"],
    m["hour"],
    m["minute"],
    m["second"]
  )
  parsed
end

archives = %x(sudo tarsnap --list-archives)
  .lines
  .map(&:chomp)
  .map{|s| parse_archive(s)}

now = Time.now
most_recent_since = {}
archives.each do |archive|
  name = archive["name"]
  since = now - archive["time"]

  if most_recent_since[name].nil? || since < most_recent_since[name]
    most_recent_since[name] = since
  end
end

stats = %x(sudo tarsnap --print-stats)
bytes = stats.chomp.match(/(\d+)\z/).to_s.to_i

most_recent_since.sort.each do |name, since|
  puts "tarsnap,name=#{name} days_since_last_run=#{since.to_f / 60.0 / 60.0 / 24.0}"
end
puts "tarsnap file_size=#{bytes}i"