class CGEBackup

Defines a class which represents and processes a single CGE backup @!attribute [r] id

an externally specified id to identify the object by.

Attributes

id[R]

Public Class Methods

new(filename1, filename2, id) click to toggle source

Configures and returns a CGEBackup object. @param [String] filename1 filename of one of the two backup files @param [String] filename2 filename of one of the two backup files @param [Integer] id identification number for the CGEbackup object

# File lib/cge_backup.rb, line 27
def initialize(filename1, filename2, id)
  failValidation unless filename1.is_a?(String) and filename2.is_a?(String)
  failValidation unless id.is_a?(Integer)

  @extracted  = false
  @downloaded = false
  @id = id

  if ! filename1 =~ /.*BACKUP-\d*_\d-[0-9a-f]*.tar/
    raise ArgumentError, 'CGE backup filename not valid.'
  end

  if ! filename2 =~ /BACKUP-\d*_\d-[0-9a-f]*.tar/
    raise ArgumentError, 'CGE backup filename not valid.'
  end

  if filename1 =~ /BACKUP-\d*_1-[0-9a-f]*.tar/
    @node1    = filename1
    @node2    = filename2
  else
    @node1    = filename2
    @node2    = filename1
  end
end

Public Instance Methods

archive(time) click to toggle source

Archive the sql dumps to sync bucket after deflating them via ZLib. @param [Time] time The time of the sync point to create or add to

# File lib/cge_backup.rb, line 117
def archive(time)
  # ensure the time is non-nil and a Time object
  failValidation if time.nil? or not time.is_a?(Time)

  # The dumps must have been extracted before they can be archived
  raise "Backups must be extracted before they can be archived." if not @extracted

  # Get the bucket
  bucket = AWS::S3.new(:access_key_id=>$AWS_ID, :secret_access_key=>$AWS_SECRET).buckets[$SYNC_BUCKET]

  # generate the epoch time prefix
  prefix = time.strftime('%s')

  # For each dump
  @dumps.each do |dump|
    #zd = Zlib::Deflate.new(Zlib::BEST_COMPRESSION,Zlib::MAX_WBITS, Zlib::DEF_MEM_LEVEL, Zlib::HUFFMAN_ONLY)
    #open dump, 'r' do |dump_file|
    #  begin
    #    open dump + '.deflated', 'w+' do |def_file|
    #      until dump_file.eof?
    #        def_file << zd.deflate(dump_file.read(16384))
    #      end
    #    end
    #  ensure
    #    zd.close
    #  end
    #end


    name = File.basename(dump)
    # Deflate the contents of the dump and write them to an S3 object
    #bucket.objects["#{prefix}/database/#{name}.deflated"].write(Pathname.new(dump + '.deflated'))
    bucket.objects["#{prefix}/database/#{name}"].write(Pathname.new(dump))
  end
end
date_time() click to toggle source

Returns the date and time of the backup as a string

@return [String] date and time of backup
# File lib/cge_backup.rb, line 19
def date_time
  Time.at(epoch_time.to_i).strftime('%Y-%m-%d %H:%M:%S')
end
download(dir) click to toggle source

Downloads the backup files @param [String] dir Directory to download the backups to. Can be relative.

# File lib/cge_backup.rb, line 54
def download(dir)
  download_path = File.expand_path(dir)

  # this assumes the role on the production account to access CGE backups
  sts = AWS::STS.new(:access_key_id=>$AWS_ID, :secret_access_key=>$AWS_SECRET)
  role = sts.assume_role(:role_arn => $ROLE_ARN, :role_session_name => SecureRandom.uuid.gsub(/-/,''), :duration_seconds => 60 * 15)

  # store role credentials
  creds = role[:credentials]

  # open the CGE backup bucket
  cge_bucket = AWS::S3.new(creds).buckets[$CGE_BUCKET]

  # Try to download the backup file from node 1
  begin
    File.open("#{File.join(download_path, @node1)}", 'wb') do |file|
      cge_bucket.objects[@node1].read do |chunk|
        file.write(chunk)
      end
    end

    @backup_archive = File.join(download_path, @node1)
  rescue    # Try node 2 if it fails. If this fails, pretend it didn't
    File.open("#{File.join(download_path, @node2)}", 'wb') do |file|
      cge_bucket.objects[@node1].read do |chunk|
        file.write(chunk)
      end
    end

    @backup_archive = File.join(download_path, @node2)
  end
  @downloaded = true
end
epoch_time() click to toggle source

Returns the epoch time of the backup as a string

@return [String] epoch time of backup
# File lib/cge_backup.rb, line 13
def epoch_time
  @node1.scan(/BACKUP-(\d*)_\d-[0-9a-f]*.tar/).flatten[0]
end
extract() click to toggle source

Extract the SQL dumps to the location of the backup archive

# File lib/cge_backup.rb, line 89
def extract
  raise "Unable to extract backup: not downloaded" if not @downloaded
  @dumps = []

  path = File.dirname(@backup_archive)

  # Setup to extract the backups
  tar_extract = Gem::Package::TarReader.new(File.open(@backup_archive))
  tar_extract.each do |entry|
    name = File.basename(entry.full_name)     # Get Filename

    next if not entry.file?                   # Skip all directories

    # Skip unneeded files
    next if name == 'test.sql'
    next if name == 'users.sql'
    next if name == 'md5sums'
    next if name =~ /.*\.(Data|log|ctl)/    # These files from from the ndb backups

    result_path = File.join(path, name)
    File.open(result_path, 'w').write(entry.read)   # write out the file
    @dumps.push(result_path)
  end
  @extracted = true
end

Private Instance Methods

get_usage() click to toggle source
Calls superclass method ErrorHandlingIface#get_usage
# File lib/cge_backup.rb, line 155
def get_usage
  #Usage cases for all the usage in this class
  if @usage.nil?
    init_usage
    @usage['archive'] = ['time = Time']
  end

  super
end