Class: S3TarBackup::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_tar_backup/backup.rb

Constant Summary collapse

COMPRESSIONS =
{
  :gzip => {:flag => '-z', :ext => 'tar.gz'},
  :bzip2 => {:flag => '-j', :ext => 'tar.bz2'},
  :lzma => {:flag => '--lzma', :ext => 'tar.lzma'},
  :lzma2 => {:flag => '-J', :ext => 'tar.xz'}
}
ENCRYPTED_EXTENSION =
'asc'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backup_dir, name, sources, exclude, compression = :bzip2, gpg_key = nil) ⇒ Backup

Returns a new instance of Backup.



25
26
27
28
29
30
31
32
33
34
# File 'lib/s3_tar_backup/backup.rb', line 25

def initialize(backup_dir, name, sources, exclude, compression=:bzip2, gpg_key=nil)
  @backup_dir, @name, @sources, @exclude = backup_dir, name, [*sources], [*exclude]
  raise "Unknown compression #{compression}. Valid options are #{COMPRESSIONS.keys.join(', ')}" unless COMPRESSIONS.has_key?(compression)
  @compression_flag = COMPRESSIONS[compression][:flag]
  @compression_ext = COMPRESSIONS[compression][:ext]
  @time = Time.now
  @gpg_key = gpg_key

  Dir.mkdir(@backup_dir) unless File.directory?(@backup_dir)
end

Class Method Details

.parse_object(object, profile) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/s3_tar_backup/backup.rb', line 64

def self.parse_object(object, profile)
  name = File.basename(object.key)
  match = name.match(/^backup-([\w\-]+)-(\d\d\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)-(\w+)\.(.*?)(\.#{ENCRYPTED_EXTENSION})?$/)
  return nil unless match && match[1] == profile

  return {
    :type => match[8].to_sym,
    :date => Time.new(match[2].to_i, match[3].to_i, match[4].to_i, match[5].to_i, match[6].to_i, match[7].to_i),
    :name => name,
    :ext => match[9],
    :size => object.content_length,
    :profile => match[1],
    :compression => COMPRESSIONS.find{ |k,v| v[:ext] == match[9] }[0],
    :encryption => !match[10].nil?
  }
end

.restore_cmd(restore_into, restore_from, verbose = false) ⇒ Object

No real point in creating a whole new class for this one



82
83
84
85
86
87
88
# File 'lib/s3_tar_backup/backup.rb', line 82

def self.restore_cmd(restore_into, restore_from, verbose=false)
  ext, encrypted = restore_from.match(/[^\.\\\/]+\.(.*?)(\.#{ENCRYPTED_EXTENSION})?$/)[1..2]
  compression_flag = COMPRESSIONS.find{ |k,v| v[:ext] == ext }[1][:flag]
  tar_archive = encrypted ? '' : "f \"#{restore_from}\""
  gpg_cmd = encrypted ? "gpg --yes --batch --no-tty -d \"#{restore_from}\" | " : ''
  "#{gpg_cmd}tar xp#{verbose ? 'v' : ''}#{tar_archive} #{compression_flag} -G -C #{restore_into}"
end

Instance Method Details

#archiveObject



48
49
50
51
52
53
# File 'lib/s3_tar_backup/backup.rb', line 48

def archive
  return @archive if @archive
  type = snar_exists? ? 'incr' : 'full'
  encrypted_bit = @gpg_key ? ".#{ENCRYPTED_EXTENSION}" : ''
  File.join(@backup_dir, "backup-#{@name}-#{@time.strftime('%Y%m%d_%H%M%S')}-#{type}.#{@compression_ext}#{encrypted_bit}")
end

#backup_cmd(verbose = false) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/s3_tar_backup/backup.rb', line 55

def backup_cmd(verbose=false)
  exclude = @exclude.map{ |e| " --exclude \"#{e}\""}.join
  sources = @sources.map{ |s| "\"#{s}\""}.join(' ')
  @archive = archive
  tar_archive = @gpg_key ? '' : "f \"#{@archive}\""
  gpg_cmd = @gpg_key ? " | gpg -r #{@gpg_key} -o \"#{@archive}\" --always-trust --yes --batch --no-tty -e" : ''
  "tar c#{verbose ? 'v' : ''}#{tar_archive} #{@compression_flag} -g \"#{snar_path}\"#{exclude} --no-check-device #{sources}#{gpg_cmd}"
end

#snarObject



36
37
38
# File 'lib/s3_tar_backup/backup.rb', line 36

def snar
  "backup-#{@name}.snar"
end

#snar_exists?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/s3_tar_backup/backup.rb', line 44

def snar_exists?
  File.exists?(snar_path)
end

#snar_pathObject



40
41
42
# File 'lib/s3_tar_backup/backup.rb', line 40

def snar_path
  File.join(@backup_dir, snar)
end