zfs_mgmt 0.10.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,164 +1,160 @@
1
1
  require "json"
2
2
 
3
3
  module ZfsMgmt::Restic
4
- def self.restic_snapshots(zfs,options,props)
4
+ def self.restic_snapshots(zfs, options, props)
5
5
  # query the restic database
6
- com = [ options[:restic_binary],
7
- 'snapshots',
8
- '--json',
9
- '--tag', 'zfsmgmt',
10
- '--path', "/#{zfs}",
11
- ]
12
- if options.has_key?('password_file')
13
- com.push('-p',options['password_file'])
6
+ com = [options[:restic_binary],
7
+ "snapshots",
8
+ "--json",
9
+ "--tag", "zfsmgmt",
10
+ "--path", "/#{zfs}"]
11
+ if options.has_key?("password_file")
12
+ com.push("-p", options["password_file"])
14
13
  end
15
- repo = props.is_a?(ZfsMgmt::Zfs) ? props.restic_repository : props['zfsmgmt:restic_repository']
16
- if options.has_key?('repo')
17
- com.push('--repo', options['repo'])
14
+ repo = props.is_a?(ZfsMgmt::Zfs) ? props.restic_repository : props["zfsmgmt:restic_repository"]
15
+ if options.has_key?("repo")
16
+ com.push("--repo", options["repo"])
18
17
  elsif repo
19
- com.push( '--repo', repo )
18
+ com.push("--repo", repo)
20
19
  end
21
20
 
22
- $logger.info("#{com.join(' ')}")
23
- restic_output = %x(#{com.join(' ')})
21
+ ZfsMgmt.logger.info(com.join(" "))
22
+ restic_output = `#{com.join(" ")}`
24
23
  unless $?.success?
25
- $logger.error("unable to query the restic database")
24
+ ZfsMgmt.logger.error("unable to query the restic database")
26
25
  raise "unable to query the restic database"
27
26
  end
28
27
  restic_snapshots = JSON.parse(restic_output)
29
28
  restic_snapshot_zfs_snapshot_index = {}
30
29
  restic_snapshots.each do |snappy|
31
- snappy['date_time'] = DateTime.parse(snappy['time'])
32
- if snappy.has_key?('tags')
33
- snappy['tags'].each do |t|
34
- if m = /^(zfsmgmt:.+?)=(.+)/.match(t)
35
- if ['zfsmgmt:level'].include?(m[1])
36
- snappy[m[1]] = m[2].to_i
30
+ snappy["date_time"] = DateTime.parse(snappy["time"])
31
+ if snappy.has_key?("tags")
32
+ snappy["tags"].each do |t|
33
+ if (m = /^(zfsmgmt:.+?)=(.+)/.match(t))
34
+ snappy[m[1]] = if ["zfsmgmt:level"].include?(m[1])
35
+ m[2].to_i
37
36
  else
38
- snappy[m[1]] = m[2]
37
+ m[2]
39
38
  end
40
- if m[1] == 'zfsmgmt:snapshot'
39
+ if m[1] == "zfsmgmt:snapshot"
41
40
  restic_snapshot_zfs_snapshot_index[m[2]] = snappy
42
41
  end
43
42
  end
44
43
  end
45
44
  end
46
45
  end
47
- return([restic_snapshots,restic_snapshot_zfs_snapshot_index])
46
+ [restic_snapshots, restic_snapshot_zfs_snapshot_index]
48
47
  end
49
48
 
50
- def self.valid_chain(snap,restic_snapshots,restic_snapshot_zfs_snapshot_index,a)
51
- if snap['zfsmgmt:level'] == 0
49
+ def self.valid_chain(snap, restic_snapshots, restic_snapshot_zfs_snapshot_index, a)
50
+ if snap["zfsmgmt:level"] == 0
52
51
  a.push(snap)
53
- $logger.debug("found complete chain culminating in full backup of: #{snap['zfsmgmt:snapshot']}")
54
- return a
55
- elsif restic_snapshot_zfs_snapshot_index.has_key?(snap['zfsmgmt:parent'])
52
+ ZfsMgmt.logger.debug("found complete chain culminating in full backup of: #{snap["zfsmgmt:snapshot"]}")
53
+ a
54
+ elsif restic_snapshot_zfs_snapshot_index.has_key?(snap["zfsmgmt:parent"])
56
55
  a.push(snap)
57
- $logger.debug("found another link in the chain: #{snap['zfsmgmt:snapshot']} => #{snap['zfsmgmt:parent']}")
58
- return valid_chain(restic_snapshot_zfs_snapshot_index[snap['zfsmgmt:parent']],restic_snapshots,restic_snapshot_zfs_snapshot_index,a)
56
+ ZfsMgmt.logger.debug("found another link in the chain: #{snap["zfsmgmt:snapshot"]} => #{snap["zfsmgmt:parent"]}")
57
+ valid_chain(restic_snapshot_zfs_snapshot_index[snap["zfsmgmt:parent"]], restic_snapshots, restic_snapshot_zfs_snapshot_index, a)
59
58
  else
60
- $logger.error("broken chain: looking for the parent of #{snap['zfsmgmt:snapshot']} (#{snap['zfsmgmt:parent']}) and failed to find")
61
- return []
59
+ ZfsMgmt.logger.error("broken chain: looking for the parent of #{snap["zfsmgmt:snapshot"]} (#{snap["zfsmgmt:parent"]}) and failed to find")
60
+ []
62
61
  end
63
62
  end
64
-
65
-
63
+
66
64
  def self.backup(backup_level: 2,
67
- options: {})
68
- ZfsMgmt.zfs_managed_list(filter: options['filter'],
69
- properties: ['name',
70
- 'zfsmgmt:restic_backup',
71
- 'zfsmgmt:restic_repository',
72
- 'userrefs',
73
- ],
74
- property_match: { restic_backup: true }).each do |zfs_obj|
65
+ options: {})
66
+ ZfsMgmt.zfs_managed_list(filter: options["filter"],
67
+ properties: ["name",
68
+ "zfsmgmt:restic_backup",
69
+ "zfsmgmt:restic_repository",
70
+ "userrefs"],
71
+ property_match: {restic_backup: true}).each do |zfs_obj|
75
72
  zfs = zfs_obj.name
76
73
  zfs_snapshots = zfs_obj.snapshots
77
74
  next if zfs_snapshots.empty?
78
- last_zfs_snapshot = zfs_snapshots.keys.sort { |a,b| zfs_snapshots[a]['creation'] <=> zfs_snapshots[b]['creation'] }.last
79
- zfs_snap_time = Time.at(zfs_snapshots[last_zfs_snapshot]['creation'])
75
+ last_zfs_snapshot = zfs_snapshots.keys.max { |a, b| zfs_snapshots[a]["creation"] <=> zfs_snapshots[b]["creation"] }
76
+ zfs_snap_time = Time.at(zfs_snapshots[last_zfs_snapshot]["creation"])
80
77
 
81
78
  level = 0
82
79
  chain = []
83
- zfs_snap_parent = ''
84
- restic_snap_parent = ''
85
- (restic_snapshots,restic_snapshot_zfs_snapshot_index) = restic_snapshots(zfs,options,zfs_obj)
80
+ zfs_snap_parent = ""
81
+ restic_snap_parent = ""
82
+ (restic_snapshots, restic_snapshot_zfs_snapshot_index) = restic_snapshots(zfs, options, zfs_obj)
86
83
  if restic_snapshot_zfs_snapshot_index.has_key?(last_zfs_snapshot)
87
- $logger.warn("backup of this snapshot #{last_zfs_snapshot} already exists in restic, cannot continue with backup of #{zfs}")
84
+ ZfsMgmt.logger.warn("backup of this snapshot #{last_zfs_snapshot} already exists in restic, cannot continue with backup of #{zfs}")
88
85
  next # next zfs filesystem to be backed up
89
86
  end
90
- if backup_level > 0 and restic_snapshots.count > 0
87
+ if backup_level > 0 && restic_snapshots.count > 0
91
88
  # reverse (oldest first) sorted restic snapshots
92
89
  restic_snap_parent = restic_snapshots.filter { |rsnap|
93
- rsnap.has_key?('zfsmgmt:zfs') and rsnap['zfsmgmt:zfs'] == zfs and
94
- rsnap.has_key?('zfsmgmt:level') and rsnap['zfsmgmt:level'] < backup_level }.sort {
95
- |a,b| a['date_time'] <=> b['date_time'] }.last
96
- if restic_snap_parent and
97
- zfs_snapshots.has_key?(restic_snap_parent['zfsmgmt:snapshot']) and
98
- chain = valid_chain(restic_snap_parent,restic_snapshots,restic_snapshot_zfs_snapshot_index,[]) and
99
- chain.length > 0
100
-
101
- level = restic_snap_parent['zfsmgmt:level'] + 1
102
- zfs_snap_parent = restic_snap_parent['zfsmgmt:snapshot']
103
- $logger.debug("restic_snap_parent: level: #{restic_snap_parent['zfsmgmt:level']} snapshot: #{zfs_snap_parent}")
90
+ rsnap.has_key?("zfsmgmt:zfs") and rsnap["zfsmgmt:zfs"] == zfs and
91
+ rsnap.has_key?("zfsmgmt:level") and rsnap["zfsmgmt:level"] < backup_level
92
+ }.max_by { |a| a["date_time"] }
93
+ if restic_snap_parent &&
94
+ zfs_snapshots.has_key?(restic_snap_parent["zfsmgmt:snapshot"]) &&
95
+ (chain = valid_chain(restic_snap_parent, restic_snapshots, restic_snapshot_zfs_snapshot_index, [])) &&
96
+ chain.length > 0
97
+
98
+ level = restic_snap_parent["zfsmgmt:level"] + 1
99
+ zfs_snap_parent = restic_snap_parent["zfsmgmt:snapshot"]
100
+ ZfsMgmt.logger.debug("restic_snap_parent: level: #{restic_snap_parent["zfsmgmt:level"]} snapshot: #{zfs_snap_parent}")
104
101
  else
105
- $logger.error("restic_snap_parent rejected: level: #{restic_snap_parent['zfsmgmt:level']} snapshot: #{restic_snap_parent['zfsmgmt:snapshot']}")
102
+ ZfsMgmt.logger.error("restic_snap_parent rejected: level: #{restic_snap_parent["zfsmgmt:level"]} snapshot: #{restic_snap_parent["zfsmgmt:snapshot"]}")
106
103
  end
107
- $logger.debug("chain of snapshots: #{chain}")
104
+ ZfsMgmt.logger.debug("chain of snapshots: #{chain}")
108
105
  end
109
- tags = [ 'zfsmgmt',
110
- "zfsmgmt:snapshot=#{last_zfs_snapshot}",
111
- "zfsmgmt:zfs=#{zfs}",
112
- "zfsmgmt:level=#{level}" ]
113
- com = [ ZfsMgmt.global_options['zfs_binary'], 'send', '-w', '-h', '-p' ]
106
+ tags = ["zfsmgmt",
107
+ "zfsmgmt:snapshot=#{last_zfs_snapshot}",
108
+ "zfsmgmt:zfs=#{zfs}",
109
+ "zfsmgmt:level=#{level}"]
110
+ com = [ZfsMgmt.global_options["zfs_binary"], "send", "-w", "-h", "-p"]
114
111
  if level > 0
115
112
  if options[:intermediary]
116
- com.push('-I')
113
+ com.push("-I")
117
114
  else
118
- com.push('-i')
115
+ com.push("-i")
119
116
  end
120
117
  com.push(zfs_snap_parent)
121
118
  tags.push("zfsmgmt:parent=#{zfs_snap_parent}")
122
119
  end
123
- com.push( last_zfs_snapshot )
124
- com.push( '|', 'mbuffer', '-m', options[:buffer], '-q' )
125
- com.push( '|', options[:restic_binary], 'backup', '--stdin',
126
- '--stdin-filename', zfs, '--time', "\"#{zfs_snap_time.strftime('%F %T')}\"" )
120
+ com.push(last_zfs_snapshot)
121
+ com.push("|", "mbuffer", "-m", options[:buffer], "-q")
122
+ com.push("|", options[:restic_binary], "backup", "--stdin",
123
+ "--stdin-filename", zfs, "--time", "\"#{zfs_snap_time.strftime("%F %T")}\"")
127
124
  tags.each do |tag|
128
- com.push( '--tag', "\"#{tag}\"" )
125
+ com.push("--tag", "\"#{tag}\"")
129
126
  end
130
- if options.has_key?('limit_upload')
131
- com.push('--limit-upload', options['limit_upload'])
127
+ if options.has_key?("limit_upload")
128
+ com.push("--limit-upload", options["limit_upload"])
132
129
  end
133
- if options.has_key?('password_file')
134
- com.push('-p',options['password_file'])
130
+ if options.has_key?("password_file")
131
+ com.push("-p", options["password_file"])
135
132
  end
136
- if options.has_key?('repo')
137
- com.push('--repo', options['repo'])
133
+ if options.has_key?("repo")
134
+ com.push("--repo", options["repo"])
138
135
  elsif zfs_obj.restic_repository
139
- com.push( '--repo', zfs_obj.restic_repository )
136
+ com.push("--repo", zfs_obj.restic_repository)
140
137
  end
141
138
  if options[:verbose]
142
- com.push('--verbose',options[:verbose])
139
+ com.push("--verbose", options[:verbose])
143
140
  elsif $stdout.isatty
144
- com.push('-v')
141
+ com.push("-v")
145
142
  end
146
- unless ZfsMgmt.zfs_holds(last_zfs_snapshot).include?('zfsmgmt_restic')
147
- ZfsMgmt.zfs_hold('zfsmgmt_restic',last_zfs_snapshot)
143
+ unless ZfsMgmt.zfs_holds(last_zfs_snapshot).include?("zfsmgmt_restic")
144
+ ZfsMgmt.zfs_hold("zfsmgmt_restic", last_zfs_snapshot)
148
145
  end
149
146
  ZfsMgmt.system_com(com)
150
147
  chain_snaps = chain.map do |rsnap|
151
- rsnap['zfsmgmt:snapshot']
148
+ rsnap["zfsmgmt:snapshot"]
152
149
  end
153
- zfs_snapshots.each do |s,d|
154
- d['userrefs'] == 0 and next
150
+ zfs_snapshots.each do |s, d|
151
+ d["userrefs"] == 0 and next
155
152
  chain_snaps.include?(s) and next
156
153
  s == last_zfs_snapshot and next
157
- if ZfsMgmt.zfs_holds(s).include?('zfsmgmt_restic')
158
- ZfsMgmt.zfs_release('zfsmgmt_restic',s)
154
+ if ZfsMgmt.zfs_holds(s).include?("zfsmgmt_restic")
155
+ ZfsMgmt.zfs_release("zfsmgmt_restic", s)
159
156
  end
160
157
  end
161
158
  end
162
159
  end
163
160
  end
164
-
@@ -1,3 +1,3 @@
1
1
  module ZfsMgmt
2
- VERSION = "0.10.0"
2
+ VERSION = "0.10.1"
3
3
  end