class Backup::Database::PostgreSQL

Attributes

additional_options[RW]

Additional “pg_dump” or “pg_dumpall” options

host[RW]

Connectivity options

name[RW]

Name of the database that needs to get dumped. To dump all databases, set this to `:all` or leave blank. username must be a PostgreSQL superuser to run `pg_dumpall`.

only_tables[RW]

Tables to dump. This in only valid if `name` is specified. If none are given, the entire database will be dumped.

password[RW]

Credentials for the specified database

port[RW]

Connectivity options

skip_tables[RW]

Tables to skip while dumping the database. If `name` is set to :all (or not specified), these are ignored.

socket[RW]

Connectivity options

sudo_user[RW]

If set the pg_dump(all) command is executed as the given user

username[RW]

Credentials for the specified database

Public Class Methods

new(model, database_id = nil, &block) click to toggle source
Calls superclass method Backup::Database::Base::new
# File lib/backup/database/postgresql.rb, line 40
def initialize(model, database_id = nil, &block)
  super
  instance_eval(&block) if block_given?

  @name ||= :all
end

Public Instance Methods

connectivity_options() click to toggle source
# File lib/backup/database/postgresql.rb, line 102
def connectivity_options
  return "--host='#{ socket }'" if socket

  opts = []
  opts << "--host='#{ host }'" if host
  opts << "--port='#{ port }'" if port
  opts.join(' ')
end
dump_all?() click to toggle source
# File lib/backup/database/postgresql.rb, line 127
def dump_all?
  name == :all
end
password_option() click to toggle source
# File lib/backup/database/postgresql.rb, line 90
def password_option
  "PGPASSWORD=#{ Shellwords.escape(password) } " if password
end
perform!() click to toggle source

Performs the pgdump command and outputs the dump file in the dump_path using dump_filename.

<trigger>/databases/PostgreSQL[-<database_id>].sql[.gz]
Calls superclass method Backup::Database::Base#perform!
# File lib/backup/database/postgresql.rb, line 52
def perform!
  super

  pipeline = Pipeline.new
  dump_ext = 'sql'

  pipeline << (dump_all? ? pgdumpall : pgdump)

  model.compressor.compress_with do |command, ext|
    pipeline << command
    dump_ext << ext
  end if model.compressor

  pipeline << "#{ utility(:cat) } > " +
      "'#{ File.join(dump_path, dump_filename) }.#{ dump_ext }'"

  pipeline.run
  if pipeline.success?
    log!(:finished)
  else
    raise Error, "Dump Failed!\n" + pipeline.error_messages
  end
end
pgdump() click to toggle source
# File lib/backup/database/postgresql.rb, line 76
def pgdump
  "#{ password_option }" +
  "#{ sudo_option }" +
  "#{ utility(:pg_dump) } #{ username_option } #{ connectivity_options } " +
  "#{ user_options } #{ tables_to_dump } #{ tables_to_skip } #{ name }"
end
pgdumpall() click to toggle source
# File lib/backup/database/postgresql.rb, line 83
def pgdumpall
  "#{ password_option }" +
  "#{ sudo_option }" +
  "#{ utility(:pg_dumpall) } #{ username_option } " +
  "#{ connectivity_options } #{ user_options }"
end
sudo_option() click to toggle source
# File lib/backup/database/postgresql.rb, line 94
def sudo_option
  "#{ utility(:sudo) } -n -H -u #{ sudo_user } " if sudo_user
end
tables_to_dump() click to toggle source
# File lib/backup/database/postgresql.rb, line 115
def tables_to_dump
  Array(only_tables).map do |table|
    "--table='#{ table }'"
  end.join(' ')
end
tables_to_skip() click to toggle source
# File lib/backup/database/postgresql.rb, line 121
def tables_to_skip
  Array(skip_tables).map do |table|
    "--exclude-table='#{ table }'"
  end.join(' ')
end
user_options() click to toggle source
# File lib/backup/database/postgresql.rb, line 111
def user_options
  Array(additional_options).join(' ')
end
username_option() click to toggle source
# File lib/backup/database/postgresql.rb, line 98
def username_option
  "--username=#{ Shellwords.escape(username) }" if username
end