Class: DatabaseExport

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/ey_db_backup/database_export.rb

Instance Method Summary collapse

Instance Method Details

#run_exportObject



8
9
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
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
# File 'lib/ey_db_backup/database_export.rb', line 8

def run_export
  
  aws_config = AWSConfiguration.new
  aws_config.read_from_yaml
  
  AWS::S3::Base.establish_connection!(
    :access_key_id     => aws_config.access_key_id ,
    :secret_access_key => aws_config.secret_access_key
  )

  bucket_name = aws_config.bucket_name
  export_path = aws_config.export_path
  environment = aws_config.environment
  databases = aws_config.databases

  databases.each do |m|

    bucket_files = AWS::S3::Bucket.objects(bucket_name)

    #bucket_files.each do |x|
      #puts x.key
    #end


    bucket_files.keep_if {|v| v.key.include? "#{environment}.#{m}"}

    puts "-------"

    most_recent = bucket_files[0]

    bucket_files.each do |n|
      if n.about["last-modified"] > most_recent.about["last-modified"]
        most_recent = n
      end
    end

    puts "Backing up #{most_recent.key} to #{export_path}#{most_recent.key}"
    puts "Current transfer status:"

    if !FileTest::directory?("#{export_path}#{environment}.#{m}")
      Dir::mkdir("#{export_path}#{environment}.#{m}")
    end

    counter = 0
    
    puts most_recent.key
    puts most_recent.about["content-length"]
    
    open("#{export_path}#{most_recent.key}", 'w') do |file|
        AWS::S3::S3Object.stream(most_recent.key, bucket_name) do |chunk|
          file.write chunk
          counter = counter + 1
          if counter == ((((most_recent.about["content-length"].to_f/10.0)/1048576)*100).truncate).to_i or ((((file.size.to_f)/(most_recent.about["content-length"].to_f))*100.0).truncate == 100)
            print "#{(((file.size.to_f)/(most_recent.about["content-length"].to_f))*100.0).truncate}% "
            counter = 0
          end

        end
    end

    puts "\nBackup of #{most_recent.key} complete"

  end
  
  if databases.length > 1
    puts "All backups complete"
  end

end