mysql_db_tool 0.3.0 → 0.4.1

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: 7b51856f5b5dc1e95daf56bfb215edef9a3a4ced392cad7fdcb99f83f5956e09
4
+ data.tar.gz: 60eae7d024f7d9fa6457b929f47c3ce5e890a6de6bb9ca7e300033250a7c1145
5
5
  SHA512:
6
- metadata.gz: 014563a869d15aef0140f1d04580896cb12616efeb5e95435e1c84dc401e2ae538657c853ff63d8376464f2091f14a859ac031e2715799d98875a12e3b067e0b
7
- data.tar.gz: 689bc7c215256672151f4b8041303d74570449cdfababbf48025155da23940e242a8697bd877987fb7a7008cc6a7a106cf6bc108ec74c7d452ad4f353759bd43
6
+ metadata.gz: 88b5e23657d090a1c3327da8a9e1ad189192e37451df741ab4da451daf881c5cdeaa2635431b0ef4046cb1c6093a80619065ba9b04ca14735d21a06b14226502
7
+ data.tar.gz: 95a1efb08ead4a9e772fa4a32058395b5b51772101f7f544e84f23d7fc93c4e7cb4b66c8a52da7706f56f3f62eb0ca5375962edf440d5fb2caf9522f966aca8a
data/README.md CHANGED
@@ -39,6 +39,8 @@ Create a config-<env>.json file under the current directory according to the dat
39
39
  gem install mysql_db_tool
40
40
  ```
41
41
 
42
+ [Example script](docs/examples.md)
43
+
42
44
  ## Data backup
43
45
 
44
46
  ```shell
@@ -53,7 +55,7 @@ you can get help by running `mysql_backup -h`
53
55
  * gzip? - default (true), whether to compress with gzip or not
54
56
 
55
57
  * 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)
58
+ if not set, it will use default options for mysqldump. (--single-transaction --skip-lock-tables --no-tablespaces)
57
59
 
58
60
  After execution, a directory named "backup-{backup id}" will be created under the current directory.
59
61
 
@@ -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,5 @@
1
-
2
-
3
- def run(isRun, command)
4
- if not isRun
1
+ def run(is_run, command)
2
+ if not is_run
5
3
  puts "[dryRun] #{command}"
6
4
  else
7
5
  puts "Running: [#{command}]"
@@ -9,12 +7,17 @@ def run(isRun, command)
9
7
  end
10
8
  end
11
9
 
12
- def backupDirName(id, dbName = "")
13
- "backup-#{id}#{dbName.empty? ? '' : "/#{dbName}"}"
10
+ def backup_dir_name(id, db_name = "")
11
+ "backup-#{id}#{db_name.empty? ? '' : "/#{db_name}"}"
14
12
  end
15
13
 
16
- def mysqlDefaultOptions(db_info, database)
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} "
14
+ def mysql_default_options(db_info, database)
15
+ dump_options = ENV['DUMP_OPTIONS'] || ''
16
+ ssl_mode_disabled = '--ssl-mode=disabled'
17
+ # Remove default --ssl-mode=disabled if any --ssl-mode is present in DUMP_OPTIONS
18
+ ssl_mode = dump_options.include?('--ssl-mode')
19
+ ssl_mode_disabled = '' if ssl_mode
20
+ " #{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
21
  end
19
22
 
20
23
  def verify_tools_exist
@@ -22,7 +25,7 @@ def verify_tools_exist
22
25
  missing_tools = []
23
26
 
24
27
  tools.each do |tool|
25
- if not system("which #{tool} > /dev/null 2>&1")
28
+ unless system("which #{tool} > /dev/null 2>&1")
26
29
  missing_tools << tool
27
30
  puts "'#{tool}' is not available"
28
31
  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"
5
- end
4
+ VERSION = "0.4.1"
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.1
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-06-05 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