module EY module Backup module CLI extend self 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 def default_options { :command => :new_backup, :format => "gzip", :engine => 'mysql', } end 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: backing up your shit since way back when..." opts.separator '*'*80 opts.on("-l", "--list-backup DATABASE", "List mysql backups for DATABASE") do |db| options[:db] = (db || 'all') 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("-d", "--download BACKUP_INDEX", "download the backup specified by index. Run eybackup -l to get the index.") do |index| options[:command] = :download options[:index] = index 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| options[:command] = :restore options[:index] = index 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 def config_for(filename) if File.exist?(filename) config = YAML::load(File.read(filename)) config[:environment] ||= config[:env] config else abort "You need to have a backup file at #{filename}" end end end end end