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 +4 -4
- data/README.md +5 -4
- data/bin/mysql_backup +7 -1
- data/bin/mysql_restore +2 -1
- data/lib/mysql_db_tool/backup.rb +27 -24
- data/lib/mysql_db_tool/db_backup_base.rb +6 -6
- data/lib/mysql_db_tool/restore.rb +39 -32
- data/lib/mysql_db_tool/version.rb +1 -1
- data/lib/mysql_db_tool.rb +6 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fdecb06617c9937d6c99c95a6f3dc167dff3398a05d7cdfd1c7d27e072204222
|
4
|
+
data.tar.gz: cb1bbc19b30dd0f1208f470254c4737eff569f641d4df76c94a3f0e5340b71d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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("-
|
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
@@ -8,10 +8,12 @@ module MySQLDBTool
|
|
8
8
|
|
9
9
|
def initialize(options = {})
|
10
10
|
@options = options
|
11
|
-
|
12
|
-
@data_tables =
|
13
|
-
@ignore_tables =
|
14
|
-
@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
|
-
|
23
|
-
|
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
|
-
|
28
|
-
|
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?
|
32
|
-
puts "[skip - directory exists] #{
|
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 #{
|
38
|
+
commands.push "mkdir -p #{backup_dir}"
|
36
39
|
|
37
|
-
|
38
|
-
|
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=#{
|
44
|
+
puts "backupFile=#{backup_file}"
|
42
45
|
|
43
|
-
|
46
|
+
ignore_tables_option = @ignore_tables.map { |e| "--ignore-table=#{e.include?('.') ? e : "#{database}.#{e}"}" }.join(' ')
|
44
47
|
|
45
|
-
commands.push
|
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
|
-
|
50
|
+
backup_tables = []
|
48
51
|
|
49
52
|
@data_tables.each {|table|
|
50
|
-
where = table[:where].empty? ? "" : "--where=\"#{table[:where]} >= '#{
|
53
|
+
where = table[:where].empty? ? "" : "--where=\"#{table[:where]} >= '#{where_date}'\""
|
51
54
|
if where.empty?
|
52
|
-
|
55
|
+
backup_tables.push(table[:name])
|
53
56
|
next
|
54
57
|
else
|
55
|
-
commands.push(
|
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(
|
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
|
65
|
-
"#{command} #{
|
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(
|
4
|
-
if not
|
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
|
13
|
-
"backup-#{id}#{
|
12
|
+
def backup_dir_name(id, db_name = "")
|
13
|
+
"backup-#{id}#{db_name.empty? ? '' : "/#{db_name}"}"
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
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
|
-
|
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
|
-
|
10
|
-
@data_tables =
|
11
|
-
@ignore_tables =
|
12
|
-
@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
|
-
|
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
|
-
|
30
|
+
backup_dir=backup_dir_name(id)
|
32
31
|
|
33
|
-
puts "backupDir=#{
|
32
|
+
puts "backupDir=#{backup_dir}"
|
34
33
|
databases = Array(@db_info[:database])
|
35
34
|
|
36
|
-
|
37
|
-
|
35
|
+
database_map = {}
|
36
|
+
same_db = true
|
38
37
|
|
39
|
-
Dir.entries(
|
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
|
-
|
44
|
-
|
42
|
+
same_db = same_db && (database == origin_database)
|
43
|
+
database_map["`#{origin_database}`\\."] = "`#{database}`."
|
45
44
|
end
|
46
45
|
|
47
|
-
|
46
|
+
replace_db_names_command = same_db ? "" : database_map.map { |k,v| ".gsub(/#{k}/, \"#{v}\")" }.join("")
|
48
47
|
|
49
|
-
Dir.entries(
|
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
|
-
|
57
|
-
|
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 #{
|
60
|
-
commands.push("cat #{MySQLDBTool.find_resource('sql/drop_all_views.sql')} | mysql #{
|
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(
|
63
|
-
restore_each(commands,
|
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(
|
66
|
-
restore_each(commands,
|
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,
|
73
|
+
def restore_each(commands, file, default_options, replace_db_names_command)
|
75
74
|
command = ""
|
76
|
-
replacing = " | ruby -pe '$_=$_#{
|
77
|
-
if file.end_with? ".sql
|
78
|
-
command = "
|
79
|
-
elsif file
|
80
|
-
command = "
|
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)
|
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
|
|
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.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:
|
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.
|
98
|
+
rubygems_version: 3.5.3
|
99
99
|
signing_key:
|
100
100
|
specification_version: 4
|
101
101
|
summary: MySQL DB Tool
|