如何使用新的heroku pg:backups来搜索db:dump?

时间:2015-03-21 23:21:45

标签: heroku rake

我一直在使用rake任务将我的数据库从heroku staging快速转储和恢复到本地开发环境。

新的pg:backups已经破坏了任务,我无法弄清楚如何修复它。

旧db.rake:

namespace :db do

  desc "Restore the DB from a production"
  task :dump do
    Bundler.with_clean_env {sh "heroku pgbackups:capture --expire  --app myapp"}
    Bundler.with_clean_env {sh "curl -o latest.dump `heroku pgbackups:url --app myapp`"}
    puts `pg_restore --verbose --clean --no-acl --no-owner -h localhost -d #{Rails.configuration.database_configuration["development"]["database"]} latest.dump`
    puts `rm latest.dump`
  end

end

新的(没有工作):

namespace :db do

  desc "Restore the DB from a production"
  task :new_dump do
    Bundler.with_clean_env {sh "heroku pg:backups capture --app myapp"}
    Bundler.with_clean_env {sh "curl -o latest.dump `heroku pg:backups public-url NEEDS_ID_OF_LAST_BACKUP_HERE --app myapp`"}
    puts `pg_restore --verbose --clean --no-acl --no-owner -h localhost -d #{Rails.configuration.database_configuration["development"]["database"]} latest.dump`
    puts `rm latest.dump`
  end

end

它不再有效的原因是因为Heroku弃用了heroku pgbackups:url命令,该命令曾用于返回最新备份的URL。

最接近的等价物,每https://devcenter.heroku.com/articles/heroku-postgres-backups运行heroku pg:backups,返回一个备份ID表:

=== Backups
ID    Backup Time                Status                              Size    Database
----  -------------------------  ----------------------------------  ------  --------
b005  2015-03-21 23:04:16 +0000  Finished 2015-03-21 23:04:20 +0000  82.1kB  VIOLET
b004  2015-03-21 22:55:33 +0000  Finished 2015-03-21 22:55:34 +0000  82.1kB  VIOLET

据我所知,我可以编写一个解析器来从表中提取ID,但这对于简单的rake任务来说似乎有些过分。

1 个答案:

答案 0 :(得分:0)

解决方案是超级getto,但它有效...

Bundler.with_clean_env {sh "rake db:drop"}
Bundler.with_clean_env {sh "rake db:create"}
Bundler.with_clean_env {sh "heroku pg:backups capture --app #{heroku_server}"}
db_id = nil
Bundler.with_clean_env do
  db_list = `heroku pg:backups -a #{heroku_server}`
  res = db_list.split("\n")[3]
  db_id = res.split(" ")[0].strip
  p "Restoring backup with id #{db_id}"
  restore_url = `heroku pg:backups public-url #{db_id} -a "#{heroku_server}"`
  restore_url = URI.extract(restore_url)
  restore_url = restore_url[0]
  p `curl -o latest.dump "#{restore_url}"`
end
puts `pg_restore --verbose --clean --no-acl --no-owner -h localhost -d dbname-dev latest.dump`
puts `rm latest.dump`