class Backup::Database::MySQL

Attributes

additional_options[RW]

Additional “mysqldump” or “innobackupex (backup creation)” options

backup_engine[RW]

Default is :mysqldump (which is built in MySQL and generates a textual SQL file), but can be changed to :innobackupex, which has more feasible restore times for large databases. See: www.percona.com/doc/percona-xtrabackup/

host[RW]

Connectivity options

name[RW]

Name of the database that needs to get dumped To dump all databases, set this to `:all` or leave blank.

only_tables[RW]

Tables to dump. This in only valid if `name` is specified. If none are given, the entire database will be dumped.

password[RW]

Credentials for the specified database

port[RW]

Connectivity options

prepare_backup[RW]

If true (which is the default behaviour), the backup will be prepared after it has been successfuly created. This option is only valid if :backup_engine is set to :innobackupex.

prepare_options[RW]

Additional innobackupex log preparation phase (“apply-logs”) options

skip_tables[RW]

Tables to skip while dumping the database

If `name` is set to :all (or not specified), these must include a database name. e.g. 'name.table'. If `name` is given, these may simply be table names.

socket[RW]

Connectivity options

ssh[RW]

SSH option

sudo_user[RW]

If set the backup engine command block is executed as the given user

username[RW]

Credentials for the specified database

verbose[RW]

If set, do not suppress innobackupdb output (useful for debugging)

Public Class Methods

new(model, database_id = nil, &block) click to toggle source
Calls superclass method Backup::Database::Base::new
# File lib/backup/database/mysql.rb, line 67
def initialize(model, database_id = nil, &block)
  super
  instance_eval(&block) if block_given?

  @name ||= :all
  @backup_engine ||= :mysqldump
  @prepare_backup = true if @prepare_backup.nil?
end

Public Instance Methods

perform!() click to toggle source

Performs the mysqldump or innobackupex command and outputs the dump file in the dump_path using dump_filename.

<trigger>/databases/MySQL[-<database_id>].[sql|tar][.gz]
Calls superclass method Backup::Database::Base#perform!
# File lib/backup/database/mysql.rb, line 81
def perform!
  super

  pipeline = Pipeline.new
  dump_ext = sql_backup? ? 'sql' : 'tar'

  pipeline << sudo_option(sql_backup? ? mysqldump : innobackupex)

  model.compressor.compress_with do |command, ext|
    pipeline << command
    dump_ext << ext
  end if model.compressor

  pipeline << "#{ utility(:cat) } > " +
      "'#{ File.join(dump_path, dump_filename) }.#{ dump_ext }'"

  pipeline.run
  if pipeline.success?
    log!(:finished)
  else
    raise Error, "Dump Failed!\n" + pipeline.error_messages
  end
end

Private Instance Methods

connectivity_options() click to toggle source
# File lib/backup/database/mysql.rb, line 128
def connectivity_options
  return "--socket='#{ socket }'" if socket

  opts = []
  opts << "--host='#{ host }'" if host
  opts << "--port='#{ port }'" if port
  opts.join(' ')
end
credential_options() click to toggle source
# File lib/backup/database/mysql.rb, line 121
def credential_options
  opts = []
  opts << "--user='#{ Shellwords.escape(username) }'" if username
  opts << "--password='#{ Shellwords.escape(password) }'" if password
  opts.join(' ')
end
dump_all?() click to toggle source
# File lib/backup/database/mysql.rb, line 160
def dump_all?
  name == :all
end
innobackupex() click to toggle source
# File lib/backup/database/mysql.rb, line 168
def innobackupex
  # Creation phase
  "#{ utility(:innobackupex) } #{ credential_options } " +
  "#{ connectivity_options } #{ user_options } " +
  "--no-timestamp #{ temp_dir } #{ quiet_option } && " +
  innobackupex_prepare +
  # Move files to tar-ed stream on stdout
  "#{ utility(:tar) } --remove-files -cf -  " +
  "-C #{ File.dirname(temp_dir) } #{ File.basename(temp_dir) }"
end
innobackupex_prepare() click to toggle source
# File lib/backup/database/mysql.rb, line 179
def innobackupex_prepare
  return "" unless @prepare_backup
  # Log applying phase (prepare for restore)
  "#{ utility(:innobackupex) } --apply-log #{ temp_dir } " +
  "#{ user_prepare_options }  #{ quiet_option } && "
end
mysqldump() click to toggle source
# File lib/backup/database/mysql.rb, line 107
def mysqldump
  [
    ("ssh #{ssh}" if ssh),
    ("MYSQL_PWD='#{Shellwords.escape(password)}'" if password),
    utility(:mysqldump),
    user_options,
    ("--user='#{Shellwords.escape(username)}'" if username),
    connectivity_options,
    name_option,
    tables_to_dump,
    tables_to_skip
  ].compact.join(' ')
end
name_option() click to toggle source
# File lib/backup/database/mysql.rb, line 145
def name_option
  dump_all? ? '--all-databases' : name
end
quiet_option() click to toggle source
# File lib/backup/database/mysql.rb, line 194
def quiet_option
  verbose ? "" : " 2> /dev/null "
end
sql_backup?() click to toggle source
# File lib/backup/database/mysql.rb, line 164
def sql_backup?
  backup_engine.to_sym == :mysqldump
end
sudo_option(command_block) click to toggle source
# File lib/backup/database/mysql.rb, line 186
def sudo_option(command_block)
  return command_block unless sudo_user

  "sudo -s -u #{ sudo_user } -- <<END_OF_SUDO\n" +
  "#{command_block}\n" +
  "END_OF_SUDO\n"
end
tables_to_dump() click to toggle source
# File lib/backup/database/mysql.rb, line 149
def tables_to_dump
  Array(only_tables).join(' ') unless dump_all?
end
tables_to_skip() click to toggle source
# File lib/backup/database/mysql.rb, line 153
def tables_to_skip
  Array(skip_tables).map do |table|
    table = (dump_all? || table['.']) ? table : "#{ name }.#{ table }"
    "--ignore-table='#{ table }'"
  end.join(' ')
end
temp_dir() click to toggle source
# File lib/backup/database/mysql.rb, line 198
def temp_dir
  File.join(dump_path, dump_filename + ".bkpdir")
end
user_options() click to toggle source
# File lib/backup/database/mysql.rb, line 137
def user_options
  Array(additional_options).join(' ')
end
user_prepare_options() click to toggle source
# File lib/backup/database/mysql.rb, line 141
def user_prepare_options
  Array(prepare_options).join(' ')
end