#!/usr/bin/env ruby #Realpath didn't exist in Ruby 1.8 so we'll add it. unless File.respond_to? :realpath class File #:nodoc: def self.realpath path return realpath(File.readlink(path)) if symlink?(path) path end end end $: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib') require 'monkey_patch' require 'rubygems' require 'gli' require 'mogbak_version' require 'validations' require 'create' require 'backup' require 'restore' require 'list' require 'active_record' require 'activerecord-import' require "mogilefs" require 'sqlite3' require 'yaml' require 'forkinator' require 'path_helper' #Set master process name $0 = "mogbak [master]" include GLI program_desc 'Backup a mogilefs domain to the filesystem. mogbak needs SELECT access to your MogileFS tracker database, which must currently be MySQL.' desc 'enable debug mode' switch ['debug'] version Mogbak::VERSION desc 'Create a new backup profile for a MogileFS domain' arg_name '[backup_path]' command :create do |c| c.desc 'tracker ip' c.default_value '127.0.0.1' c.flag :trackerip c.desc 'tracker port' c.default_value 7001 c.flag :trackerport c.desc 'mogilefs domain' c.default_value 'test' c.flag :domain c.desc 'mysql db server host' c.default_value 'localhost' c.flag :dbhost c.desc 'mysql db server port' c.default_value 3306 c.flag :dbport c.desc 'database name' c.default_value 'mogilefs' c.flag :db c.desc 'database user' c.default_value 'mogile' c.flag :dbuser c.desc 'database password (REQUIRED)' c.flag :dbpass c.action do |global_options, options, args| #required options and arguments raise '--dbpass is required - see: mogbak help creater' unless options[:dbpass] raise '[backup_path] is required see mogadm - see: mogbak help create' unless args[0] $backup_path = args[0] mog = Create.new(:tracker_ip => options[:trackerip], :tracker_port => options[:trackerport], :domain => options[:domain], :db_host => options[:dbhost], :db_port => options[:dbport], :db => options[:db], :db_user => options[:dbuser], :db_pass => options[:dbpass], :backup_path => args[0]) puts "Backup profile successfully created. To start a backup run: mogbak backup #{args[0]}" end end desc 'Start a backup using an existing backup profile' arg_name '[backup_path]' command :backup do |c| c.desc 'do not remove deleted files from the backup (faster)' c.switch ['no-delete'] c.desc 'Number of worker processes' c.default_value 1 c.flag :workers c.action do |global_options, options, args| raise '[backup_path] is required - see: mogbak help backup' unless args[0] $backup_path = args[0] mog = Backup.new(:backup_path => args[0], :workers => options[:workers].to_i) mog.backup(:no_delete => options[:"no-delete"]) end end desc 'Restore a backup to a new MogileFS domain.' long_desc < options[:trackerip], :tracker_port => options[:trackerport], :domain => options[:domain], :backup_path => args[0], :workers => options[:workers]) restore.restore(options[:"single-file"]) end end desc 'List the files in a backup' long_desc < args[0]) list.list end end #If the user passes in --debug we can detect it here. pre do |global_options, command, options, args| $debug = global_options[:debug] true end #If debug is enabled we'll spit out the exception and the entire backtrace. Otherwise it will just output #the exception message on_error do |exception| if $debug puts exception puts '' puts 'Backtrace:' puts exception.backtrace return false end true end exit GLI.run(ARGV)