mysql_db_tool 0.2.2 → 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: 9c70b37689d0fb784905734790283707a98f4956c635fd29a3f17ae19fc18a2c
4
- data.tar.gz: 71982b0e54f79191e98d34436f9c78f410cbd59db904f00b00cb86c5eacf4fa2
3
+ metadata.gz: fdecb06617c9937d6c99c95a6f3dc167dff3398a05d7cdfd1c7d27e072204222
4
+ data.tar.gz: cb1bbc19b30dd0f1208f470254c4737eff569f641d4df76c94a3f0e5340b71d6
5
5
  SHA512:
6
- metadata.gz: eb654e6a3b37d3da2c100db8274d600e167afdb6cdba6100d3a57cf8f22f04ff91706050785d8f310eda2a5aa8902ae6a32a9e58e0dd5285a93a81f7e64754a5
7
- data.tar.gz: dcea7f1a62444d8e074b7bee219328a4e3a616e719e56ac2baf880d3688c332381c1dc6df793337848023d2186bb1c54268af46b44c4483d61730653ec5ade89
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
 
@@ -77,6 +77,10 @@ gen_create_db_user {user} {password} {db} {host}
77
77
 
78
78
  ## Installing Ruby
79
79
 
80
+ You can use OS default ruby if it is already installed.
81
+
82
+ Below is additional steps to install or develop with the latest version of Ruby.
83
+
80
84
  Install using `rbenv` on Mac OS X
81
85
 
82
86
  ```bash
@@ -87,6 +91,3 @@ rbenv global 3.3.0
87
91
  ruby -v
88
92
  ```
89
93
 
90
- ## TODO
91
-
92
- * [ ] Add option to get db configuration information from spring cloud config
data/bin/mysql_backup CHANGED
@@ -9,7 +9,8 @@ options = {
9
9
  env: "local",
10
10
  id: "0",
11
11
  run: false,
12
- gzip: true
12
+ gzip: true,
13
+ gzip_suffix: ".gz"
13
14
  }
14
15
 
15
16
  # Parse command line arguments
@@ -19,11 +20,16 @@ OptionParser.new do |opts|
19
20
  MySQLDBTool.env_opt(options, opts)
20
21
  MySQLDBTool.id_opt(options, opts)
21
22
  MySQLDBTool.run_opt(options, opts)
23
+ MySQLDBTool.database_opt(options, opts)
22
24
 
23
25
  opts.on("-g", "--[no-]gzip", "Enable or disable gzip (default: enabled)") do |gzip|
24
26
  options[:gzip] = gzip
25
27
  end
26
28
 
29
+ opts.on("-s", "--gzip-suffix SUFFIX", "Set the gzip suffix (default: .gz), some platform requires '.Z'") do |gzip_suffix|
30
+ options[:gzip_suffix] = gzip_suffix
31
+ end
32
+
27
33
  opts.on("-h", "--help", "Show this help message") do
28
34
  puts opts
29
35
  exit
data/bin/mysql_restore CHANGED
@@ -19,8 +19,9 @@ OptionParser.new do |opts|
19
19
  MySQLDBTool.env_opt(options, opts)
20
20
  MySQLDBTool.id_opt(options, opts)
21
21
  MySQLDBTool.run_opt(options, opts)
22
+ MySQLDBTool.database_opt(options, opts)
22
23
 
23
- opts.on("-d", "--[no-]drop-all-tables", "Drop all tables before restoring (default: disabled)") do |drop_all_tables|
24
+ opts.on("-D", "--[no-]drop-all-tables", "Drop all tables before restoring (default: disabled)") do |drop_all_tables|
24
25
  options[:drop_all_tables] = drop_all_tables
25
26
  end
26
27
 
@@ -8,10 +8,12 @@ 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]
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
+ @db_info[:database] = @options[:database] if @options[:database]
15
17
  end
16
18
 
17
19
  def perform
@@ -19,50 +21,51 @@ module MySQLDBTool
19
21
  verify_tools_exist
20
22
 
21
23
  id=@options[:id] || "0"
22
- isGzip=@options[:gzip]
23
- limitDays=@options[:limit_days] || 3
24
+ use_gzip=@options[:gzip]
25
+ limit_days=@options[:limit_days] || 3
26
+ gzip_suffix = @options[:gzip_suffix] || '.gz'
24
27
 
25
28
  Array(@db_info[:database]).flat_map.each_with_index do |database, index |
26
29
 
27
- defaultOptions="--column-statistics=0 #{mysqlDefaultOptions(@db_info, database)}"
28
- 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}")
29
32
 
30
33
  commands = []
31
- if File.exist? backupDir
32
- puts "[skip - directory exists] #{backupDir}"
34
+ if File.exist? backup_dir
35
+ puts "[skip - directory exists] #{backup_dir}"
33
36
  return commands
34
37
  end
35
- commands.push "mkdir -p #{backupDir}"
38
+ commands.push "mkdir -p #{backup_dir}"
36
39
 
37
- backupFile="#{backupDir}/#{DateTime.now.strftime("%Y-%m-%d")}_#{id}"
38
- whereDate=(Date.today - limitDays).strftime("%Y-%m-%d 00:00:00")
39
- 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"
40
43
 
41
- puts "backupFile=#{backupFile}"
44
+ puts "backupFile=#{backup_file}"
42
45
 
43
- ignoreTablesOption = @ignore_tables.map { |e| "--ignore-table=#{@db_info[:database]}.#{e}" }.join(' ')
46
+ ignore_tables_option = @ignore_tables.map { |e| "--ignore-table=#{e.include?('.') ? e : "#{database}.#{e}"}" }.join(' ')
44
47
 
45
- commands.push gzipCommand("mysqldump --no-data #{ignoreTablesOption} #{defaultOptions}", isGzip, "#{backupFile}-schema.sql#{isGzip ? '.gz' : ''}")
48
+ commands.push gzip_command("mysqldump --no-data #{ignore_tables_option} #{options} #{default_options}", use_gzip, "#{backup_file}-schema.sql", gzip_suffix)
46
49
 
47
- backupTables = []
50
+ backup_tables = []
48
51
 
49
52
  @data_tables.each {|table|
50
- where = table[:where].empty? ? "" : "--where=\"#{table[:where]} >= '#{whereDate}'\""
53
+ where = table[:where].empty? ? "" : "--where=\"#{table[:where]} >= '#{where_date}'\""
51
54
  if where.empty?
52
- backupTables.push(table[:name])
55
+ backup_tables.push(table[:name])
53
56
  next
54
57
  else
55
- commands.push(gzipCommand("mysqldump --no-create-info #{options} #{where} #{defaultOptions} #{table[:name]}", isGzip, "#{backupFile}-#{table[:name]}.sql#{isGzip ? '.gz' : ''}"))
58
+ commands.push(gzip_command("mysqldump --no-create-info #{options} #{where} #{default_options} #{table[:name]}", use_gzip, "#{backup_file}-#{table[:name]}.sql", gzip_suffix))
56
59
  end
57
60
  }
58
61
 
59
- commands.push(gzipCommand("mysqldump --no-create-info #{options} #{defaultOptions} #{backupTables.join(' ')}", isGzip, "#{backupFile}-all-other-tables.sql#{isGzip ? '.gz' : ''}"))
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))
60
63
  commands
61
64
  end
62
65
  end
63
66
 
64
- def gzipCommand(command, isGzip, file)
65
- "#{command} #{isGzip ? '| gzip ' : ''} > #{file}"
67
+ def gzip_command(command, use_gzip, file, gzip_suffix = '.gz')
68
+ "#{command} #{use_gzip ? '| gzip ' : ''} > #{file}#{use_gzip ? gzip_suffix : ''}"
66
69
  end
67
70
 
68
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,12 @@ 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
+
14
+ @db_info[:database] = @options[:database] if @options[:database]
13
15
  end
14
16
 
15
17
  def perform
@@ -23,47 +25,44 @@ module MySQLDBTool
23
25
  end
24
26
 
25
27
  id=@options[:id] || "0"
26
- isRun=@options[:run]
27
- isDropAllTables=@options[:drop_all_tables]
28
-
29
- puts "ARGV=#{ARGV}, env=#{env}, id=#{id}, run=#{isRun} isDropAllTables=#{isDropAllTables}"
28
+ is_drop_all_tables=@options[:drop_all_tables]
30
29
 
31
- backupDir=backupDirName(id)
30
+ backup_dir=backup_dir_name(id)
32
31
 
33
- puts "backupDir=#{backupDir}"
32
+ puts "backupDir=#{backup_dir}"
34
33
  databases = Array(@db_info[:database])
35
34
 
36
- databaseMap = {}
37
- sameDb = true
35
+ database_map = {}
36
+ same_db = true
38
37
 
39
- 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|
40
39
 
41
40
  index, origin_database = split_integer_and_string(f)
42
41
  database = get_element_or_last(databases, index)
43
- sameDb = sameDb && (database == origin_database)
44
- databaseMap["`#{origin_database}`\\."] = "`#{database}`."
42
+ same_db = same_db && (database == origin_database)
43
+ database_map["`#{origin_database}`\\."] = "`#{database}`."
45
44
  end
46
45
 
47
- 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("")
48
47
 
49
- 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|
50
49
 
51
50
  commands = []
52
51
 
53
52
  index, origin_database = split_integer_and_string(f)
54
53
  database = get_element_or_last(databases, index)
55
54
 
56
- defaultOptions=mysqlDefaultOptions(@db_info, database)
57
- backupDir=backupDirName(id, f)
55
+ default_options=mysql_default_options(@db_info, database)
56
+ backup_dir=backup_dir_name(id, f)
58
57
 
59
- commands.push("cat #{MySQLDBTool.find_resource('sql/drop_all_tables.sql')} | mysql #{defaultOptions}") if isDropAllTables
60
- 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
61
60
 
62
- Dir.entries(backupDir).reject {|f| File.directory? f} .select {|f| f.include?("-schema.sql")} .each {|f|
63
- 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)
64
63
  }
65
- Dir.entries(backupDir).reject {|f| File.directory? f} .reject {|f| f.include?("-schema.sql")} .each {|f|
66
- 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)
67
66
  }
68
67
  commands
69
68
  end
@@ -71,18 +70,18 @@ module MySQLDBTool
71
70
 
72
71
  private
73
72
 
74
- def restore_each(commands, file, defaultOptions, gsubstring)
73
+ def restore_each(commands, file, default_options, replace_db_names_command)
75
74
  command = ""
76
- replacing = " | ruby -pe '$_=$_#{gsubstring}'" unless gsubstring.empty?
77
- if file.end_with? ".sql.gz"
78
- command = "zcat #{file} #{replacing} | mysql #{defaultOptions}"
79
- elsif file.end_with? ".sql"
80
- command = "cat #{file} #{replacing} | mysql #{defaultOptions}"
75
+ replacing = " | ruby -pe '$_=$_#{replace_db_names_command}'" unless replace_db_names_command.empty?
76
+ if file.end_with? ".sql"
77
+ command = "cat #{file} #{replacing} | mysql #{default_options}"
78
+ elsif gzip_file?(file)
79
+ command = "zcat #{file} #{replacing} | mysql #{default_options}"
81
80
  else
82
81
  puts "not supported file #{file}"
83
82
  end
84
83
 
85
- commands.push(command) if not command.empty?
84
+ commands.push(command) unless command.empty?
86
85
  end
87
86
 
88
87
  def split_integer_and_string(input)
@@ -96,6 +95,14 @@ module MySQLDBTool
96
95
  index < array.length ? array[index] : array.last
97
96
  end
98
97
 
98
+ def gzip_file?(file_path)
99
+ magic_number = "\x1F\x8B".force_encoding('ASCII-8BIT')
100
+
101
+ File.open(file_path, "rb") do |file|
102
+ file.read(2) == magic_number
103
+ end
104
+ end
105
+
99
106
  end
100
107
  end
101
108
 
@@ -1,5 +1,5 @@
1
1
  # lib/mysql_db_tool/version.rb
2
2
 
3
3
  module MySQLDBTool
4
- VERSION = "0.2.2"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/mysql_db_tool.rb CHANGED
@@ -32,6 +32,12 @@ module MySQLDBTool
32
32
  end
33
33
  end
34
34
 
35
+ def self.database_opt(options, opts)
36
+ opts.on("-d", "--database DATABASE", "Override option for dbInfo.database configuration") do |database|
37
+ options[:database] = database.split(',')
38
+ end
39
+ end
40
+
35
41
  # You might want to add methods to easily access your main functionalities
36
42
  def self.backup(options = {})
37
43
  commands = Backup.new(options).perform
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.2.2
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-14 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