Class: PXCBackup::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/pxcbackup/application.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Application

Returns a new instance of Application.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pxcbackup/application.rb', line 11

def initialize(argv)
  parse_options(argv)

  config = File.join(ENV['HOME'], '.pxcbackup')
  if @options[:config]
    config = @options[:config]
    raise 'cannot find given config file' unless File.file?(config)
  end
  if File.file?(config)
    config_options = YAML.load_file(config)
    config_options = config_options.inject({}) { |hash, (k, v)| hash[k.to_sym] = v; hash }
    @options = config_options.merge(@options)
  end
end

Instance Method Details

#parse_options(argv) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
105
106
107
# File 'lib/pxcbackup/application.rb', line 41

def parse_options(argv)
  @options ||= {}
  parser = OptionParser.new do |opt|
    opt.banner = "Usage: #{$0} COMMAND [OPTIONS]"
    opt.separator ''
    opt.separator 'Commands'
    opt.separator '    create             create a new backup'
    opt.separator '    help               show this help'
    opt.separator '    list               list available backups'
    opt.separator '    restore [time]     restore to a point in time'
    opt.separator ''
    opt.separator 'Options'

    opt.on('-c', '--config', '=CONFIG_FILE', 'config file to use instead of ~/.pxcbackup') do |config_file|
      @options[:config] = config_file
    end

    opt.on('-d', '--dir', '=BACKUP_DIR', 'local repository to store backups') do |backup_dir|
      @options[:backup_dir] = backup_dir
    end

    opt.on('-f', '--full', 'create a full backup') do
      @options[:type] = :full
    end

    opt.on('-i', '--incremental', 'create an incremental backup') do
      @options[:type] = :incremental
    end

    opt.on('-l', '--local', 'stay local, i.e. do not communicate with S3') do
      @options[:local] = true
    end

    opt.on('--no-color', 'disable color output') do
      @options[:no_color] = true
    end

    opt.on('-r', '--remote', '=REMOTE_URI', 'remote URI to sync backups to, e.g. s3://my-aws-bucket/') do |remote|
      @options[:remote] = remote
    end

    opt.on('-v', '--verbose', 'verbose output') do
      Logger.raise_verbosity
    end

    opt.on('--version', 'print version and exit') do
      puts "pxcbackup #{VERSION}"
      exit
    end

    opt.on('-y', '--yes', 'skip confirmation on backup restore') do
      @options[:skip_confirmation] = true
    end
  end

  begin
    @command, *@arguments = parser.parse(argv)
    if @command == 'help'
      puts parser
      exit
    end
    raise 'no command given' if @command.to_s == ''
    raise "invalid command #{@command}" unless ['create', 'list', 'restore'].include?(@command)
  rescue => e
    abort "#{$0}: #{e.message}\n#{parser}"
  end
end

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pxcbackup/application.rb', line 26

def run
  Logger.color_output = ENV['TERM'] && !@options[:no_color]
  backupper = Backupper.new(@options)

  case @command
  when 'create'
    backupper.make_backup(@options)
  when 'list'
    backupper.list_backups
  when 'restore'
    time = @arguments.any? ? Time.parse(@arguments.first) : Time.now
    backupper.restore_backup(time, @options)
  end
end