MSSQL On Linux备份与还原

这天老鸟喜笑颜开的找到菜鸟:“听说微软11月16号发布了MSSQL ON Linux版本了?要不要尝尝鲜?”。
“老大,我已经尝过了,你不知道我写了一篇《Happy Birthday to MSSQL On Linux》?”,这次换菜鸟得意洋洋了。
“不错嘛,那你要不要研究看看MSSQL On Linux备份与还原,看看和Windows上有没有什么区别。”,老鸟总是能给菜鸟找到活干。

Ubuntu 16.04下安装SQL Server for Linux

从Windows迁移SQL Server数据库到Linux 

备份与还原

菜鸟虽然对MSSQL On Linux也很有兴趣,并且也想测试下备份与还原功能。但是,被老鸟赶着走总觉得不爽快。不爽归不爽,老鸟安排的任务还是要完成的,要不然等会儿老鸟不爽起来,后果很严重。

备份

菜鸟的备份过程非常简单:创建表table1 => 做一个完全备份 => 创建表table2 => 做一个差异备份 => 创建表table3 => 做一个事务日志备份。

USE master GO --If not exists, create testing database IF DB_ID('TestDB') IS NULL CREATE DATABASE TestDB; GO --create the 1st table USE TestDB GO IF OBJECT_ID('dbo.Table1', 'U') IS NOT NULL DROP TABLE dbo.Table1 GO CREATE TABLE dbo.Table1( RowID INT ) GO --make a full backup BACKUP DATABASE [TestDB] TO DISK = N'C:\var\opt\mssql\data\TestDB_full.bak' WITH NOFORMAT, NOINIT, NAME = N'TestDB-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10 GO --create the second table IF OBJECT_ID('dbo.Table2', 'U') IS NOT NULL DROP TABLE dbo.Table2 GO CREATE TABLE dbo.Table2( RowID INT ) GO --take a diff backup BACKUP DATABASE [TestDB] TO DISK = N'C:\var\opt\mssql\data\TestDB_diff.bak' WITH DIFFERENTIAL , NOFORMAT, NOINIT, NAME = N'TestDB-Differential Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10 GO --create the third table IF OBJECT_ID('dbo.Table3', 'U') IS NOT NULL DROP TABLE dbo.Table3 GO CREATE TABLE dbo.Table3( RowID INT ) GO --take transaction log backup BACKUP LOG [TestDB] TO DISK = N'C:\var\opt\mssql\data\TestDB_log.bak' WITH NOFORMAT, NOINIT, NAME = N'TestDB-Transaction Log Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10 GO

备份文件在Linux系统中的位置:

[root@localhost ~]# ls /var/opt/mssql/data/ | grep bak TestDB_diff.bak TestDB_full.bak TestDB_log.bak 还原

数据库完全备份,差异备份和事务日志备份完毕后,接下来菜鸟做了还原的测试,思路是:删除测试数据库 => 还原数据库完全备份文件 => 还原差异备份文件 => 还原事务日志备份文件 => 检查数据库中是不是三个表都存在,如果存在则成功,否则失败。

--==============next is for restore operation USE master GO --drop database if exists for testing IF DB_ID('TestDB') IS NOT NULL DROP DATABASE TestDB; GO --restore full database backup RESTORE DATABASE [TestDB] FROM DISK = 'C:\var\opt\mssql\data\TestDB_full.bak' WITH FILE = 1, MOVE N'TestDB' TO N'C:\var\opt\mssql\data\TestDB.mdf', MOVE N'TestDB_log' TO N'C:\var\opt\mssql\data\TestDB_log.ldf', NORECOVERY, NOUNLOAD, REPLACE, STATS = 10 GO --restore the diff backup RESTORE DATABASE [TestDB] FROM DISK = N'C:\var\opt\mssql\data\TestDB_diff.bak' WITH FILE = 1 , NORECOVERY, NOUNLOAD, STATS = 10 GO --restore the transaction log backup RESTORE LOG [TestDB] FROM DISK = N'C:\var\opt\mssql\data\TestDB_log.bak' WITH FILE = 1 , NOUNLOAD, STATS = 10 GO

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/2b507265033492f30856b2bfdbbb66b1.html