如何自动将 Heroku pgbackups 发送到 S3

发布于 2024-11-05 08:32:08 字数 226 浏览 0 评论 0原文

我们使用 Heroku 的 PG Backups,它非常棒 http://addons.heroku.com/pgbackups

我目前每天手动将备份下载到我的计算机,然后将其上传到我的 S3 帐户。有谁知道如何设置 cron 作业来自动创建备份(每天都可以)并将其直接发送到我的 S3 帐户?

We use Heroku's PG Backups which has been great http://addons.heroku.com/pgbackups

I'm currently manually downloading the backups everyday to my computer and then uploading it to my S3 account. Does anyone know how to setup a cron job to automatically create a backup (daily is fine) and send it directly to my S3 account?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳哥哥 2024-11-12 08:32:08

我已经修补了 gem 以修复 B 七提到的系统调用。获取数据库参数的正则表达式已损坏,因为非共享数据库的新 DATABASE_URL 现在包含端口号。

https://github.com/mokolabs/heroku_s3_backup

如果您使用的是 Cedar 堆栈,则此补丁应该为你工作。但我在 Bamboo 上,由于与 pg_dump 版本冲突,它对我不起作用。 Postgres 在 Bamboo 上是 8.x 版本,因此 pg_dump 无法从新的非共享数据库转储数据,因为它们都在 Postgres 9.1 版本上运行。

如果我能够找到 Bamboo 堆栈的解决方法,我将在这里更新 gem 和我的评论。

I've patched the gem to fix the system call mentioned by B Seven. The regex which grabbed the database params was breaking because the new DATABASE_URL for non-shared databases now contains a port number.

https://github.com/mokolabs/heroku_s3_backup

If you're on the Cedar stack, then this patch should work for you. But I'm on Bamboo and it doesn't work for me because of a version conflict with pg_dump. Postgres is version 8.x on Bamboo, so pg_dump can't dump data from the new non-shared databases because they are all running on Postgres version 9.1.

If I'm able to find a workaround for the Bamboo stack, I will update the gem and my comment here.

忆沫 2024-11-12 08:32:08

如果将其添加到 lib/tasks 并运行 cron 插件,herkou 将运行以下命令:

namespace :heroku do
  desc "PostgreSQL database backups from Heroku to Amazon S3"
  task :backup => :environment do
    begin
      require 'right_aws'
      puts "[#{Time.now}] heroku:backup started"
      name = "#{ENV['APP_NAME']}-#{Time.now.strftime('%Y-%m-%d-%H%M%S')}.dump"
      db = ENV['DATABASE_URL'].match(/postgres:\/\/([^:]+):([^@]+)@([^\/]+)\/(.+)/)
      system "PGPASSWORD=#{db[2]} pg_dump -Fc -i --username=#{db[1]} --host=#{db[3]} #{db[4]} > tmp/#{name}"
      s3 = RightAws::S3.new(ENV['s3_access_key_id'], ENV['s3_secret_access_key'])
      bucket = s3.bucket("#{ENV['APP_NAME']}-heroku-backups", true, 'private')
      bucket.put(name, open("tmp/#{name}"))
      system "rm tmp/#{name}"
      puts "[#{Time.now}] heroku:backup complete"
    # rescue Exception => e
    #   require 'toadhopper'
    #   Toadhopper(ENV['hoptoad_key']).post!(e)
    end
  end
end

task :cron => :environment do
  Rake::Task['heroku:backup'].invoke

If you add this to lib/tasks and have the cron addon running herkou will run this:

namespace :heroku do
  desc "PostgreSQL database backups from Heroku to Amazon S3"
  task :backup => :environment do
    begin
      require 'right_aws'
      puts "[#{Time.now}] heroku:backup started"
      name = "#{ENV['APP_NAME']}-#{Time.now.strftime('%Y-%m-%d-%H%M%S')}.dump"
      db = ENV['DATABASE_URL'].match(/postgres:\/\/([^:]+):([^@]+)@([^\/]+)\/(.+)/)
      system "PGPASSWORD=#{db[2]} pg_dump -Fc -i --username=#{db[1]} --host=#{db[3]} #{db[4]} > tmp/#{name}"
      s3 = RightAws::S3.new(ENV['s3_access_key_id'], ENV['s3_secret_access_key'])
      bucket = s3.bucket("#{ENV['APP_NAME']}-heroku-backups", true, 'private')
      bucket.put(name, open("tmp/#{name}"))
      system "rm tmp/#{name}"
      puts "[#{Time.now}] heroku:backup complete"
    # rescue Exception => e
    #   require 'toadhopper'
    #   Toadhopper(ENV['hoptoad_key']).post!(e)
    end
  end
end

task :cron => :environment do
  Rake::Task['heroku:backup'].invoke
傻比既视感 2024-11-12 08:32:08

恕我直言,最简单快捷(10 分钟)的方法是配置 heroku-db -backup-s3 构建包。无需代码,只需添加构建包并设置一些环境变量和一个重新安排。

IMHO the easiest and fast (10 minutes) way is to configure heroku-db-backup-s3 buildpack. No code need, only adding the buildpack and setup some environment variables and one reschedule.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文