Class: Backup::Database::MySQL

Inherits:
Base
  • Object
show all
Defined in:
lib/backup/database/mysql.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Attributes inherited from Base

#database_id, #dump_path, #model

Instance Method Summary collapse

Methods included from Config::Helpers

included

Constructor Details

#initialize(model, database_id = nil, &block) ⇒ MySQL

Returns a new instance of MySQL.



63
64
65
66
67
68
69
70
# File 'lib/backup/database/mysql.rb', line 63

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Backup::Config::Helpers

Instance Attribute Details

#additional_optionsObject

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



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

def additional_options
  @additional_options
end

#backup_engineObject

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/



47
48
49
# File 'lib/backup/database/mysql.rb', line 47

def backup_engine
  @backup_engine
end

#hostObject

Connectivity options



19
20
21
# File 'lib/backup/database/mysql.rb', line 19

def host
  @host
end

#nameObject

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



11
12
13
# File 'lib/backup/database/mysql.rb', line 11

def name
  @name
end

#only_tablesObject

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



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

def only_tables
  @only_tables
end

#passwordObject

Credentials for the specified database



15
16
17
# File 'lib/backup/database/mysql.rb', line 15

def password
  @password
end

#portObject

Connectivity options



19
20
21
# File 'lib/backup/database/mysql.rb', line 19

def port
  @port
end

#prepare_backupObject

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.



53
54
55
# File 'lib/backup/database/mysql.rb', line 53

def prepare_backup
  @prepare_backup
end

#prepare_optionsObject

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



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

def prepare_options
  @prepare_options
end

#skip_tablesObject

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.



27
28
29
# File 'lib/backup/database/mysql.rb', line 27

def skip_tables
  @skip_tables
end

#socketObject

Connectivity options



19
20
21
# File 'lib/backup/database/mysql.rb', line 19

def socket
  @socket
end

#sudo_userObject

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



57
58
59
# File 'lib/backup/database/mysql.rb', line 57

def sudo_user
  @sudo_user
end

#usernameObject

Credentials for the specified database



15
16
17
# File 'lib/backup/database/mysql.rb', line 15

def username
  @username
end

#verboseObject

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



61
62
63
# File 'lib/backup/database/mysql.rb', line 61

def verbose
  @verbose
end

Instance Method Details

#perform!Object

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]


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/backup/database/mysql.rb', line 77

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