Class: Backup::Database::PostgreSQL
- Defined in:
- lib/backup/database/postgresql.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#additional_options ⇒ Object
Additional “pg_dump” or “pg_dumpall” options.
-
#host ⇒ Object
Connectivity options.
-
#name ⇒ Object
Name of the database that needs to get dumped.
-
#only_tables ⇒ Object
Tables to dump.
-
#password ⇒ Object
Credentials for the specified database.
-
#port ⇒ Object
Connectivity options.
-
#skip_tables ⇒ Object
Tables to skip while dumping the database.
-
#socket ⇒ Object
Connectivity options.
-
#sudo_user ⇒ Object
If set the pg_dump(all) command is executed as the given user.
-
#username ⇒ Object
Credentials for the specified database.
Attributes inherited from Base
#database_id, #dump_path, #model
Instance Method Summary collapse
- #connectivity_options ⇒ Object
- #dump_all? ⇒ Boolean
-
#initialize(model, database_id = nil, &block) ⇒ PostgreSQL
constructor
A new instance of PostgreSQL.
- #password_option ⇒ Object
-
#perform! ⇒ Object
Performs the pgdump command and outputs the dump file in the
dump_path
usingdump_filename
. - #pgdump ⇒ Object
- #pgdumpall ⇒ Object
- #sudo_option ⇒ Object
- #tables_to_dump ⇒ Object
- #tables_to_skip ⇒ Object
- #user_options ⇒ Object
- #username_option ⇒ Object
Methods included from Config::Helpers
Methods included from Utilities::Helpers
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_options ⇒ Object
Additional “pg_dump” or “pg_dumpall” options
38 39 40 |
# File 'lib/backup/database/postgresql.rb', line 38 def @additional_options end |
#host ⇒ Object
Connectivity options
24 25 26 |
# File 'lib/backup/database/postgresql.rb', line 24 def host @host end |
#name ⇒ Object
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_tables ⇒ Object
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 |
#password ⇒ Object
Credentials for the specified database
16 17 18 |
# File 'lib/backup/database/postgresql.rb', line 16 def password @password end |
#port ⇒ Object
Connectivity options
24 25 26 |
# File 'lib/backup/database/postgresql.rb', line 24 def port @port end |
#skip_tables ⇒ Object
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 |
#socket ⇒ Object
Connectivity options
24 25 26 |
# File 'lib/backup/database/postgresql.rb', line 24 def socket @socket end |
#sudo_user ⇒ Object
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 |
#username ⇒ Object
Credentials for the specified database
16 17 18 |
# File 'lib/backup/database/postgresql.rb', line 16 def username @username end |
Instance Method Details
#connectivity_options ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/backup/database/postgresql.rb', line 102 def return "--host='#{ socket }'" if socket opts = [] opts << "--host='#{ host }'" if host opts << "--port='#{ port }'" if port opts.join(' ') end |
#dump_all? ⇒ Boolean
127 128 129 |
# File 'lib/backup/database/postgresql.rb', line 127 def dump_all? name == :all end |
#password_option ⇒ Object
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. end end |
#pgdump ⇒ Object
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 } #{ } " + "#{ } #{ tables_to_dump } #{ tables_to_skip } #{ name }" end |
#pgdumpall ⇒ Object
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 } " + "#{ } #{ }" end |
#sudo_option ⇒ Object
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_dump ⇒ Object
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_skip ⇒ Object
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_options ⇒ Object
111 112 113 |
# File 'lib/backup/database/postgresql.rb', line 111 def Array().join(' ') end |
#username_option ⇒ Object
98 99 100 |
# File 'lib/backup/database/postgresql.rb', line 98 def username_option "--username=#{ Shellwords.escape(username) }" if username end |