Class: S3backup::CLI

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

Constant Summary collapse

DEFAULT_CONFIG =
'./backup.yml'

Class Method Summary collapse

Class Method Details

.execute(stdout, arguments = []) ⇒ Object



6
7
8
9
10
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/s3backup/cli.rb', line 6

def self.execute(stdout, arguments=[])

  # NOTE: the option -p/--path= is given as an example, and should be replaced in your application.
  options = {
    :restore=> false,
    :config_file => DEFAULT_CONFIG,
    :verbose => false,
    :log => nil,
    :output_dir => '.'
  }
  begin 
    parser = OptionParser.new do |opt|
      opt.banner = "Usage: #{File.basename($0)} [Option]"
      opt.on("-r","--restore","restore backup.") {
        options[:restore] = true
      }
      opt.on("-f","--file config",String,"location config file. default: #{DEFAULT_CONFIG}") {|o|
        options[:config_file] = o
      }
      opt.on("-o","--output directory",String,"restore location of directory. default: current directory.") {|o|
        options[:output_dir] = o
      }
      opt.on("-v","--verbose","verbose message to log file"){
        options[:verbose] = true
      }
      opt.on("-l","--log path",String,"path to log file"){|o|
        options[:log] = o
      }
      opt.on("-h","--help","print this message and quit") {
        puts opt.help
        exit(0)
      }
      opt.parse!(arguments)
    end
  rescue OptionParser::ParseError => err
    S3log.error(err.message)
    exit(-1)
  end
  S3log.set_debug(options[:verbose])
  if !File.file?(options[:config_file])
    S3log.error("config #{options[:config_file]} is not exist.")
    exit(-1)
  end
  if options[:log]
    S3log.set_logfile(File.open(options[:log],"a"))
  end
  if options[:restore]
    FileUtils.mkdir_p(options[:output_dir])
    if !File.directory?(options[:output_dir])
      S3log.error("output directory #{options[:output_dir]} is not exist.")
      exit(-1)
    end
    rt = Restore.new(options[:output_dir],YAML.load_file(options[:config_file]))
    rt.start
  else
    bk = Backup.new(YAML.load_file(options[:config_file]))
    bk.start
  end
end