mysql_db_tool 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 246f356370dec4811df1950c8972b0110618d4551bc3776c746efd597413f5b4
4
- data.tar.gz: 58e81526ba38e1f9201396d808d5aeb6499bb7f91d479a5e4df8f4110d07652f
3
+ metadata.gz: fdecb06617c9937d6c99c95a6f3dc167dff3398a05d7cdfd1c7d27e072204222
4
+ data.tar.gz: cb1bbc19b30dd0f1208f470254c4737eff569f641d4df76c94a3f0e5340b71d6
5
5
  SHA512:
6
- metadata.gz: 014563a869d15aef0140f1d04580896cb12616efeb5e95435e1c84dc401e2ae538657c853ff63d8376464f2091f14a859ac031e2715799d98875a12e3b067e0b
7
- data.tar.gz: 689bc7c215256672151f4b8041303d74570449cdfababbf48025155da23940e242a8697bd877987fb7a7008cc6a7a106cf6bc108ec74c7d452ad4f353759bd43
6
+ metadata.gz: a4b72b0ae34adf46c5dd689e3192d85b25d3d11607d96c2eb4bab7e192d518c30f049593ddadba81e2cede2fa028c3d8f8cefc2ce0379154b4ba7fb60f7e489e
7
+ data.tar.gz: 4b941a0eb4851b49405dc6bfbbcbfc78c46f13b659903654e37212dac7eda213e52b2815fc7e4839fd40af66d24faa4eba133935c0fceef1dfc26814a0c4e4cc
data/README.md CHANGED
@@ -53,7 +53,7 @@ you can get help by running `mysql_backup -h`
53
53
  * gzip? - default (true), whether to compress with gzip or not
54
54
 
55
55
  * DUMP_OPTIONS - you can set common mysqldump command options by this Environment variable,
56
- if not set, it will use default options for mysqldump. (--single-transaction --skip-lock-tables)
56
+ if not set, it will use default options for mysqldump. (--single-transaction --skip-lock-tables --no-tablespaces)
57
57
 
58
58
  After execution, a directory named "backup-{backup id}" will be created under the current directory.
59
59
 
@@ -8,11 +8,11 @@ module MySQLDBTool
8
8
 
9
9
  def initialize(options = {})
10
10
  @options = options
11
- tableConfig = MySQLDBTool::Config::ConfigLoader.load(options[:env])
12
- @data_tables = tableConfig[:data_tables]
13
- @ignore_tables = tableConfig[:ignore_tables]
14
- @db_info = tableConfig[:db_info]
15
-
11
+ config = MySQLDBTool::Config::ConfigLoader.load(options[:env])
12
+ @data_tables = config[:data_tables]
13
+ @ignore_tables = config[:ignore_tables]
14
+ @db_info = config[:db_info]
15
+
16
16
  @db_info[:database] = @options[:database] if @options[:database]
17
17
  end
18
18
 
@@ -21,51 +21,51 @@ module MySQLDBTool
21
21
  verify_tools_exist
22
22
 
23
23
  id=@options[:id] || "0"
24
- isGzip=@options[:gzip]
25
- limitDays=@options[:limit_days] || 3
26
- gzipSuffix = @options[:gzip_suffix] || '.gz'
24
+ use_gzip=@options[:gzip]
25
+ limit_days=@options[:limit_days] || 3
26
+ gzip_suffix = @options[:gzip_suffix] || '.gz'
27
27
 
28
28
  Array(@db_info[:database]).flat_map.each_with_index do |database, index |
29
29
 
30
- defaultOptions="--column-statistics=0 #{mysqlDefaultOptions(@db_info, database)}"
31
- backupDir=backupDirName(id, "#{index}_#{database}")
30
+ default_options="--column-statistics=0 #{mysql_default_options(@db_info, database)}"
31
+ backup_dir=backup_dir_name(id, "#{index}_#{database}")
32
32
 
33
33
  commands = []
34
- if File.exist? backupDir
35
- puts "[skip - directory exists] #{backupDir}"
34
+ if File.exist? backup_dir
35
+ puts "[skip - directory exists] #{backup_dir}"
36
36
  return commands
37
37
  end
38
- commands.push "mkdir -p #{backupDir}"
38
+ commands.push "mkdir -p #{backup_dir}"
39
39
 
40
- backupFile="#{backupDir}/#{DateTime.now.strftime("%Y-%m-%d")}_#{id}"
41
- whereDate=(Date.today - limitDays).strftime("%Y-%m-%d 00:00:00")
42
- options=ENV['DUMP_OPTIONS'] || "--single-transaction --skip-lock-tables"
40
+ backup_file="#{backup_dir}/#{DateTime.now.strftime("%Y-%m-%d")}_#{id}"
41
+ where_date=(Date.today - limit_days).strftime("%Y-%m-%d 00:00:00")
42
+ options=ENV['DUMP_OPTIONS'] || "--single-transaction --skip-lock-tables --no-tablespaces"
43
43
 
44
- puts "backupFile=#{backupFile}"
44
+ puts "backupFile=#{backup_file}"
45
45
 
46
- ignoreTablesOption = @ignore_tables.map { |e| "--ignore-table=#{e.include?('.') ? e : "#{database}.#{e}"}" }.join(' ')
46
+ ignore_tables_option = @ignore_tables.map { |e| "--ignore-table=#{e.include?('.') ? e : "#{database}.#{e}"}" }.join(' ')
47
47
 
48
- commands.push gzipCommand("mysqldump --no-data #{ignoreTablesOption} #{defaultOptions}", isGzip, "#{backupFile}-schema.sql", gzipSuffix)
48
+ commands.push gzip_command("mysqldump --no-data #{ignore_tables_option} #{options} #{default_options}", use_gzip, "#{backup_file}-schema.sql", gzip_suffix)
49
49
 
50
- backupTables = []
50
+ backup_tables = []
51
51
 
52
52
  @data_tables.each {|table|
53
- where = table[:where].empty? ? "" : "--where=\"#{table[:where]} >= '#{whereDate}'\""
53
+ where = table[:where].empty? ? "" : "--where=\"#{table[:where]} >= '#{where_date}'\""
54
54
  if where.empty?
55
- backupTables.push(table[:name])
55
+ backup_tables.push(table[:name])
56
56
  next
57
57
  else
58
- commands.push(gzipCommand("mysqldump --no-create-info #{options} #{where} #{defaultOptions} #{table[:name]}", isGzip, "#{backupFile}-#{table[:name]}.sql", gzipSuffix))
58
+ commands.push(gzip_command("mysqldump --no-create-info #{options} #{where} #{default_options} #{table[:name]}", use_gzip, "#{backup_file}-#{table[:name]}.sql", gzip_suffix))
59
59
  end
60
60
  }
61
61
 
62
- commands.push(gzipCommand("mysqldump --no-create-info #{ignoreTablesOption} #{options} #{defaultOptions} #{backupTables.join(' ')}", isGzip, "#{backupFile}-all-other-tables.sql", gzipSuffix))
62
+ commands.push(gzip_command("mysqldump --no-create-info #{ignore_tables_option} #{options} #{default_options} #{backup_tables.join(' ')}", use_gzip, "#{backup_file}-all-other-tables.sql", gzip_suffix))
63
63
  commands
64
64
  end
65
65
  end
66
66
 
67
- def gzipCommand(command, isGzip, file, gzipSuffix = '.gz')
68
- "#{command} #{isGzip ? '| gzip ' : ''} > #{file}#{isGzip ? gzipSuffix : ''}"
67
+ def gzip_command(command, use_gzip, file, gzip_suffix = '.gz')
68
+ "#{command} #{use_gzip ? '| gzip ' : ''} > #{file}#{use_gzip ? gzip_suffix : ''}"
69
69
  end
70
70
 
71
71
  end
@@ -1,7 +1,7 @@
1
1
 
2
2
 
3
- def run(isRun, command)
4
- if not isRun
3
+ def run(is_run, command)
4
+ if not is_run
5
5
  puts "[dryRun] #{command}"
6
6
  else
7
7
  puts "Running: [#{command}]"
@@ -9,11 +9,11 @@ def run(isRun, command)
9
9
  end
10
10
  end
11
11
 
12
- def backupDirName(id, dbName = "")
13
- "backup-#{id}#{dbName.empty? ? '' : "/#{dbName}"}"
12
+ def backup_dir_name(id, db_name = "")
13
+ "backup-#{id}#{db_name.empty? ? '' : "/#{db_name}"}"
14
14
  end
15
15
 
16
- def mysqlDefaultOptions(db_info, database)
16
+ def mysql_default_options(db_info, database)
17
17
  " --ssl-mode=disabled -h #{db_info[:host]} -u #{db_info[:user]} #{db_info[:password].to_s.empty? ? '' : " -p'#{db_info[:password]}'"} #{db_info[:port].to_s.empty? ? '' : " -P'#{db_info[:port]}'"} #{database} "
18
18
  end
19
19
 
@@ -22,7 +22,7 @@ def verify_tools_exist
22
22
  missing_tools = []
23
23
 
24
24
  tools.each do |tool|
25
- if not system("which #{tool} > /dev/null 2>&1")
25
+ unless system("which #{tool} > /dev/null 2>&1")
26
26
  missing_tools << tool
27
27
  puts "'#{tool}' is not available"
28
28
  end
@@ -6,10 +6,10 @@ module MySQLDBTool
6
6
 
7
7
  def initialize(options = {})
8
8
  @options = options
9
- tableConfig = MySQLDBTool::Config::ConfigLoader.load(options[:env])
10
- @data_tables = tableConfig[:data_tables]
11
- @ignore_tables = tableConfig[:ignore_tables]
12
- @db_info = tableConfig[:db_info]
9
+ config = MySQLDBTool::Config::ConfigLoader.load(options[:env])
10
+ @data_tables = config[:data_tables]
11
+ @ignore_tables = config[:ignore_tables]
12
+ @db_info = config[:db_info]
13
13
 
14
14
  @db_info[:database] = @options[:database] if @options[:database]
15
15
  end
@@ -25,47 +25,44 @@ module MySQLDBTool
25
25
  end
26
26
 
27
27
  id=@options[:id] || "0"
28
- isRun=@options[:run]
29
- isDropAllTables=@options[:drop_all_tables]
28
+ is_drop_all_tables=@options[:drop_all_tables]
30
29
 
31
- puts "ARGV=#{ARGV}, env=#{env}, id=#{id}, run=#{isRun} isDropAllTables=#{isDropAllTables}"
30
+ backup_dir=backup_dir_name(id)
32
31
 
33
- backupDir=backupDirName(id)
34
-
35
- puts "backupDir=#{backupDir}"
32
+ puts "backupDir=#{backup_dir}"
36
33
  databases = Array(@db_info[:database])
37
34
 
38
- databaseMap = {}
39
- sameDb = true
35
+ database_map = {}
36
+ same_db = true
40
37
 
41
- Dir.entries(backupDir).reject {|f| File.directory? f}.sort.each do |f|
38
+ Dir.entries(backup_dir).reject {|f| File.directory? f}.sort.each do |f|
42
39
 
43
40
  index, origin_database = split_integer_and_string(f)
44
41
  database = get_element_or_last(databases, index)
45
- sameDb = sameDb && (database == origin_database)
46
- databaseMap["`#{origin_database}`\\."] = "`#{database}`."
42
+ same_db = same_db && (database == origin_database)
43
+ database_map["`#{origin_database}`\\."] = "`#{database}`."
47
44
  end
48
45
 
49
- gsubstring = sameDb ? "" : databaseMap.map { |k,v| ".gsub(/#{k}/, \"#{v}\")" }.join("")
46
+ replace_db_names_command = same_db ? "" : database_map.map { |k,v| ".gsub(/#{k}/, \"#{v}\")" }.join("")
50
47
 
51
- Dir.entries(backupDir).reject {|f| File.directory? f}.sort.flat_map do |f|
48
+ Dir.entries(backup_dir).reject {|f| File.directory? f}.sort.flat_map do |f|
52
49
 
53
50
  commands = []
54
51
 
55
52
  index, origin_database = split_integer_and_string(f)
56
53
  database = get_element_or_last(databases, index)
57
54
 
58
- defaultOptions=mysqlDefaultOptions(@db_info, database)
59
- backupDir=backupDirName(id, f)
55
+ default_options=mysql_default_options(@db_info, database)
56
+ backup_dir=backup_dir_name(id, f)
60
57
 
61
- commands.push("cat #{MySQLDBTool.find_resource('sql/drop_all_tables.sql')} | mysql #{defaultOptions}") if isDropAllTables
62
- commands.push("cat #{MySQLDBTool.find_resource('sql/drop_all_views.sql')} | mysql #{defaultOptions}") if isDropAllTables
58
+ commands.push("cat #{MySQLDBTool.find_resource('sql/drop_all_tables.sql')} | mysql #{default_options}") if is_drop_all_tables
59
+ commands.push("cat #{MySQLDBTool.find_resource('sql/drop_all_views.sql')} | mysql #{default_options}") if is_drop_all_tables
63
60
 
64
- Dir.entries(backupDir).reject {|f| File.directory? f} .select {|f| f.include?("-schema.sql")} .each {|f|
65
- restore_each(commands, backupDir+"/"+f, defaultOptions, gsubstring)
61
+ Dir.entries(backup_dir).reject {|f| File.directory? f}.select {|f| f.include?("-schema.sql")}.each {|f|
62
+ restore_each(commands, backup_dir+"/"+f, default_options, replace_db_names_command)
66
63
  }
67
- Dir.entries(backupDir).reject {|f| File.directory? f} .reject {|f| f.include?("-schema.sql")} .each {|f|
68
- restore_each(commands, backupDir+"/"+f, defaultOptions, gsubstring)
64
+ Dir.entries(backup_dir).reject {|f| File.directory? f}.reject {|f| f.include?("-schema.sql")}.each {|f|
65
+ restore_each(commands, backup_dir+"/"+f, default_options, replace_db_names_command)
69
66
  }
70
67
  commands
71
68
  end
@@ -73,18 +70,18 @@ module MySQLDBTool
73
70
 
74
71
  private
75
72
 
76
- def restore_each(commands, file, defaultOptions, gsubstring)
73
+ def restore_each(commands, file, default_options, replace_db_names_command)
77
74
  command = ""
78
- replacing = " | ruby -pe '$_=$_#{gsubstring}'" unless gsubstring.empty?
75
+ replacing = " | ruby -pe '$_=$_#{replace_db_names_command}'" unless replace_db_names_command.empty?
79
76
  if file.end_with? ".sql"
80
- command = "cat #{file} #{replacing} | mysql #{defaultOptions}"
77
+ command = "cat #{file} #{replacing} | mysql #{default_options}"
81
78
  elsif gzip_file?(file)
82
- command = "zcat #{file} #{replacing} | mysql #{defaultOptions}"
79
+ command = "zcat #{file} #{replacing} | mysql #{default_options}"
83
80
  else
84
81
  puts "not supported file #{file}"
85
82
  end
86
83
 
87
- commands.push(command) if not command.empty?
84
+ commands.push(command) unless command.empty?
88
85
  end
89
86
 
90
87
  def split_integer_and_string(input)
@@ -1,5 +1,5 @@
1
1
  # lib/mysql_db_tool/version.rb
2
2
 
3
3
  module MySQLDBTool
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql_db_tool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soonoh Jung
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-17 00:00:00.000000000 Z
11
+ date: 2025-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  requirements: []
98
- rubygems_version: 3.5.17
98
+ rubygems_version: 3.5.3
99
99
  signing_key:
100
100
  specification_version: 4
101
101
  summary: MySQL DB Tool