Class: S3snapshot::DirUpload

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

Constant Summary collapse

MAX_RETRY_COUNT =
5

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, local_dir, tmp_dir = nil) ⇒ DirUpload

Returns a new instance of DirUpload.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/s3snapshot/dir_upload.rb', line 20

def initialize(aws_id, aws_key, bucket_name, prefix, local_dir, tmp_dir = nil )
  super(aws_id, aws_key, bucket_name)
  @local_dir = local_dir
  @prefix = prefix

  @tmpdir = tmp_dir ? tmp_dir : Dir.tmp
  if File.exists? @tmpdir
    puts "Temp directory #{@tmpdir} exists."
  else
    begin
      FileUtils.mkdir_p @tmpdir
      FileUtils.chmod 0777, @tmpdir
    rescue Exception => e
      puts "Unable to create directory #{@tmpdir} due to #{e.message}"
      puts e.backtrace.join("\n")
      exit 5  
    end
  end
end

Instance Method Details

#uploadObject



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
# File 'lib/s3snapshot/dir_upload.rb', line 40

def upload

  start_time = TimeFactory.utc_time
  
  prefix_path = timepath(@prefix, start_time)
  
  files = get_local_files

  files.each do |file|
    file_name = file[@local_dir.length..-1];
            
    path = "#{prefix_path}/#{file_name}"

    split_threshhold = 1024*1024*1024*4
    fsize = File.size file
    puts "uploading '#{file}' [#{fsize} bytes] to '#{@bucket_name}/#{path}'"
    # check if file is greater than 5G 
    if fsize > split_threshhold
      upload_file_as_multipart(file, path)
    else
      # normal upload
      File.open(file) do |fb|
        bucket.files.create(:key =>path, :body => fb)
      end
      
    end

  end
  
  puts "Writing complete marker"
  
  #Upload the complete marker
  bucket.files.create(:key => complete_path(@prefix, start_time), :body => TimeFactory.utc_time.iso8601)
  
  puts "backup complete!"
end