module Snapsync # 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 class SyncAll # The path to the directory containing snapper configuration files attr_reader :config_dir # The path to the root directory to which we should sync attr_reader :target_dir # Creates a sync-all operation for the given target directory # # @param [Pathname] target_dir the target directory # @param [Boolean,nil] autoclean 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 def initialize(target_dir, config_dir: Pathname.new('/etc/snapper/configs'), autoclean: nil) @config_dir = config_dir @target_dir = target_dir @autoclean = autoclean end # 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 # # @return [Boolean,nil] def autoclean? @autoclean end # Enumerate the targets available under {#target_dir} 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 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 end end