Module: DataExporter::Actions

Included in:
CLI
Defined in:
lib/data_exporter/actions.rb

Instance Method Summary collapse

Instance Method Details

#compress(file_name = '-') ⇒ Object



24
25
26
# File 'lib/data_exporter/actions.rb', line 24

def compress(file_name = '-')
  ['gzip', '--to-stdout', file_name]
end

#configObject



50
51
52
# File 'lib/data_exporter/actions.rb', line 50

def config
  DataExporter.config
end

#decrypt(in_file_name = '-') ⇒ Object



12
13
14
# File 'lib/data_exporter/actions.rb', line 12

def decrypt(in_file_name = '-')
  ['openssl', 'enc', '-d', '-aes-256-cbc', '-salt', '-pass', "file:#{config.backup_key}", '-in', in_file_name]
end

#encrypt(backup_key) ⇒ Object



28
29
30
# File 'lib/data_exporter/actions.rb', line 28

def encrypt(backup_key)
  ['openssl', 'enc', '-aes-256-cbc', '-salt', '-pass', "file:#{backup_key}"]
end

#expand(file_name = '-') ⇒ Object



16
17
18
# File 'lib/data_exporter/actions.rb', line 16

def expand(file_name = '-')
  ['gunzip', file_name]
end

#export(backup_key, encrypted_file, archive_dir = nil) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/data_exporter/actions.rb', line 41

def export(backup_key, encrypted_file, archive_dir = nil)
  case encrypted_file
  when /\.tar\.gz\.enc\z/
    export_encrypted_archive(backup_key, encrypted_file, archive_dir)
  when /\.sql\.gz\.enc\z/
    export_encrypted_file(backup_key, encrypted_file)
  end
end

#find_last_backup(prefix, suffix, backup_dir) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/data_exporter/actions.rb', line 54

def find_last_backup(prefix, suffix, backup_dir)
  if config.sftp_enabled?
    find_last_sftp_backup(prefix, suffix, backup_dir)
  else
    find_last_s3_backup(prefix, suffix, backup_dir)
  end
end

#find_last_s3_backup(prefix, suffix, backup_dir = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/data_exporter/actions.rb', line 80

def find_last_s3_backup(prefix, suffix, backup_dir = nil)
  s3_backup = nil
  s3_bucket = config.s3[:bucket]
  s3_prefix = backup_dir ? File.join(backup_dir, prefix) : prefix
  s3.buckets[s3_bucket].objects.with_prefix(s3_prefix).each do |s3_object|
    next unless s3_object.key =~ /#{glob_escape(suffix)}\Z/
    s3_backup = s3_object
  end
  OpenStruct.new(:name => s3_backup.key, :mtime => s3_backup.last_modified.to_i, :size => s3_backup.content_length, :io => s3_backup) if s3_backup
rescue => e
  log "#{e.to_s}"
  nil
end

#find_last_sftp_backup(prefix, suffix, backup_dir = '/') ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/data_exporter/actions.rb', line 62

def find_last_sftp_backup(prefix, suffix, backup_dir = '/')
  backup_entry = nil
  sftp.dir.glob(backup_dir, prefix + '*' + suffix) do |entry|
    backup_entry ||= entry
    if entry.attributes.mtime > backup_entry.attributes.mtime
      backup_entry = entry
    end
  end
  OpenStruct.new(:name => File.join(backup_dir, backup_entry.name), :mtime => backup_entry.attributes.mtime, :size => backup_entry.attributes.size) if backup_entry
rescue => e
  log "#{e.to_s}"
  nil
end

#glob_escape(glob) ⇒ Object



76
77
78
# File 'lib/data_exporter/actions.rb', line 76

def glob_escape(glob)
  glob.gsub('.', '\\.').gsub('*', '.*')
end

#unarchive(file_name = '-') ⇒ Object



20
21
22
# File 'lib/data_exporter/actions.rb', line 20

def unarchive(file_name = '-')
  ['tar', '-xf', file_name]
end

#unpack(encrypted_file, unpack_dir) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/data_exporter/actions.rb', line 32

def unpack(encrypted_file, unpack_dir)
  case encrypted_file
  when /\.tar\.gz\.enc\z/
    unpack_encrypted_archive(encrypted_file, unpack_dir)
  when /\.sql\.gz\.enc\z/
    unpack_encrypted_file(encrypted_file, unpack_dir)
  end
end