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

#runObject



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
# File 'lib/snapsync/sync_all.rb', line 39

def run
    SnapperConfig.each_in_dir(config_dir) do |config|
        dir = target_dir + config.name
        if !dir.exist?
            Snapsync.warn "not synchronizing #{config.name}, there are no corresponding directory in #{target_dir}. Call snapsync init to create a proper target directory"
        else
            target = LocalTarget.new(dir)
            if !target.enabled?
                Snapsync.warn "not synchronizing #{config.name}, it is disabled"
                next
            end

            LocalSync.new(config, target).sync
            if should_autoclean_target?(target)
                if target.cleanup
                    Snapsync.info "running cleanup"
                    target.cleanup.cleanup(target)
                else
                    Snapsync.info "#{target.sync_policy.class.name} policy set, no cleanup to do"
                end
            else
                Snapsync.info "autoclean not set"
            end
        end
    end
end

#should_autoclean_target?(target) ⇒ Boolean

Whether the given target should be cleaned after synchronization.

This is determined either by #autoclean? if new was called with true or false, or by the target’s own autoclean flag if new was called with nil

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'lib/snapsync/sync_all.rb', line 31

def should_autoclean_target?(target)
    if @autoclean.nil?
        target.autoclean?
    else
        @autoclean
    end
end