Module: EY::Backup::CLI

Extended by:
CLI
Included in:
CLI
Defined in:
lib/ey_backup/cli.rb

Overview

rename to option parser and config loader

Instance Method Summary collapse

Instance Method Details

#config_for(filename) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ey_backup/cli.rb', line 97

def config_for(filename)
  if File.exist?(filename)
    # Make the loaded config have symbols rather than strings
    config_orig = YAML::load(File.read(filename))
    config = {}
    config_orig.each do |key, val|
      config[key.to_sym] = val
    end
    config[:environment] ||= config[:env]
    config
  else
    abort "You need to have a backup file at #{filename}"
  end
end

#default_optionsObject



14
15
16
17
18
19
20
# File 'lib/ey_backup/cli.rb', line 14

def default_options
  {
    :command => :new_backup,
    :format => "gzip",
    :engine => 'mysql',
  }
end

#opt_parse(argv) ⇒ Object



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
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
# File 'lib/ey_backup/cli.rb', line 22

def opt_parse(argv)
  # Build a parser for the command line arguments
  options = {}

  opts = OptionParser.new do |opts|
    opts.version = EY::CloudServer::VERSION

    opts.banner = "Usage: eybackup [-flag] [argument]"
    opts.define_head "eybackup: manage dump (mysqldump/pg_dump) style backups of your database."
    opts.separator '*'*80

    opts.on("-l", "--list-backup DATABASE", "List mysql backups for DATABASE") do |db|
      if db == "all"
        db = nil
      end
      options[:db] = db
      options[:command] = :list
    end

    opts.on("-n", "--new-backup", "Create new mysql backup") do
      options[:command] = :new_backup
    end

    opts.on("-c", "--config CONFIG", "Use config file.") do |config|
      options[:config] = config
    end

    opts.on("-b", "--bucket BUCKET", "Override default S3 bucket name. (Be Careful!)") do |bucket|
      options[:backup_bucket] = bucket
    end

    opts.on("-t", "--tmp_dir TMPDIR", "Use the given directory for temporary storage.") do |tmp_dir|
      options[:tmp_dir] = tmp_dir
    end

    opts.on("-d", "--download BACKUP_INDEX", "download the backup specified by index. Run eybackup -l to get the index.") do |index_and_db|
      options[:command] = :download
      db, index = split_index(index_and_db)
      options[:index] = index
      options[:db] = db
    end

    opts.on("-e", "--engine DATABASE_ENGINE", "The database engine. ex: mysql, postgres.") do |engine|
      options[:engine] = engine
    end

    opts.on("-r", "--restore BACKUP_INDEX", "Download and apply the backup specified by index WARNING! will overwrite the current db with the backup. Run eybackup -l to get the index.") do |index_and_db|
      options[:command] = :restore
      db, index = split_index(index_and_db)
      options[:index] = index
      options[:db] = db
    end

    opts.on("-k", "--key KEYID", "Public key ID to use for the backup operation") do |key_id|
      options[:key_id] = key_id
    end

    opts.on("-q", "--quiet", "Supress output to STDOUT") do
      options[:quiet] = true
    end

    opts.on("-s", "--split_size INTEGER", "Maximum size of a single backup file before splitting.") do |split_size|
      options[:split_size] = split_size.to_i
    end
  end

  opts.parse!(argv)

  options
end

#run(argv) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/ey_backup/cli.rb', line 6

def run(argv)
  options = default_options.merge(opt_parse(argv))

  config_path = options[:config] || "/etc/.#{options[:engine]}.backups.yml"

  config_for(config_path).merge(options)
end

#split_index(index) ⇒ Object



93
94
95
# File 'lib/ey_backup/cli.rb', line 93

def split_index(index)
  index.split(':').reverse
end