2

我需要将每日备份从一个数据库自动传输到另一个数据库。数据库和应用程序都托管在 heroku 上。我知道如果使用以下命令从本地计算机手动执行此操作是可能的:

heroku pgbackups:restore DATABASE `heroku pgbackups:url --app production-app` --app staging-app

但是这个过程应该是自动化的,而不是从本地机器上运行。我有一个想法写一个 rake 来执行这个命令;并在 Heroku Scheduler 插件的帮助下每天运行这个 rake。

任何想法如何更好地做到这一点?或者也许有更好的方法来完成这项任务?

提前致谢。

4

1 回答 1

1

我设法自己解决了这个问题。它似乎没有那么复杂。这是解决方案,也许对其他人有用: 1.我编写了一个脚本,它将最新转储从某个服务器复制到当前服务器的数据库

namespace :backup do
  desc "copy the latest dump from a certain server to the DB of the current server"
  task restore_last_write_dump: :environment do
    last_dump_url = %x(heroku pgbackups:url --app [source_app_name])
    system("heroku pgbackups:restore [DB_to_target_app] '#{last_dump_url}' -a [target_app_name] --confirm [target_app_name]")
    puts "Restored dump: #{last_dump_url}"
  end

end
  1. 为了避免对服务器的每个请求进行身份验证,请在应用程序根目录中创建一个文件 .netrc(请参阅此处的详细信息https://devcenter.heroku.com/articles/authentication#usage-examples

  2. 为 heroku 设置调度程序插件并添加我们的 rake 任务及其运行频率。

就这些。

于 2014-09-15T13:13:34.493 回答