Class: MysqlBackup::Entity::Identifier

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

Overview

There are four different kinds of things you store for MySQL backups.

  • Completed logs.

  • The current log (the log file that MySQL is writing to)

  • mysqldump files (output from the mysqldump command)

  • binary files (tar | gzip | split of the files in the mysql data directory)

They’re stored using the following name schemes:

:log:type_complete:thelog.000005
  A completed log file.

:log:type_current:log_file_thelog.0000000006:log_position_0000000311:n_parts_0000000001:part_number_0000000000
  A current log file.  The position is 311 (that's where mysql will write the next statement).
  There's only one part, and this is it.  (Part numbers start with 0.)
  In this release, log files can only have one part - no attempt is made to split them
  into chunks small enough to fit in S3.  Don't let your log files grow larger than 5G.
:full:type_mysqldump:log_file_thelog.0000000006:log_position_0000000382:n_parts_0000000001:part_number_0000000000
  A full mysqldump file.  The current log file is thelog.0000000006, and the position in
  that log is 382.  Since we don't flush logs, anything written after 382 might not be
  complete.
:full:type_binary:log_file_thelog.0000000006:log_position_0000000311:n_parts_0000000001:part_number_0000000000
  A full copy of the MySQL data files, created by tarring up all the data files,
  then passing them through gzip and split to make sure they'll fit in S3 objects that
  can only hold 5G.

Direct Known Subclasses

Full, Log

Defined Under Namespace

Classes: Full, Log

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FactoryCreateMethod

included

Constructor Details

#initialize(args) ⇒ Identifier

Returns a new instance of Identifier.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mysql_backup/entity/identifier.rb', line 64

def initialize args
  super
  
  part_number ||= 0
  part_number = sprintf "%06d", part_number.to_i
  
  n_parts ||= 1
  n_parts = sprintf "%06d", n_parts.to_i
  
  throw :bad if log_file == 'foo'
end

Instance Attribute Details

#categoryObject

category is one of

:full => a full backup of the mysql files
:log_current => the mysql binary log file that's being written to
:log_complete => mysql binary log files that are complete


41
42
43
# File 'lib/mysql_backup/entity/identifier.rb', line 41

def category
  @category
end

#log_fileObject

The name of the log file



50
51
52
# File 'lib/mysql_backup/entity/identifier.rb', line 50

def log_file
  @log_file
end

#log_positionObject

The numeric position in the log file



53
54
55
# File 'lib/mysql_backup/entity/identifier.rb', line 53

def log_position
  @log_position
end

#n_partsObject

For multipart storage units, the total number of parts



59
60
61
# File 'lib/mysql_backup/entity/identifier.rb', line 59

def n_parts
  @n_parts
end

#part_numberObject

For multipart storage units, the part number



56
57
58
# File 'lib/mysql_backup/entity/identifier.rb', line 56

def part_number
  @part_number
end

#timestampObject

Time the object was created



62
63
64
# File 'lib/mysql_backup/entity/identifier.rb', line 62

def timestamp
  @timestamp
end

#typeObject

type depends on the category.

for category full, type is one of

:binary, :mysqldump


47
48
49
# File 'lib/mysql_backup/entity/identifier.rb', line 47

def type
  @type
end

Class Method Details

.string_to_args(s) ⇒ Object

Examples of the four identifiers:

log/type_complete/thelog.000001 log/type_current/log_file_thelog.0000000006/log_position_0000000311/n_parts_0000000001/part_number_0000000000 full/type_mysqldump/log_file_thelog.0000000006/log_position_0000000382/n_parts_0000000001/part_number_0000000000 full/type_binary/log_file_thelog.0000000006/log_position_0000000311/n_parts_0000000001/part_number_0000000000



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mysql_backup/entity/identifier.rb', line 112

def self.string_to_args s
  parts = s.split(':')
  args = {}
  args[:category] = parts.shift.to_sym
  args[:type] = (parts.shift)[/^type_(.*)/, 1].to_sym
  args[:log_file] = (parts.shift)[/log_file_(.*)/, 1]
  unless parts.empty?
    args[:log_position] = (parts.shift)[/log_position_(.*)/, 1].to_i
  end
  unless parts.empty?
    args[:n_parts] = (parts.shift)[/n_parts_(.*)/, 1].to_i
    args[:part_number] = (parts.shift)[/part_number_(.*)/, 1].to_i
  end
  args
end

Instance Method Details

#<=>(rhs) ⇒ Object



132
133
134
# File 'lib/mysql_backup/entity/identifier.rb', line 132

def <=> rhs
  category <=> rhs.category || self[:type] <=> rhs[:type] || log_file <=> rhs.log_file || log_position <=> rhs.log_position
end

#[](x) ⇒ Object



128
129
130
# File 'lib/mysql_backup/entity/identifier.rb', line 128

def [] x
  send x  
end

#log_file_numberObject

Get the sequence number from the log file name.

mylogfile.000034 => 34


102
103
104
# File 'lib/mysql_backup/entity/identifier.rb', line 102

def log_file_number
  log_file[/\.(\d+)$/, 1].to_i
end

#merge(new_values_hash) ⇒ Object



76
77
78
# File 'lib/mysql_backup/entity/identifier.rb', line 76

def merge new_values_hash
  to_hash.merge new_values_hash
end

#n_digits(x) ⇒ Object



95
96
97
# File 'lib/mysql_backup/entity/identifier.rb', line 95

def n_digits x
  sprintf "%010d", x
end

#to_hashObject



80
81
82
83
84
85
86
# File 'lib/mysql_backup/entity/identifier.rb', line 80

def to_hash
  result = {}
  [:category, :type, :log_file, :log_position, :n_parts, :part_number].each do |i|
    result[i] = send i
  end
  result
end

#to_sObject



88
89
90
91
92
93
# File 'lib/mysql_backup/entity/identifier.rb', line 88

def to_s
  pos = n_digits log_position
  np = n_digits n_parts
  pn = n_digits part_number
  return "#{category}:type_#{@type}:log_file_#{log_file}:log_position_#{pos}:n_parts_#{np}:part_number_#{pn}"
end