MSSQL On Linux备份与还原
问题引出
这天老鸟喜笑颜开的找到菜鸟:“听说微软11月16号发布了MSSQL ON Linux版本了?要不要尝尝鲜?”。
“老大,我已经尝过了,你不知道我写了一篇《Happy Birthday to MSSQL On Linux》?”,这次换菜鸟得意洋洋了。
“不错嘛,那你要不要研究看看MSSQL On Linux备份与还原,看看和Windows上有没有什么区别。”,老鸟总是能给菜鸟找到活干。
备份与还原
菜鸟虽然对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
查看结果,从结果来看三个表存在数据库TestDB中,结果成功如预期。
写在最后
从整个测试过程来看,MSSQL On Linux与MSSQL On Windows的备份还原功能保持一致,甚至连语法格式都一样。这种风格的一惯性和如丝般平顺过度的策略非常赞。
相关推荐
farwang 2020-11-25
星愿心愿 2020-11-24
tianhuak 2020-11-24
zhjn0 2020-11-24
昭君出塞 2020-11-23
bluecarrot 2020-11-23
linuxwcj 2020-10-21
以梦为马不负韶华 2020-10-20
彼岸随笔 2020-10-20
yutou0 2020-10-17
applecarelte 2020-10-16
ourtimes 2020-10-16
waterhorse 2020-09-19
MRFENGG 2020-11-11
rainandtear 2020-10-30
kyssfanhui 2020-10-20
liuhangtiant 2020-10-20