Class: Snapsync::LocalTarget
- Inherits:
-
Object
- Object
- Snapsync::LocalTarget
- Defined in:
- lib/snapsync/local_target.rb
Defined Under Namespace
Classes: InvalidTargetPath, InvalidUUIDError, NoUUIDError
Instance Attribute Summary collapse
-
#cleanup ⇒ Object
readonly
The cleanup object.
-
#dir ⇒ Object
readonly
This target’s directory.
-
#sync_policy ⇒ Object
readonly
The target sync policy.
-
#uuid ⇒ String
readonly
The target’s UUID.
Instance Method Summary collapse
-
#autoclean? ⇒ Boolean
Whether the target should be autocleaned on synchronization.
- #change_policy(type, options) ⇒ Object
-
#config_path ⇒ Pathname
Path to the target’s configuration file.
- #delete(s, dry_run: false) ⇒ Object
-
#disable ⇒ Object
Disable this target, i.e.
- #each_snapshot(&block) ⇒ Object
-
#enable ⇒ Object
Enable this target, i.e.
-
#enabled? ⇒ Boolean
Whether this target is enabled or not.
-
#initialize(dir, create_if_needed: true) ⇒ LocalTarget
constructor
A new instance of LocalTarget.
- #parse_config(config) ⇒ Object
- #read_config ⇒ Object
- #write_config ⇒ Object
Constructor Details
#initialize(dir, create_if_needed: true) ⇒ LocalTarget
Returns a new instance of LocalTarget.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/snapsync/local_target.rb', line 37 def initialize(dir, create_if_needed: true) if !dir.directory? raise ArgumentError, "#{dir} does not exist" end @dir = dir begin read_config rescue NoUUIDError if !create_if_needed raise end @uuid = SecureRandom.uuid @sync_policy = DefaultSyncPolicy.new @cleanup = nil @enabled = true @autoclean = true end write_config end |
Instance Attribute Details
#cleanup ⇒ Object (readonly)
The cleanup object
15 16 17 |
# File 'lib/snapsync/local_target.rb', line 15 def cleanup @cleanup end |
#dir ⇒ Object (readonly)
This target’s directory
9 10 11 |
# File 'lib/snapsync/local_target.rb', line 9 def dir @dir end |
#sync_policy ⇒ Object (readonly)
The target sync policy
12 13 14 |
# File 'lib/snapsync/local_target.rb', line 12 def sync_policy @sync_policy end |
#uuid ⇒ String (readonly)
The target’s UUID
6 7 8 |
# File 'lib/snapsync/local_target.rb', line 6 def uuid @uuid end |
Instance Method Details
#autoclean? ⇒ Boolean
Whether the target should be autocleaned on synchronization
Defaults to true
31 |
# File 'lib/snapsync/local_target.rb', line 31 def autoclean?; !!@autoclean end |
#change_policy(type, options) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/snapsync/local_target.rb', line 114 def change_policy(type, ) case type when 'default' sync_policy = DefaultSyncPolicy cleanup = nil when 'timeline' sync_policy = TimelineSyncPolicy cleanup = TimelineSyncPolicy when 'last' sync_policy = SyncLastPolicy cleanup = SyncLastPolicy else raise InvalidConfiguration, "synchronization policy #{type} does not exist" end @sync_policy = sync_policy.from_config() @cleanup = if cleanup Cleanup.new(cleanup.from_config()) end end |
#config_path ⇒ Pathname
Path to the target’s configuration file
110 111 112 |
# File 'lib/snapsync/local_target.rb', line 110 def config_path dir + "snapsync.config" end |
#delete(s, dry_run: false) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/snapsync/local_target.rb', line 135 def delete(s, dry_run: false) Snapsync.info "Removing snapshot #{s.num} #{s.date.to_time} at #{s.subvolume_dir}" return if dry_run IO.popen(["btrfs", "subvolume", "delete", s.subvolume_dir.to_s, err: '/dev/null']) do |io| io.read end if $?.success? s.snapshot_dir.rmtree Snapsync.info "Flushing data to disk" IO.popen(["btrfs", "filesystem", "sync", s.snapshot_dir.to_s, err: '/dev/null']).read else Snapsync.warn "failed to remove snapshot at #{s.subvolume_dir}, keeping the rest of the snapshot" end end |
#disable ⇒ Object
Disable this target, i.e. remove it from the auto synchronization and cleanup commands
26 |
# File 'lib/snapsync/local_target.rb', line 26 def disable; @enabled = false; self end |
#each_snapshot(&block) ⇒ Object
58 59 60 |
# File 'lib/snapsync/local_target.rb', line 58 def each_snapshot(&block) Snapshot.each(dir, &block) end |
#enable ⇒ Object
Enable this target, i.e. add it to the auto synchronization and cleanup commands
22 |
# File 'lib/snapsync/local_target.rb', line 22 def enable; @enabled = true; self end |
#enabled? ⇒ Boolean
Whether this target is enabled or not
18 |
# File 'lib/snapsync/local_target.rb', line 18 def enabled?; @enabled end |
#parse_config(config) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/snapsync/local_target.rb', line 89 def parse_config(config) uuid = config['uuid'] if uuid.length != 36 raise InvalidUUIDError, "uuid in #{uuid_path} was expected to be 36 characters long, but is #{uuid.length}" end @uuid = uuid @enabled = config.fetch('enabled', true) @autoclean = config.fetch('autoclean', true) if policy_config = config['policy'] change_policy(policy_config['type'], policy_config['options'] || Array.new) else @sync_policy = DefaultSyncPolicy.new @cleanup = nil end end |
#read_config ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/snapsync/local_target.rb', line 80 def read_config begin raw_config = YAML.load(config_path.read) rescue Errno::ENOENT => e raise NoUUIDError, e., e.backtrace end parse_config(raw_config) end |
#write_config ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/snapsync/local_target.rb', line 62 def write_config config = Hash['uuid' => uuid, 'policy' => Hash.new] config['policy']['type'] = case sync_policy when TimelineSyncPolicy then 'timeline' when SyncLastPolicy then 'last' when DefaultSyncPolicy then 'default' end config['policy']['options'] = sync_policy.to_config config['enabled'] = enabled? config['autoclean'] = autoclean? File.open(config_path, 'w') do |io| io.write YAML.dump(config) end end |