Class: Tms::Backup

Inherits:
Object
  • Object
show all
Extended by:
BetterAttrAccessor
Defined in:
lib/tms/backup.rb

Constant Summary collapse

VOLUMES_PATH =
'/Volumes/'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BetterAttrAccessor

better_attr_accessor, better_attr_reader

Constructor Details

#initialize(path, in_progress = false) ⇒ Backup

Returns a new instance of Backup.



103
104
105
106
# File 'lib/tms/backup.rb', line 103

def initialize(path, in_progress = false)
  @path = path
  @in_progress = in_progress
end

Class Method Details

.add_filter_dir(filter_dir) ⇒ Object



64
65
66
# File 'lib/tms/backup.rb', line 64

def add_filter_dir(filter_dir)
  filter_dirs << File.expand_path(filter_dir)
end

.backup_volumeObject



13
14
15
# File 'lib/tms/backup.rb', line 13

def backup_volume
  Tms.backup_volume or abort('backup volume not available')
end

.backups_dirObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tms/backup.rb', line 21

def backups_dir
  unless @backups_dir
    if system('which -s tmutil')
      $stderr.puts "Using tmutil to detect and mount Time Machine volume"
      self.backups_dir = `tmutil machinedirectory`.strip
    else
      self.backups_dir = Path.new(backup_volume) / 'Backups.backupdb' / computer_name
    end
    $stderr.puts "Detected: #{self.backups_dir}"
  end
  @backups_dir
end

.backups_dir=(backups_dir) ⇒ Object



33
34
35
36
37
# File 'lib/tms/backup.rb', line 33

def backups_dir=(backups_dir)
  backups_dir = Path.new(backups_dir)
  abort %{backups dir «#{backups_dir}» is not a dir} unless backups_dir.directory?
  @backups_dir = backups_dir
end

.colorize?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/tms/backup.rb', line 74

def colorize?
  !colorize.nil? ? colorize : $stdout.tty?
end

.computer_nameObject



17
18
19
# File 'lib/tms/backup.rb', line 17

def computer_name
  Tms.computer_name or abort('can\'t get computer name')
end

.filter_dirsObject



58
59
60
# File 'lib/tms/backup.rb', line 58

def filter_dirs
  @filter_dirs ||= []
end

.filter_dirs?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/tms/backup.rb', line 61

def filter_dirs?
  !filter_dirs.empty?
end

.listObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tms/backup.rb', line 82

def list
  @list ||= begin
    backups_dir.children.map do |path|
      case path.basename.to_s
      when /^\d{4}-\d{2}-\d{2}-\d{6}$/
        new(path)
      when /^\d{4}-\d{2}-\d{2}-\d{6}\.inProgress$/
        if show_in_progress?
          path.children.select(&:directory?).map do |path_in_progress|
            new(path_in_progress, true)
          end
        end
      end
    end.flatten.compact.sort
  end
end

.real_path(path) ⇒ Object



47
48
49
50
# File 'lib/tms/backup.rb', line 47

def real_path(path)
  path = File.join(VOLUMES_PATH, path)
  path == root_volume_path ? '/' : path
end

.root_volume_pathObject



52
53
54
55
56
# File 'lib/tms/backup.rb', line 52

def root_volume_path
  @root_volume_path ||= Dir["#{VOLUMES_PATH}*"].find do |volume_path|
    File.symlink?(volume_path) && File.readlink(volume_path) == '/'
  end or abort('can\'t find /Volumes path for root')
end

.show_progress?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/tms/backup.rb', line 78

def show_progress?
  !show_progress.nil? ? show_progress : $stderr.tty?
end

.tm_path(path) ⇒ Object



40
41
42
43
44
45
# File 'lib/tms/backup.rb', line 40

def tm_path(path)
  unless path[0, VOLUMES_PATH.length].downcase == VOLUMES_PATH.downcase
    path = File.join(root_volume_path, path)
  end
  path[VOLUMES_PATH.length - 1..-1]
end

Instance Method Details

#<=>(other) ⇒ Object



134
135
136
# File 'lib/tms/backup.rb', line 134

def <=>(other)
  name <=> other.name
end

#completed_inObject



118
119
120
# File 'lib/tms/backup.rb', line 118

def completed_in
  finished_at - started_at
end

#finished_atObject



115
116
117
# File 'lib/tms/backup.rb', line 115

def finished_at
  @finished_at ||= Time.at(xattr.get('com.apple.backupd.SnapshotCompletionDate').to_i / 1_000_000.0)
end

#nameObject



108
109
110
# File 'lib/tms/backup.rb', line 108

def name
  @name ||= in_progress? ? "#{path.dirname.basename}/#{path.basename}" : path.basename.to_s
end

#started_atObject



112
113
114
# File 'lib/tms/backup.rb', line 112

def started_at
  @start_date ||= Time.at(xattr.get('com.apple.backupd.SnapshotStartDate').to_i / 1_000_000.0)
end