mysql_db_tool 0.1.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +83 -0
- data/bin/gen_create_db_user +17 -0
- data/bin/mysql_backup +24 -0
- data/bin/mysql_restore +27 -0
- data/lib/mysql_db_tool/backup.rb +70 -0
- data/lib/mysql_db_tool/config/config_loader.rb +47 -0
- data/lib/mysql_db_tool/db_backup_base.rb +38 -0
- data/lib/mysql_db_tool/restore.rb +101 -0
- data/lib/mysql_db_tool/sql/create_db_user_template.sql +9 -0
- data/lib/mysql_db_tool/sql/drop_all_tables.sql +14 -0
- data/lib/mysql_db_tool/sql/drop_all_views.sql +14 -0
- data/lib/mysql_db_tool/version.rb +5 -0
- data/lib/mysql_db_tool.rb +29 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b7ba1c8784d5842dc7f1212c89ccaf8cd3fbd31793be8c3dd1c168bf75366ff7
|
4
|
+
data.tar.gz: e2eb44bd513921e7957f293076386ce63f614ee02bafbd242ee8f71bb8599145
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fbe7207993d444b6cbf7c408f2860702bd0c63af6f09776865905598ab1c54aa7b4217e64921c5a742f6a1f7db1a19d4381d33aa8488eae2a31aa3fae0e03552
|
7
|
+
data.tar.gz: f90055728240fa5754b159b81e2798964956d299376c56eac7d31edad5e280483b739e6d3c73e47da5b11193c10f927625c503fe3bbff2437faded8d363a4f2b
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 VReRV
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# MySQL Backup & Restore Tool
|
2
|
+
|
3
|
+
[🇰🇷 (한êµì–´)](./README_KO.md) | [🇬🇧 (English)](./README.md)
|
4
|
+
|
5
|
+
A ruby script tool for backing up and restoring MySQL data.
|
6
|
+
|
7
|
+
## Requirements
|
8
|
+
|
9
|
+
* Tested on linux, Mac OS X environments only
|
10
|
+
* ruby, mysql, mysqldump, gzip, zcat commands must be installed
|
11
|
+
|
12
|
+
## Configuration
|
13
|
+
|
14
|
+
Create a config-<env>.json file under the current directory according to the database environment.
|
15
|
+
|
16
|
+
```json
|
17
|
+
{
|
18
|
+
"dataTables": [],
|
19
|
+
"ignoreTables": [],
|
20
|
+
"dbInfo": {
|
21
|
+
"host": "localhost",
|
22
|
+
"port": 3306,
|
23
|
+
"user": "root",
|
24
|
+
"password": "",
|
25
|
+
"database": [
|
26
|
+
"test_db"
|
27
|
+
]
|
28
|
+
}
|
29
|
+
}
|
30
|
+
```
|
31
|
+
|
32
|
+
* dataTables - allows you to set a column for putting a --where condition to backup only the latest(3 days) rows of a large table.
|
33
|
+
* { "name": "table_name", "where": "column_name" }
|
34
|
+
* ignoreTables - allows you to set which tables should be excluded from backups as unused tables.
|
35
|
+
|
36
|
+
## Data backup
|
37
|
+
|
38
|
+
```shell
|
39
|
+
./bin/backup {env} {backup id} {run?} {gzip?}
|
40
|
+
```
|
41
|
+
|
42
|
+
* env - default (local), key to find the configuration file. e.g.) config-local.json
|
43
|
+
* backup id - default (0), ID to use when restoring as a string
|
44
|
+
* run? - Default (false), you can check in advance which command will be executed, if true, it will be executed
|
45
|
+
* gzip? - default (true), whether to compress with gzip or not
|
46
|
+
|
47
|
+
* DUMP_OPTIONS - you can set common mysqldump command options by this Environment variable,
|
48
|
+
if not set, it will use default options for mysqldump. (--single-transaction --skip-lock-tables)
|
49
|
+
|
50
|
+
After execution, a directory named "backup-{backup id}" will be created under the current directory.
|
51
|
+
|
52
|
+
## restore backup data
|
53
|
+
|
54
|
+
```shell
|
55
|
+
./bin/restore <env> {backup id} {run?} {drop all tables?}
|
56
|
+
```
|
57
|
+
|
58
|
+
* drop all tables? - Default (false), to keep existing tables, or true, which may cause integration check error if not set to true
|
59
|
+
|
60
|
+
## Generate creating db and user sql
|
61
|
+
|
62
|
+
You can generate a sql script to create a db and user.
|
63
|
+
|
64
|
+
```shell
|
65
|
+
./bin/gen_create_db_user {user} {password} {db} {host}
|
66
|
+
```
|
67
|
+
|
68
|
+
## Installing Ruby
|
69
|
+
|
70
|
+
Install using `rbenv` on Mac OS X
|
71
|
+
|
72
|
+
```bash
|
73
|
+
brew install rbenv
|
74
|
+
rbenv init
|
75
|
+
rbenv install 3.3.0
|
76
|
+
rbenv global 3.3.0
|
77
|
+
ruby -v
|
78
|
+
```
|
79
|
+
|
80
|
+
## TODO
|
81
|
+
|
82
|
+
* [ ] Add option to get db configuration information from spring cloud config
|
83
|
+
* [ ] Change linux standard command line argument input format to --backup-id=1 when inputting arguments
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "mysql_db_tool"
|
4
|
+
|
5
|
+
user=ARGV.count > 0 ? ARGV[0] : "db_user"
|
6
|
+
password=ARGV.count > 1 ? ARGV[1] : "db_user_password"
|
7
|
+
db=ARGV.count > 2 ? ARGV[2] : "db_name"
|
8
|
+
host=ARGV.count > 3 ? ARGV[3] : "localhost"
|
9
|
+
|
10
|
+
replace_map = {
|
11
|
+
"DB_USER" => user,
|
12
|
+
"DB_PASSWORD" => password,
|
13
|
+
"DB_NAME" => db,
|
14
|
+
"DB_HOST" => host
|
15
|
+
}
|
16
|
+
|
17
|
+
puts `cat #{MySQLDBTool.find_resource('sql/create_db_user_template.sql')} | ruby -pe '$_=$_#{replace_map.map { |k,v| ".gsub(/#{k}/, \"#{v}\")" }.join("")}' `
|
data/bin/mysql_backup
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mysql_db_tool"
|
5
|
+
|
6
|
+
# Parse command line arguments
|
7
|
+
options = {}
|
8
|
+
options[:env] = ARGV[0] || "local"
|
9
|
+
options[:id] = ARGV[1] || "0"
|
10
|
+
options[:run] = ARGV[2] == "true"
|
11
|
+
options[:gzip] = ARGV.count > 3 ? ARGV[3] == "true" : true
|
12
|
+
|
13
|
+
# Print the options for verification
|
14
|
+
puts "options=#{options}"
|
15
|
+
|
16
|
+
# Perform the backup
|
17
|
+
# begin
|
18
|
+
MySQLDBTool.backup(options)
|
19
|
+
# rescue => e
|
20
|
+
# puts "Error during backup: #{e.message}"
|
21
|
+
# exit 1
|
22
|
+
# end
|
23
|
+
|
24
|
+
puts "Backup completed successfully."
|
data/bin/mysql_restore
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mysql_db_tool"
|
5
|
+
|
6
|
+
# Parse command line arguments
|
7
|
+
options = {}
|
8
|
+
options[:env] = ARGV[0] || "local"
|
9
|
+
options[:id] = ARGV[1] || "0"
|
10
|
+
options[:run] = ARGV[2] == "true"
|
11
|
+
options[:drop_all_tables] = ARGV.count > 3 ? ARGV[3] == "true" : true
|
12
|
+
|
13
|
+
# Print the options for verification
|
14
|
+
puts "Env: #{options[:env]}"
|
15
|
+
puts "Backup ID: #{options[:id]}"
|
16
|
+
puts "Run: #{options[:run]}"
|
17
|
+
puts "DropAllTables: #{options[:drop_all_tables]}"
|
18
|
+
|
19
|
+
# Perform the backup
|
20
|
+
# begin
|
21
|
+
MySQLDBTool.restore(options)
|
22
|
+
# rescue => e
|
23
|
+
# puts "Error during backup: #{e.message}"
|
24
|
+
# exit 1
|
25
|
+
# end
|
26
|
+
|
27
|
+
puts "Restore completed successfully."
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'date'
|
2
|
+
require_relative "db_backup_base"
|
3
|
+
require_relative "config/config_loader"
|
4
|
+
|
5
|
+
module MySQLDBTool
|
6
|
+
|
7
|
+
class Backup
|
8
|
+
|
9
|
+
def initialize(options = {})
|
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]
|
15
|
+
end
|
16
|
+
|
17
|
+
def perform
|
18
|
+
|
19
|
+
verify_tools_exist
|
20
|
+
|
21
|
+
id=@options[:id] || "0"
|
22
|
+
isGzip=@options[:gzip]
|
23
|
+
limitDays=@options[:limit_days] || 3
|
24
|
+
|
25
|
+
Array(@db_info[:database]).flat_map.each_with_index do |database, index |
|
26
|
+
|
27
|
+
defaultOptions="--column-statistics=0 #{mysqlDefaultOptions(@db_info, database)}"
|
28
|
+
backupDir=backupDirName(id, "#{index}_#{database}")
|
29
|
+
|
30
|
+
commands = []
|
31
|
+
if File.exist? backupDir
|
32
|
+
puts "[skip - directory exists] #{backupDir}"
|
33
|
+
return commands
|
34
|
+
end
|
35
|
+
commands.push "mkdir -p #{backupDir}"
|
36
|
+
|
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
|
+
|
41
|
+
puts "backupFile=#{backupFile}"
|
42
|
+
|
43
|
+
ignoreTablesOption = @ignore_tables.map { |e| "--ignore-table=#{@db_info[:database]}.#{e}" }.join(' ')
|
44
|
+
|
45
|
+
commands.push gzipCommand("mysqldump --no-data #{ignoreTablesOption} #{defaultOptions}", isGzip, "#{backupFile}-schema.sql")
|
46
|
+
|
47
|
+
backupTables = []
|
48
|
+
|
49
|
+
@data_tables.each {|table|
|
50
|
+
where = table[:where].empty? ? "" : "--where=\"#{table[:where]} >= '#{whereDate}'\""
|
51
|
+
if where.empty?
|
52
|
+
backupTables.push(table[:name])
|
53
|
+
next
|
54
|
+
else
|
55
|
+
commands.push(gzipCommand("mysqldump --no-create-info #{options} #{where} #{defaultOptions} #{table[:name]}", isGzip, "#{backupFile}-#{table[:name]}.sql"))
|
56
|
+
end
|
57
|
+
}
|
58
|
+
|
59
|
+
commands.push(gzipCommand("mysqldump --no-create-info #{options} #{defaultOptions} #{backupTables.join(' ')}", isGzip, "#{backupFile}-all-other-tables.sql"))
|
60
|
+
commands
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def gzipCommand(command, isGzip, file)
|
65
|
+
"#{command} #{isGzip ? '| gzip ' : ''} > #{file}"
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module MySQLDBTool
|
4
|
+
module Config
|
5
|
+
class ConfigLoader
|
6
|
+
DEFAULT_CONFIG = {
|
7
|
+
db_info: {
|
8
|
+
host: "localhost",
|
9
|
+
user: "root",
|
10
|
+
password: "",
|
11
|
+
database: ["mysql"],
|
12
|
+
port: 3306
|
13
|
+
},
|
14
|
+
data_tables: [
|
15
|
+
# { name: "large_table1", where: "updated_at" }
|
16
|
+
],
|
17
|
+
ignore_tables: [
|
18
|
+
]
|
19
|
+
}
|
20
|
+
|
21
|
+
def self.load(environment)
|
22
|
+
file_path = File.join(Dir.pwd, "config-#{environment}.json")
|
23
|
+
if File.exist?(file_path)
|
24
|
+
file_contents = File.read(file_path)
|
25
|
+
json_data = JSON.parse(file_contents)
|
26
|
+
{
|
27
|
+
db_info: symbolize_keys(json_data['dbInfo']),
|
28
|
+
data_tables: json_data['dataTables'].map { |table| symbolize_keys(table) },
|
29
|
+
ignore_tables: json_data['ignoreTables']
|
30
|
+
}
|
31
|
+
else
|
32
|
+
puts "Warning: config-#{environment}.json not found in the current directory. Using default configuration."
|
33
|
+
DEFAULT_CONFIG
|
34
|
+
end
|
35
|
+
rescue JSON::ParserError => e
|
36
|
+
puts "Error parsing config-#{environment}.json: #{e.message}. Using default configuration."
|
37
|
+
DEFAULT_CONFIG
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def self.symbolize_keys(hash)
|
43
|
+
hash.transform_keys(&:to_sym)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
def run(isRun, command)
|
4
|
+
if not isRun
|
5
|
+
puts "[dryRun] #{command}"
|
6
|
+
else
|
7
|
+
puts "Running: [#{command}]"
|
8
|
+
puts `#{command}`
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def backupDirName(id, dbName = "")
|
13
|
+
"backup-#{id}#{dbName.empty? ? '' : "/#{dbName}"}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def mysqlDefaultOptions(db_info, database)
|
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
|
+
end
|
19
|
+
|
20
|
+
def verify_tools_exist
|
21
|
+
tools = ["mysql", "mysqldump", "gzip", "zcat"]
|
22
|
+
missing_tools = []
|
23
|
+
|
24
|
+
tools.each do |tool|
|
25
|
+
if not system("which #{tool} > /dev/null 2>&1")
|
26
|
+
missing_tools << tool
|
27
|
+
puts "'#{tool}' is not available"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
if missing_tools.empty?
|
32
|
+
puts "All required tools are available."
|
33
|
+
else
|
34
|
+
puts "Missing tools: #{missing_tools.join(', ')}"
|
35
|
+
puts "Please install the missing tools."
|
36
|
+
exit 1
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require_relative "db_backup_base"
|
2
|
+
|
3
|
+
module MySQLDBTool
|
4
|
+
|
5
|
+
class Restore
|
6
|
+
|
7
|
+
def initialize(options = {})
|
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]
|
13
|
+
end
|
14
|
+
|
15
|
+
def perform
|
16
|
+
|
17
|
+
verify_tools_exist
|
18
|
+
|
19
|
+
env=@options[:env] || "local"
|
20
|
+
|
21
|
+
if env.downcase.end_with? "prod"
|
22
|
+
abort "Aborted. Never restore production DB!"
|
23
|
+
end
|
24
|
+
|
25
|
+
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}"
|
30
|
+
|
31
|
+
backupDir=backupDirName(id)
|
32
|
+
|
33
|
+
puts "backupDir=#{backupDir}"
|
34
|
+
databases = Array(@db_info[:database])
|
35
|
+
|
36
|
+
databaseMap = {}
|
37
|
+
sameDb = true
|
38
|
+
|
39
|
+
Dir.entries(backupDir).reject {|f| File.directory? f}.sort.each do |f|
|
40
|
+
|
41
|
+
index, origin_database = split_integer_and_string(f)
|
42
|
+
database = get_element_or_last(databases, index)
|
43
|
+
sameDb = sameDb && (database == origin_database)
|
44
|
+
databaseMap["`#{origin_database}`\\."] = "`#{database}`."
|
45
|
+
end
|
46
|
+
|
47
|
+
gsubstring = sameDb ? "" : databaseMap.map { |k,v| ".gsub(/#{k}/, \"#{v}\")" }.join("")
|
48
|
+
|
49
|
+
commands = []
|
50
|
+
|
51
|
+
Dir.entries(backupDir).reject {|f| File.directory? f}.sort.flat_map do |f|
|
52
|
+
|
53
|
+
index, origin_database = split_integer_and_string(f)
|
54
|
+
database = get_element_or_last(databases, index)
|
55
|
+
|
56
|
+
defaultOptions=mysqlDefaultOptions(@db_info, database)
|
57
|
+
backupDir=backupDirName(id, f)
|
58
|
+
|
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
|
61
|
+
|
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)
|
64
|
+
}
|
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)
|
67
|
+
}
|
68
|
+
commands
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def restore_each(commands, file, defaultOptions, gsubstring)
|
75
|
+
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}"
|
81
|
+
else
|
82
|
+
puts "not supported file #{file}"
|
83
|
+
end
|
84
|
+
|
85
|
+
commands.push(command) if not command.empty?
|
86
|
+
end
|
87
|
+
|
88
|
+
def split_integer_and_string(input)
|
89
|
+
parts = input.split('_', 2)
|
90
|
+
integer = parts[0].to_i
|
91
|
+
string = parts[1]
|
92
|
+
[integer, string]
|
93
|
+
end
|
94
|
+
|
95
|
+
def get_element_or_last(array, index)
|
96
|
+
index < array.length ? array[index] : array.last
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
@@ -0,0 +1,9 @@
|
|
1
|
+
CREATE DATABASE DB_NAME;
|
2
|
+
|
3
|
+
CREATE USER 'DB_USER'@'DB_HOST' IDENTIFIED WITH mysql_native_password BY 'DB_PASSWORD';
|
4
|
+
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, REFERENCES, CREATE TEMPORARY TABLES, LOCK TABLES ON DB_NAME.* TO 'DB_USER'@'DB_HOST';
|
5
|
+
GRANT SELECT, CREATE VIEW, SHOW VIEW, LOCK TABLES, EVENT, TRIGGER ON DB_NAME.* TO 'DB_USER'@'DB_HOST';
|
6
|
+
-- privileges for mysqldump command
|
7
|
+
GRANT FILE, RELOAD, REPLICATION CLIENT, SESSION_VARIABLES_ADMIN, SYSTEM_VARIABLES_ADMIN ON *.* TO 'DB_USER'@'DB_HOST';
|
8
|
+
|
9
|
+
FLUSH PRIVILEGES;
|
@@ -0,0 +1,14 @@
|
|
1
|
+
SET FOREIGN_KEY_CHECKS = 0;
|
2
|
+
SET GROUP_CONCAT_MAX_LEN=32768;
|
3
|
+
SET @tables = NULL;
|
4
|
+
SELECT GROUP_CONCAT('`', table_name, '`') INTO @tables
|
5
|
+
FROM information_schema.tables
|
6
|
+
WHERE table_schema = (SELECT DATABASE());
|
7
|
+
SELECT IFNULL(@tables,'dummy') INTO @tables;
|
8
|
+
|
9
|
+
SET @tables = CONCAT('DROP TABLE IF EXISTS ', @tables);
|
10
|
+
PREPARE stmt FROM @tables;
|
11
|
+
EXECUTE stmt;
|
12
|
+
DEALLOCATE PREPARE stmt;
|
13
|
+
SET FOREIGN_KEY_CHECKS = 1;
|
14
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
SET FOREIGN_KEY_CHECKS = 0;
|
2
|
+
SET GROUP_CONCAT_MAX_LEN=32768;
|
3
|
+
SET @tables = NULL;
|
4
|
+
SELECT GROUP_CONCAT('`', table_name, '`') INTO @tables
|
5
|
+
FROM information_schema.views
|
6
|
+
WHERE table_schema = (SELECT DATABASE());
|
7
|
+
SELECT IFNULL(@tables,'dummy') INTO @tables;
|
8
|
+
|
9
|
+
SET @tables = CONCAT('DROP VIEW IF EXISTS ', @tables);
|
10
|
+
PREPARE stmt FROM @tables;
|
11
|
+
EXECUTE stmt;
|
12
|
+
DEALLOCATE PREPARE stmt;
|
13
|
+
SET FOREIGN_KEY_CHECKS = 1;
|
14
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Require all necessary files
|
2
|
+
require_relative 'mysql_db_tool/backup'
|
3
|
+
require_relative 'mysql_db_tool/restore'
|
4
|
+
require_relative 'mysql_db_tool/db_backup_base'
|
5
|
+
|
6
|
+
# Define the main module
|
7
|
+
module MySQLDBTool
|
8
|
+
class Error < StandardError; end
|
9
|
+
|
10
|
+
# You might want to add methods to easily access your main functionalities
|
11
|
+
def self.backup(options = {})
|
12
|
+
isRun = options[:run]
|
13
|
+
commands = Backup.new(options).perform
|
14
|
+
commands.each { |command| run(isRun, command) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.restore(options = {})
|
18
|
+
isRun = options[:run]
|
19
|
+
commands = Restore.new(options).perform
|
20
|
+
commands.each { |command| run(isRun, command) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.find_resource(relative_path)
|
24
|
+
File.join(File.dirname(__FILE__), 'mysql_db_tool', relative_path)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# You might want to include a version file
|
29
|
+
require_relative 'mysql_db_tool/version'
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mysql_db_tool
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Soonoh Jung
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-08-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: A Ruby gem for backing up and restoring MySQL databases
|
56
|
+
email:
|
57
|
+
- soonoh.jung@vrerv.com
|
58
|
+
executables:
|
59
|
+
- mysql_backup
|
60
|
+
- mysql_restore
|
61
|
+
- gen_create_db_user
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- bin/gen_create_db_user
|
68
|
+
- bin/mysql_backup
|
69
|
+
- bin/mysql_restore
|
70
|
+
- lib/mysql_db_tool.rb
|
71
|
+
- lib/mysql_db_tool/backup.rb
|
72
|
+
- lib/mysql_db_tool/config/config_loader.rb
|
73
|
+
- lib/mysql_db_tool/db_backup_base.rb
|
74
|
+
- lib/mysql_db_tool/restore.rb
|
75
|
+
- lib/mysql_db_tool/sql/create_db_user_template.sql
|
76
|
+
- lib/mysql_db_tool/sql/drop_all_tables.sql
|
77
|
+
- lib/mysql_db_tool/sql/drop_all_views.sql
|
78
|
+
- lib/mysql_db_tool/version.rb
|
79
|
+
homepage: https://github.com/vrerv/mysql-db-tool
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubygems_version: 3.5.17
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: MySQL DB Tool
|
102
|
+
test_files: []
|