mysql_db_tool 0.3.0 → 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 +1 -1
- data/lib/mysql_db_tool/backup.rb +26 -26
- data/lib/mysql_db_tool/db_backup_base.rb +6 -6
- data/lib/mysql_db_tool/restore.rb +27 -30
- data/lib/mysql_db_tool/version.rb +1 -1
- 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
|
|
data/lib/mysql_db_tool/backup.rb
CHANGED
@@ -8,11 +8,11 @@ module MySQLDBTool
|
|
8
8
|
|
9
9
|
def initialize(options = {})
|
10
10
|
@options = options
|
11
|
-
|
12
|
-
@data_tables =
|
13
|
-
@ignore_tables =
|
14
|
-
@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
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
31
|
-
|
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?
|
35
|
-
puts "[skip - directory exists] #{
|
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 #{
|
38
|
+
commands.push "mkdir -p #{backup_dir}"
|
39
39
|
|
40
|
-
|
41
|
-
|
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=#{
|
44
|
+
puts "backupFile=#{backup_file}"
|
45
45
|
|
46
|
-
|
46
|
+
ignore_tables_option = @ignore_tables.map { |e| "--ignore-table=#{e.include?('.') ? e : "#{database}.#{e}"}" }.join(' ')
|
47
47
|
|
48
|
-
commands.push
|
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
|
-
|
50
|
+
backup_tables = []
|
51
51
|
|
52
52
|
@data_tables.each {|table|
|
53
|
-
where = table[:where].empty? ? "" : "--where=\"#{table[:where]} >= '#{
|
53
|
+
where = table[:where].empty? ? "" : "--where=\"#{table[:where]} >= '#{where_date}'\""
|
54
54
|
if where.empty?
|
55
|
-
|
55
|
+
backup_tables.push(table[:name])
|
56
56
|
next
|
57
57
|
else
|
58
|
-
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))
|
59
59
|
end
|
60
60
|
}
|
61
61
|
|
62
|
-
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))
|
63
63
|
commands
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
-
def
|
68
|
-
"#{command} #{
|
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,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,10 @@ 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
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
|
-
|
29
|
-
isDropAllTables=@options[:drop_all_tables]
|
28
|
+
is_drop_all_tables=@options[:drop_all_tables]
|
30
29
|
|
31
|
-
|
30
|
+
backup_dir=backup_dir_name(id)
|
32
31
|
|
33
|
-
backupDir
|
34
|
-
|
35
|
-
puts "backupDir=#{backupDir}"
|
32
|
+
puts "backupDir=#{backup_dir}"
|
36
33
|
databases = Array(@db_info[:database])
|
37
34
|
|
38
|
-
|
39
|
-
|
35
|
+
database_map = {}
|
36
|
+
same_db = true
|
40
37
|
|
41
|
-
Dir.entries(
|
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
|
-
|
46
|
-
|
42
|
+
same_db = same_db && (database == origin_database)
|
43
|
+
database_map["`#{origin_database}`\\."] = "`#{database}`."
|
47
44
|
end
|
48
45
|
|
49
|
-
|
46
|
+
replace_db_names_command = same_db ? "" : database_map.map { |k,v| ".gsub(/#{k}/, \"#{v}\")" }.join("")
|
50
47
|
|
51
|
-
Dir.entries(
|
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
|
-
|
59
|
-
|
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 #{
|
62
|
-
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
|
63
60
|
|
64
|
-
Dir.entries(
|
65
|
-
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)
|
66
63
|
}
|
67
|
-
Dir.entries(
|
68
|
-
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)
|
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,
|
73
|
+
def restore_each(commands, file, default_options, replace_db_names_command)
|
77
74
|
command = ""
|
78
|
-
replacing = " | ruby -pe '$_=$_#{
|
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 #{
|
77
|
+
command = "cat #{file} #{replacing} | mysql #{default_options}"
|
81
78
|
elsif gzip_file?(file)
|
82
|
-
command = "zcat #{file} #{replacing} | mysql #{
|
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)
|
84
|
+
commands.push(command) unless command.empty?
|
88
85
|
end
|
89
86
|
|
90
87
|
def split_integer_and_string(input)
|
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
|