【问题标题】:How to rake db:dump with new heroku pg:backups?如何用新的heroku pg:backups rake db:dump?
【发布时间】:2015-03-21 23:21:45
【问题描述】:

我一直在使用 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 任务来说似乎有点过头了。

【问题讨论】:

    标签: heroku rake


    【解决方案1】:

    解决方案超级简单,但它确实有效......

    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`
    

    【讨论】:

      猜你喜欢
      • 2015-08-23
      • 2017-04-08
      • 2016-12-23
      • 2017-08-02
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      相关资源
      最近更新 更多