2014-09-10 78 views
2

我需要實現每日備份從一個數據庫到另一個數據庫的自動轉移。數據庫和應用程序都託管在heroku上。 我知道這是可能的,如果與命令手動從本地機器:Heroku自動恢復

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

但是這個過程應該是自動化,而不是從本機上運行。 我有一個想法寫一個將執行此命令的耙子;並在Heroku Scheduler插件的幫助下每天運行這個rake。

任何想法如何更好地做到這一點?或者也許有更好的方法來完成這項任務?

在此先感謝。

+0

也許你可以添加每日chronjob將做到這一點 – 2014-09-10 12:22:55

回答

1

我設法自己解決這個問題。看起來並不複雜。這裏是解決方案,也許這將是對別人有用: 1.我寫了一個腳本複製特定服務器的最新轉儲到當前服務器的DB

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 
  • 爲了避免authenication在每次請求到服務器,在應用程序根一個瞭解創建文件.netrc文件(見詳情這裏https://devcenter.heroku.com/articles/authentication#usage-examples

  • 設置調度程序添加爲Heroku的,並與一起加入我們的rake任務其運行頻率。

  • 就是這樣。