如何使用新的heroku pg:backings耙db:dump



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

新的pg:备份破坏了任务,我不知道如何修复它。

旧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任务来说,这似乎有些过头了。

解决方案是超级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`

最新更新