Class: Backup::Database::PostgreSQL
- Defined in:
- lib/backup/database/postgresql.rb
Constant Summary
Constants included from CLI::Helpers
Instance Attribute Summary collapse
-
#additional_options ⇒ Object
Additional “pg_dump” options.
-
#host ⇒ Object
Connectivity options.
-
#name ⇒ Object
Name of the database that needs to get dumped.
-
#only_tables ⇒ Object
Tables to dump, tables that aren’t specified won’t get dumped.
-
#password ⇒ Object
Credentials for the specified database.
-
#pg_dump_utility ⇒ Object
Path to pg_dump utility (optional).
-
#port ⇒ Object
Connectivity options.
-
#skip_tables ⇒ Object
Tables to skip while dumping the database.
-
#socket ⇒ Object
Connectivity options.
-
#username ⇒ Object
Credentials for the specified database.
Attributes inherited from Base
Instance Method Summary collapse
-
#connectivity_options ⇒ Object
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.
-
#initialize(model, &block) ⇒ PostgreSQL
constructor
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.
-
#password_options ⇒ Object
Builds the password syntax PostgreSQL uses to authenticate the user to perform database dumping.
-
#perform! ⇒ Object
Performs the pgdump command and outputs the data to the specified path based on the ‘trigger’.
-
#pgdump ⇒ Object
Builds the full pgdump string based on all attributes.
-
#tables_to_dump ⇒ Object
Builds the PostgreSQL syntax for specifying which tables to dump during the dumping of the database.
-
#tables_to_skip ⇒ Object
Builds the PostgreSQL syntax for specifying which tables to skip during the dumping of the database.
-
#user_options ⇒ Object
Builds a PostgreSQL compatible string for the additional options specified by the user.
-
#username_options ⇒ Object
Builds the credentials PostgreSQL syntax to authenticate the user to perform the database dumping process.
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_options ⇒ Object
Additional “pg_dump” options
29 30 31 |
# File 'lib/backup/database/postgresql.rb', line 29 def @additional_options end |
#host ⇒ Object
Connectivity options
17 18 19 |
# File 'lib/backup/database/postgresql.rb', line 17 def host @host end |
#name ⇒ Object
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_tables ⇒ Object
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 |
#password ⇒ Object
Credentials for the specified database
13 14 15 |
# File 'lib/backup/database/postgresql.rb', line 13 def password @password end |
#pg_dump_utility ⇒ Object
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 |
#port ⇒ Object
Connectivity options
17 18 19 |
# File 'lib/backup/database/postgresql.rb', line 17 def port @port end |
#skip_tables ⇒ Object
Tables to skip while dumping the database
21 22 23 |
# File 'lib/backup/database/postgresql.rb', line 21 def skip_tables @skip_tables end |
#socket ⇒ Object
Connectivity options
17 18 19 |
# File 'lib/backup/database/postgresql.rb', line 17 def socket @socket end |
#username ⇒ Object
Credentials for the specified database
13 14 15 |
# File 'lib/backup/database/postgresql.rb', line 13 def username @username end |
Instance Method Details
#connectivity_options ⇒ Object
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 %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_options ⇒ Object
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.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. "#{ database_name } Complete!" else raise Errors::Database::PipelineError, "#{ database_name } Dump Failed!\n" + pipeline. end end |
#pgdump ⇒ Object
Builds the full pgdump string based on all attributes
87 88 89 90 91 |
# File 'lib/backup/database/postgresql.rb', line 87 def pgdump "#{}" + "#{ pg_dump_utility } #{ } #{ } " + "#{ } #{ tables_to_dump } #{ tables_to_skip } #{ name }" end |
#tables_to_dump ⇒ Object
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_skip ⇒ Object
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_options ⇒ Object
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 .join(' ') end |
#username_options ⇒ Object
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.to_s.empty? ? '' : "--username='#{username}'" end |