Class: MysqlTruck::Dumper

Inherits:
Object
  • Object
show all
Includes:
FileUtils, Helper
Defined in:
lib/mysql_truck/dumper.rb

Constant Summary collapse

REGEX =
/,?\s*(UNIQUE)?\s*KEY\s`[A-Za-z\d_]+`\s*\([A-Za-z\d_,`]+\),?\s*/m

Instance Method Summary collapse

Methods included from Helper

#base_path, #benchmark, #bucket_dir, #config, #csv_options, #db_connection_options, #formatted_time, #initialize_directories, #initialize_s3, #local_host?, #remote_host?, #remove_directories, #s3_path, #tmp_path

Constructor Details

#initialize(config) ⇒ Dumper

Returns a new instance of Dumper.



8
9
10
11
12
13
14
# File 'lib/mysql_truck/dumper.rb', line 8

def initialize(config)
  @config = config
  @time = Time.now # Sets the directory for dump

  initialize_s3
  initialize_directories
end

Instance Method Details

#dumpObject



16
17
18
19
20
# File 'lib/mysql_truck/dumper.rb', line 16

def dump
  dump_data
  upload
  remove_directories
end

#dump_dataObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mysql_truck/dumper.rb', line 22

def dump_data
  tables.each do |table|
    puts "Dumping #{table}..."
    next if compressed_files_exist?(table) && smartly?

    if dump_table?(table)
      base_cmd = "mysqldump --quick"

      # Dump schema
      schema_cmd = "#{base_cmd} --no-data "
      schema_cmd += "#{db_connection_options} #{table}"
      schema_cmd += " > #{filename(table)[:schema_file]}"
      puts schema_cmd
      `#{schema_cmd}`

      # Dump data
      puts "Writing data for #{table} ..."
      mysql = Mysql.init
      mysql.options(Mysql::SET_CHARSET_NAME, "utf8")
      mysql.connect(
        config[:host],
        config[:username],
        config[:password],
        config[:database],
        config[:port]
      )
      mysql.query_with_result = false # Do not store result in memory
      mysql.query("SET NAMES utf8")
      CSV.open(filename(table)[:data_file], 'wb', :col_sep => "\t") do |csv|
        mysql.query("SELECT * FROM #{table}")
        results = mysql.use_result # Stream the rows
        results.each do |row|
          csv << row.collect { |col| col && col.match(/\t/) ? "    " : col }
        end
      end
    end

    if split_schema_file?(table)
      schema_contents = filename(table)[:schema_file].read

      # Create schema with no indexes
      File.open(filename(table)[:no_index_schema_file], 'w') do |f|
        f.write(schema_contents.gsub(REGEX, ''))
      end

      # Create an alter table
      indices = []
      File.open(filename(table)[:alter_table_file], 'w') do |f|
        f.write("ALTER TABLE #{table}\n")

        schema_contents.gsub(/^,?\s*((UNIQUE)?\s*KEY\s`[A-Za-z\d_]+`\s*\([A-Za-z\d_,`]+\)),?\s*$/) do |part|
          indices << $1
        end
        f.write(indices.collect {|i| "ADD #{i}"}.join(",\n"))
      end
    end

    if compress_files?(table)
      puts "compressing #{filename(table)[:no_index_schema_file]}."

      compression_cmd = "lzop -8 -U"
      `#{compression_cmd} #{filename(table)[:no_index_schema_file]}`

      puts "compressing #{filename(table)[:alter_table_file]}."
      `#{compression_cmd} #{filename(table)[:alter_table_file]}`

      puts "compressing #{filename(table)[:data_file]}."
      `#{compression_cmd} #{filename(table)[:data_file]}`
    end

    puts "#{table} dumped.\n\n"
  end
end

#uploadObject



96
97
98
99
100
101
102
103
# File 'lib/mysql_truck/dumper.rb', line 96

def upload
  Dir["#{tmp_path}/*"].each do |file|
    next if File.extname(file) != ".lzo"
    puts "Uploading #{file} ..."
    upload_file file
  end
  puts "Finished uploading backups."
end