Восстановить базу данных MS SQL - PullRequest
0 голосов
/ 26 мая 2020

Я пытаюсь восстановить базу данных Microsoft Server SQL, работающую в centos, выбросить докеризованный экземпляр.

У меня есть 5 баз данных (OctopusDeploy, master, model, msdb и tempdb), но невозможно создать резервную копию tempdb.

Чтобы получить резервную копию, я запускаю следующие команды:

sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd    -S localhost -U SA -P 'N0tS3cr3t!'    -Q "BACKUP DATABASE [OctopusDeploy] TO DISK = N'/var/opt/mssql/backup/db_OctopusDeploy.bak' WITH NOFORMAT, NOINIT, NAME = 'Octopus-Deploy-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10"
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd    -S localhost -U SA -P 'N0tS3cr3t!'    -Q "BACKUP DATABASE [master] TO DISK = N'/var/opt/mssql/backup/db_master.bak' WITH NOFORMAT, NOINIT, NAME = 'master-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10"
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd    -S localhost -U SA -P 'N0tS3cr3t!'    -Q "BACKUP DATABASE [model] TO DISK = N'/var/opt/mssql/backup/db_model.bak' WITH NOFORMAT, NOINIT, NAME = 'model-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10"
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd    -S localhost -U SA -P 'N0tS3cr3t!'    -Q "BACKUP DATABASE [msdb] TO DISK = N'/var/opt/mssql/backup/db_msdb.bak' WITH NOFORMAT, NOINIT, NAME = 'msdb-full', SKIP, NOREWIND, NOUNLOAD, STATS = 10"

Эта команда генерирует файлы db_OctopusDeploy.bak, db_master.bak ...

После этого я удаляю базу данных, перезапускаю сервер и выполняю следующие команды, чтобы восстановить базу данных (до эти команды я выполняю другие команды, чтобы передать файлы .bak в / var / opt / mssql / backup / внутри docker):

sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "RESTORE FILELISTONLY FROM DISK = '/var/opt/mssql/backup/$db_octopus'"  | tr -s ' ' | cut -d ' ' -f 1-2
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "RESTORE FILELISTONLY FROM DISK = '/var/opt/mssql/backup/$db_master'"  | tr -s ' ' | cut -d ' ' -f 1-2
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "RESTORE FILELISTONLY FROM DISK = '/var/opt/mssql/backup/$db_model'"  | tr -s ' ' | cut -d ' ' -f 1-2
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "RESTORE FILELISTONLY FROM DISK = '/var/opt/mssql/backup/$db_msdb'"  | tr -s ' ' | cut -d ' ' -f 1-2


sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "USE master"
echo "USE master"
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "ALTER DATABASE OctopusDeploy SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
echo "Alter OctopusDeploy"
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "ALTER DATABASE master SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
echo "Alter master"
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "ALTER DATABASE model SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
echo "Alter model"
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "ALTER DATABASE msdb SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
echo "Alter msdb"

sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "RESTORE DATABASE OctopusDeploy FROM DISK = '/var/opt/mssql/backup/$db_octopus' WITH MOVE 'OctopusDeploy' TO '/var/opt/mssql/data/OctopusDeploy.mdf', MOVE 'OctopusDeploy_log' TO '/var/opt/mssql/data/OctopusDeploy_log.ldf'"
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "RESTORE DATABASE master FROM DISK = '/var/opt/mssql/backup/$db_master' WITH MOVE 'master' TO '/var/opt/mssql/data/master.mdf', MOVE 'mastlog' TO '/var/opt/mssql/data/mastlog.ldf'"
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "RESTORE DATABASE model FROM DISK = '/var/opt/mssql/backup/$db_model' WITH MOVE 'modeldev' TO '/var/opt/mssql/data/model.mdf', MOVE 'modellog' TO '/var/opt/mssql/data/modellog.ldf'"
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "RESTORE DATABASE msdb FROM DISK = '/var/opt/mssql/backup/$db_msdb' WITH MOVE 'MSDBData' TO '/var/opt/mssql/data/MSDBData.mdf', MOVE 'MSDBLog' TO '/var/opt/mssql/data/MSDBLog.ldf'"

sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "ALTER DATABASE OctopusDeploy SET MULTI_USER"
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "ALTER DATABASE master SET MULTI_USER"
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "ALTER DATABASE model SET MULTI_USER"
sudo docker exec -it $db_container_name /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'N0tS3cr3t!' -Q "ALTER DATABASE msdb SET MULTI_USER"

Журнал, который я получаю для выполнения вышеупомянутого, следующее:

Changed database context to 'master'.
USE master
Nonqualified transactions are being rolled back. Estimated rollback completion: 0%.
Nonqualified transactions are being rolled back. Estimated rollback completion: 100%.
Alter OctopusDeploy
Msg 5058, Level 16, State 5, Server 9453f1d192ea, Line 1
Option 'SINGLE_USER' cannot be set in database 'master'.
Alter master
Alter model
Alter msdb
Msg 3154, Level 16, State 4, Server 9453f1d192ea, Line 1
The backup set holds a backup of a database other than the existing 'OctopusDeploy' database.
Msg 3013, Level 16, State 1, Server 9453f1d192ea, Line 1
RESTORE DATABASE is terminating abnormally.
Msg 3108, Level 16, State 1, Server 9453f1d192ea, Line 1
To restore the master database, the server must be running in single-user mode. For information on starting in single-user mode, see "How to: Start an Instance of SQL Server (sqlservr.exe)" in Books Online.
Msg 3013, Level 16, State 1, Server 9453f1d192ea, Line 1
RESTORE DATABASE is terminating abnormally.
Msg 3159, Level 16, State 1, Server 9453f1d192ea, Line 1
The tail of the log for the database "model" has not been backed up. Use BACKUP LOG WITH NORECOVERY to backup the log if it contains work you do not want to lose. Use the WITH REPLACE or WITH STOPAT clause of the RESTORE statement to just overwrite the contents of the log.
Msg 3013, Level 16, State 1, Server 9453f1d192ea, Line 1
RESTORE DATABASE is terminating abnormally.
Processed 1760 pages for database 'msdb', file 'MSDBData' on file 1.
Processed 2 pages for database 'msdb', file 'MSDBLog' on file 1.
RESTORE DATABASE successfully processed 1762 pages in 0.023 seconds (598.335 MB/sec).
Msg 1205, Level 13, State 68, Server 9453f1d192ea, Line 1
Transaction (Process ID 54) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
Msg 5069, Level 16, State 1, Server 9453f1d192ea, Line 1
ALTER DATABASE statement failed.
Msg 5058, Level 16, State 5, Server 9453f1d192ea, Line 1
Option 'MULTI_USER' cannot be set in database 'master'.
octopus_db
octopus

Таким образом, кажется, что база данных msdb восстановлена ​​успешно, но не другие 3 Что я делаю неправильно?

Из того, что я видел, важной базой данных является OctopusDeploy так что восстановления этого мне было бы достаточно.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...