mysql_db_tool 0.2.2 → 0.3.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: 246f356370dec4811df1950c8972b0110618d4551bc3776c746efd597413f5b4
4
+ data.tar.gz: 58e81526ba38e1f9201396d808d5aeb6499bb7f91d479a5e4df8f4110d07652f
5
5
  SHA512:
6
- metadata.gz: eb654e6a3b37d3da2c100db8274d600e167afdb6cdba6100d3a57cf8f22f04ff91706050785d8f310eda2a5aa8902ae6a32a9e58e0dd5285a93a81f7e64754a5
7
- data.tar.gz: dcea7f1a62444d8e074b7bee219328a4e3a616e719e56ac2baf880d3688c332381c1dc6df793337848023d2186bb1c54268af46b44c4483d61730653ec5ade89
6
+ metadata.gz: 014563a869d15aef0140f1d04580896cb12616efeb5e95435e1c84dc401e2ae538657c853ff63d8376464f2091f14a859ac031e2715799d98875a12e3b067e0b
7
+ data.tar.gz: 689bc7c215256672151f4b8041303d74570449cdfababbf48025155da23940e242a8697bd877987fb7a7008cc6a7a106cf6bc108ec74c7d452ad4f353759bd43
data/README.md CHANGED
@@ -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
 
@@ -12,6 +12,8 @@ module MySQLDBTool
12
12
  @data_tables = tableConfig[:data_tables]
13
13
  @ignore_tables = tableConfig[:ignore_tables]
14
14
  @db_info = tableConfig[:db_info]
15
+
16
+ @db_info[:database] = @options[:database] if @options[:database]
15
17
  end
16
18
 
17
19
  def perform
@@ -21,6 +23,7 @@ module MySQLDBTool
21
23
  id=@options[:id] || "0"
22
24
  isGzip=@options[:gzip]
23
25
  limitDays=@options[:limit_days] || 3
26
+ gzipSuffix = @options[:gzip_suffix] || '.gz'
24
27
 
25
28
  Array(@db_info[:database]).flat_map.each_with_index do |database, index |
26
29
 
@@ -40,9 +43,9 @@ module MySQLDBTool
40
43
 
41
44
  puts "backupFile=#{backupFile}"
42
45
 
43
- ignoreTablesOption = @ignore_tables.map { |e| "--ignore-table=#{@db_info[:database]}.#{e}" }.join(' ')
46
+ ignoreTablesOption = @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 gzipCommand("mysqldump --no-data #{ignoreTablesOption} #{defaultOptions}", isGzip, "#{backupFile}-schema.sql", gzipSuffix)
46
49
 
47
50
  backupTables = []
48
51
 
@@ -52,17 +55,17 @@ module MySQLDBTool
52
55
  backupTables.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(gzipCommand("mysqldump --no-create-info #{options} #{where} #{defaultOptions} #{table[:name]}", isGzip, "#{backupFile}-#{table[:name]}.sql", gzipSuffix))
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(gzipCommand("mysqldump --no-create-info #{ignoreTablesOption} #{options} #{defaultOptions} #{backupTables.join(' ')}", isGzip, "#{backupFile}-all-other-tables.sql", gzipSuffix))
60
63
  commands
61
64
  end
62
65
  end
63
66
 
64
- def gzipCommand(command, isGzip, file)
65
- "#{command} #{isGzip ? '| gzip ' : ''} > #{file}"
67
+ def gzipCommand(command, isGzip, file, gzipSuffix = '.gz')
68
+ "#{command} #{isGzip ? '| gzip ' : ''} > #{file}#{isGzip ? gzipSuffix : ''}"
66
69
  end
67
70
 
68
71
  end
@@ -10,6 +10,8 @@ module MySQLDBTool
10
10
  @data_tables = tableConfig[:data_tables]
11
11
  @ignore_tables = tableConfig[:ignore_tables]
12
12
  @db_info = tableConfig[:db_info]
13
+
14
+ @db_info[:database] = @options[:database] if @options[:database]
13
15
  end
14
16
 
15
17
  def perform
@@ -74,10 +76,10 @@ module MySQLDBTool
74
76
  def restore_each(commands, file, defaultOptions, gsubstring)
75
77
  command = ""
76
78
  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"
79
+ if file.end_with? ".sql"
80
80
  command = "cat #{file} #{replacing} | mysql #{defaultOptions}"
81
+ elsif gzip_file?(file)
82
+ command = "zcat #{file} #{replacing} | mysql #{defaultOptions}"
81
83
  else
82
84
  puts "not supported file #{file}"
83
85
  end
@@ -96,6 +98,14 @@ module MySQLDBTool
96
98
  index < array.length ? array[index] : array.last
97
99
  end
98
100
 
101
+ def gzip_file?(file_path)
102
+ magic_number = "\x1F\x8B".force_encoding('ASCII-8BIT')
103
+
104
+ File.open(file_path, "rb") do |file|
105
+ file.read(2) == magic_number
106
+ end
107
+ end
108
+
99
109
  end
100
110
  end
101
111
 
@@ -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.3.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.3.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: 2024-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler