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'}
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Backup.



22
23
24
25
26
27
28
29
30
# File 'lib/s3_tar_backup/backup.rb', line 22

def initialize(backup_dir, name, sources, exclude, compression=:bzip2)
  @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

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

Class Method Details

.parse_object(object, profile) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/s3_tar_backup/backup.rb', line 57

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+)\.(.*)$/)
  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],
  }
end

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

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



73
74
75
76
77
# File 'lib/s3_tar_backup/backup.rb', line 73

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

Instance Method Details

#archiveObject



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

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

#backup_cmd(verbose = false) ⇒ Object



50
51
52
53
54
55
# File 'lib/s3_tar_backup/backup.rb', line 50

def backup_cmd(verbose=false)
  exclude = @exclude.map{ |e| "\"#{e}\""}.join(' ')
  sources = @sources.map{ |s| "\"#{s}\""}.join(' ')
  @archive = archive
  "tar c#{verbose ? 'v' : ''}f \"#{@archive}\" #{@compression_flag} -g \"#{snar_path}\" --exclude #{exclude} --no-check-device #{sources}"
end

#snarObject



32
33
34
# File 'lib/s3_tar_backup/backup.rb', line 32

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

#snar_exists?Boolean

Returns:

  • (Boolean)


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

def snar_exists?
  File.exists?(snar_path)
end

#snar_pathObject



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

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