class Backup::Database::Base

Attributes

database_id[R]
dump_path[R]
model[R]

Public Class Methods

new(model, database_id = nil) click to toggle source

If given, database_id will be appended to the dump_filename. This is required if multiple Databases of the same class are added to the model.

# File lib/backup/database/base.rb, line 16
def initialize(model, database_id = nil)
  @model = model
  @database_id = database_id.to_s.gsub(/\W/, '_') if database_id
  @dump_path = File.join(Config.tmp_path, model.trigger, 'databases')
  load_defaults!
end

Public Instance Methods

perform!() click to toggle source
# File lib/backup/database/base.rb, line 23
def perform!
  log!(:started)
  prepare!
end

Private Instance Methods

database_name() click to toggle source
# File lib/backup/database/base.rb, line 70
def database_name
  @database_name ||= self.class.to_s.sub('Backup::', '') +
      (database_id ? " (#{ database_id })" : '')
end
dump_filename() click to toggle source

Sets the base filename for the final dump file to be saved in dump_path, based on the class name. e.g. databases/MySQL.sql

database_id will be appended if it is defined. e.g. databases/MySQL-database_id.sql

If multiple Databases of the same class are defined and no database_id is defined, the user will be warned and one will be auto-generated.

Model#initialize calls this method after all defined databases have been initialized so `backup check` can report these warnings.

# File lib/backup/database/base.rb, line 46
      def dump_filename
        @dump_filename ||= begin
          unless database_id
            if model.databases.select {|d| d.class == self.class }.count > 1
              sleep 1; @database_id = Time.now.to_i.to_s[-5, 5]
              Logger.warn Error.new("                Database Identifier Missing
                When multiple Databases are configured in a single Backup Model
                that have the same class (MySQL, PostgreSQL, etc.), the optional
                +database_id+ must be specified to uniquely identify each instance.
                e.g. database MySQL, :database_id do |db|
                This will result in an output file in your final backup package like:
                databases/MySQL-database_id.sql

                Backup has auto-generated an identifier (#{ database_id }) for this
                database dump and will now continue.
")
            end
          end

          self.class.name.split('::').last + (database_id ? "-#{ database_id }" : '')
        end
      end
log!(action) click to toggle source
# File lib/backup/database/base.rb, line 75
def log!(action)
  msg = case action
        when :started then 'Started...'
        when :finished then 'Finished!'
        end
  Logger.info "#{ database_name } #{ msg }"
end
prepare!() click to toggle source
# File lib/backup/database/base.rb, line 30
def prepare!
  FileUtils.mkdir_p(dump_path)
end