Module: HerokuSuperBackup

Defined in:
lib/heroku_super_backup.rb,
lib/heroku_super_backup/errors.rb,
lib/heroku_super_backup/railtie.rb,
lib/heroku_super_backup/version.rb

Defined Under Namespace

Modules: Errors Classes: Railtie

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.backup_name(to_url) ⇒ Object



32
33
34
35
36
# File 'lib/heroku_super_backup.rb', line 32

def backup_name(to_url)
  # translate s3://bucket/email/foo/bar.dump => foo/bar
  parts = to_url.split('/')
  parts.slice(4..-1).join('/').gsub(/\.dump$/, '')
end

.backups_urlObject



16
17
18
# File 'lib/heroku_super_backup.rb', line 16

def backups_url
  ENV["PGBACKUPS_URL"]
end

.clientObject



20
21
22
# File 'lib/heroku_super_backup.rb', line 20

def client
  @client ||= PGBackups::Client.new(ENV["PGBACKUPS_URL"])
end

.create_initializerObject



116
117
118
# File 'lib/heroku_super_backup.rb', line 116

def create_initializer
  FileUtils.copy(File.join(File.dirname(File.expand_path(__FILE__)), "heroku_super_backup", "heroku_super_backup.sample.rb"), "#{Rails.root}/config/initializers/heroku_super_backup.rb")
end

.databasesObject



24
25
26
27
28
29
30
# File 'lib/heroku_super_backup.rb', line 24

def databases
  if db = ENV["HEROKU_BACKUP_DATABASES"]
    db.split(",").map(&:strip)
  else
    ["DATABASE_URL"]
  end
end

.executeObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/heroku_super_backup.rb', line 38

def execute
  log "heroku:backup started"
  begin   
    CLOUD_CREDENTIALS.each do |credential|

      @bucket_name = credential[:bucket_name] || raise(HerokuSuperBackup::Errors::NotFound.new("Please provide a 'bucket_name' on the CLOUD_CREDENTIALS variable."))
      @backup_path = credential[:backup_path] || "db"
      @provider    = credential[:provider]    || raise(HerokuSuperBackup::Errors::NotFound.new("Please provide a 'provider' on the CLOUD_CREDENTIALS variable."))
      @key1        = credential[:key1]        || raise(HerokuSuperBackup::Errors::NotFound.new("Please provide a 'HCB_KEY1' on the CLOUD_CREDENTIALS variable.")) 
      @key2        = credential[:key2]        || raise(HerokuSuperBackup::Errors::NotFound.new("Please provide a 'HCB_KEY2' CLOUD_CREDENTIALS variable."))
      @encrypted   = credential[:encrypted]   || "false"  
 
      b = client.get_backups.last
      raise HerokuSuperBackup::Errors::NoBackups.new("You don't have any pgbackups. Please run heroku pgbackups:capture first.") unless b
  
      begin
        case @provider
          when 'aws'
            @connection = Fog::Storage.new(
              :provider => 'AWS',
              :aws_access_key_id     => @key1,
              :aws_secret_access_key => @key2
            )
          when 'rackspace'
            @connection = Fog::Storage.new(
              :provider => 'Rackspace',
              :rackspace_username => @key1,
              :rackspace_api_key  => @key2
            )
          when 'google'
            @connection = Fog::Storage.new(
              :provider => 'Google',
              :google_storage_secret_access_key => @key1,
              :google_storage_access_key_id     => @key2
            )
        else
          raise HerokuSuperBackup::Errors::InvalidProvider.new("Your provider was invalid. Valid values are 'aws', 'rackspace', or 'google'")
        end
      rescue
        raise HerokuSuperBackup::Errors::ConnectionError.new("There was an error connecting to your provider.")
      end

      begin
        directory = @connection.directories.get(@bucket_name)
      rescue Excon::Errors::Forbidden
        raise HerokuSuperBackup::Errors::Forbidden.new("You do not have access to this bucket name. It's possible this bucket name is already owned by another user. Please check your credentials (access keys) or select a different bucket name.")
      end

      if !directory
        directory = @connection.directories.create(:key => @bucket_name)
      end

      public_url = b["public_url"]
      created_at = DateTime.parse b["created_at"]
      db_name = b["from_name"]
      name = "#{created_at.strftime('%Y-%m-%d-%H%M%S')}.dump"
      begin
        log "creating #{@backup_path}/#{b["from_name"]}/#{name}"
        file = directory.files.new(:key => "#{@backup_path}/#{b["from_name"]}/#{name}", :body => open(public_url))
        if @provider == "aws" and @encrypted
          options = {"x-amz-server-side-encryption"=>"AES256"}
        else
          options = {}
        end
        file.save(options)
      rescue Exception => e
        raise HerokuSuperBackup::Errors::UploadError.new(e.message)
      end

      prune(@connection, @bucket_name, @backup_path)

      log "heroku:backup complete"
    end
  rescue Exception => e 
    raise HerokuSuperBackup::Errors::NotFound.new(e.message)
  end
end

.log(message) ⇒ Object



12
13
14
# File 'lib/heroku_super_backup.rb', line 12

def log(message)
  puts "[#{Time.now}] #{message}"
end