T-SQL 备份和还原
2009-09-14 00:00:00 来源:WEB开发网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;
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
- ››sqlserver安装和简单的使用
- ››SQL Sever 2008 R2 数据库管理
- ››SQL SERVER无法安装成功,sqlstp.log文件提示[未发...
- ››Sql Server中通过父记录查找出所有关联的子记录
- ››SqlServer触发器、存储过程和函数
- ››SQL Server 中的事务(含义,属性,管理)
- ››Sqlite数据库插入和读取图片数据
- ››Sql server 2005拒绝了对对象 'xx表' (数...
- ››Sql server 2005拒绝了对对象 'xx表' (数...
更多精彩
赞助商链接