Class: S3

Inherits:
Rehabilitate::Plugin show all
Defined in:
lib/rehabilitate/plugins/s3.rb

Instance Method Summary collapse

Methods inherited from Rehabilitate::Plugin

derivative_dirs, #log

Instance Method Details

#download(options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rehabilitate/plugins/s3.rb', line 42

def download(options)
  log "S3 downloading backup"
  log "Number #{options.number}"

  location = parse_upload_string(options.location)
  s3 = setup_fog(location)
  local_file = ""
  dir = s3.directories.get(location[:bucket])
  backups = sorted_backups(dir.files)
  if backups[options.number]
    log "Restoring #{backups[options.number]}"
    restore_key = backups[options.number]
    output = %x{ s3cmd --config /etc/s3cfg get --recursive --skip-existing s3://#{location[:bucket]}/#{backups[options.number]} #{options.tmp}}
    output.split("\n").each {|f| log f}
  else
    log "Invalid number specified, use --list first to get the id you want to restore"
  end
  backup_files =  Dir.glob("#{options.tmp}/#{backups[options.number].split("/").last}/*")
  log "Joining #{backup_files.size} files..."
  joiner = Splitter.new
  joined = backup_files.size > 1 ? joiner.join(backup_files) : backup_files
  options._tmp_files << joined
  options._backup_files = joined.is_a?(Array) ? joined : [joined]
end

#list(options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rehabilitate/plugins/s3.rb', line 7

def list(options)
  location = parse_upload_string(options.location)
  s3 = setup_fog(location)

  log "Listing bucket contents for #{location[:bucket]}"
  dir = s3.directories.get(location[:bucket])
  backups = sorted_backups(dir.files)
  backups.each_with_index do |backup, i|
    say "#{i}: #{prettify(backup)}"
  end
end

#upload(options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rehabilitate/plugins/s3.rb', line 19

def upload(options)
  location = parse_upload_string(options.location)
  s3 = setup_fog(location)
  location[:dir] = "#{location[:dir]}/#{options._base_backup_name}"

  log "Creating bucket #{location[:bucket]} if it doesn't exist"
  s3.directories.create(:key => location[:bucket])
  options._backup_files.collect! do |local_file|
    if File.size(local_file) > options.split_size
      splitter = Splitter.new
      local_file = splitter.split(local_file, options)
    else
      [local_file]
    end
    local_file.each do |local_file|
      log "Uploading #{local_file}"
      log "   => #{local_file}"
      log %{ s3cmd --config /etc/s3cfg #{options.s3_options} put #{local_file} s3://#{location[:bucket]}/#{location[:dir]}/ }
      log %x{ s3cmd --config /etc/s3cfg #{options.s3_options} put #{local_file} s3://#{location[:bucket]}/#{location[:dir]}/ }
    end
  end
end