【转】T-SQL 备份和还原
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
总结了一些SQL Server常用的备份还原T-SQL 语句,代码基本是支持2005/2008的,不过部分功能需要企业版数据库:
Code
/*
Author:Terry.Sai.M.J 浪客
Location:BeiJing
DateTime:GETDATE()
Description:使用镜像备份
*/
IF DB_ID('db') IS NOT NULL
DROP DATABASE db;
GO
CREATE DATABASE db;
GO
CREATE TABLE db.dbo.T(ID INT);
INSERT INTO db.dbo.T SELECT 1;
BACKUP DATABASE db TO DISK='c:\1.bak' MIRROR TO DISK='c:\2.bak'
WITH FORMAT,MEDIANAME='MydbMedia';
RESTORE DATABASE db FROM DISK='c:\2.bak' WITH RECOVERY,REPLACE
SELECT COUNT(*) FROM db.dbo.T
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH RECOVERY,REPLACE
SELECT COUNT(*) FROM db.dbo.T
DROP DATABASE db;
GO
Code
/*
Author:Terry.Sai.M.J 浪客
Location:BeiJing
DateTime:GETDATE()
Description:使用多备份设备组成媒体集进行备份还原
*/
--删除已经存在的db数据库
IF DB_ID('db') IS NOT NULL
DROP DATABASE db;
GO
--创建数据库db
CREATE DATABASE db;
GO
--创建T表
CREATE TABLE db.dbo.T(ID INT PRIMARY KEY IDENTITY(1,1));
--插入一条记录
INSERT INTO db.dbo.T DEFAULT VALUES
--首先使用FORMAT启用一个新的媒体标头,然后分别放到3个磁盘中,形成一个备份集,同时命名一个媒体名
BACKUP DATABASE db TO DISK='c:\1.bak',DISK='d:\1.bak',DISK='e:\1.bak'
WITH FORMAT,MEDIANAME='MydbMedia'
--插入一条记录
INSERT INTO db.dbo.T DEFAULT VALUES
--创建一个差异备份,必须强制的与主备份一样的路径(3个磁盘),使用相同的路径的话,可以通过媒体标头区分上文的主备份与现在的差异备份,因为我们使用了 NOFORMAT关键字,就是不重新初始化媒体(这也叫追加媒体集).还有加上DIFFERENTIAL以表示是差异备份.还要指定媒体名称
BACKUP DATABASE db TO DISK='c:\1.bak',DISK='d:\1.bak',DISK='e:\1.bak'
WITH NOFORMAT/*或者使用NOINIT,效果一样的哈*/,MEDIANAME='MydbMedia',DIFFERENTIAL;
GO
--还原主备份
RESTORE DATABASE db FROM DISK='c:\1.bak',DISK='d:\1.bak',DISK='e:\1.bak'
WITH MEDIANAME='MydbMedia',FILE=1,NORECOVERY,REPLACE;
--还原差异备份(注意FILE=2,因为他标头的备份是属于2顺序的.第一个备份的就是1,依次类推)
RESTORE DATABASE db FROM DISK='c:\1.bak',DISK='d:\1.bak',DISK='e:\1.bak'
WITH MEDIANAME='MydbMedia',FILE=2,NORECOVERY;
--数据库在线
RESTORE DATABASE db WITH RECOVERY
--测试记录
SELECT * FROM db.dbo.T
GO
Code
/*
Author:Terry.Sai.M.J 浪客
Location:BeiJing
DateTime:GETDATE()
Description:数据库的在线还原(读写文件组完整恢复模式)
*/
IF DB_ID('db') IS NOT NULL
DROP DATABASE db;
GO
CREATE DATABASE db
ON PRIMARY
(
NAME=db_data,
FILENAME='c:\1.mdf'
),
FILEGROUP FG
(
NAME=db_data_fg,
FILENAME='c:\2.ndf'
)
LOG ON
(
NAME=db_log,
FILENAME='c:\1.ldf'
)
GO
--在主文件组下创建表T
CREATE TABLE db.dbo.T(ID INT PRIMARY KEY) ON [PRIMARY];
--插入记录
INSERT INTO db.dbo.T SELECT 1
GO
--主文件组在线,那么就视为数据库在线
--创建文件db_data_fg备份
BACKUP DATABASE db FILE='db_data_fg' TO DISK='c:\1.bak' WITH FORMAT
--进行在先还原,指定NORECOVERY 以后,就只能按顺序进行前滚,同时文件离线还原状态.
RESTORE DATABASE db FILE='db_data_fg' FROM DISK='c:\1.bak' WITH NORECOVERY
--创建表失败,因为离线
CREATE TABLE db.dbo.T1(ID INT PRIMARY KEY) ON fg;
--主文件组不受影响,因为只是FG离线还原,只有企业版才支持哈.这种情况下 生产环境中,不至于数据库全部挂了.
SELECT * FROM db.dbo.T
--备份日志,使用COPY_ONLY,因为是要获取的日志是离线状态时候的,COPY_ONLY仅复制备份是在 SQL Server 2005 中引入的,用于在执行特殊目的的备份(例如在联机文件还原前备份日志)时使用
--如果是只读文件的话,明显不会出现差异数据,所以不需要日志备份了
--简单恢复模式的话,日志都不能备份,所以也是一样
--顺便提一句,如果数据库是文件损坏,而且损坏时候都在线,要使用NO_TRUNCATE获取日志
BACKUP LOG db TO DISK='c:\2.bak' WITH FORMAT,COPY_ONLY;
--恢复LOG
RESTORE LOG db FROM DISK='c:\2.bak' WITH RECOVERY
--FG终于在线了,恭喜下,创建一个T1表
CREATE TABLE db.dbo.T1(ID INT PRIMARY KEY) ON fg;
GO
DROP DATABASE db;
Code
/*
Author:Terry.Sai.M.J 浪客
Location:BeiJing
DateTime:GETDATE()
Description:数据库的段落还原(完整恢复模式)
*/
IF DB_ID('db') IS NOT NULL
DROP DATABASE db
GO
--创建包含多个文件组的数据库db
CREATE DATABASE db
ON PRIMARY
(
NAME=db_data,
FILENAME='c:\db_data.mdf'
),
FILEGROUP A
(
NAME=db_data_a,
FILENAME='c:\db_data_a.ndf'
),
FILEGROUP B
(
NAME=db_data_b,
FILENAME='c:\db_data_b.ndf'
),
FILEGROUP C
(
NAME=db_data_c,
FILENAME='c:\db_data_c.ndf'
)
LOG ON
(
NAME=db_log,
FILENAME='c:\db_log.ldf'
)
GO
--在主文件组下创建表
CREATE TABLE db.dbo.T(ID INT PRIMARY KEY IDENTITY(1,1)) ON [PRIMARY];
--备份主文件组PRIMARY
BACKUP DATABASE db FILEGROUP='PRIMARY' TO DISK='c:\db_data.bak' WITH FORMAT;
INSERT INTO db.dbo.T DEFAULT VALUES
--备份当前日志
BACKUP LOG db TO DISK='c:\db_data_1.bak' WITH FORMAT;
GO
--在B文件组下面创建表
CREATE TABLE db.dbo.T1(ID INT PRIMARY KEY IDENTITY(1,1)) ON [B];
--备份文件组B
BACKUP DATABASE db FILEGROUP='B' TO DISK='c:\db_data_b_1.bak' WITH FORMAT;
INSERT INTO db.dbo.T1 DEFAULT VALUES
--备份当前日志
BACKUP LOG db TO DISK='c:\db_data_2.bak' WITH FORMAT;
GO
--备份C
BACKUP DATABASE db FILEGROUP='C' TO DISK='c:\db_data_c.bak' WITH FORMAT;
--备份A
BACKUP DATABASE db FILEGROUP='A' TO DISK='c:\db_data_a.bak' WITH FORMAT;
INSERT INTO db.dbo.T1 DEFAULT VALUES
--备份当前日志
BACKUP LOG db TO DISK='c:\db_data_b.bak' WITH FORMAT;
GO
--模拟破坏,删除其中一个ndf,然后制定NORECOVERY关键字,使数据库处于RESTOING状态,NO_TRUNCATE表示损坏还能备份
BACKUP LOG db TO DISK='c:\db_log.bak' WITH NORECOVERY, NO_TRUNCATE,FORMAT
GO
--首先恢复主文件组,注意现在是段落还原,所以要制定PARTIAL,而且只能出现一次该关键字.
RESTORE DATABASE db FILEGROUP='PRIMARY' FROM DISK='c:\db_data.bak' WITH PARTIAL,NORECOVERY;
--恢复所有日志,注意先后顺序,日志是不区分文件组,所以全部都要恢复
RESTORE LOG db FROM DISK='c:\db_data_1.bak' WITH NORECOVERY;
RESTORE LOG db FROM DISK='c:\db_data_2.bak' WITH NORECOVERY;
RESTORE LOG db FROM DISK='c:\db_data_b.bak' WITH NORECOVERY
RESTORE LOG db FROM DISK='c:\db_log.bak' WITH RECOVERY
--结果正确 ,返回1行记录
SELECT * FROM db.dbo.T
--出错,因为段落未还原的文件组,都处于离线状态
SELECT * FROM db.dbo.T1
GO
--恢复A文件组,还原日志,注意顺序,因为LOG的LSN肯定要晚于文件组A的备份
RESTORE DATABASE db FILEGROUP='A' FROM DISK='c:\db_data_a.bak' WITH NORECOVERY
RESTORE LOG db FROM DISK='c:\db_data_b.bak' WITH NORECOVERY
RESTORE LOG db FROM DISK='c:\db_log.bak' WITH RECOVERY
GO
--恢复B文件组,还原日志,注意顺序,因为LOG的LSN肯定要晚于文件组B的备份
RESTORE DATABASE db FILEGROUP='B' FROM DISK='c:\db_data_b_1.bak' WITH NORECOVERY
RESTORE LOG db FROM DISK='c:\db_data_2.bak' WITH NORECOVERY;
RESTORE LOG db FROM DISK='c:\db_data_b.bak' WITH NORECOVERY
RESTORE LOG db FROM DISK='c:\db_log.bak' WITH RECOVERY
SELECT * FROM db.dbo.T1
GO
--恢复C文件组,还原日志,注意顺序,因为LOG的LSN肯定要晚于文件组C的备份
RESTORE DATABASE db FILEGROUP='C' FROM DISK='c:\db_data_c.bak' WITH NORECOVERY
RESTORE LOG db FROM DISK='c:\db_data_b.bak' WITH NORECOVERY
RESTORE LOG db FROM DISK='c:\db_log.bak' WITH RECOVERY
GO
--删除测试数据库
DROP DATABASE db
GO
Code
/*
Author:Terry.Sai.M.J 浪客
Location:BeiJing
DateTime:GETDATE()
Description:恢复到特定的备份日期
*/
IF DB_ID('db') IS NOT NULL
DROP DATABASE db;
GO
CREATE DATABASE db;
GO
CREATE TABLE db.dbo.T(ID INT PRIMARY KEY IDENTITY(1,1));
GO
BACKUP DATABASE db TO DISK='c:\1.bak' WITH FORMAT;
GO
INSERT INTO db.dbo.T DEFAULT VALUES;
GO
WAITFOR DELAY '00:00:01';
DECLARE @Datetime BINARY(128);
SET @Datetime=CAST(GETDATE() AS BINARY(128));
SET CONTEXT_INFO @Datetime
GO
INSERT INTO db.dbo.T DEFAULT VALUES;
GO
BACKUP LOG db TO DISK='c:\2.bak' WITH FORMAT;
GO
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH NORECOVERY,REPLACE;
GO
DECLARE @Now DATETIME;
SET @Now=DATEADD(SECOND,-1,(SELECT CAST(CONTEXT_INFO() AS DATETIME)));
--SET @Now=DATEADD(MS,-20,(SELECT CAST(CONTEXT_INFO() AS DATETIME)));
RESTORE LOG db FROM DISK='c:\2.bak' WITH RECOVERY,STOPAT=@Now;
GO
SELECT * FROM db.dbo.T
DROP DATABASE db;
GO
Code
/*
Author:Terry.Sai.M.J 浪客
Location:BeiJing
DateTime:GETDATE()
Description:使用日志恢复数据库
*/
IF DB_ID('db') IS NOT NULL
DROP DATABASE db;
GO
CREATE DATABASE db;
GO
CREATE TABLE db.dbo.T(ID INT PRIMARY KEY IDENTITY(1,1));
GO
BACKUP DATABASE db TO DISK='c:\1.bak' WITH FORMAT;
GO
INSERT INTO db.dbo.T DEFAULT VALUES;
GO
BACKUP LOG db TO DISK='c:\2.bak' WITH FORMAT;
GO
INSERT INTO db.dbo.T DEFAULT VALUES;
GO
BACKUP LOG db TO DISK='c:\3.bak' WITH FORMAT;
GO
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH RECOVERY,REPLACE;
GO
SELECT * FROM db.dbo.T
GO
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH NORECOVERY,REPLACE;
GO
RESTORE LOG db FROM DISK='c:\2.bak' WITH RECOVERY;
GO
SELECT * FROM db.dbo.T
GO
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH NORECOVERY,REPLACE;
GO
RESTORE LOG db FROM DISK='c:\2.bak' WITH NORECOVERY;
GO
RESTORE LOG db FROM DISK='c:\3.bak' WITH RECOVERY;
GO
SELECT * FROM db.dbo.T
GO
DROP DATABASE db;
Code
/*
Author:Terry.Sai.M.J 浪客
Location:BeiJing
DateTime:GETDATE()
Description:使用NO_TRUNCATE 备份损坏的DB
*/
IF DB_ID('db') IS NOT NULL
DROP DATABASE db;
GO
CREATE DATABASE db
ON PRIMARY
(
NAME=db_data,
FILENAME='c:\db_data.mdf'
),
(
NAME=db_data1,
FILENAME='c:\db_data1.ndf'
)
LOG ON
(
NAME=db_log,
FILENAME='c:\db_log.log'
);
GO
CREATE TABLE db.dbo.T(ID INT PRIMARY KEY IDENTITY(1,1));
GO
BACKUP DATABASE db TO DISK='c:\1.bak' WITH FORMAT;
GO
INSERT INTO db.dbo.T DEFAULT VALUES;
GO
--CMD-> net stop mssqlserver
--删除c:\db_data1.ndf 模拟破坏
--CMD-> net start mssqlserver
BACKUP LOG db TO DISK='c:\2.bak' WITH FORMAT,NO_TRUNCATE
GO
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH NORECOVERY;
GO
RESTORE LOG db FROM DISK='c:\2.bak' WITH RECOVERY;
GO
SELECT * FROM db.dbo.T
DROP DATABASE db;
GO
Code
/*
Author:Terry.Sai.M.J 浪客
Location:BeiJing
DateTime:GETDATE()
Description:差异备份,日志备份还原
*/
IF DB_ID('db') IS NOT NULL
DROP DATABASE db
GO
CREATE DATABASE db
GO
CREATE TABLE db.dbo.T(ID INT PRIMARY KEY IDENTITY(1,1));
GO
BACKUP DATABASE db TO DISK='c:\1.bak' WITH FORMAT
GO
INSERT INTO db.dbo.T DEFAULT VALUES
GO
BACKUP DATABASE db TO DISK='c:\2.bak' WITH FORMAT,DIFFERENTIAL
GO
INSERT INTO db.dbo.T DEFAULT VALUES
GO
BACKUP LOG db TO DISK='c:\3.bak' WITH FORMAT
GO
DROP DATABASE db;
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH RECOVERY,REPLACE;
GO
SELECT COUNT(*) FROM db.dbo.T;
GO
DROP DATABASE db;
GO
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH NORECOVERY,REPLACE;
GO
RESTORE DATABASE db FROM DISK='c:\2.bak' WITH RECOVERY;
GO
SELECT COUNT(*) FROM db.dbo.T;
DROP DATABASE db;
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH NORECOVERY,REPLACE;
GO
RESTORE DATABASE db FROM DISK='c:\2.bak' WITH NORECOVERY;
GO
RESTORE LOG db FROM DISK='c:\3.bak' WITH RECOVERY;
GO
SELECT COUNT(*) FROM db.dbo.T;
DROP DATABASE db;
Code
/*
Author:Terry.Sai.M.J 浪客
Location:BeiJing
DateTime:GETDATE()
Description:还原master数据库
*/
--Bakup master database
BACKUP DATABASE [master] TO DISK='F:\Documents and Settings\Administrator\桌面\master.bak' WITH FORMAT
--Test for creating database
CREATE DATABASE db
ON PRIMARY
(
NAME=db_data,
FILENAME='F:\Documents and Settings\Administrator\桌面\db_data.mdf'
)
LOG ON
(
NAME=db_log,
FILENAME='F:\Documents and Settings\Administrator\桌面\db_log.ldf'
);
--开始菜单->运行->services.msc->找到sqlserver服务以及对应的实例->找到对应服务的路径 (F:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Binn \)->在运行中输入cmd->cd 刚才的目录->输入sqlservr.exe -m(单用户模式登录)->然后打开 SSMS->新建一个查询(注意只有一个连接,不要连接SSMS数据库引擎了)->然后运行下面的sql
RESTORE DATABASE master FROM DISK='F:\Documents and Settings\Administrator\桌面\master.bak'
--还原db,
CREATE DATABASE db
ON PRIMARY
(
NAME=db_data,
FILENAME='F:\Documents and Settings\Administrator\桌面\db_data.mdf'
)
LOG ON
(
NAME=db_log,
FILENAME='F:\Documents and Settings\Administrator\桌面\db_log.ldf'
)
FOR ATTACH
DROP DATABASE db
Code
--Create test database
CREATE DATABASE db
GO
--Create test table on database named db
CREATE TABLE db.dbo.T (ID INT PRIMARY KEY);
GO
--Create full backup to disk 'F:\Documents and Settings\Administrator\桌面\1.bak'
BACKUP DATABASE db TO DISK='F:\Documents and Settings\Administrator\桌面\1.bak' WITH FORMAT;
GO
--Begin a marked transaction "Tran1"
BEGIN TRAN Tran1 WITH MARK
INSERT INTO db.dbo.T SELECT 1
COMMIT TRAN Tran1
--Backup the transaction log to disk 'F:\Documents and Settings\Administrator\桌面\2.bak'
BACKUP LOG db TO DISK='F:\Documents and Settings\Administrator\桌面\2.bak' WITH FORMAT
GO
DROP DATABASE db;
GO
RESTORE DATABASE db FROM DISK='F:\Documents and Settings\Administrator\桌面\1.bak' WITH NORECOVERY;
GO
RESTORE LOG db FROM DISK='F:\Documents and Settings\Administrator\桌面\2.bak' WITH STOPBEFOREMARK='Tran1';
GO
SELECT COUNT(*) FROM db.dbo.T
DROP DATABASE db;
GO
RESTORE DATABASE db FROM DISK='F:\Documents and Settings\Administrator\桌面\1.bak' WITH NORECOVERY;
GO
RESTORE LOG db FROM DISK='F:\Documents and Settings\Administrator\桌面\2.bak' WITH STOPATMARK='Tran1';
GO
SELECT COUNT(*) FROM db.dbo.T
DROP DATABASE db
Code
/*
Author:Terry.Sai.M.J 浪客
Location:BeiJing
DateTime:GETDATE()
Description:使用同一个媒体簇来保存不同数据库的备份
*/
CREATE DATABASE db;
GO
CREATE DATABASE db1;
GO
CREATE TABLE db.dbo.T(ID INT PRIMARY KEY IDENTITY(1,1));
INSERT INTO db.dbo.T DEFAULT VALUES
CREATE TABLE db1.dbo.T(ID INT PRIMARY KEY IDENTITY(1,1));
INSERT INTO db1.dbo.T DEFAULT VALUES
--备份db,先清空媒体标头等其他媒体信息
BACKUP DATABASE db TO DISK='c:\1.bak' WITH FORMAT;
--备份数据库db1,使用NOFORMAT,保存现在备份1.bak的媒体信息.
BACKUP DATABASE db1 TO DISK='c:\1.bak' WITH NOFORMAT;
--指定FILE=1还原,因为db先备份
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH RECOVERY,REPLACE,FILE=1;
--指定FILE=2还原,因为db1后备份
RESTORE DATABASE db1 FROM DISK='c:\1.bak' WITH RECOVERY,REPLACE,FILE=2;
--测试
SELECT * FROM db.dbo.T
UNION ALL
SELECT * FROM db1.dbo.T
GO
--删除实例数据库
DROP DATABASE db,db1
GO