mysql_db_tool 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b7ba1c8784d5842dc7f1212c89ccaf8cd3fbd31793be8c3dd1c168bf75366ff7
4
- data.tar.gz: e2eb44bd513921e7957f293076386ce63f614ee02bafbd242ee8f71bb8599145
3
+ metadata.gz: 072265e57ad114a3724d8be02c24656ebe8aadb4dca3f0bfa397bdafafb00451
4
+ data.tar.gz: 0cb1d78f8b4144669f27c97000419b25c7d3fe3a1a724fd03750b13720528af3
5
5
  SHA512:
6
- metadata.gz: fbe7207993d444b6cbf7c408f2860702bd0c63af6f09776865905598ab1c54aa7b4217e64921c5a742f6a1f7db1a19d4381d33aa8488eae2a31aa3fae0e03552
7
- data.tar.gz: f90055728240fa5754b159b81e2798964956d299376c56eac7d31edad5e280483b739e6d3c73e47da5b11193c10f927625c503fe3bbff2437faded8d363a4f2b
6
+ metadata.gz: 2cac1bb6cb7de94e1a6415c5980f3ec8067dc3849c164aa36a167b3dd77a901b3391f8aac81da9357dd695675ce8fd1a687df0a09ed8c4d77ea2b58a43f188dd
7
+ data.tar.gz: ea432104fb0514200af032ccc619eebbc316b530263678fb0990e8d90dcf60b3b4ea20b07f9d5f7959070592ad62fea53207b65d7e947ce7c21b3073a21ce788
data/README.md CHANGED
@@ -36,7 +36,7 @@ Create a config-<env>.json file under the current directory according to the dat
36
36
  ## Data backup
37
37
 
38
38
  ```shell
39
- ./bin/backup {env} {backup id} {run?} {gzip?}
39
+ ./bin/backup -e {env} -i {backup id} -r {run?} --gzip
40
40
  ```
41
41
 
42
42
  * env - default (local), key to find the configuration file. e.g.) config-local.json
@@ -52,7 +52,7 @@ After execution, a directory named "backup-{backup id}" will be created under th
52
52
  ## restore backup data
53
53
 
54
54
  ```shell
55
- ./bin/restore <env> {backup id} {run?} {drop all tables?}
55
+ ./bin/restore -e {env} -i {backup id} -r {run?} --drop-all-tables
56
56
  ```
57
57
 
58
58
  * drop all tables? - Default (false), to keep existing tables, or true, which may cause integration check error if not set to true
@@ -80,4 +80,3 @@ ruby -v
80
80
  ## TODO
81
81
 
82
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
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
- 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
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
- # begin
37
+ begin
18
38
  MySQLDBTool.backup(options)
19
- # rescue => e
20
- # puts "Error during backup: #{e.message}"
21
- # exit 1
22
- # end
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: true
13
+ }
5
14
 
6
15
  # 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
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: enabled)") 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 "Env: #{options[:env]}"
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 backup
20
- # begin
36
+ # Perform the restore
37
+ begin
21
38
  MySQLDBTool.restore(options)
22
- # rescue => e
23
- # puts "Error during backup: #{e.message}"
24
- # exit 1
25
- # end
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."
@@ -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
@@ -1,5 +1,5 @@
1
1
  # lib/mysql_db_tool/version.rb
2
2
 
3
3
  module MySQLDBTool
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  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(isRun, command) }
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(isRun, command) }
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.0
4
+ version: 0.2.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-13 00:00:00.000000000 Z
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: '0'
91
+ version: '2.6'
92
92
  required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ">="