Class: MysqlBackup::Entity::Mysqldump

Inherits:
Object
  • Object
show all
Includes:
NamedArguments
Defined in:
lib/mysql_backup/entity/mysqldump.rb

Overview

Mysqldump files are stored gzipped and split

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#logObject

Returns the value of attribute log.



7
8
9
# File 'lib/mysql_backup/entity/mysqldump.rb', line 7

def log
  @log
end

#log_fileObject

Returns the value of attribute log_file.



8
9
10
# File 'lib/mysql_backup/entity/mysqldump.rb', line 8

def log_file
  @log_file
end

#log_positionObject

Returns the value of attribute log_position.



9
10
11
# File 'lib/mysql_backup/entity/mysqldump.rb', line 9

def log_position
  @log_position
end

Instance Method Details

#compress_and_split(file_obj) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/mysql_backup/entity/mysqldump.rb', line 50

def compress_and_split file_obj
  destination_name = "#{file_obj.path}xxx"
  cmd = "cat #{file_obj.path} | gzip | split --suffix-length=4 --bytes=10240000 --numeric-suffixes - #{destination_name}"
  log && log.info("running " + cmd)
  system cmd
  Pathname.glob destination_name + "*"
end

#createObject

Create a mysqldump file and yield a hash with a MysqlBackup::Entity::Identifier.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mysql_backup/entity/mysqldump.rb', line 13

def create 
  Tempfile.open 'mysqldump' do |f|
    cmd = "mysqldump --opt --all-databases --single-transaction --master-data=2 > #{f.path}"
    log && log.info("running #{cmd}")
    system cmd or raise RuntimeError, "failed to run command: #{cmd}"
    f.flush
    get_log_position f or raise RuntimeError, "could not get log position"
    f.seek 0
    compressed_files = compress_and_split f
    identifier = MysqlBackup::Entity::Identifier.create_object :category => :full, :type => :mysqldump, :n_parts => compressed_files.length, :log_position => log_position, :log_file => log_file
    compressed_files.each_with_index do |cf, n|
      i = identifier.dup
      i.part_number = n
      yield :identifier => i, :file => cf  
    end
  end
end

#get_log_position(file_obj) ⇒ Object

Search through a mysqldump file looking for a line like

CHANGE MASTER TO MASTER_LOG_FILE='thelog.000005', MASTER_LOG_POS=1163;

Set log_position and log_file to the corresponding values.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mysql_backup/entity/mysqldump.rb', line 35

def get_log_position file_obj
  result = {}
  n = 0
  file_obj.each_line do |l|
    if l =~ /CHANGE MASTER TO MASTER_LOG_FILE='(.*)', MASTER_LOG_POS=(\d+)/i
      @log_file = $1
      @log_position = $2.to_i
      return true
    end
    n += 1
    break if n > 100
  end
  false
end