Class: Snapsync::SyncAll

Inherits:
Object
  • Object
show all
Defined in:
lib/snapsync/sync_all.rb

Overview

Synchronizes all snapshots to a directory

A snapshot will be synchronized if (1) the target directory has a subdirectory of the config’s name and (2) this directory is not disabled through its config file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_dir, config_dir: Pathname.new('/etc/snapper/configs'), autoclean: nil) ⇒ SyncAll

Creates a sync-all operation for the given target directory

Parameters:

  • target_dir (Pathname)

    the target directory

  • autoclean (Boolean, nil) (defaults to: nil)

    if true or false, will control whether the targets should be cleaned of obsolete snapshots after synchronization. If nil (the default), the target’s own autoclean flag will be used to determine this



20
21
22
23
24
# File 'lib/snapsync/sync_all.rb', line 20

def initialize(target_dir, config_dir: Pathname.new('/etc/snapper/configs'), autoclean: nil)
    @config_dir = config_dir
    @target_dir = target_dir
    @autoclean  = autoclean
end

Instance Attribute Details

#config_dirObject (readonly)

The path to the directory containing snapper configuration files



9
10
11
# File 'lib/snapsync/sync_all.rb', line 9

def config_dir
  @config_dir
end

#target_dirObject (readonly)

The path to the root directory to which we should sync



11
12
13
# File 'lib/snapsync/sync_all.rb', line 11

def target_dir
  @target_dir
end

Instance Method Details

#autoclean?Boolean?

Whether the target should be forced to autoclean(true), force to not run cleanup (false) or use their own config file to decide (nil)

The default is nil

Returns:

  • (Boolean, nil)


32
33
34
# File 'lib/snapsync/sync_all.rb', line 32

def autoclean?
    @autoclean
end

#each_targetObject

Enumerate the targets available under #target_dir



37
38
39
40
41
42
43
44
45
46
# File 'lib/snapsync/sync_all.rb', line 37

def each_target
    SnapperConfig.each_in_dir(config_dir) do |config|
        dir = target_dir + config.name
        if !dir.exist?
            Snapsync.warn "no directory for configuration #{config.name} in #{target_dir}"
        else
            yield(config, LocalTarget.new(dir))
        end
    end
end

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/snapsync/sync_all.rb', line 48

def run
    each_target do |config, target|
        if !target.enabled?
            Snapsync.warn "not synchronizing to #{target.dir}, it is disabled"
            next
        end
        begin
            Sync.new(config, target, autoclean: autoclean?).run
        rescue Interrupt
            raise
        rescue Exception => e
            Snapsync.warn "failed to synchronize #{config.name} on #{target.dir}"
            PP.pp(e, buffer = String.new)
            buffer.each_line do |line|
                Snapsync.warn "  #{line.chomp}"
            end
            e.backtrace.each do |line|
                Snapsync.debug "  #{line}"
            end
        end
    end
end