Class: SafetyNetHeroku::BackupDatabaseWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
lib/safety_net_heroku/backup_database_worker.rb

Instance Method Summary collapse

Instance Method Details

#performObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/safety_net_heroku/backup_database_worker.rb', line 10

def perform

  database_name = SafetyNetHeroku.config[:database_name]
  region = SafetyNetHeroku.config[:aws_region]
  bucket = SafetyNetHeroku.config[:aws_bucket]
  backups_directory = SafetyNetHeroku.config[:aws_backups_directory]

  if database_name.nil?
    raise Exception.new 'SafetyNetHeroku.config[:database_name] not set'
  end

  if region.nil?
    raise Exception.new 'SafetyNetHeroku.config[:aws_region] not set'
  end

  if bucket.nil?
    raise Exception.new 'SafetyNetHeroku.config[:aws_bucket] not set'
  end

  if backups_directory.nil?
    raise Exception.new 'SafetyNetHeroku.config[:aws_backups_directory] not set'
  end

  temp_backups_root_directory = "#{Rails.root}/safety_net_heroku"
  temp_backups_directory = "#{temp_backups_root_directory}/database-backups"
  system("mongodump --uri=#{ENV['DB_URI_NO_OPTIONS']} --out=\"#{temp_backups_directory}\"")

  output_file_name = "#{Time.now.to_formatted_s(:long_ordinal).gsub(" ", "-").gsub(",", "")}.zip"
  output_path = "#{temp_backups_directory}/#{output_file_name}"
  system("zip -rj \"#{output_path}\" \"#{temp_backups_directory}/#{database_name}\"")

  s3 = Aws::S3::Resource.new(region: region)
  obj = s3.bucket(bucket).object("#{backups_directory}/#{output_file_name}")
  obj.upload_file(output_path)

  system("rm -r \"#{temp_backups_root_directory}\"")
end