Class: Backup::Database::PostgreSQL

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

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Attributes inherited from Base

#database_id, #dump_path, #model

Instance Method Summary collapse

Methods included from Config::Helpers

included

Methods included from Utilities::Helpers

#utility_remote

Constructor Details

#initialize(model, database_id = nil, &block) ⇒ PostgreSQL

Returns a new instance of PostgreSQL.



40
41
42
43
44
45
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Backup::Config::Helpers

Instance Attribute Details

#additional_optionsObject

Additional “pg_dump” or “pg_dumpall” options



38
39
40
# File 'lib/backup/database/postgresql.rb', line 38

def additional_options
  @additional_options
end

#hostObject

Connectivity options



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

def host
  @host
end

#nameObject

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`.



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

def name
  @name
end

#only_tablesObject

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



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

def only_tables
  @only_tables
end

#passwordObject

Credentials for the specified database



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

def password
  @password
end

#portObject

Connectivity options



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

def port
  @port
end

#skip_tablesObject

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



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

def skip_tables
  @skip_tables
end

#socketObject

Connectivity options



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

def socket
  @socket
end

#sudo_userObject

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



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

def sudo_user
  @sudo_user
end

#usernameObject

Credentials for the specified database



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

def username
  @username
end

Instance Method Details

#connectivity_optionsObject



102
103
104
105
106
107
108
109
# 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?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/backup/database/postgresql.rb', line 127

def dump_all?
  name == :all
end

#password_optionObject



90
91
92
# File 'lib/backup/database/postgresql.rb', line 90

def password_option
  "PGPASSWORD=#{ Shellwords.escape(password) } " if password
end

#perform!Object

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

<trigger>/databases/PostgreSQL[-<database_id>].sql[.gz]


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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

#pgdumpObject



76
77
78
79
80
81
# 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

#pgdumpallObject



83
84
85
86
87
88
# 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_optionObject



94
95
96
# File 'lib/backup/database/postgresql.rb', line 94

def sudo_option
  "#{ utility(:sudo) } -n -H -u #{ sudo_user } " if sudo_user
end

#tables_to_dumpObject



115
116
117
118
119
# 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_skipObject



121
122
123
124
125
# 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_optionsObject



111
112
113
# File 'lib/backup/database/postgresql.rb', line 111

def user_options
  Array(additional_options).join(' ')
end

#username_optionObject



98
99
100
# File 'lib/backup/database/postgresql.rb', line 98

def username_option
  "--username=#{ Shellwords.escape(username) }" if username
end