5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/heroku-db-sync/task_helper.rb', line 5
def self.perform_sync arguments
Bundler.with_clean_env do
@arguments = arguments
from_remote = arguments[:from_remote] || 'production'
to_remote = arguments[:to_remote]
puts "(1/4) capturing #{from_remote} database snapshot..."
run "heroku pg:backups capture --remote #{from_remote}"
if to_remote
puts "(2/4) fetching backup url from remote #{from_remote}"
backup_url = runr("heroku pg:backups public-url -q --remote #{from_remote}")
print "(3/4) backing up #{to_remote} and restoring it from snapshot from #{from_remote} ... "
run "heroku pg:backups capture --remote #{to_remote}"
puts "captured. restoring .."
restore_cmd = "heroku pg:backups restore DATABASE '#{backup_url}' --remote #{to_remote}"
puts restore_cmd
run restore_cmd puts "(4/4) restarting remote #{to_remote}"
run "heroku restart --remote #{to_remote}"
else
dumpfile = 'latest.dump'
puts "(2/4) downloading snapshot..."
run "curl -o #{dumpfile} \`heroku pg:backups public-url -q --remote #{from_remote}\`"
puts "(3/4) restoring snapshot to #{host} database #{database} ... "
`pg_restore --verbose --clean --no-acl --no-owner -h #{host} -U #{user} -d #{database} #{dumpfile}`
keep = arguments[:keepdump] || false
unless keep
puts "(4/4) cleaning up..."
`rm #{dumpfile}`
else
puts "(4/4) keeping #{dumpfile}"
end
end
puts "Success! Ah, that was exhausting, pop a beer!"
end
end
|