Class: RBarman::CliCommand
- Inherits:
-
Object
- Object
- RBarman::CliCommand
- Defined in:
- lib/rbarman/cli_command.rb
Overview
Wrapper for the barman command line tool
Instance Attribute Summary collapse
Class Method Summary collapse
-
.size_in_bytes(size, unit) ⇒ Integer
Converts the size according to the unit to bytes.
Instance Method Summary collapse
-
#backup(server, backup_id, opts = {}) ⇒ Backup
Instructs barman to get information about a specific backup.
-
#backups(server, opts = {}) ⇒ Backups
Instructs barman to get information about backups.
-
#create(server) ⇒ void
Instructs barman to create a backup.
-
#create_recovery_cmd_args(opts = {}) ⇒ String
Creates an argument string for barman recovery command based on opts Hash.
-
#delete(server, backup_id) ⇒ void
Instructs barman to delete a specific backup.
-
#initialize(path_to_binary = nil, path_to_barman_home = nil) ⇒ CliCommand
constructor
Creates a new instance of CliCommand.
-
#parse_backup_info_file(backup) ⇒ void
Assigns various values to a Backup by parsing the according “backup.info”.
-
#parse_backup_list(lines) ⇒ Array<Backup>
Creates an array of Backup by parsing lines reported by barman.
-
#parse_check_lines(server, lines) ⇒ Object
Parses lines reported by barman’s ‘check` and assigns according values.
-
#parse_list_server_lines(lines) ⇒ Array<String>
Parses lines reported by barman’s ‘list-server`.
-
#parse_show_server_lines(server, lines) ⇒ Server
Creates a Server object by parsing lines reported by barman’s ‘show-server`.
-
#parse_wal_files_list(lines) ⇒ WalFiles
Creates a WalFiles object by parsing lines reported by barman.
-
#recover(server, backup_id, path, opts = {}) ⇒ void
Instructs barman to recover a backup.
-
#server(name, opts = {}) ⇒ Server
Instructs barman to get information about a server.
-
#servers(opts = {}) ⇒ Servers
Instructs barman to get information about all servers.
-
#wal_files(server, backup_id) ⇒ WalFiles
Instructs barman to list all wal files for a specific backup id.
Constructor Details
#initialize(path_to_binary = nil, path_to_barman_home = nil) ⇒ CliCommand
Creates a new instance of CliCommand
28 29 30 31 |
# File 'lib/rbarman/cli_command.rb', line 28 def initialize(path_to_binary=nil, =nil) self.binary=path_to_binary || Configuration.instance.binary self.= || Configuration.instance. end |
Instance Attribute Details
#barman_home ⇒ String #barman_home= ⇒ Object
23 24 25 |
# File 'lib/rbarman/cli_command.rb', line 23 def @barman_home end |
#binary ⇒ String #binary= ⇒ Object
15 16 17 |
# File 'lib/rbarman/cli_command.rb', line 15 def binary @binary end |
Class Method Details
.size_in_bytes(size, unit) ⇒ Integer
Converts the size according to the unit to bytes
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/rbarman/cli_command.rb', line 275 def self.size_in_bytes(size, unit) raise(ArgumentError, "unit not one of B|KiB|MiB|GiB|TiB") if !unit.match(/(B|KiB|MiB|GiB|TiB)/) size_b = 0 case unit when "B" size_b = size when "KiB" size_b = size * 1024 when "MiB" size_b = size * 1024 ** 2 when "GiB" size_b = size * 1024 ** 3 when "TiB" size_b = size * 1024 ** 4 end return size_b.to_i end |
Instance Method Details
#backup(server, backup_id, opts = {}) ⇒ Backup
Instructs barman to get information about a specific backup
51 52 53 54 55 |
# File 'lib/rbarman/cli_command.rb', line 51 def backup(server, backup_id, opts = {}) raise(ArgumentError, "backup id must not be nil!") if backup_id.nil? opts[:backup_id] = backup_id return backups(server, opts)[0] end |
#backups(server, opts = {}) ⇒ Backups
Instructs barman to get information about backups
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rbarman/cli_command.rb', line 63 def backups(server, opts = {}) list = ("list-backup #{server}") list = list.grep(/#{opts[:backup_id]}/) if !opts[:backup_id].nil? backups = Backups.new(parse_backup_list(list)) backups.each do |backup| parse_backup_info_file(backup) if opts[:with_wal_files] wals = wal_files(backup.server, backup.id) # using exact wal file size from xlog.db. show-backup is too inaccurate backup.wal_files = wals backup.wal_file_size = 0 wals.each { |w| backup.wal_file_size += w.size } end end return backups end |
#create(server) ⇒ void
This method returns an undefined value.
Instructs barman to create a backup
304 305 306 |
# File 'lib/rbarman/cli_command.rb', line 304 def create(server) ("backup #{server}") end |
#create_recovery_cmd_args(opts = {}) ⇒ String
Creates an argument string for barman recovery command based on opts Hash
335 336 337 338 339 340 341 342 |
# File 'lib/rbarman/cli_command.rb', line 335 def create_recovery_cmd_args(opts={}) args = Array.new args << "--remote-ssh-command='#{opts[:remote_ssh_cmd]}'" if opts[:remote_ssh_cmd] args << "--target-time '#{opts[:target_time].to_s}'" if opts[:target_time] args << "--target-xid #{opts[:target_xid]}" if opts[:target_xid] args << "--exclusive" if opts[:exclusive] return args.join(" ") end |
#delete(server, backup_id) ⇒ void
This method returns an undefined value.
Instructs barman to delete a specific backup
297 298 299 |
# File 'lib/rbarman/cli_command.rb', line 297 def delete(server, backup_id) ("delete #{server} #{backup_id}") end |
#parse_backup_info_file(backup) ⇒ void
This method returns an undefined value.
Assigns various values to a Backup by parsing the according “backup.info”
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/rbarman/cli_command.rb', line 230 def parse_backup_info_file(backup) raise(ArgumentError, "arg not of type Backup") if !backup.is_a? Backup raise(ArgumentError, "Backup.id not set") if backup.id.nil? raise(ArgumentError, "Backup.server not set") if backup.server.nil? backup_info = file_content("#{}/#{backup.server}/base/#{backup.id}/backup.info") backup_info.each do |l| key, value = l.split("=") case key when "status" backup.status = value.downcase.to_sym end unless value == 'None' case key when "begin_time" backup.backup_start = value when "end_time" backup.backup_end = value when "size" backup.size = value.to_i when "timeline" backup.timeline = value.to_i when "begin_wal" backup.begin_wal = WalFile.parse(value) when "end_wal" backup.end_wal = WalFile.parse(value) when "pgdata" backup.pgdata = value when "version" backup.pg_version = value.to_i when "error" backup.error = value end end end end |
#parse_backup_list(lines) ⇒ Array<Backup>
Creates an array of Backup by parsing lines reported by barman
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/rbarman/cli_command.rb', line 203 def parse_backup_list(lines) result = Array.new lines.each do |l| match = l.match(/^(.+)\s(\d+T\d+)/) b = Backup.new b.server = match[1] b.id = match[2] status_match = l.match(/.+(FAILED|STARTED)/) status_match.nil? ? b.status = :done : b.status = status_match[1].downcase.to_sym if b.status == :done sizematch = l.match(/.+Size:\s(\S+)\s(\S+)\s-.+Size:\s(\S+)\s(\S+)/) b.size = CliCommand.size_in_bytes(sizematch[1].to_f, sizematch[2]) b.wal_file_size = CliCommand.size_in_bytes(sizematch[3].to_f, sizematch[4]) end result << b end return result end |
#parse_check_lines(server, lines) ⇒ Object
Parses lines reported by barman’s ‘check` and assigns according values
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/rbarman/cli_command.rb', line 176 def parse_check_lines(server, lines) raise(ArgumentError, 'arg server not of type Server') if !server.is_a? Server lines.each do |l| key, value = l.gsub("\t","").split(": ") case key.chomp when "ssh" server.ssh_check_ok = value =~ /OK/ ? true : false when "PostgreSQL" server.pg_conn_ok = value == "OK" ? true : false end end end |
#parse_list_server_lines(lines) ⇒ Array<String>
Parses lines reported by barman’s ‘list-server`
135 136 137 138 139 140 141 |
# File 'lib/rbarman/cli_command.rb', line 135 def parse_list_server_lines(lines) result = Array.new lines.each do |l| result << l.split("-")[0].strip end return result end |
#parse_show_server_lines(server, lines) ⇒ Server
Creates a Server object by parsing lines reported by barman’s ‘show-server`
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/rbarman/cli_command.rb', line 148 def parse_show_server_lines(server, lines) s = Server.new(server) lines.each do |l| key, value = l.gsub("\t","").split(": ") case key.chomp when "active" s.active = value.to_bool when "ssh_command" s.ssh_cmd = value when "conninfo" s.conn_info = value when "backup_directory" s.backup_dir = value when "basebackups_directory" s.base_backups_dir = value when "wals_directory" s.wals_dir = value when "server_txt_version" s.pg_version = value end end return s end |
#parse_wal_files_list(lines) ⇒ WalFiles
Creates a WalFiles object by parsing lines reported by barman
192 193 194 195 196 197 198 |
# File 'lib/rbarman/cli_command.rb', line 192 def parse_wal_files_list(lines) wal_files = Array.new lines.each do |line| wal_files << WalFile.parse(line.split("/").last) end return wal_files end |
#recover(server, backup_id, path, opts = {}) ⇒ void
when :remote_ssh_cmd is passed in options, ‘path’ is the path on the remote host, otherwise local
This method returns an undefined value.
Instructs barman to recover a backup
322 323 324 325 |
# File 'lib/rbarman/cli_command.rb', line 322 def recover(server, backup_id, path, opts={}) args = create_recovery_cmd_args(opts) ("recover #{args} #{server} #{backup_id} #{path}") end |
#server(name, opts = {}) ⇒ Server
Instructs barman to get information about a server
87 88 89 90 91 92 93 94 |
# File 'lib/rbarman/cli_command.rb', line 87 def server(name, opts = {}) lines = ("show-server #{name}") server = parse_show_server_lines(name, lines) lines = ("check #{name}", { :abort_on_error => false }) parse_check_lines(server, lines) server.backups = backups(server.name, opts) if opts[:with_backups] return server end |
#servers(opts = {}) ⇒ Servers
Instructs barman to get information about all servers
101 102 103 104 105 106 107 108 109 |
# File 'lib/rbarman/cli_command.rb', line 101 def servers(opts = {}) result = Servers.new lines = ("list-server") server_names = parse_list_server_lines(lines) server_names.each do |name| result << server(name, opts) end return result end |
#wal_files(server, backup_id) ⇒ WalFiles
Instructs barman to list all wal files for a specific backup id
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rbarman/cli_command.rb', line 117 def wal_files(server, backup_id) lines = ("list-files --target wal #{server} #{backup_id}") wal_files = parse_wal_files_list(lines) xlog_db = read_xlog_db(server) wal_files.each do |w| wal = "#{w.timeline}#{w.xlog}#{w.segment}" entry = xlog_db[wal] w.size = entry[:size] w.compression = entry[:compression] w.created = entry[:created].to_i end return wal_files end |