Class: VCAP::Services::Base::Backup
Instance Method Summary collapse
- #check_mount_points ⇒ Object
- #echo(output, err = false) ⇒ Object
- #exit_fun ⇒ Object
- #get_dump_path(name, options = {}) ⇒ Object
-
#initialize ⇒ Backup
constructor
A new instance of Backup.
- #more_options(opts) ⇒ Object
- #parse_options ⇒ Object
- #script_file ⇒ Object
- #single_app(&blk) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ Backup
Returns a new instance of Backup.
31 32 33 34 35 36 |
# File 'lib/base/backup.rb', line 31 def initialize @run_lock = Mutex.new @shutdown = false trap("TERM") { exit_fun } trap("INT") { exit_fun } end |
Instance Method Details
#check_mount_points ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/base/backup.rb', line 124 def check_mount_points # make sure the backup base dir is mounted pn = Pathname.new(@config["backup_base_dir"]) if !@tolerant && !pn.mountpoint? echo @config["backup_base_dir"] + " is not mounted, exit",true exit end end |
#echo(output, err = false) ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/base/backup.rb', line 133 def echo(output, err=false) if err $stderr.puts(output) unless @logger @logger.error(output) if @logger else $stdout.puts(output) unless @logger @logger.info(output) if @logger end end |
#exit_fun ⇒ Object
42 43 44 45 46 47 |
# File 'lib/base/backup.rb', line 42 def exit_fun @shutdown = true Thread.new do @run_lock.synchronize { exit } end end |
#get_dump_path(name, options = {}) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/base/backup.rb', line 112 def get_dump_path(name, ={}) name = name.sub(/^(mongodb|redis)-/,'') mode = [:mode] || 0 time = [:time] || Time.now case mode when 1 File.join(@config['backup_base_dir'], 'backups', @config['service_name'],name, time.to_i.to_s,@config['node_id']) else File.join(@config['backup_base_dir'], 'backups', @config['service_name'], name[0,2], name[2,2], name[4,2], name, time.to_i.to_s) end end |
#more_options(opts) ⇒ Object
160 161 |
# File 'lib/base/backup.rb', line 160 def (opts) end |
#parse_options ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/base/backup.rb', line 143 def OptionParser.new do |opts| opts. = "Usage: #{File.basename(script_file)} [options]" opts.on("-c", "--config [ARG]", "Node configuration File") do |opt| @config_file = opt end opts.on("-h", "--help", "Help") do puts opts exit end opts.on("-t", "--tolerant", "Tolerant mode") do @tolerant = true end (opts) end.parse! end |
#script_file ⇒ Object
38 39 40 |
# File 'lib/base/backup.rb', line 38 def script_file $0 end |
#single_app(&blk) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/base/backup.rb', line 49 def single_app(&blk) if File.open(script_file).flock(File::LOCK_EX|File::LOCK_NB) blk.call else echo "Script #{ script_file } is already running",true end end |
#start ⇒ Object
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 108 109 110 |
# File 'lib/base/backup.rb', line 57 def start single_app do echo "#{File.basename(script_file)} starts" @config_file = default_config_file echo "Load config file" # load conf file begin @config = YAML.load(File.open(@config_file)) rescue => e echo "Could not read configuration file: #{e}",true exit end # Setup logger echo @config["logging"] logging_config = Steno::Config.from_hash(@config["logging"]) Steno.init(logging_config) # Use running binary name for logger identity name. @logger = Steno.logger(File.basename(script_file)) # Make pidfile if @config["pid"] pf = VCAP::PidFile.new(@config["pid"]) pf.unlink_at_exit end echo "Check mount points" check_mount_points # make sure backup dir on nfs storage exists @nfs_base = @config["backup_base_dir"] + "/backups/" + @config["service_name"] echo "Check NFS base" if File.directory? @nfs_base echo @nfs_base + " exists" else echo @nfs_base + " does not exist, create it" begin FileUtils.mkdir_p @nfs_base rescue => e echo "Could not create dir on nfs!",true exit end end echo "Run backup task" @run_lock.synchronize { backup_db } echo "#{File.basename(script_file)} task is completed" end rescue => e echo "Error: #{e.}\n #{e.backtrace}",true rescue Interrupt => it echo "Backup is interrupted!" end |