Class: CoralBackup::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/coral_backup/cli.rb

Constant Summary collapse

OSX_VOLUME_ROOT_EXCLUSIONS =
%w[
  .DocumentRevisions-V100
  .fseventsd
  .Spotlight-V100
  .TemporaryItems
  .Trashes
  .VolumeIcon.icns
]

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



15
16
17
18
# File 'lib/coral_backup/cli.rb', line 15

def initialize(*args)
  super
  @settings = Settings.new
end

Instance Method Details

#__run(action_name) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/coral_backup/cli.rb', line 77

def __run(action_name)
  unless rsync_version.split(".").first.to_i >= 3
    warn "ERROR: rsync version must be larger than 3.X.X"
    exit 1
  end

  time = Time.now
  data = @settings.action_data(action_name)

  destination = data[:destination]
  dry_run = options[:"dry-run"]
  updating_time = options[:"updating-time"]
  exclusions = data[:exclusions]
  source = data[:source]

  args = ["rsync", "-rlptgoxS", "--delete", "-X", "--progress", "--stats"]
  args << "--dry-run" if dry_run

  new_destination = File.expand_path("#{action_name} backup #{Time.now.strftime("%F-%H%M%S")}", destination)
  old_destination =
    Dir.chdir(destination) {
      Dir["*"]
    }.select{|dirname|
      dirname.match(/#{Regexp.escape(action_name)} backup \d{4}-\d{2}-\d{2}-\d{6}/)
    }.sort.last

  if old_destination
    args << "--link-dest"
    args << Pathname.new(File.expand_path(old_destination, destination)).relative_path_from(Pathname.new(new_destination)).to_s
  end

  exclusions.each do |exclusion|
    args << "--exclude"
    args << Pathname.new(exclusion).relative_path_from(Pathname.new(source)).to_s
  end

  if RUBY_PLATFORM.match(/darwin/)
    if File.expand_path("..", source) == "/Volumes"
      OSX_VOLUME_ROOT_EXCLUSIONS.each do |vr_exclusion|
        args << "--exclude"
        args << vr_exclusion
      end
    end
  end

  args << source
  args << new_destination

  system(args.flatten.shelljoin)

  @settings.update_time(action_name, time) if !dry_run && updating_time
end

#add(action_name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/coral_backup/cli.rb', line 21

def add(action_name)
  if @settings.exist_action?(action_name)
    warn "ERROR: Backup action `#{action_name}' already exists."
    exit 1
  end

  puts "Drag and drop or input source directory."
  source = FileSelector.single_select
  unless FileTest.directory?(source)
    warn "ERROR: Not a directory: #{source}"
    exit 1
  end
  source << "/" unless source.end_with?("/")

  puts "Drag and drop or input exclusion files/directories."
  puts "Press Ctrl + D to finish."
  exclusions = FileSelector.select
  exclusions.map! {|filename|
    if FileTest.directory?(filename)
      filename << "/" unless filename.end_with?("/")
    end
    filename
  }
  exclusions.uniq!

  puts "Drag and drop or input destination directory."
  destination = FileSelector.single_select
  unless FileTest.directory?(destination)
    warn "ERROR: Not a directory: #{destination}"
    exit 1
  end
  destination << "/" unless destination.end_with?("/")

  @settings.add(action_name, source, destination, exclusions)
rescue RuntimeError => e
  warn "ERROR: #{e}"
  exit 1
end

#delete(action_name) ⇒ Object



61
62
63
64
65
66
# File 'lib/coral_backup/cli.rb', line 61

def delete(action_name)
  @settings.delete(action_name)
rescue ArgumentError => e
  warn "ERROR: #{e}"
  exit 1
end

#info(action_name) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/coral_backup/cli.rb', line 132

def info(action_name)
  data = @settings.action_data(action_name)

  puts "Source: #{data[:source]}"
  puts "Destination: #{data[:destination]}"
  print "exclusions: "
  if data[:exclusions].empty?
    puts "(No exclusions)"
  else
    puts "#{data[:exclusions].size} exclusion(s)"
    puts data[:exclusions]
  end

  print "Last backup run at: "
  if data[:last_run_time].nil?
    puts "No backup yet"
  else
    puts data[:last_run_time]
  end
rescue  ArgumentError => e
  warn "ERROR: #{e}"
  exit 1
end

#listObject



69
70
71
# File 'lib/coral_backup/cli.rb', line 69

def list
  puts @settings.action_names
end

#versionObject



157
158
159
# File 'lib/coral_backup/cli.rb', line 157

def version
  puts VERSION
end