Sha256: 8181cd43ed968082af771b9bce9a3c1fcd5898b49ab6c31bc142f7b3f5fdfae2
Contents?: true
Size: 1.92 KB
Versions: 10
Compression:
Stored size: 1.92 KB
Contents
module EY module Backup class Postgresql < Engine register 'postgresql' def dump(database_name, basename) file = basename + '.dump' command = "PGPASSWORD='#{password}' pg_dump -h #{host} --format=c -Upostgres #{database_name}" if gpg? command << " | " << GPGEncryptor.command_for(key_id) file << GPGEncryptor.extension end command << " > #{file}" run(command) file end def load(database_name, file) if database_exists?(database_name) cycle_database(database_name) else create_database(database_name) end command = "cat #{file}" if gpg? raise "Cannot load a GPG backup" end command << " | PGPASSWORD='#{password}' pg_restore -h #{host} --format=c -Upostgres -d #{database_name}" run(command) end def database_exists?(database_name) runs?("PGPASSWORD='#{password}' psql -l -h #{host} | grep '#{database_name}'") end def drop_database(database_name) stdout = StringIO.new() active_connections = spawn(%Q{PGPASSWORD='#{password}' psql -U postgres -t -c "select count(*) from pg_stat_activity where datname='#{database_name}';"}, stdout) if stdout.string.to_i > 0 EY::Backup.logger.fatal(%Q{ERROR: Target database has active connections. For more information, see "Restore or load a database" in docs.engineyard.com}) end spawn("PGPASSWORD='#{password}' dropdb -h #{host} -U#{username} #{database_name}") end def create_database(database_name) spawn("PGPASSWORD='#{password}' createdb -U#{username} -h #{host} #{database_name}") end def cycle_database(database_name) drop_database(database_name) create_database(database_name) end def suffix /\.(dump|gpz)$/ end end end end
Version data entries
10 entries across 10 versions & 1 rubygems