diff --git a/bin/mysql_backup b/bin/mysql_backup index e6ff4e7..9634fe2 100755 --- a/bin/mysql_backup +++ b/bin/mysql_backup @@ -20,6 +20,7 @@ OptionParser.new do |opts| MySQLDBTool.env_opt(options, opts) MySQLDBTool.id_opt(options, opts) MySQLDBTool.run_opt(options, opts) + MySQLDBTool.database_opt(options, opts) opts.on("-g", "--[no-]gzip", "Enable or disable gzip (default: enabled)") do |gzip| options[:gzip] = gzip diff --git a/bin/mysql_restore b/bin/mysql_restore index e50c77c..8c0eb70 100755 --- a/bin/mysql_restore +++ b/bin/mysql_restore @@ -19,6 +19,7 @@ OptionParser.new do |opts| MySQLDBTool.env_opt(options, opts) MySQLDBTool.id_opt(options, opts) MySQLDBTool.run_opt(options, opts) + MySQLDBTool.database_opt(options, opts) opts.on("-D", "--[no-]drop-all-tables", "Drop all tables before restoring (default: disabled)") do |drop_all_tables| options[:drop_all_tables] = drop_all_tables diff --git a/lib/mysql_db_tool.rb b/lib/mysql_db_tool.rb index 26e2f65..a4e0c6c 100644 --- a/lib/mysql_db_tool.rb +++ b/lib/mysql_db_tool.rb @@ -32,6 +32,12 @@ def self.run_opt(options, opts) end end + def self.database_opt(options, opts) + opts.on("-d", "--database DATABASE", "Override option for dbInfo.database configuration") do |database| + options[:database] = database.split(',') + end + end + # You might want to add methods to easily access your main functionalities def self.backup(options = {}) commands = Backup.new(options).perform diff --git a/lib/mysql_db_tool/backup.rb b/lib/mysql_db_tool/backup.rb index b7af32f..c927a56 100755 --- a/lib/mysql_db_tool/backup.rb +++ b/lib/mysql_db_tool/backup.rb @@ -12,6 +12,8 @@ def initialize(options = {}) @data_tables = tableConfig[:data_tables] @ignore_tables = tableConfig[:ignore_tables] @db_info = tableConfig[:db_info] + + @db_info[:database] = @options[:database] if @options[:database] end def perform diff --git a/lib/mysql_db_tool/restore.rb b/lib/mysql_db_tool/restore.rb index f310999..791760f 100755 --- a/lib/mysql_db_tool/restore.rb +++ b/lib/mysql_db_tool/restore.rb @@ -10,6 +10,8 @@ def initialize(options = {}) @data_tables = tableConfig[:data_tables] @ignore_tables = tableConfig[:ignore_tables] @db_info = tableConfig[:db_info] + + @db_info[:database] = @options[:database] if @options[:database] end def perform diff --git a/spec/mysql_db_tool/backup_spec.rb b/spec/mysql_db_tool/backup_spec.rb index 1dbad7f..3d73316 100644 --- a/spec/mysql_db_tool/backup_spec.rb +++ b/spec/mysql_db_tool/backup_spec.rb @@ -87,4 +87,20 @@ end end + describe '#perform' do + + let(:options) { { env: 'backup-test-env', id: '42', run: false, gzip: false, database: 'test_db2' } } + let(:instance) { described_class.new(options) } + + it 'backs up with database option' do + + commands = instance.perform + expect(commands).to eq ([ + "mkdir -p backup-42/0_test_db2", + "mysqldump --no-data --column-statistics=0 --ssl-mode=disabled -h my-host -u test-user test_db2 > backup-42/0_test_db2/2024-07-17_42-schema.sql", + "mysqldump --no-create-info --single-transaction --skip-lock-tables --column-statistics=0 --ssl-mode=disabled -h my-host -u test-user test_db2 > backup-42/0_test_db2/2024-07-17_42-all-other-tables.sql" + ]) + end + end + end \ No newline at end of file diff --git a/spec/mysql_db_tool/restore_spec.rb b/spec/mysql_db_tool/restore_spec.rb index 12cf81e..60589db 100644 --- a/spec/mysql_db_tool/restore_spec.rb +++ b/spec/mysql_db_tool/restore_spec.rb @@ -5,14 +5,15 @@ require 'mysql_db_tool/restore' RSpec.describe MySQLDBTool::Restore do - let(:options) { { environment: 'test', backup_id: '1', run: false, drop_all_tables: false } } - let(:instance) { described_class.new(options) } before do - + end describe '#perform' do + let(:options) { { environment: 'test', backup_id: '1', run: false, drop_all_tables: false } } + let(:instance) { described_class.new(options) } + it 'restores one db' do allow(MySQLDBTool::Config::ConfigLoader).to receive(:load).and_return ({ db_info: { user: 'test-user', password: '', host: 'my-host', database: ['test_db-abc'] }, @@ -46,4 +47,28 @@ end end + describe '#perform with database option' do + let(:options) { { environment: 'test', backup_id: '1', run: false, drop_all_tables: false, database: ['rdb-1', 'rdb-2'] } } + let(:instance) { described_class.new(options) } + + it 'restores' do + + allow(MySQLDBTool::Config::ConfigLoader).to receive(:load).and_return ({ + db_info: { user: 'test-user', password: '', host: 'my-host', database: ['db-1', 'db-2'] }, + data_tables: [], + ignore_tables: [] + }) + allow(Dir).to receive(:entries).with('backup-0').and_return(['.', '..', '0_db-1', '1_db-2']) + allow(Dir).to receive(:entries).with('backup-0/0_db-1').and_return(['.', '..', 'a.sql']) + allow(Dir).to receive(:entries).with('backup-0/1_db-2').and_return(['.', '..', 'b.sql']) + + commands = instance.perform + puts "commands=#{commands}" + expect(commands).to eq ([ + "cat backup-0/0_db-1/a.sql | ruby -pe '$_=$_.gsub(/`db-1`\\./, \"`rdb-1`.\").gsub(/`db-2`\\./, \"`rdb-2`.\")' | mysql --ssl-mode=disabled -h my-host -u test-user rdb-1 ", + "cat backup-0/1_db-2/b.sql | ruby -pe '$_=$_.gsub(/`db-1`\\./, \"`rdb-1`.\").gsub(/`db-2`\\./, \"`rdb-2`.\")' | mysql --ssl-mode=disabled -h my-host -u test-user rdb-2 " + ]) + end + end + end \ No newline at end of file