Class: RBarman::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/rbarman/backup.rb

Overview

Represents a barman Backup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBackup

Returns a new instance of Backup.



114
115
116
# File 'lib/rbarman/backup.rb', line 114

def initialize
  @deleted = false
end

Instance Attribute Details

#backup_endTime? #backup_end=Object

Overloads:

  • #backup_endTime?

    Returns time when the backup stopped.

    Returns:

    • (Time, nil)

      time when the backup stopped.

  • #backup_end=Object

    End of the backup

    Parameters:

    • b_end (#to_s)

      time when the backup stopped



41
42
43
# File 'lib/rbarman/backup.rb', line 41

def backup_end
  @backup_end
end

#backup_startTime? #backup_start=Object

Overloads:

  • #backup_startTime?

    Returns time when the backup started.

    Returns:

    • (Time, nil)

      time when the backup started.

  • #backup_start=Object

    Start of the backup

    Parameters:

    • start (#to_s)

      time when the backup started



34
35
36
# File 'lib/rbarman/backup.rb', line 34

def backup_start
  @backup_start
end

#begin_walWalFile? #begin_wal=Object

Overloads:

  • #begin_walWalFile?

    Returns first wal file after backup started.

    Returns:

    • (WalFile, nil)

      first wal file after backup started

  • #begin_wal=Object

    First wal file after backup started

    Parameters:



78
79
80
# File 'lib/rbarman/backup.rb', line 78

def begin_wal
  @begin_wal
end

#deletedBoolean (readonly)

Returns if the backup has been deleted.

Returns:

  • (Boolean)

    if the backup has been deleted



112
113
114
# File 'lib/rbarman/backup.rb', line 112

def deleted
  @deleted
end

#end_walWalFile? #end_wal=Object

Overloads:

  • #end_walWalFile?

    Returns last wal file after backup stopped.

    Returns:

    • (WalFile, nil)

      last wal file after backup stopped

  • #end_wal=Object

    Last wal file after backup stopped

    Parameters:



85
86
87
# File 'lib/rbarman/backup.rb', line 85

def end_wal
  @end_wal
end

#errorString? #error=Object

Overloads:

  • #errorString?

    Returns error message.

    Returns:

    • (String, nil)

      error message

  • #error=Object

    Contains the error message if backup failed

    Parameters:



108
109
110
# File 'lib/rbarman/backup.rb', line 108

def error
  @error
end

#idString? #id=Object

Overloads:

  • #idString?

    Returns id (like ‘20130304T080002’) which identifies the backup and is unique among all backups of a server.

    Returns:

    • (String, nil)

      id (like ‘20130304T080002’) which identifies the backup and is unique among all backups of a server.

  • #id=Object

    Id of the backup

    Parameters:

    • id (#to_s)

      id of the backup

    Raises:

    See Also:



27
28
29
# File 'lib/rbarman/backup.rb', line 27

def id
  @id
end

#pg_versionInteger?

Returns the backup’s PostgreSQL version.

Returns:

  • (Integer, nil)

    the backup’s PostgreSQL version



17
18
19
# File 'lib/rbarman/backup.rb', line 17

def pg_version
  @pg_version
end

#pgdataString? #pgdata=Object

Overloads:

  • #pgdataString?

    Returns server’s data directory.

    Returns:

    • (String, nil)

      server’s data directory

  • #pgdata=Object

    Server’s data directory

    Parameters:

    • path (String)

      path to directory

    Raises:

    • (ArgumentError)

      if path is empty



101
102
103
# File 'lib/rbarman/backup.rb', line 101

def pgdata
  @pgdata
end

#serverString?

Returns name of the server to which the backup belongs.

Returns:

  • (String, nil)

    name of the server to which the backup belongs.



14
15
16
# File 'lib/rbarman/backup.rb', line 14

def server
  @server
end

#sizeInteger? #size=Object

Overloads:

  • #sizeInteger?

    Returns size of data (in bytes).

    Returns:

    • (Integer, nil)

      size of data (in bytes)

  • #size=Object

    Size of data (in bytes)

    Parameters:

    • size (#to_i)

      size (in bytes



64
65
66
# File 'lib/rbarman/backup.rb', line 64

def size
  @size
end

#statusSymbol? #status=Object

Overloads:

  • #statusSymbol?

    Returns status of the backup, ‘:empty`, `:started`, `:done` or `:failed`.

    Returns:

    • (Symbol, nil)

      status of the backup, ‘:empty`, `:started`, `:done` or `:failed`

  • #status=Object

    Status of the backup

    Parameters:

    • status (Symbol)

      status of the backup

    Raises:

    • (ArgumentError)

      if status is not one of ‘:empty`, `:started`, `:done` or `:failed`



49
50
51
# File 'lib/rbarman/backup.rb', line 49

def status
  @status
end

#timelineInteger? #timeline=Object

Overloads:

  • #timelineInteger?

    Returns timeline of the backup.

    Returns:

    • (Integer, nil)

      timeline of the backup.

  • #timeline=Object

    Timeline of the backup

    Parameters:

    • i (Integer)

      timeline of the backup

    Raises:

    • (ArgumentError)

      if timeline == 0



93
94
95
# File 'lib/rbarman/backup.rb', line 93

def timeline
  @timeline
end

#wal_file_sizeInteger? #wal_file_size=Object

Overloads:

  • #wal_file_sizeInteger?

    Returns size of wal files (in bytes).

    Returns:

    • (Integer, nil)

      size of wal files (in bytes)

  • #wal_file_size=Object

    Size of wal files (in bytes)

    Parameters:

    • size (#to_i)

      size of wal files (in bytes)



71
72
73
# File 'lib/rbarman/backup.rb', line 71

def wal_file_size
  @wal_file_size
end

#wal_filesWalFiles? #wal_files=Object

Overloads:

  • #wal_filesWalFiles?

    Returns All wal files.

    Returns:

  • #wal_files=Object

    All wal files

    Parameters:

    • wal_files (Array, WalFiles)

      all wal files

    Raises:

    • (ArgumentError)

      if argument is not an Array



57
58
59
# File 'lib/rbarman/backup.rb', line 57

def wal_files
  @wal_files
end

Class Method Details

.backup_id_valid?(id) ⇒ Boolean

Returns if id is a valid backup id.

Examples:

Check if a backup id is valid

Backup.backup_id_valid?("20130304T080002") #=> true
Backup.backup_id_valid?("213") #=> false

Parameters:

  • id (#to_s)

    the backup id

Returns:

  • (Boolean)

    if id is a valid backup id



197
198
199
200
# 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 = {}) ⇒ Backup

Get a specific backup

Parameters:

  • server (String)

    server name

  • backup_id (String)

    id of the backup

  • opts (Hash) (defaults to: {})

    options for creating a RBarman::Backup

Options Hash (opts):

Returns:



296
297
298
299
# 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) ⇒ Backup

Instructs the underlying (barman) command to create a new backup.

Parameters:

  • server (String)

    server name for which a backup should be created

Returns:

  • (Backup)

    a new backup object with wal files



283
284
285
286
287
288
# 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

Instance Method Details

#add_wal_file(wal_file) ⇒ void

This method returns an undefined value.

Adds a wal file to the backup

Parameters:



180
181
182
183
# 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

#deleteObject

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



203
204
205
206
207
# File 'lib/rbarman/backup.rb', line 203

def delete
  cmd = CliCommand.new
  cmd.delete(@server, @id)
  @deleted = true
end

#missing_wal_filesWalFiles

Returns all wal files which don’t exist in this backup.

Returns:

  • (WalFiles)

    all wal files which don’t exist in this backup



248
249
250
251
252
253
254
255
256
257
258
259
260
261
# 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_filesWalFiles

Returns all wal files which should exist in this backup.

Returns:

  • (WalFiles)

    all wal files which should exist in this backup



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# 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

#recover(path, opts = {}) ⇒ void

Note:

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 the underlying (barman) command to recover this backup

Examples:

backup.recover('/var/lib/postgresql/9.2/main', { :remote_ssh_cmd => 'ssh [email protected]' })

Parameters:

  • path (String)

    the path to which the backup should be restored

  • opts (Hash) (defaults to: {})

    options passed as arguments to barman recover cmd

Options Hash (opts):

  • :remote_ssh_cmd (String)

    the ssh command to be used for remote recovery

  • :target_time (String, Time)

    the timestamp as recovery target

  • :target_xid (String)

    the transaction ID as recovery target

  • :exclusive (Boolean)

    whether to stop immediately before or after the recovery target

Since:

  • 0.0.3



275
276
277
278
# File 'lib/rbarman/backup.rb', line 275

def recover(path, opts = {})
  cmd = CliCommand.new
  cmd.recover(@server, @id, path, opts)
end

#wal_file_already_added?(wal_file) ⇒ Boolean

Returns if the wal file is already added to the backup.

Parameters:

Returns:

  • (Boolean)

    if the wal file is already added to the backup



187
188
189
190
# 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

#xlog_rangeArray, String

Returns a range of available xlog entries.

Returns:

  • (Array, String)

    a range of available xlog entries



210
211
212
213
214
215
216
217
218
# 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