Class: Backup::Database::RemoteMySQL
- Includes:
- SSHKit::DSL
- Defined in:
- lib/backup/database/remote_mysql.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#additional_options ⇒ Object
Additional “mysqldump” or “innobackupex (backup creation)” options.
-
#backup_engine ⇒ Object
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.
-
#host ⇒ Object
Connectivity options.
-
#name ⇒ Object
Name of the database that needs to get dumped To dump all databases, set this to ‘:all` or leave blank.
-
#only_tables ⇒ Object
Tables to dump.
-
#password ⇒ Object
Credentials for the specified database.
-
#port ⇒ Object
Connectivity options.
-
#prepare_backup ⇒ Object
If true (which is the default behaviour), the backup will be prepared after it has been successfuly created.
-
#prepare_options ⇒ Object
Additional innobackupex log preparation phase (“apply-logs”) options.
-
#server_backup_path ⇒ Object
Returns the value of attribute server_backup_path.
-
#server_host ⇒ Object
server options.
-
#server_ssh_key ⇒ Object
Returns the value of attribute server_ssh_key.
-
#server_ssh_options ⇒ Object
Returns the value of attribute server_ssh_options.
-
#server_ssh_password ⇒ Object
Returns the value of attribute server_ssh_password.
-
#server_ssh_port ⇒ Object
Returns the value of attribute server_ssh_port.
-
#server_ssh_user ⇒ Object
Returns the value of attribute server_ssh_user.
-
#skip_tables ⇒ Object
Tables to skip while dumping the database.
-
#socket ⇒ Object
Connectivity options.
-
#sudo_user ⇒ Object
If set the backup engine command block is executed as the given user.
-
#username ⇒ Object
Credentials for the specified database.
-
#verbose ⇒ Object
If set, do not suppress innobackupdb output (useful for debugging).
Attributes inherited from Base
#database_id, #dump_path, #model
Instance Method Summary collapse
- #build_server_ssh_options ⇒ Object
-
#initialize(model, database_id = nil, &block) ⇒ RemoteMySQL
constructor
A new instance of RemoteMySQL.
-
#perform! ⇒ Object
Performs the mysqldump or innobackupex command and outputs the dump file in the
dump_path
usingdump_filename
.
Methods included from Config::Helpers
Methods included from Utilities::Helpers
Constructor Details
#initialize(model, database_id = nil, &block) ⇒ RemoteMySQL
Returns a new instance of RemoteMySQL.
82 83 84 85 86 87 88 89 |
# File 'lib/backup/database/remote_mysql.rb', line 82 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_options ⇒ Object
Additional “mysqldump” or “innobackupex (backup creation)” options
55 56 57 |
# File 'lib/backup/database/remote_mysql.rb', line 55 def end |
#backup_engine ⇒ Object
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/
66 67 68 |
# File 'lib/backup/database/remote_mysql.rb', line 66 def backup_engine @backup_engine end |
#host ⇒ Object
Connectivity options
38 39 40 |
# File 'lib/backup/database/remote_mysql.rb', line 38 def host @host end |
#name ⇒ Object
Name of the database that needs to get dumped To dump all databases, set this to ‘:all` or leave blank.
30 31 32 |
# File 'lib/backup/database/remote_mysql.rb', line 30 def name @name end |
#only_tables ⇒ Object
Tables to dump. This in only valid if ‘name` is specified. If none are given, the entire database will be dumped.
51 52 53 |
# File 'lib/backup/database/remote_mysql.rb', line 51 def only_tables @only_tables end |
#password ⇒ Object
Credentials for the specified database
34 35 36 |
# File 'lib/backup/database/remote_mysql.rb', line 34 def password @password end |
#port ⇒ Object
Connectivity options
38 39 40 |
# File 'lib/backup/database/remote_mysql.rb', line 38 def port @port end |
#prepare_backup ⇒ Object
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.
72 73 74 |
# File 'lib/backup/database/remote_mysql.rb', line 72 def prepare_backup @prepare_backup end |
#prepare_options ⇒ Object
Additional innobackupex log preparation phase (“apply-logs”) options
59 60 61 |
# File 'lib/backup/database/remote_mysql.rb', line 59 def end |
#server_backup_path ⇒ Object
Returns the value of attribute server_backup_path.
24 25 26 |
# File 'lib/backup/database/remote_mysql.rb', line 24 def server_backup_path @server_backup_path end |
#server_host ⇒ Object
server options
17 18 19 |
# File 'lib/backup/database/remote_mysql.rb', line 17 def server_host @server_host end |
#server_ssh_key ⇒ Object
Returns the value of attribute server_ssh_key.
22 23 24 |
# File 'lib/backup/database/remote_mysql.rb', line 22 def server_ssh_key @server_ssh_key end |
#server_ssh_options ⇒ Object
Returns the value of attribute server_ssh_options.
18 19 20 |
# File 'lib/backup/database/remote_mysql.rb', line 18 def end |
#server_ssh_password ⇒ Object
Returns the value of attribute server_ssh_password.
21 22 23 |
# File 'lib/backup/database/remote_mysql.rb', line 21 def server_ssh_password @server_ssh_password end |
#server_ssh_port ⇒ Object
Returns the value of attribute server_ssh_port.
19 20 21 |
# File 'lib/backup/database/remote_mysql.rb', line 19 def server_ssh_port @server_ssh_port end |
#server_ssh_user ⇒ Object
Returns the value of attribute server_ssh_user.
20 21 22 |
# File 'lib/backup/database/remote_mysql.rb', line 20 def server_ssh_user @server_ssh_user end |
#skip_tables ⇒ Object
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.
46 47 48 |
# File 'lib/backup/database/remote_mysql.rb', line 46 def skip_tables @skip_tables end |
#socket ⇒ Object
Connectivity options
38 39 40 |
# File 'lib/backup/database/remote_mysql.rb', line 38 def socket @socket end |
#sudo_user ⇒ Object
If set the backup engine command block is executed as the given user
76 77 78 |
# File 'lib/backup/database/remote_mysql.rb', line 76 def sudo_user @sudo_user end |
#username ⇒ Object
Credentials for the specified database
34 35 36 |
# File 'lib/backup/database/remote_mysql.rb', line 34 def username @username end |
#verbose ⇒ Object
If set, do not suppress innobackupdb output (useful for debugging)
80 81 82 |
# File 'lib/backup/database/remote_mysql.rb', line 80 def verbose @verbose end |
Instance Method Details
#build_server_ssh_options ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/backup/database/remote_mysql.rb', line 151 def = {} v = self.server_ssh_user [:user] = v if v v = self.server_ssh_password [:password] = v if v v = self.server_ssh_key [:key] = v if v v = self.server_ssh_port [:port] = v if v self. = end |
#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]
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/backup/database/remote_mysql.rb', line 96 def perform! super # prepare # 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 dump_remote_file = File.join('/tmp', dump_filename+"."+dump_ext) pipeline << "#{ utility(:cat) } > '#{ dump_remote_file }'" # generate backup on remote server cmd_remote = pipeline.commands.join(" | ") remote = Backup::Remote::Command.new res_generate = remote.run_ssh_cmd(server_host, , cmd_remote) if res_generate[:res]==0 raise "Cannot create backup on server. #{res_generate[:error]}" end # download backup dump_file = File.join(dump_path, dump_filename+"."+dump_ext) res_download = remote.ssh_download_file(server_host, , dump_remote_file, dump_file) if res_download[:res]==0 raise 'Cannot download file from server' end #puts "pipe: #{pipeline.commands.inspect}" #puts "cmd: #{cmd_remote}" # download dump from server #pipeline.run #if pipeline.success? # log!(:finished) #else # raise Error, "Dump Failed!\n" + pipeline.error_messages #end end |