Class: Backup::Database::PostgreSQL

Inherits:
Base
  • Object
show all
Defined in:
lib/backup/database/postgresql.rb

Constant Summary

Constants included from CLI::Helpers

CLI::Helpers::UTILITY

Instance Attribute Summary collapse

Attributes inherited from Base

#utility_path

Instance Method Summary collapse

Methods included from Configuration::Helpers

#clear_defaults!, #load_defaults!

Methods included from CLI::Helpers

#command_name, #raise_if_command_failed!, #run, #utility

Constructor Details

#initialize(model, &block) ⇒ PostgreSQL

Creates a new instance of the PostgreSQL adapter object Sets the PGPASSWORD environment variable to the password so it doesn’t prompt and hang in the process



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/backup/database/postgresql.rb', line 39

def initialize(model, &block)
  super(model)

  @skip_tables        ||= Array.new
  @only_tables        ||= Array.new
  @additional_options ||= Array.new

  instance_eval(&block) if block_given?

  if @utility_path
    Logger.warn "[DEPRECATED] " +
      "Database::PostgreSQL#utility_path has been deprecated.\n" +
      "  Use Database::PostgreSQL#pg_dump_utility instead."
    @pg_dump_utility ||= @utility_path
  end
  @pg_dump_utility ||= utility(:pg_dump)
end

Instance Attribute Details

#additional_optionsObject

Additional “pg_dump” options



29
30
31
# File 'lib/backup/database/postgresql.rb', line 29

def additional_options
  @additional_options
end

#hostObject

Connectivity options



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

def host
  @host
end

#nameObject

Name of the database that needs to get dumped



9
10
11
# File 'lib/backup/database/postgresql.rb', line 9

def name
  @name
end

#only_tablesObject

Tables to dump, tables that aren’t specified won’t get dumped



25
26
27
# File 'lib/backup/database/postgresql.rb', line 25

def only_tables
  @only_tables
end

#passwordObject

Credentials for the specified database



13
14
15
# File 'lib/backup/database/postgresql.rb', line 13

def password
  @password
end

#pg_dump_utilityObject

Path to pg_dump utility (optional)



33
34
35
# File 'lib/backup/database/postgresql.rb', line 33

def pg_dump_utility
  @pg_dump_utility
end

#portObject

Connectivity options



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

def port
  @port
end

#skip_tablesObject

Tables to skip while dumping the database



21
22
23
# File 'lib/backup/database/postgresql.rb', line 21

def skip_tables
  @skip_tables
end

#socketObject

Connectivity options



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

def socket
  @socket
end

#usernameObject

Credentials for the specified database



13
14
15
# File 'lib/backup/database/postgresql.rb', line 13

def username
  @username
end

Instance Method Details

#connectivity_optionsObject

Builds the PostgreSQL connectivity options syntax to connect the user to perform the database dumping process, socket gets gsub’d to host since that’s the option PostgreSQL takes for socket connections as well. In case both the host and the socket are specified, the socket will take priority over the host



112
113
114
115
116
117
# File 'lib/backup/database/postgresql.rb', line 112

def connectivity_options
  %w[host port socket].map do |option|
    next if send(option).to_s.empty?
    "--#{option}='#{send(option)}'".gsub('--socket=', '--host=')
  end.compact.join(' ')
end

#password_optionsObject

Builds the password syntax PostgreSQL uses to authenticate the user to perform database dumping



96
97
98
# File 'lib/backup/database/postgresql.rb', line 96

def password_options
  password.to_s.empty? ? '' : "PGPASSWORD='#{password}' "
end

#perform!Object

Performs the pgdump command and outputs the data to the specified path based on the ‘trigger’



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/backup/database/postgresql.rb', line 60

def perform!
  super

  pipeline = Pipeline.new
  dump_ext = 'sql'

  pipeline << pgdump
  if @model.compressor
    @model.compressor.compress_with do |command, ext|
      pipeline << command
      dump_ext << ext
    end
  end
  pipeline << "cat > '#{ File.join(@dump_path, name) }.#{ dump_ext }'"

  pipeline.run
  if pipeline.success?
    Logger.message "#{ database_name } Complete!"
  else
    raise Errors::Database::PipelineError,
        "#{ database_name } Dump Failed!\n" +
        pipeline.error_messages
  end
end

#pgdumpObject

Builds the full pgdump string based on all attributes



87
88
89
90
91
# File 'lib/backup/database/postgresql.rb', line 87

def pgdump
  "#{password_options}" +
  "#{ pg_dump_utility } #{ username_options } #{ connectivity_options } " +
  "#{ user_options } #{ tables_to_dump } #{ tables_to_skip } #{ name }"
end

#tables_to_dumpObject

Builds the PostgreSQL syntax for specifying which tables to dump during the dumping of the database



129
130
131
132
133
# File 'lib/backup/database/postgresql.rb', line 129

def tables_to_dump
  only_tables.map do |table|
    "--table='#{table}'"
  end.join(' ')
end

#tables_to_skipObject

Builds the PostgreSQL syntax for specifying which tables to skip during the dumping of the database



138
139
140
141
142
# File 'lib/backup/database/postgresql.rb', line 138

def tables_to_skip
  skip_tables.map do |table|
    "--exclude-table='#{table}'"
  end.join(' ')
end

#user_optionsObject

Builds a PostgreSQL compatible string for the additional options specified by the user



122
123
124
# File 'lib/backup/database/postgresql.rb', line 122

def user_options
  additional_options.join(' ')
end

#username_optionsObject

Builds the credentials PostgreSQL syntax to authenticate the user to perform the database dumping process



103
104
105
# File 'lib/backup/database/postgresql.rb', line 103

def username_options
  username.to_s.empty? ? '' : "--username='#{username}'"
end