Class: MySQLDBTool::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_db_tool/backup.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Backup

Returns a new instance of Backup.



9
10
11
12
13
14
15
16
17
# File 'lib/mysql_db_tool/backup.rb', line 9

def initialize(options = {})
  @options = options
  tableConfig = MySQLDBTool::Config::ConfigLoader.load(options[:env])
  @data_tables = tableConfig[:data_tables]
  @ignore_tables = tableConfig[:ignore_tables]
  @db_info = tableConfig[:db_info]
  
  @db_info[:database] = @options[:database] if @options[:database]
end

Instance Method Details

#gzipCommand(command, isGzip, file, gzipSuffix = '.gz') ⇒ Object



67
68
69
# File 'lib/mysql_db_tool/backup.rb', line 67

def gzipCommand(command, isGzip, file, gzipSuffix = '.gz')
  "#{command} #{isGzip ? '| gzip ' : ''} > #{file}#{isGzip ? gzipSuffix : ''}"
end

#performObject



19
20
21
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
# File 'lib/mysql_db_tool/backup.rb', line 19

def perform

  verify_tools_exist

  id=@options[:id] || "0"
  isGzip=@options[:gzip]
  limitDays=@options[:limit_days] || 3
  gzipSuffix = @options[:gzip_suffix] || '.gz'

  Array(@db_info[:database]).flat_map.each_with_index do |database, index |

    defaultOptions="--column-statistics=0 #{mysqlDefaultOptions(@db_info, database)}"
    backupDir=backupDirName(id, "#{index}_#{database}")

    commands = []
    if File.exist? backupDir
      puts "[skip - directory exists] #{backupDir}"
      return commands
    end
    commands.push "mkdir -p #{backupDir}"

    backupFile="#{backupDir}/#{DateTime.now.strftime("%Y-%m-%d")}_#{id}"
    whereDate=(Date.today - limitDays).strftime("%Y-%m-%d 00:00:00")
    options=ENV['DUMP_OPTIONS'] || "--single-transaction --skip-lock-tables"

    puts "backupFile=#{backupFile}"

    ignoreTablesOption = @ignore_tables.map { |e| "--ignore-table=#{e.include?('.') ? e : "#{database}.#{e}"}" }.join(' ')

    commands.push gzipCommand("mysqldump --no-data #{ignoreTablesOption} #{defaultOptions}", isGzip, "#{backupFile}-schema.sql", gzipSuffix)

    backupTables = []

    @data_tables.each {|table|
      where = table[:where].empty? ? "" : "--where=\"#{table[:where]} >= '#{whereDate}'\""
      if where.empty?
        backupTables.push(table[:name])
        next
      else
        commands.push(gzipCommand("mysqldump --no-create-info #{options} #{where} #{defaultOptions} #{table[:name]}", isGzip, "#{backupFile}-#{table[:name]}.sql", gzipSuffix))
      end
    }

    commands.push(gzipCommand("mysqldump --no-create-info #{ignoreTablesOption} #{options} #{defaultOptions} #{backupTables.join(' ')}", isGzip, "#{backupFile}-all-other-tables.sql", gzipSuffix))
    commands
  end
end