Class: CoralBackup::CLI

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



5
6
7
8
# File 'lib/coral_backup/cli.rb', line 5

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

Instance Method Details

#__run(action_name) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
# File 'lib/coral_backup/cli.rb', line 63

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]
  dryrun = options[:"dry-run"]
  exclusions = data[:exclusions]
  source = data[:source]

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

  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

  args << source
  args << new_destination

  system(args.flatten.shelljoin)

  @settings.update_time(action_name, time) unless dryrun
end

#add(action_name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
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
# File 'lib/coral_backup/cli.rb', line 11

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)
end

#delete(action_name) ⇒ Object



48
49
50
51
52
53
# File 'lib/coral_backup/cli.rb', line 48

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

#listObject



56
57
58
# File 'lib/coral_backup/cli.rb', line 56

def list
  puts @settings.action_names
end

#show(action_name) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/coral_backup/cli.rb', line 108

def show(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

#versionObject



133
134
135
# File 'lib/coral_backup/cli.rb', line 133

def version
  puts VERSION
end