mysql_db_tool 0.1.0 → 0.2.1
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 +13 -4
- data/bin/mysql_backup +31 -11
- data/bin/mysql_restore +33 -16
- data/lib/mysql_db_tool/backup.rb +3 -3
- data/lib/mysql_db_tool/version.rb +1 -1
- data/lib/mysql_db_tool.rb +27 -4
- 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: 2f58a8a6254b856cac959a89c42c2b261f8f2888bb8eb7042b64155c25d67461
|
4
|
+
data.tar.gz: 9e955ad5bb771f353290099daf3b813793bc557c2f72615e39b5a97c8919703e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89ae0acb67eecfc7fc4a7a598ed484edd63adece7f4777f92bf23f36843e522d8f3a69aa942cfe499708324d4ff23b7399982ffb4187b61e9f0185689a064d4c
|
7
|
+
data.tar.gz: 461425b16cf7f41ba6a92d10541adc3cedb42e948bf3e12a5dc247c107930011e2dd5b4d04a74f67e82a83587d7e7ffe1c3ee89b1f2f4cd5fb1dbe2ecfcf3825
|
data/README.md
CHANGED
@@ -33,12 +33,20 @@ Create a config-<env>.json file under the current directory according to the dat
|
|
33
33
|
* { "name": "table_name", "where": "column_name" }
|
34
34
|
* ignoreTables - allows you to set which tables should be excluded from backups as unused tables.
|
35
35
|
|
36
|
+
## Install
|
37
|
+
|
38
|
+
```shell
|
39
|
+
gem install mysql_db_tool
|
40
|
+
```
|
41
|
+
|
36
42
|
## Data backup
|
37
43
|
|
38
44
|
```shell
|
39
|
-
|
45
|
+
mysql_backup -e {env} -i {backup id} -r {run?} --gzip
|
40
46
|
```
|
41
47
|
|
48
|
+
you can get help by running `mysql_backup -h`
|
49
|
+
|
42
50
|
* env - default (local), key to find the configuration file. e.g.) config-local.json
|
43
51
|
* backup id - default (0), ID to use when restoring as a string
|
44
52
|
* run? - Default (false), you can check in advance which command will be executed, if true, it will be executed
|
@@ -52,9 +60,11 @@ After execution, a directory named "backup-{backup id}" will be created under th
|
|
52
60
|
## restore backup data
|
53
61
|
|
54
62
|
```shell
|
55
|
-
|
63
|
+
mysql_restore -e {env} -i {backup id} -r {run?} --drop-all-tables
|
56
64
|
```
|
57
65
|
|
66
|
+
you can get help by running `mysql_restore -h`
|
67
|
+
|
58
68
|
* drop all tables? - Default (false), to keep existing tables, or true, which may cause integration check error if not set to true
|
59
69
|
|
60
70
|
## Generate creating db and user sql
|
@@ -62,7 +72,7 @@ After execution, a directory named "backup-{backup id}" will be created under th
|
|
62
72
|
You can generate a sql script to create a db and user.
|
63
73
|
|
64
74
|
```shell
|
65
|
-
|
75
|
+
gen_create_db_user {user} {password} {db} {host}
|
66
76
|
```
|
67
77
|
|
68
78
|
## Installing Ruby
|
@@ -80,4 +90,3 @@ ruby -v
|
|
80
90
|
## TODO
|
81
91
|
|
82
92
|
* [ ] 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
|
data/bin/mysql_backup
CHANGED
@@ -2,23 +2,43 @@
|
|
2
2
|
|
3
3
|
require "bundler/setup"
|
4
4
|
require "mysql_db_tool"
|
5
|
+
require "optparse"
|
6
|
+
|
7
|
+
# Define the options
|
8
|
+
options = {
|
9
|
+
env: "local",
|
10
|
+
id: "0",
|
11
|
+
run: false,
|
12
|
+
gzip: true
|
13
|
+
}
|
5
14
|
|
6
15
|
# Parse command line arguments
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
options
|
11
|
-
options
|
16
|
+
OptionParser.new do |opts|
|
17
|
+
opts.banner = "Usage: mysql_backup [options]\n\n#{MySQLDBTool::ENV_VAR_DESCRIPTION}\n"
|
18
|
+
|
19
|
+
MySQLDBTool.env_opt(options, opts)
|
20
|
+
MySQLDBTool.id_opt(options, opts)
|
21
|
+
MySQLDBTool.run_opt(options, opts)
|
22
|
+
|
23
|
+
opts.on("-g", "--[no-]gzip", "Enable or disable gzip (default: enabled)") do |gzip|
|
24
|
+
options[:gzip] = gzip
|
25
|
+
end
|
26
|
+
|
27
|
+
opts.on("-h", "--help", "Show this help message") do
|
28
|
+
puts opts
|
29
|
+
exit
|
30
|
+
end
|
31
|
+
end.parse!
|
12
32
|
|
13
33
|
# Print the options for verification
|
14
34
|
puts "options=#{options}"
|
15
35
|
|
16
36
|
# Perform the backup
|
17
|
-
|
37
|
+
begin
|
18
38
|
MySQLDBTool.backup(options)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
39
|
+
rescue => e
|
40
|
+
puts "Error during backup: #{e.message}"
|
41
|
+
exit 1
|
42
|
+
end
|
23
43
|
|
24
|
-
puts "Backup completed successfully."
|
44
|
+
puts "Backup completed successfully."
|
data/bin/mysql_restore
CHANGED
@@ -2,26 +2,43 @@
|
|
2
2
|
|
3
3
|
require "bundler/setup"
|
4
4
|
require "mysql_db_tool"
|
5
|
+
require "optparse"
|
6
|
+
|
7
|
+
# Define the options with default values
|
8
|
+
options = {
|
9
|
+
env: "local",
|
10
|
+
id: "0",
|
11
|
+
run: false,
|
12
|
+
drop_all_tables: false
|
13
|
+
}
|
5
14
|
|
6
15
|
# Parse command line arguments
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
options
|
11
|
-
options
|
16
|
+
OptionParser.new do |opts|
|
17
|
+
opts.banner = "Usage: mysql_restore [options]"
|
18
|
+
|
19
|
+
MySQLDBTool.env_opt(options, opts)
|
20
|
+
MySQLDBTool.id_opt(options, opts)
|
21
|
+
MySQLDBTool.run_opt(options, opts)
|
22
|
+
|
23
|
+
opts.on("-d", "--[no-]drop-all-tables", "Drop all tables before restoring (default: disabled)") do |drop_all_tables|
|
24
|
+
options[:drop_all_tables] = drop_all_tables
|
25
|
+
end
|
26
|
+
|
27
|
+
opts.on("-h", "--help", "Show this help message") do
|
28
|
+
puts opts
|
29
|
+
exit
|
30
|
+
end
|
31
|
+
end.parse!
|
12
32
|
|
13
33
|
# Print the options for verification
|
14
|
-
puts "
|
15
|
-
puts "Backup ID: #{options[:id]}"
|
16
|
-
puts "Run: #{options[:run]}"
|
17
|
-
puts "DropAllTables: #{options[:drop_all_tables]}"
|
34
|
+
puts "options=#{options}"
|
18
35
|
|
19
|
-
# Perform the
|
20
|
-
|
36
|
+
# Perform the restore
|
37
|
+
begin
|
21
38
|
MySQLDBTool.restore(options)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
39
|
+
rescue => e
|
40
|
+
puts "Error during restore: #{e.message}"
|
41
|
+
exit 1
|
42
|
+
end
|
26
43
|
|
27
|
-
puts "Restore completed successfully."
|
44
|
+
puts "Restore completed successfully."
|
data/lib/mysql_db_tool/backup.rb
CHANGED
@@ -42,7 +42,7 @@ module MySQLDBTool
|
|
42
42
|
|
43
43
|
ignoreTablesOption = @ignore_tables.map { |e| "--ignore-table=#{@db_info[:database]}.#{e}" }.join(' ')
|
44
44
|
|
45
|
-
commands.push gzipCommand("mysqldump --no-data #{ignoreTablesOption} #{defaultOptions}", isGzip, "#{backupFile}-schema.sql")
|
45
|
+
commands.push gzipCommand("mysqldump --no-data #{ignoreTablesOption} #{defaultOptions}", isGzip, "#{backupFile}-schema.sql#{isGzip ? '.gz' : ''}")
|
46
46
|
|
47
47
|
backupTables = []
|
48
48
|
|
@@ -52,11 +52,11 @@ module MySQLDBTool
|
|
52
52
|
backupTables.push(table[:name])
|
53
53
|
next
|
54
54
|
else
|
55
|
-
commands.push(gzipCommand("mysqldump --no-create-info #{options} #{where} #{defaultOptions} #{table[:name]}", isGzip, "#{backupFile}-#{table[:name]}.sql"))
|
55
|
+
commands.push(gzipCommand("mysqldump --no-create-info #{options} #{where} #{defaultOptions} #{table[:name]}", isGzip, "#{backupFile}-#{table[:name]}.sql#{isGzip ? '.gz' : ''}"))
|
56
56
|
end
|
57
57
|
}
|
58
58
|
|
59
|
-
commands.push(gzipCommand("mysqldump --no-create-info #{options} #{defaultOptions} #{backupTables.join(' ')}", isGzip, "#{backupFile}-all-other-tables.sql"))
|
59
|
+
commands.push(gzipCommand("mysqldump --no-create-info #{options} #{defaultOptions} #{backupTables.join(' ')}", isGzip, "#{backupFile}-all-other-tables.sql#{isGzip ? '.gz' : ''}"))
|
60
60
|
commands
|
61
61
|
end
|
62
62
|
end
|
data/lib/mysql_db_tool.rb
CHANGED
@@ -7,17 +7,40 @@ require_relative 'mysql_db_tool/db_backup_base'
|
|
7
7
|
module MySQLDBTool
|
8
8
|
class Error < StandardError; end
|
9
9
|
|
10
|
+
# Environment variable description
|
11
|
+
ENV_VAR_DESCRIPTION = <<~DESC
|
12
|
+
Environment Variables:
|
13
|
+
DUMP_OPTIONS - Additional options to pass to the MySQL dump command during the backup process.
|
14
|
+
This can include any options supported by mysqldump, such as --skip-lock-tables.
|
15
|
+
DESC
|
16
|
+
|
17
|
+
def self.env_opt(options, opts)
|
18
|
+
opts.on("-e", "--env ENVIRONMENT", "Environment (default: local) - key to find the configuration file. e.g.) config-local.json") do |env|
|
19
|
+
options[:env] = env
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.id_opt(options, opts)
|
24
|
+
opts.on("-i", "--backup-id BACKUP_ID", "BACKUP_ID (default: 0)") do |id|
|
25
|
+
options[:id] = id
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.run_opt(options, opts)
|
30
|
+
opts.on("-r", "--run", "Run the backup (default: false) or Just show the commands") do
|
31
|
+
options[:run] = true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
10
35
|
# You might want to add methods to easily access your main functionalities
|
11
36
|
def self.backup(options = {})
|
12
|
-
isRun = options[:run]
|
13
37
|
commands = Backup.new(options).perform
|
14
|
-
commands.each { |command| run(
|
38
|
+
commands.each { |command| run(options[:run], command) }
|
15
39
|
end
|
16
40
|
|
17
41
|
def self.restore(options = {})
|
18
|
-
isRun = options[:run]
|
19
42
|
commands = Restore.new(options).perform
|
20
|
-
commands.each { |command| run(
|
43
|
+
commands.each { |command| run(options[:run], command) }
|
21
44
|
end
|
22
45
|
|
23
46
|
def self.find_resource(relative_path)
|
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.1
|
4
|
+
version: 0.2.1
|
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-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -88,7 +88,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
88
|
requirements:
|
89
89
|
- - ">="
|
90
90
|
- !ruby/object:Gem::Version
|
91
|
-
version: '
|
91
|
+
version: '2.6'
|
92
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - ">="
|