Class: EY::Backup::Postgresql
- Inherits:
-
Engine
show all
- Defined in:
- lib/ey_backup/engines/postgresql_engine.rb
Constant Summary
Constants included
from Spawner
Spawner::CHUNK_SIZE
Instance Attribute Summary
Attributes inherited from Engine
#host, #key_id, #password, #username
Instance Method Summary
collapse
Methods inherited from Engine
descendants, #gpg?, inherited, #initialize, label, lookup, register
Methods included from Spawner
#ioify, #run, #runs?, #spawn
Methods inherited from Base
#logger
Instance Method Details
#create_database(database_name) ⇒ Object
56
57
58
|
# File 'lib/ey_backup/engines/postgresql_engine.rb', line 56
def create_database(database_name)
spawn("PGPASSWORD='#{password}' createdb -U#{username} -h #{host} #{database_name}")
end
|
#cycle_database(database_name) ⇒ Object
60
61
62
63
|
# File 'lib/ey_backup/engines/postgresql_engine.rb', line 60
def cycle_database(database_name)
drop_database(database_name)
create_database(database_name)
end
|
#database_exists?(database_name) ⇒ Boolean
41
42
43
|
# File 'lib/ey_backup/engines/postgresql_engine.rb', line 41
def database_exists?(database_name)
runs?("PGPASSWORD='#{password}' psql -l -h #{host} | grep '#{database_name}'")
end
|
#drop_database(database_name) ⇒ Object
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/ey_backup/engines/postgresql_engine.rb', line 45
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
|
#dump(database_name, basename) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/ey_backup/engines/postgresql_engine.rb', line 6
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
|
#load(database_name, file) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/ey_backup/engines/postgresql_engine.rb', line 23
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
|
#suffix ⇒ Object
65
66
67
|
# File 'lib/ey_backup/engines/postgresql_engine.rb', line 65
def suffix
/\.(dump|gpz)$/
end
|