Class: Autobackup

Inherits:
Object
  • Object
show all
Defined in:
lib/Autobackup.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Autobackup

Returns a new instance of Autobackup.



17
18
19
20
21
22
# File 'lib/Autobackup.rb', line 17

def initialize(attrs={})
  Heroku::Command.load
  Heroku::Auth.credentials = [ ENV['HEROKU_USERNAME'], ENV['HEROKU_API_KEY'] ]


end

Instance Attribute Details

#backup_urlObject

Returns the value of attribute backup_url.



11
12
13
# File 'lib/Autobackup.rb', line 11

def backup_url
  @backup_url
end

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/Autobackup.rb', line 10

def client
  @client
end

#created_atObject

Returns the value of attribute created_at.



11
12
13
# File 'lib/Autobackup.rb', line 11

def created_at
  @created_at
end

Class Method Details

.callObject



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

def self.call
  new.call
end

Instance Method Details

#callObject



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

def call
  # initialize the heroku client interface
  ENV['APPNAME'].split(",").each do |app|
    execute_backup(app)
  end
end

#download_file(dbname) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/Autobackup.rb', line 57

def download_file(dbname)
  puts "Downloading file in local system"
  # download the file from heroku and save it into a temp file
  File.open(temp_file, "wb") do |output|
    streamer = lambda do |chunk, remaining_bytes, total_bytes|
      output.write chunk
    end

    # https://github.com/excon/excon/issues/475
    Excon.get backup_url,
      :response_block    => streamer,
      :omit_default_port => true
  end
  # initiate upload to S3
  puts "Download complete..."
  upload_file(temp_file,dbname)
end

#execute_backup(appname) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/Autobackup.rb', line 31

def execute_backup(appname)
  puts "================================="
  puts "Starting back up for "+appname

  @client = Heroku::Command::Pg.new([], :app =>appname)
  # database is standard database_url but is configurable in case the app has multiple DBs
  begin
    attachment = client.send(:generate_resolver).resolve(database)

    # Actual backup interface to heroku
    backup = client.send(:hpg_client, attachment).backups_capture
    client.send(:poll_transfer, "backup", backup[:uuid])
    puts "Backup complete ..."
    # get the public url of the backup file for uploading to S3
    self.backup_url = Heroku::Client::HerokuPostgresqlApp
    .new(appname).transfers_public_url(backup[:num])[:url]
    download_file(appname)

    puts "Back up for "+ appname + " complete"
    puts "================================="
  rescue
    puts "The application "+ appname + "is not accessible to you"
  end

end

#upload_file(filepath, dbname) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/Autobackup.rb', line 76

def upload_file(filepath,dbname)
  # code for actual s3 upload
  puts "Uploading file in S3..."
  Aws.config.update({
                      region: 'ap-southeast-1',
                      credentials: Aws::Credentials.new(ENV['S3_ACCESS_ID'],ENV['S3_ACCESS_SECRET'])

  })
  s3 = Aws::S3::Resource.new(region:ENV['S3_REGION'])
  obj = s3.bucket(ENV['S3_BUCKET']).object(generate_file_name(dbname))
  obj.upload_file(filepath)
  puts "Upload Complete"
end