zfs_mgmt 0.5.6 → 0.6.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.
- checksums.yaml +4 -4
- data/.github/workflows/rspec.yml +20 -0
- data/AGENTS.md +35 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +56 -1
- data/bin/zfsmgr +4 -2
- data/lib/zfs_mgmt/restic.rb +10 -7
- data/lib/zfs_mgmt/version.rb +1 -1
- data/lib/zfs_mgmt/zfs.rb +242 -0
- data/lib/zfs_mgmt/zfs_mgr/list.rb +7 -3
- data/lib/zfs_mgmt/zfs_mgr/send.rb +33 -0
- data/lib/zfs_mgmt/zfs_mgr/snapshot.rb +3 -1
- data/lib/zfs_mgmt.rb +273 -139
- data/zfs_mgmt.gemspec +3 -0
- metadata +46 -1
data/lib/zfs_mgmt.rb
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# coding: utf-8
|
|
2
2
|
require "zfs_mgmt/version"
|
|
3
|
+
require "thor"
|
|
4
|
+
require "zfs_mgmt/zfs"
|
|
3
5
|
require "zfs_mgmt/restic"
|
|
4
6
|
require "zfs_mgmt/zfs_mgr"
|
|
5
7
|
require 'pp'
|
|
@@ -9,6 +11,9 @@ require 'text-table'
|
|
|
9
11
|
require 'open3'
|
|
10
12
|
require 'filesize'
|
|
11
13
|
require 'timeout'
|
|
14
|
+
require 'pstore'
|
|
15
|
+
require 'json'
|
|
16
|
+
require 'fileutils'
|
|
12
17
|
|
|
13
18
|
$logger = Logger.new(STDERR, progname: 'zfs_mgmt')
|
|
14
19
|
|
|
@@ -46,6 +51,8 @@ module ZfsMgmt
|
|
|
46
51
|
end
|
|
47
52
|
class ZfsGetError < StandardError
|
|
48
53
|
end
|
|
54
|
+
class ZfsDatasetNotFoundError < ZfsGetError
|
|
55
|
+
end
|
|
49
56
|
def self.custom_properties()
|
|
50
57
|
return [
|
|
51
58
|
'policy',
|
|
@@ -61,6 +68,7 @@ module ZfsMgmt
|
|
|
61
68
|
'send',
|
|
62
69
|
'remote',
|
|
63
70
|
'destination',
|
|
71
|
+
'recv_state',
|
|
64
72
|
].map do |p|
|
|
65
73
|
['zfsmgmt',p].join(':')
|
|
66
74
|
end
|
|
@@ -123,6 +131,12 @@ module ZfsMgmt
|
|
|
123
131
|
raise ZfsGetError, "process was signalled \"#{com.join(' ')}\", termsig #{status.termsig}"
|
|
124
132
|
end
|
|
125
133
|
unless status.success?
|
|
134
|
+
if se =~ /cannot open '.*': dataset does not exist/i || se =~ /dataset does not exist/i
|
|
135
|
+
$logger.debug("dataset does not exist when running \"#{com.join(' ')}\", exit status #{status.exitstatus}")
|
|
136
|
+
so.split("\n").each { |l| $logger.debug("stdout: #{l}") }
|
|
137
|
+
se.split("\n").each { |l| $logger.debug("stderr: #{l}") }
|
|
138
|
+
raise ZfsDatasetNotFoundError, "dataset does not exist: #{zfs}"
|
|
139
|
+
end
|
|
126
140
|
$logger.error("failed to execute \"#{com.join(' ')}\", exit status #{status.exitstatus}")
|
|
127
141
|
so.split("\n").each { |l| $logger.debug("stdout: #{l}") }
|
|
128
142
|
se.split("\n").each { |l| $logger.error("stderr: #{l}") }
|
|
@@ -163,18 +177,25 @@ module ZfsMgmt
|
|
|
163
177
|
end
|
|
164
178
|
return [results['hourly'],results['daily'],results['weekly'],results['monthly'],results['yearly']]
|
|
165
179
|
end
|
|
166
|
-
def self.snapshot_destroy_policy(zfs,props,snaps)
|
|
180
|
+
def self.snapshot_destroy_policy(zfs, props = nil, snaps = nil)
|
|
181
|
+
if zfs.is_a?(ZfsMgmt::Zfs)
|
|
182
|
+
zfs_obj = zfs
|
|
183
|
+
snaps ||= zfs_obj.snapshots
|
|
184
|
+
else
|
|
185
|
+
zfs_obj = ZfsMgmt::Zfs.from_zfs_props(zfs, props)
|
|
186
|
+
end
|
|
187
|
+
|
|
167
188
|
minage = 0
|
|
168
|
-
if
|
|
169
|
-
minage = timespec_to_seconds(
|
|
189
|
+
if zfs_obj.minage
|
|
190
|
+
minage = timespec_to_seconds(zfs_obj.minage)
|
|
170
191
|
end
|
|
171
192
|
strategy = 'oldest'
|
|
172
|
-
if
|
|
193
|
+
if zfs_obj.strategy == 'youngest'
|
|
173
194
|
strategy = 'youngest'
|
|
174
195
|
end
|
|
175
196
|
sorted = snaps.keys.sort { |a,b| snaps[b]['creation'] <=> snaps[a]['creation'] }
|
|
176
197
|
|
|
177
|
-
counters = policy_parser(
|
|
198
|
+
counters = policy_parser(zfs_obj.policy)
|
|
178
199
|
$logger.debug(counters)
|
|
179
200
|
saved = {}
|
|
180
201
|
|
|
@@ -184,23 +205,22 @@ module ZfsMgmt
|
|
|
184
205
|
end
|
|
185
206
|
|
|
186
207
|
sorted.each do |snap_name|
|
|
187
|
-
if
|
|
188
|
-
$logger.debug("skipping #{snap_name} because it matches ignoresnaps pattern: #{
|
|
208
|
+
if zfs_obj.ignoresnaps and /#{zfs_obj.ignoresnaps}/ =~ snap_name.split('@')[1]
|
|
209
|
+
$logger.debug("skipping #{snap_name} because it matches ignoresnaps pattern: #{zfs_obj.ignoresnaps}")
|
|
189
210
|
next
|
|
190
211
|
end
|
|
191
|
-
if
|
|
192
|
-
$logger.debug("skipping #{snap_name} because it does not match matchsnaps pattern: #{
|
|
212
|
+
if zfs_obj.matchsnaps and not /#{zfs_obj.matchsnaps}/ =~ snap_name.split('@')[1]
|
|
213
|
+
$logger.debug("skipping #{snap_name} because it does not match matchsnaps pattern: #{zfs_obj.matchsnaps}")
|
|
193
214
|
next
|
|
194
215
|
end
|
|
195
216
|
snaptime = local_epoch_to_datetime(snaps[snap_name]['creation'])
|
|
196
217
|
$date_patterns.each do |d,p|
|
|
197
218
|
pat = snaptime.strftime(p)
|
|
198
219
|
if saved[d].has_key?(pat)
|
|
199
|
-
|
|
200
|
-
if props.has_key?('zfsmgmt:prefersnaps') and /#{props['zfsmgmt:prefersnaps']}/ !~ saved[d][pat].split('@')[1] and /#{props['zfsmgmt:prefersnaps']}/ =~ snap_name.split('@')[1]
|
|
220
|
+
if zfs_obj.prefersnaps and /#{zfs_obj.prefersnaps}/ !~ saved[d][pat].split('@')[1] and /#{zfs_obj.prefersnaps}/ =~ snap_name.split('@')[1]
|
|
201
221
|
$logger.debug("updating the saved snapshot, we prefer this one: \"#{pat}\" to #{snap_name} at #{snaptime}")
|
|
202
222
|
saved[d][pat] = snap_name
|
|
203
|
-
elsif strategy == 'oldest' and ( not
|
|
223
|
+
elsif strategy == 'oldest' and ( not zfs_obj.prefersnaps or /#{zfs_obj.prefersnaps}/ =~ snap_name.split('@')[1] )
|
|
204
224
|
# update the existing current save snapshot for this timeframe
|
|
205
225
|
$logger.debug("updating the saved snapshot for \"#{pat}\" to #{snap_name} at #{snaptime}")
|
|
206
226
|
saved[d][pat] = snap_name
|
|
@@ -226,8 +246,8 @@ module ZfsMgmt
|
|
|
226
246
|
# delete everything not in the list of saved snapshots
|
|
227
247
|
deleteme = sorted - saved_snaps
|
|
228
248
|
deleteme = deleteme.select { |snap|
|
|
229
|
-
if
|
|
230
|
-
$logger.debug("skipping #{snap} because it matches ignoresnaps pattern: #{
|
|
249
|
+
if zfs_obj.ignoresnaps and /#{zfs_obj.ignoresnaps}/ =~ snap.split('@')[1]
|
|
250
|
+
$logger.debug("skipping #{snap} because it matches ignoresnaps pattern: #{zfs_obj.ignoresnaps}")
|
|
231
251
|
false
|
|
232
252
|
elsif minage > 0 and Time.at(snaps[snap]['creation'] + minage) > Time.now()
|
|
233
253
|
$logger.debug("skipping due to minage: #{snap} #{local_epoch_to_datetime(snaps[snap]['creation']).strftime('%F %T')}")
|
|
@@ -241,74 +261,90 @@ module ZfsMgmt
|
|
|
241
261
|
}
|
|
242
262
|
return saved,saved_snaps,deleteme
|
|
243
263
|
end
|
|
244
|
-
def self.zfs_managed_list(filter: '.+', properties: ['all'], property_match: {
|
|
245
|
-
zfss = []
|
|
246
|
-
zfsget(properties: properties).each do |
|
|
247
|
-
unless
|
|
264
|
+
def self.zfs_managed_list(filter: '.+', properties: ['all'], property_match: { manage: true } )
|
|
265
|
+
zfss = []
|
|
266
|
+
zfsget(properties: properties).each do |zfs_name,props|
|
|
267
|
+
unless match_filter?(zfs_name, filter)
|
|
248
268
|
next
|
|
249
269
|
end
|
|
270
|
+
zfs = ZfsMgmt::Zfs.from_zfs_props(zfs_name, props)
|
|
250
271
|
managed = true
|
|
251
272
|
property_match.each do |k,v|
|
|
252
|
-
|
|
273
|
+
matched = if k.to_s.start_with?('zfsmgmt:')
|
|
274
|
+
key_comp?(props, k, v)
|
|
275
|
+
else
|
|
276
|
+
attr_name = (k.to_sym == :send ? :send_enabled : k.to_sym)
|
|
277
|
+
match_value?(zfs.public_send(attr_name), v)
|
|
278
|
+
end
|
|
279
|
+
unless matched
|
|
253
280
|
managed = false
|
|
254
281
|
break
|
|
255
282
|
end
|
|
256
283
|
end
|
|
257
284
|
next unless managed
|
|
258
|
-
|
|
259
|
-
if snaps.length == 0
|
|
260
|
-
$logger.debug("not processing #{zfs} as there are no snapshots")
|
|
261
|
-
next
|
|
262
|
-
end
|
|
263
|
-
zfss.push([zfs,props,snaps])
|
|
285
|
+
zfss.push(zfs)
|
|
264
286
|
end
|
|
265
287
|
return zfss
|
|
266
288
|
end
|
|
267
|
-
def self.snapshot_policy(filter: '.+')
|
|
268
|
-
zfs_managed_list(filter: filter).each do |zfs
|
|
269
|
-
unless
|
|
270
|
-
$logger.error("zfs_mgmt is configured to manage #{zfs}, but there is no policy configuration in zfsmgmt:policy, skipping")
|
|
289
|
+
def self.snapshot_policy(filter: '.+', show_destroy: false)
|
|
290
|
+
zfs_managed_list(filter: filter).each do |zfs|
|
|
291
|
+
unless zfs.policy
|
|
292
|
+
$logger.error("zfs_mgmt is configured to manage #{zfs.name}, but there is no policy configuration in zfsmgmt:policy, skipping")
|
|
271
293
|
next # zfs
|
|
272
294
|
end
|
|
273
295
|
|
|
296
|
+
snaps = zfs.snapshots
|
|
297
|
+
if snaps.empty?
|
|
298
|
+
$logger.debug("not processing #{zfs.name} as there are no snapshots")
|
|
299
|
+
next
|
|
300
|
+
end
|
|
301
|
+
|
|
274
302
|
begin
|
|
275
303
|
# call the function that decides who to save and who to delete
|
|
276
|
-
(saved,saved_snaps,deleteme) = snapshot_destroy_policy(zfs
|
|
304
|
+
(saved,saved_snaps,deleteme) = snapshot_destroy_policy(zfs)
|
|
277
305
|
rescue ArgumentError
|
|
278
|
-
$logger.error("zfs_mgmt is configured to manage #{zfs}, but there is no valid policy configuration, skipping")
|
|
306
|
+
$logger.error("zfs_mgmt is configured to manage #{zfs.name}, but there is no valid policy configuration, skipping")
|
|
279
307
|
next
|
|
280
308
|
end
|
|
281
309
|
|
|
282
|
-
|
|
283
|
-
|
|
310
|
+
snaps_to_show = show_destroy ? (saved_snaps + deleteme).uniq : saved_snaps
|
|
311
|
+
|
|
312
|
+
if snaps_to_show.length == 0
|
|
313
|
+
$logger.info("no snapshots marked as saved by policy for #{zfs.name}")
|
|
284
314
|
next
|
|
285
315
|
end
|
|
286
316
|
# print a table of saved snapshots with the reasons it is being saved
|
|
287
317
|
table = Text::Table.new
|
|
288
|
-
table.head = [zfs,'creation','hourly','daily','weekly','monthly','yearly']
|
|
318
|
+
table.head = [zfs.name,'creation','hourly','daily','weekly','monthly','yearly']
|
|
289
319
|
table.rows = []
|
|
290
|
-
|
|
320
|
+
snaps_to_show.sort { |a,b| snaps[b]['creation'] <=> snaps[a]['creation'] }.each do |snap|
|
|
291
321
|
table.rows << [snap.split('@')[1],local_epoch_to_datetime(snaps[snap]['creation'])] + find_saved_reason(saved,snap)
|
|
292
322
|
end
|
|
293
323
|
print table.to_s
|
|
294
324
|
end
|
|
295
325
|
end
|
|
296
326
|
def self.snapshot_destroy(noop: false, verbose: false, filter: '.+')
|
|
297
|
-
zfs_managed_list(filter: filter).each do |zfs
|
|
298
|
-
unless
|
|
299
|
-
$logger.error("zfs_mgmt is configured to manage #{zfs}, but there is no policy configuration in zfsmgmt:policy, skipping")
|
|
327
|
+
zfs_managed_list(filter: filter).each do |zfs|
|
|
328
|
+
unless zfs.policy
|
|
329
|
+
$logger.error("zfs_mgmt is configured to manage #{zfs.name}, but there is no policy configuration in zfsmgmt:policy, skipping")
|
|
300
330
|
next # zfs
|
|
301
331
|
end
|
|
302
332
|
|
|
333
|
+
snaps = zfs.snapshots
|
|
334
|
+
if snaps.empty?
|
|
335
|
+
$logger.debug("not processing #{zfs.name} as there are no snapshots")
|
|
336
|
+
next
|
|
337
|
+
end
|
|
338
|
+
|
|
303
339
|
begin
|
|
304
340
|
# call the function that decides who to save and who to delete
|
|
305
|
-
(saved,saved_snaps,deleteme) = snapshot_destroy_policy(zfs
|
|
341
|
+
(saved,saved_snaps,deleteme) = snapshot_destroy_policy(zfs)
|
|
306
342
|
rescue ArgumentError
|
|
307
|
-
$logger.error("zfs_mgmt is configured to manage #{zfs}, but there is no valid policy configuration, skipping")
|
|
343
|
+
$logger.error("zfs_mgmt is configured to manage #{zfs.name}, but there is no valid policy configuration, skipping")
|
|
308
344
|
next
|
|
309
345
|
end
|
|
310
346
|
|
|
311
|
-
$logger.info("deleting #{deleteme.length} snapshots for #{zfs}")
|
|
347
|
+
$logger.info("deleting #{deleteme.length} snapshots for #{zfs.name}")
|
|
312
348
|
deleteme.reverse! # oldest first for removal
|
|
313
349
|
deleteme.each do |snap_name|
|
|
314
350
|
$logger.debug("delete: #{snap_name} #{local_epoch_to_datetime(snaps[snap_name]['creation']).strftime('%F %T')}")
|
|
@@ -322,7 +358,7 @@ module ZfsMgmt
|
|
|
322
358
|
for i in 0..(deleteme.length - 1) do
|
|
323
359
|
max = deleteme.length - 1 - i
|
|
324
360
|
$logger.debug("attempting to remove snaps 0 through #{max} out of #{deleteme.length} snapshots")
|
|
325
|
-
bigarg = "#{zfs}@#{deleteme[0..max].map { |s| s.split('@')[1] }.join(',')}"
|
|
361
|
+
bigarg = "#{zfs.name}@#{deleteme[0..max].map { |s| s.split('@')[1] }.join(',')}"
|
|
326
362
|
com = com_base + [bigarg]
|
|
327
363
|
$logger.debug("size of bigarg: #{bigarg.length} size of com: #{com.length}")
|
|
328
364
|
if bigarg.length >= 131072 or com.length >= (2097152-10000)
|
|
@@ -354,23 +390,14 @@ module ZfsMgmt
|
|
|
354
390
|
# snapshot all filesystems configured for snapshotting
|
|
355
391
|
def self.snapshot_create(noop: false, filter: '.+')
|
|
356
392
|
dt = DateTime.now
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
)
|
|
366
|
-
}.each do |zfs,props|
|
|
367
|
-
prefix = ( props.has_key?('zfsmgmt:snap_prefix') ? props['zfsmgmt:snap_prefix'] : 'zfsmgmt' )
|
|
368
|
-
ts = ( props.has_key?('zfsmgmt:snap_timestamp') ? props['zfsmgmt:snap_timestamp'] : '%FT%T%z' )
|
|
369
|
-
com = [global_options['zfs_binary'],'snapshot']
|
|
370
|
-
if key_comp?(props,'zfsmgmt:snapshot','recursive') and key_comp?(props,'zfsmgmt:snapshot@source',['local','received'])
|
|
371
|
-
com.push('-r')
|
|
372
|
-
end
|
|
373
|
-
com.push("#{zfs}@#{[prefix,dt.strftime(ts)].join('-')}")
|
|
393
|
+
zfs_managed_list(filter: filter, property_match: { snapshot: true }).each do |zfs|
|
|
394
|
+
next unless zfs.snapshot
|
|
395
|
+
|
|
396
|
+
prefix = zfs.snap_prefix || 'zfsmgmt'
|
|
397
|
+
ts = zfs.snap_timestamp || '%FT%T%z'
|
|
398
|
+
com = [global_options['zfs_binary'] || global_options[:zfs_binary], 'snapshot']
|
|
399
|
+
com.push('-r') if zfs.snapshot_recursive
|
|
400
|
+
com.push("#{zfs.name}@#{[prefix,dt.strftime(ts)].join('-')}")
|
|
374
401
|
system_com(com,noop)
|
|
375
402
|
end
|
|
376
403
|
end
|
|
@@ -382,36 +409,36 @@ module ZfsMgmt
|
|
|
382
409
|
unless $?.success?
|
|
383
410
|
$logger.error("command failed: #{$?.exitstatus}")
|
|
384
411
|
end
|
|
412
|
+
return $?
|
|
385
413
|
end
|
|
386
414
|
end
|
|
387
|
-
def self.zfs_send(options,zfs,props,snaps)
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
destination_path = ( options[:destination] ? options[:destination] : props['zfsmgmt:destination'] )
|
|
392
|
-
if props['zfsmgmt:destination@source'] == 'local'
|
|
393
|
-
destination_path = File.join( destination_path,
|
|
394
|
-
File.basename(zfs)
|
|
395
|
-
)
|
|
396
|
-
elsif m = /inherited from (.+)/.match(props['zfsmgmt:destination@source'])
|
|
397
|
-
destination_path = File.join( destination_path,
|
|
398
|
-
File.basename(m[1]),
|
|
399
|
-
zfs.sub(m[1],'')
|
|
400
|
-
)
|
|
415
|
+
def self.zfs_send(options, zfs, props = nil, snaps = nil)
|
|
416
|
+
if zfs.is_a?(ZfsMgmt::Zfs)
|
|
417
|
+
zfs_obj = zfs
|
|
418
|
+
snaps ||= zfs_obj.snapshots
|
|
401
419
|
else
|
|
402
|
-
|
|
420
|
+
zfs_obj = ZfsMgmt::Zfs.from_zfs_props(zfs, props)
|
|
421
|
+
end
|
|
422
|
+
zfs_name = zfs_obj.name
|
|
423
|
+
sorted = snaps.keys.sort { |a,b| snaps[a]['creation'] <=> snaps[b]['creation'] }
|
|
424
|
+
destination_path = zfs_obj.resolved_destination(options[:destination])
|
|
425
|
+
unless destination_path
|
|
426
|
+
$logger.error("fatal error: unable to determine destination path for #{zfs_name}")
|
|
403
427
|
exit(1)
|
|
404
428
|
end
|
|
405
429
|
# does the destination zfs already exist?
|
|
406
430
|
remote_zfs_state = ''
|
|
407
431
|
begin
|
|
408
432
|
recv_zfs = zfsget(zfs: destination_path,
|
|
409
|
-
command_prefix: recv_command_prefix(options,
|
|
433
|
+
command_prefix: recv_command_prefix(options, zfs_obj),
|
|
410
434
|
#properties: ['receive_resume_token'],
|
|
411
435
|
)
|
|
412
|
-
rescue
|
|
436
|
+
rescue ZfsDatasetNotFoundError
|
|
413
437
|
$logger.debug("recv filesystem doesn't exist: #{destination_path}")
|
|
414
438
|
remote_zfs_state = 'missing'
|
|
439
|
+
rescue ZfsGetError
|
|
440
|
+
$logger.error("unable to check remote filesystem #{destination_path}")
|
|
441
|
+
return
|
|
415
442
|
else
|
|
416
443
|
if recv_zfs[destination_path].has_key?('receive_resume_token')
|
|
417
444
|
remote_zfs_state = recv_zfs[destination_path]['receive_resume_token']
|
|
@@ -426,21 +453,22 @@ module ZfsMgmt
|
|
|
426
453
|
source = sorted[0]
|
|
427
454
|
if options[:initial_snapshot] == 'newest' or
|
|
428
455
|
key_comp?(options, 'replicate') or
|
|
429
|
-
|
|
456
|
+
zfs_obj.send_replicate
|
|
430
457
|
source = sorted[-1]
|
|
431
458
|
end
|
|
432
459
|
com += zfs_send_com(options,
|
|
433
|
-
|
|
460
|
+
zfs_obj,
|
|
434
461
|
[],
|
|
435
462
|
source,
|
|
436
463
|
)
|
|
437
464
|
e = zfs_send_estimate(com) if options[:verbose] == 'pv'
|
|
438
465
|
com += mbuffer_command(options) if options[:mbuffer]
|
|
439
466
|
com += pv_command(options,e) if options[:verbose] == 'pv'
|
|
440
|
-
com += zfs_recv_com(options,[],
|
|
467
|
+
com += zfs_recv_com(options,[],zfs_obj,destination_path)
|
|
441
468
|
|
|
442
|
-
system_com(com)
|
|
443
|
-
unless
|
|
469
|
+
status = system_com(com)
|
|
470
|
+
record_send_state(zfs_name, status) unless options[:noop]
|
|
471
|
+
unless status && status.success?
|
|
444
472
|
return
|
|
445
473
|
end
|
|
446
474
|
|
|
@@ -454,23 +482,11 @@ module ZfsMgmt
|
|
|
454
482
|
com += mbuffer_command(options) if options[:mbuffer]
|
|
455
483
|
com += pv_command(options,e) if options[:verbose] == 'pv'
|
|
456
484
|
|
|
457
|
-
|
|
458
|
-
recv.push('-n') if options[:noop]
|
|
459
|
-
recv.push('-u') if options[:unmount]
|
|
460
|
-
recv.push('-v') if options[:verbose] and ( options[:verbose] == 'receive' or options[:verbose] == 'recv' )
|
|
461
|
-
recv.push(dq(destination_path))
|
|
462
|
-
|
|
463
|
-
if options[:remote] or props['zfsmgmt:remote']
|
|
464
|
-
if options[:mbuffer]
|
|
465
|
-
recv = mbuffer_command(options) + recv
|
|
466
|
-
end
|
|
467
|
-
recv = recv_command_prefix(options,props) + [ sq(recv.join(' ')) ]
|
|
468
|
-
end
|
|
469
|
-
|
|
470
|
-
com += recv
|
|
485
|
+
com += zfs_recv_com(options,[],zfs_obj,destination_path)
|
|
471
486
|
|
|
472
|
-
system_com(com)
|
|
473
|
-
unless
|
|
487
|
+
status = system_com(com)
|
|
488
|
+
record_send_state(zfs_name, status) unless options[:noop]
|
|
489
|
+
unless status && status.success?
|
|
474
490
|
return
|
|
475
491
|
end
|
|
476
492
|
end
|
|
@@ -479,7 +495,7 @@ module ZfsMgmt
|
|
|
479
495
|
begin
|
|
480
496
|
remote_snaps = zfsget(zfs: destination_path,
|
|
481
497
|
types: ['snapshot'],
|
|
482
|
-
command_prefix: recv_command_prefix(options,
|
|
498
|
+
command_prefix: recv_command_prefix(options, zfs_obj),
|
|
483
499
|
properties: ['creation','userrefs'],
|
|
484
500
|
)
|
|
485
501
|
rescue ZfsGetError
|
|
@@ -490,35 +506,34 @@ module ZfsMgmt
|
|
|
490
506
|
$logger.error("receiving filesystem has NO snapshots, it must be destroyed: #{destination_path}")
|
|
491
507
|
return
|
|
492
508
|
end
|
|
493
|
-
if remote_snaps.has_key?(sorted[-1].sub(
|
|
494
|
-
$logger.info("the most recent local snapshot (#{sorted[-1]}) already exists on the remote side (#{sorted[-1].sub(
|
|
509
|
+
if remote_snaps.has_key?(sorted[-1].sub(zfs_name,destination_path))
|
|
510
|
+
$logger.info("the most recent local snapshot (#{sorted[-1]}) already exists on the remote side (#{sorted[-1].sub(zfs_name,destination_path)})")
|
|
495
511
|
return
|
|
496
512
|
end
|
|
497
513
|
remote_snaps.sort_by { |k,v| -v['creation'] }.each do |rsnap,v|
|
|
498
514
|
# oldest first
|
|
499
|
-
|
|
500
|
-
#pp snaps
|
|
501
|
-
if snaps.has_key?(rsnap.sub(destination_path,zfs))
|
|
515
|
+
if snaps.has_key?(rsnap.sub(destination_path,zfs_name))
|
|
502
516
|
$logger.debug("process #{rsnap} to #{sorted[-1]}")
|
|
503
517
|
com = []
|
|
504
518
|
i_opt = '-i'
|
|
505
519
|
# allow the command line option for intermediary to override the property
|
|
506
520
|
if key_comp?(options,'intermediary',[true,false])
|
|
507
521
|
i_opt = '-I' if key_comp?(options, 'intermediary', true)
|
|
508
|
-
elsif
|
|
522
|
+
elsif zfs_obj.send_intermediary
|
|
509
523
|
i_opt = '-I'
|
|
510
524
|
end
|
|
511
525
|
|
|
512
|
-
com += zfs_send_com(options,
|
|
526
|
+
com += zfs_send_com(options,zfs_obj,[i_opt, dq('@' + rsnap.split('@')[1])], sorted[-1])
|
|
513
527
|
e = zfs_send_estimate(com) if options[:verbose] == 'pv'
|
|
514
528
|
com += mbuffer_command(options) if options[:mbuffer]
|
|
515
529
|
com += pv_command(options,e) if options[:verbose] == 'pv'
|
|
516
|
-
com += zfs_recv_com(options,[],
|
|
530
|
+
com += zfs_recv_com(options,[],zfs_obj,destination_path)
|
|
517
531
|
|
|
518
|
-
system_com(com)
|
|
532
|
+
status = system_com(com)
|
|
533
|
+
record_send_state(zfs_name, status) unless options[:noop]
|
|
519
534
|
return
|
|
520
535
|
end
|
|
521
|
-
$logger.debug("skipping remote snapshot #{rsnap} because the same snapshot doesn't exist locally #{rsnap.sub(destination_path,
|
|
536
|
+
$logger.debug("skipping remote snapshot #{rsnap} because the same snapshot doesn't exist locally #{rsnap.sub(destination_path,zfs_name)}")
|
|
522
537
|
end
|
|
523
538
|
$logger.error("receiving filesystem has no snapshots that still exists on the sending side, it must be destroyed: #{destination_path}")
|
|
524
539
|
|
|
@@ -531,6 +546,7 @@ module ZfsMgmt
|
|
|
531
546
|
mbuffer_command
|
|
532
547
|
end
|
|
533
548
|
def self.zfs_send_com(options,props,extra_opts,target)
|
|
549
|
+
zfs_obj = props.is_a?(ZfsMgmt::Zfs) ? props : ZfsMgmt::Zfs.from_zfs_props('', props)
|
|
534
550
|
zfs_send_com = [ ZfsMgmt.global_options[:zfs_binary], 'send' ]
|
|
535
551
|
zfs_send_com.push('-v','-P') if key_comp?(options,'verbose','send')
|
|
536
552
|
send_opts = {
|
|
@@ -547,14 +563,27 @@ module ZfsMgmt
|
|
|
547
563
|
# allow the command line options to override the properties value
|
|
548
564
|
if key_comp?(options,p,[true,false])
|
|
549
565
|
zfs_send_com.push(o) if key_comp?(options,p,true)
|
|
550
|
-
elsif
|
|
566
|
+
elsif zfs_obj.public_send("send_#{p}")
|
|
551
567
|
zfs_send_com.push(o)
|
|
552
568
|
end
|
|
553
569
|
end
|
|
554
570
|
zfs_send_com + extra_opts + [dq(target),'|']
|
|
555
571
|
end
|
|
572
|
+
def self.prop_off?(v)
|
|
573
|
+
['false','off'].include?(v)
|
|
574
|
+
end
|
|
575
|
+
def self.recv_state_enabled?(options, props)
|
|
576
|
+
['state', :state, 'recv_state', :recv_state].each do |k|
|
|
577
|
+
return options[k] if key_comp?(options, k, [true, false])
|
|
578
|
+
end
|
|
579
|
+
return props.recv_state if props.is_a?(ZfsMgmt::Zfs)
|
|
580
|
+
return false if key_comp?(props, 'zfsmgmt:recv_state', method(:prop_off?))
|
|
581
|
+
true
|
|
582
|
+
end
|
|
556
583
|
def self.zfs_recv_com(options,extra_opts,props,target)
|
|
557
|
-
|
|
584
|
+
zfs_obj = props.is_a?(ZfsMgmt::Zfs) ? props : ZfsMgmt::Zfs.from_zfs_props('', props)
|
|
585
|
+
zfs_recv_com = [ ZfsMgmt.global_options[:zfs_binary], 'recv' ]
|
|
586
|
+
zfs_recv_com.push('-s') if recv_state_enabled?(options, zfs_obj)
|
|
558
587
|
recv_opts = {
|
|
559
588
|
'noop' => '-n',
|
|
560
589
|
'drop_holds' => '-h',
|
|
@@ -567,7 +596,7 @@ module ZfsMgmt
|
|
|
567
596
|
# allow the command line options to override the properties value
|
|
568
597
|
if key_comp?(options,p,[true,false])
|
|
569
598
|
zfs_recv_com.push(o) if key_comp?(options,p,true)
|
|
570
|
-
elsif
|
|
599
|
+
elsif zfs_obj.public_send("recv_#{p}")
|
|
571
600
|
zfs_recv_com.push(o)
|
|
572
601
|
end
|
|
573
602
|
end
|
|
@@ -585,18 +614,17 @@ module ZfsMgmt
|
|
|
585
614
|
zfs_recv_com += extra_opts
|
|
586
615
|
zfs_recv_com.push(dq(target))
|
|
587
616
|
|
|
588
|
-
if options[:remote] or
|
|
617
|
+
if options[:remote] or zfs_obj.remote
|
|
589
618
|
if options[:mbuffer]
|
|
590
619
|
zfs_recv_com = mbuffer_command(options) + zfs_recv_com
|
|
591
620
|
end
|
|
592
|
-
zfs_recv_com = recv_command_prefix(options,
|
|
621
|
+
zfs_recv_com = recv_command_prefix(options, zfs_obj) + [ sq(zfs_recv_com.join(' ')) ]
|
|
593
622
|
end
|
|
594
623
|
zfs_recv_com
|
|
595
624
|
end
|
|
596
625
|
def self.recv_command_prefix(options,props)
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
[] )
|
|
626
|
+
remote = options[:remote] || (props.is_a?(ZfsMgmt::Zfs) ? props.remote : props['zfsmgmt:remote'])
|
|
627
|
+
remote ? [ 'ssh', remote ] : []
|
|
600
628
|
end
|
|
601
629
|
def self.zfs_send_estimate(com)
|
|
602
630
|
lcom = com.dup
|
|
@@ -643,24 +671,29 @@ module ZfsMgmt
|
|
|
643
671
|
def self.match_filter?(zfs, filter)
|
|
644
672
|
/#{filter}/ =~ zfs
|
|
645
673
|
end
|
|
646
|
-
def self.
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
return
|
|
651
|
-
elsif
|
|
652
|
-
return
|
|
653
|
-
elsif
|
|
654
|
-
return
|
|
655
|
-
elsif v.kind_of?(Regexp)
|
|
656
|
-
return v =~ h[p]
|
|
674
|
+
def self.match_value?(actual, expected)
|
|
675
|
+
if expected.kind_of?(Array)
|
|
676
|
+
return expected.include?(actual)
|
|
677
|
+
elsif expected.kind_of?(Hash)
|
|
678
|
+
return expected.keys.include?(actual)
|
|
679
|
+
elsif expected.kind_of?(Method) || expected.kind_of?(Proc)
|
|
680
|
+
return expected.call(actual)
|
|
681
|
+
elsif expected.kind_of?(Regexp)
|
|
682
|
+
return expected =~ actual
|
|
657
683
|
else
|
|
658
684
|
# string, boolean, numbers?
|
|
659
|
-
return
|
|
685
|
+
return actual == expected
|
|
660
686
|
end
|
|
661
687
|
end
|
|
662
|
-
def self.
|
|
663
|
-
|
|
688
|
+
def self.key_comp?(h,p,v = method(:prop_on?))
|
|
689
|
+
#$logger.debug("p:#{p}\th[p]:#{h[p]}\tv:#{v}")
|
|
690
|
+
return false unless h.has_key?(p)
|
|
691
|
+
match_value?(h[p], v)
|
|
692
|
+
end
|
|
693
|
+
def self.set_log_level(sev = nil)
|
|
694
|
+
sev = ENV['ZFS_MGMT_LOGLEVEL'] if sev.nil? || sev.to_s.strip.empty?
|
|
695
|
+
return if sev.nil? || sev.to_s.strip.empty?
|
|
696
|
+
case sev.to_s.strip.downcase
|
|
664
697
|
when 'debug'
|
|
665
698
|
$logger.level = Logger::DEBUG
|
|
666
699
|
when 'info'
|
|
@@ -671,25 +704,29 @@ module ZfsMgmt
|
|
|
671
704
|
$logger.level = Logger::ERROR
|
|
672
705
|
when 'fatal'
|
|
673
706
|
$logger.level = Logger::FATAL
|
|
707
|
+
when 'unknown', 'off', 'none', 'silent'
|
|
708
|
+
$logger.level = Logger::UNKNOWN
|
|
709
|
+
when /\A\d+\z/
|
|
710
|
+
$logger.level = sev.to_i
|
|
674
711
|
end
|
|
675
712
|
end
|
|
713
|
+
set_log_level
|
|
676
714
|
def self.zfs_send_all(options)
|
|
677
715
|
zfs_managed_list(filter: options[:filter],
|
|
678
|
-
property_match: {
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
$logger.debug("skipping received filesystem: #{zfs}")
|
|
716
|
+
property_match: { send_enabled: true }).each do |zfs|
|
|
717
|
+
unless zfs.send_enabled
|
|
718
|
+
$logger.debug("skipping filesystem with send disabled: #{zfs.name}")
|
|
682
719
|
next
|
|
683
720
|
end
|
|
684
|
-
|
|
685
|
-
$logger.
|
|
721
|
+
unless zfs.destination
|
|
722
|
+
$logger.error("#{zfs.name}: you must specify a destination zfs path via the user property zfsmgmt:destination, even if using --destination on the command line, skipping")
|
|
686
723
|
next
|
|
687
724
|
end
|
|
688
|
-
|
|
689
|
-
$logger.
|
|
725
|
+
if zfs.snapshots.empty?
|
|
726
|
+
$logger.debug("not processing #{zfs.name} as there are no snapshots")
|
|
690
727
|
next
|
|
691
728
|
end
|
|
692
|
-
zfs_send(options,zfs
|
|
729
|
+
zfs_send(options, zfs)
|
|
693
730
|
end
|
|
694
731
|
end
|
|
695
732
|
def self.lock(options)
|
|
@@ -735,4 +772,101 @@ module ZfsMgmt
|
|
|
735
772
|
lock.close()
|
|
736
773
|
end
|
|
737
774
|
end
|
|
775
|
+
|
|
776
|
+
SEND_WARN_SECONDS = 36 * 60 * 60
|
|
777
|
+
SEND_CRIT_SECONDS = 72 * 60 * 60
|
|
778
|
+
|
|
779
|
+
def self.send_state_db_path
|
|
780
|
+
if global_options && global_options[:state_db]
|
|
781
|
+
return global_options[:state_db]
|
|
782
|
+
end
|
|
783
|
+
dir = if Process.euid == 0
|
|
784
|
+
'/var/lib/zfs_mgmt'
|
|
785
|
+
elsif ENV['XDG_STATE_HOME'] && !ENV['XDG_STATE_HOME'].empty?
|
|
786
|
+
File.join(ENV['XDG_STATE_HOME'], 'zfs_mgmt')
|
|
787
|
+
else
|
|
788
|
+
File.join(Dir.home, '.local', 'state', 'zfs_mgmt')
|
|
789
|
+
end
|
|
790
|
+
File.join(dir, 'send_state.pstore')
|
|
791
|
+
end
|
|
792
|
+
|
|
793
|
+
def self.send_state_store(path = send_state_db_path)
|
|
794
|
+
FileUtils.mkdir_p(File.dirname(path))
|
|
795
|
+
PStore.new(path)
|
|
796
|
+
end
|
|
797
|
+
|
|
798
|
+
def self.record_send_state(zfs, status, timestamp: Time.now.to_i)
|
|
799
|
+
exit_code = if status.nil?
|
|
800
|
+
1
|
|
801
|
+
elsif status.respond_to?(:exitstatus)
|
|
802
|
+
status.exitstatus || (status.respond_to?(:termsig) && status.termsig ? 128 + status.termsig : 1)
|
|
803
|
+
else
|
|
804
|
+
status.to_i
|
|
805
|
+
end
|
|
806
|
+
store = send_state_store
|
|
807
|
+
store.transaction do
|
|
808
|
+
store[zfs] = JSON.generate({ 'timestamp' => timestamp.to_i, 'exit' => exit_code })
|
|
809
|
+
end
|
|
810
|
+
end
|
|
811
|
+
|
|
812
|
+
def self.send_state_entries(filter: '.+')
|
|
813
|
+
path = send_state_db_path
|
|
814
|
+
return {} unless File.exist?(path)
|
|
815
|
+
store = send_state_store(path)
|
|
816
|
+
entries = {}
|
|
817
|
+
store.transaction(true) do
|
|
818
|
+
store.roots.sort.each do |zfs|
|
|
819
|
+
next unless match_filter?(zfs, filter)
|
|
820
|
+
raw = store[zfs]
|
|
821
|
+
data = raw.is_a?(String) ? JSON.parse(raw) : raw
|
|
822
|
+
entries[zfs] = data
|
|
823
|
+
end
|
|
824
|
+
end
|
|
825
|
+
entries
|
|
826
|
+
end
|
|
827
|
+
|
|
828
|
+
def self.send_state_delete(zfs)
|
|
829
|
+
store = send_state_store
|
|
830
|
+
store.transaction do
|
|
831
|
+
store.delete(zfs)
|
|
832
|
+
end
|
|
833
|
+
end
|
|
834
|
+
|
|
835
|
+
def self.send_state_wipe
|
|
836
|
+
store = send_state_store
|
|
837
|
+
store.transaction do
|
|
838
|
+
store.roots.each do |k|
|
|
839
|
+
store.delete(k)
|
|
840
|
+
end
|
|
841
|
+
end
|
|
842
|
+
end
|
|
843
|
+
|
|
844
|
+
def self.send_state_show(check: false, filter: '.+')
|
|
845
|
+
entries = send_state_entries(filter: filter)
|
|
846
|
+
if check
|
|
847
|
+
now = Time.now.to_i
|
|
848
|
+
entries.each do |zfs, data|
|
|
849
|
+
ts = data['timestamp'].to_i
|
|
850
|
+
exit_code = data['exit'].to_i
|
|
851
|
+
age = [now - ts, 0].max
|
|
852
|
+
time_str = local_epoch_to_datetime(ts).to_s
|
|
853
|
+
state = if exit_code != 0 || age > SEND_CRIT_SECONDS
|
|
854
|
+
2
|
|
855
|
+
elsif age > SEND_WARN_SECONDS
|
|
856
|
+
1
|
|
857
|
+
else
|
|
858
|
+
0
|
|
859
|
+
end
|
|
860
|
+
puts "#{state} \"zfs_send #{zfs}\" age=#{age};#{SEND_WARN_SECONDS};#{SEND_CRIT_SECONDS} last send #{time_str} exit #{exit_code}"
|
|
861
|
+
end
|
|
862
|
+
else
|
|
863
|
+
return if entries.empty?
|
|
864
|
+
table = Text::Table.new
|
|
865
|
+
table.head = ['zfs', 'timestamp', 'exit']
|
|
866
|
+
table.rows = entries.map do |zfs, data|
|
|
867
|
+
[zfs, local_epoch_to_datetime(data['timestamp'].to_i), data['exit']]
|
|
868
|
+
end
|
|
869
|
+
print table.to_s
|
|
870
|
+
end
|
|
871
|
+
end
|
|
738
872
|
end
|