mysql_db_tool 0.2.1 → 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 +4 -4
- data/README.md +4 -3
- data/bin/mysql_backup +7 -1
- data/bin/mysql_restore +2 -1
- data/lib/mysql_db_tool/backup.rb +9 -6
- data/lib/mysql_db_tool/restore.rb +15 -5
- data/lib/mysql_db_tool/version.rb +1 -1
- data/lib/mysql_db_tool.rb +6 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 246f356370dec4811df1950c8972b0110618d4551bc3776c746efd597413f5b4
|
4
|
+
data.tar.gz: 58e81526ba38e1f9201396d808d5aeb6499bb7f91d479a5e4df8f4110d07652f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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("-
|
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
|
|
data/lib/mysql_db_tool/backup.rb
CHANGED
@@ -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=#{
|
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
|
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
|
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
|
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
|
@@ -46,10 +48,10 @@ module MySQLDBTool
|
|
46
48
|
|
47
49
|
gsubstring = sameDb ? "" : databaseMap.map { |k,v| ".gsub(/#{k}/, \"#{v}\")" }.join("")
|
48
50
|
|
49
|
-
commands = []
|
50
|
-
|
51
51
|
Dir.entries(backupDir).reject {|f| File.directory? f}.sort.flat_map do |f|
|
52
52
|
|
53
|
+
commands = []
|
54
|
+
|
53
55
|
index, origin_database = split_integer_and_string(f)
|
54
56
|
database = get_element_or_last(databases, index)
|
55
57
|
|
@@ -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
|
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
|
|
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.
|
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-
|
11
|
+
date: 2024-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|