Class: S3snapshot::DirDownload

Inherits:
SyncOp
  • Object
show all
Defined in:
lib/s3snapshot/dir_download.rb

Constant Summary

Constants inherited from SyncOp

SyncOp::COMPLETE_EXTENSION, SyncOp::COMPLETE_FILE, SyncOp::COMPLETE_MARKER

Instance Method Summary collapse

Methods inherited from SyncOp

#aws, #bucket, #complete_path, #complete_prefix, #timepath

Constructor Details

#initialize(aws_id, aws_key, bucket_name, prefix, time, local_dir) ⇒ DirDownload

Returns a new instance of DirDownload.



15
16
17
18
19
20
# File 'lib/s3snapshot/dir_download.rb', line 15

def initialize(aws_id, aws_key, bucket_name, prefix, time, local_dir )
  super(aws_id, aws_key, bucket_name)
  @prefix = prefix
  @time = time
  @local_dir = local_dir
end

Instance Method Details

#downloadObject



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
77
78
# File 'lib/s3snapshot/dir_download.rb', line 22

def download
  
  
  #Check if the backup is complete
  manager = BackupManager.new(@aws_id, @aws_key, @bucket_name)
  
  unless manager.exists?(@prefix, @time)
    $stderr.puts "Backup with prefix '#{@prefix}' and time #{@time.iso8601} does not exist.  Please check the prefix and time"
    return
  end
  
  unless manager.complete?(@prefix, @time)
    $stderr.puts "Backup with prefix '#{@prefix}' and time #{@time.iso8601} is not complete.  The backup is either in progress or never finished.  This snapshot is not safe to restore!"
    return
  end
  
  
  #Get all files from this backup
  files = manager.list_files(@prefix, @time)
  
  #Make the local directory
  unless File.directory?(@local_dir)
    FileUtils.mkdir(@local_dir)
  end
  
  prefix_path = timepath(@prefix, @time)
  
  
  files.each do |remotefile|
    #We have to reload state from s3.  Otherwise we can't download when the restore process takes a while
    destination_path = "#{@local_dir}/#{remotefile.key[prefix_path.length+1..-1]}"
    
    directory = destination_path[0..-File.basename(destination_path).length-1]
    
    #Create the parent directory for the file if it doesn't exist
    unless File.directory?(directory)
      FileUtils.mkdir(directory)
    end
    
    puts "downloading '#{remotefile.key}' to '#{destination_path}'"
    
    File.open(destination_path, File::RDWR|File::CREAT) do |file|
      bucket.files.get(remotefile.key) do |chunk, remaining_bytes, total_bytes|
        file.write(chunk)
        puts "#{remaining_bytes}b of #{total_bytes}b remaining"
      end
    end
    
  end
  
  
  
  puts "Writing complete marker"
  
  
  puts "backup complete!"
end