class RBarman::Backup

Represents a barman Backup

Attributes

backup_end[R]

@overload backup_end

@return [Time, nil] time when the backup stopped.

@overload backup_end=

End of the backup
@param [#to_s] b_end time when the backup stopped
backup_start[R]

@overload backup_start

@return [Time, nil] time when the backup started.

@overload backup_start=

Start of the backup 
@param [#to_s] start time when the backup started
begin_wal[R]

@overload begin_wal

@return [WalFile, nil] first wal file after backup started

@overload begin_wal=

First wal file after backup started
@param [String,WalFile] wal_file the wal file
deleted[R]

@return [Boolean] if the backup has been deleted

end_wal[R]

@overload end_wal

@return [WalFile, nil] last wal file after backup stopped

@overload end_wal=

Last wal file after backup stopped
@param [String,WalFile] wal_file the wal file
error[R]

@overload error

@return [String, nil] error message

@overload error=

Contains the error message if backup failed
@param [String] the error
id[R]

@overload id

@return [String, nil] id (like '20130304T080002') which identifies 
  the backup and is unique among all backups of a server.

@overload id=

Id of the backup
@param [#to_s] id id of the backup
@raise [InvalidBackupIdError] if the id is not valid
@see backup_id_valid?
pg_version[RW]

@return [Integer, nil] the backup's PostgreSQL version

pgdata[R]

@overload pgdata

@return [String, nil] server's data directory

@overload pgdata=

Server's data directory
@param [String] path path to directory
@raise [ArgumentError] if path is empty
server[RW]

@return [String, nil] name of the server to which the backup belongs.

size[R]

@overload size

@return [Integer, nil] size of data (in bytes)

@overload size=

Size of data (in bytes)
@param [#to_i] size size (in bytes
status[R]

@overload status

@return [Symbol, nil] status of the backup, `:empty`, `:started`, `:done` or `:failed`

@overload status=

Status of the backup
@param [Symbol] status status of the backup 
@raise [ArgumentError] if status is not one of `:empty`, `:started`, `:done` or `:failed`
timeline[R]

@overload timeline

@return [Integer, nil] timeline of the backup.

@overload timeline=

Timeline of the backup
@param [Integer] i timeline of the backup
@raise [ArgumentError] if timeline == 0
wal_file_size[R]

@overload wal_file_size

@return [Integer, nil] size of wal files (in bytes)

@overload wal_file_size=

Size of wal files (in bytes)
@param [#to_i] size size of wal files (in bytes)
wal_files[R]

@overload wal_files

@return [WalFiles, nil] All wal files

@overload wal_files=

All wal files
@param [Array, WalFiles] wal_files all wal files
@raise [ArgumentError] if argument is not an Array

Public Class Methods

backup_id_valid?(id) click to toggle source

@return [Boolean] if id is a valid backup id @param [#to_s] id the backup id @example Check if a backup id is valid

Backup.backup_id_valid?("20130304T080002") #=> true
Backup.backup_id_valid?("213") #=> false
# File lib/rbarman/backup.rb, line 197
def self.backup_id_valid?(id)
  return false if id.nil?
  return !id.to_s.match(/\d{8,8}T\d{6,6}/).nil?
end
by_id(server, backup_id, opts = {}) click to toggle source

Get a specific backup @param [String] server server name @param [String] backup_id id of the backup @param [Hash] opts options for creating a {Backup} @option opts [Boolean] :with_wal_files whether to include {WalFiles} in {Backup} @return [Backup] the backup

# File lib/rbarman/backup.rb, line 296
def self.by_id(server, backup_id, opts = {})
  cmd = CliCommand.new
  return cmd.backup(server, backup_id, opts)
end
create(server) click to toggle source

Instructs the underlying (barman) command to create a new backup. @param [String] server server name for which a backup should be created @return [Backup] a new backup object with wal files

# File lib/rbarman/backup.rb, line 283
def self.create(server)
  cmd = CliCommand.new
  cmd.create(server)
  backups = Backups.all(server, { :with_wal_files => false })
  return Backup.by_id(server, backups.latest.id, { :with_wal_files => true })
end
new() click to toggle source
# File lib/rbarman/backup.rb, line 114
def initialize
  @deleted = false
end

Public Instance Methods

add_wal_file(wal_file) click to toggle source

Adds a wal file to the backup @param [String, WalFile] wal_file the wal file @return [void]

# File lib/rbarman/backup.rb, line 180
def add_wal_file(wal_file)
  @wal_files = WalFiles.new if @wal_files.nil?
  @wal_files << WalFile.parse(wal_file)
end
backup_end=(b_end) click to toggle source
# File lib/rbarman/backup.rb, line 127
def backup_end=(b_end)
  @backup_end = Time.parse(b_end.to_s)
end
backup_start=(start) click to toggle source
# File lib/rbarman/backup.rb, line 123
def backup_start=(start)
  @backup_start = Time.parse(start.to_s)
end
begin_wal=(wal_file) click to toggle source
# File lib/rbarman/backup.rb, line 151
def begin_wal=(wal_file)
  @begin_wal = WalFile.parse(wal_file)
end
delete() click to toggle source

Instructs the underlying (barman) command to delete the backup and sets its flag {#deleted} to true

# File lib/rbarman/backup.rb, line 203
def delete
  cmd = CliCommand.new
  cmd.delete(@server, @id)
  @deleted = true
end
end_wal=(wal_file) click to toggle source
# File lib/rbarman/backup.rb, line 155
def end_wal=(wal_file)
  @end_wal = WalFile.parse(wal_file)
end
error=(error) click to toggle source
# File lib/rbarman/backup.rb, line 173
def error=(error)
  @error = error
end
id=(id) click to toggle source
# File lib/rbarman/backup.rb, line 118
def id=(id)
  raise InvalidBackupIdError if !Backup.backup_id_valid?(id.to_s)
  @id = id.to_s
end
missing_wal_files() click to toggle source

@return [WalFiles] all wal files which don't exist in this backup

# File lib/rbarman/backup.rb, line 248
def missing_wal_files
  missing = Array.new
  needed_wal_files.each do |needed|
    existing = nil
    @wal_files.each do |f|
      if f == needed
        existing = f
        break
      end
    end
    missing << needed unless existing 
  end
  WalFiles.new(missing)
end
needed_wal_files() click to toggle source

@return [WalFiles] all wal files which should exist in this backup

# File lib/rbarman/backup.rb, line 221
def needed_wal_files
  needed = Array.new
  xlog_range.each do |xlog|
    start = 0
    if @begin_wal.xlog == xlog.to_s.rjust(8,'0')
      start = @begin_wal.segment.to_i(16)
    end

    # http://wiki.postgresql.org/wiki/What's_new_in_PostgreSQL_9.3#WAL_filenames_may_end_in_FF
    end_segment = 255
    if @pg_version.nil? || @pg_version < 90300
      end_segment = 254
    end

    (start..end_segment).each do |seg|
      n_timeline = @begin_wal.timeline
      n_xlog = xlog.rjust(8,'0')
      n_segment = seg.to_s(16).rjust(8,'0').upcase
      w = WalFile.parse(n_timeline + n_xlog + n_segment)
      needed << w
      break if w == @wal_files.last
    end
  end
  WalFiles.new(needed)
end
pgdata=(path) click to toggle source
# File lib/rbarman/backup.rb, line 168
def pgdata=(path)
  raise(ArgumentError, "path is empty") if path == ""
  @pgdata = path
end
recover(path, opts = {}) click to toggle source

Instructs the underlying (barman) command to recover this backup @param [String] path the path to which the backup should be restored @param [Hash] opts options passed as arguments to barman recover cmd @option opts [String] :remote_ssh_cmd the ssh command to be used for remote recovery @option opts [String, Time] :target_time the timestamp as recovery target @option opts [String] :target_xid the transaction ID as recovery target @option opts [Boolean] :exclusive whether to stop immediately before or after the recovery target @return [void] @note when :remote_ssh_cmd is passed in options, 'path' is the path on the remote host, otherwise local @since 0.0.3 @example

backup.recover('/var/lib/postgresql/9.2/main', { :remote_ssh_cmd => 'ssh postgres@10.20.20.2' })
# File lib/rbarman/backup.rb, line 275
def recover(path, opts = {})
  cmd = CliCommand.new
  cmd.recover(@server, @id, path, opts)
end
size=(size) click to toggle source
# File lib/rbarman/backup.rb, line 147
def size=(size)
  @size = size.to_i
end
status=(status) click to toggle source
# File lib/rbarman/backup.rb, line 131
def status=(status)
  if status != :empty and
    status != :started and
    status != :done and
    status != :failed
    raise(ArgumentError,"only :empty, :started, :done or :failed allowed!")
  end

  @status = status
end
timeline=(i) click to toggle source
# File lib/rbarman/backup.rb, line 163
def timeline=(i)
  raise(ArgumentError, "timeline should be > 0") if i.to_i == 0
  @timeline = i.to_i
end
wal_file_already_added?(wal_file) click to toggle source

@return [Boolean] if the wal file is already added to the backup @param [String, WalFile] wal_file the wal file

# File lib/rbarman/backup.rb, line 187
def wal_file_already_added?(wal_file)
  return false if @wal_files.nil?
  return @wal_files.include?(WalFile.parse(wal_file))
end
wal_file_size=(size) click to toggle source
# File lib/rbarman/backup.rb, line 159
def wal_file_size=(size)
  @wal_file_size = size.to_i
end
wal_files=(wal_files) click to toggle source
# File lib/rbarman/backup.rb, line 142
def wal_files=(wal_files)
  raise(ArgumentError, "argument not of type array") if !wal_files.is_a? Array
  @wal_files = wal_files
end
xlog_range() click to toggle source

@return [Array, String] a range of available xlog entries

# File lib/rbarman/backup.rb, line 210
def xlog_range
  start_xlog = @begin_wal.xlog
  end_xlog = @wal_files.last.xlog
  xlog_range = Array.new
  (start_xlog.to_i(16)..end_xlog.to_i(16)).to_a.each do |i|
    xlog_range << i.to_s(16).upcase
  end
  xlog_range
end