SQL Service Database BACKUP & RESTORE

1. 完整恢复模式下的数据库备份

 USE master;
ALTER DATABASE AdventureWorks2012 SET RECOVERY FULL;
GO
-- Back up the AdventureWorks2012 database to new media set (backup set 1).
BACKUP DATABASE AdventureWorks2012
TO DISK = 'Z:\SQLServerBackups\AdventureWorks2012FullRM.bak'
WITH FORMAT;
GO
--Create a routine log backup (backup set 2).
BACKUP LOG AdventureWorks2012 TO DISK = 'Z:\SQLServerBackups\AdventureWorks2012FullRM.bak';
GO

2. 完整数据库还原(完整恢复模式)

 USE master;
--Create tail-log backup.
BACKUP LOG AdventureWorks2012
TO DISK = 'Z:\SQLServerBackups\AdventureWorksFullRM.bak'
WITH NORECOVERY;
GO
--Restore the full database backup (from backup set 1).
RESTORE DATABASE AdventureWorks2012
FROM DISK = 'Z:\SQLServerBackups\AdventureWorksFullRM.bak'
WITH FILE=1,
NORECOVERY; --Restore the regular log backup (from backup set 2).
RESTORE LOG AdventureWorks2012
FROM DISK = 'Z:\SQLServerBackups\AdventureWorksFullRM.bak'
WITH FILE=2,
NORECOVERY; --Restore the tail-log backup (from backup set 3).
RESTORE LOG AdventureWorks2012
FROM DISK = 'Z:\SQLServerBackups\AdventureWorksFullRM.bak'
WITH FILE=3,
NORECOVERY;
GO
--recover the database:
RESTORE DATABASE AdventureWorks2012 WITH RECOVERY;
GO

3. 根据数据库生成相应的SQL 脚本

SELECT Name ,

N'ALTER DATABASE '+Name+'  SET RECOVERY FULL;
GO
BACKUP DATABASE '+Name+' TO DISK = ''C:\Databases_Backup\'+Name+'_FullRM.bak'' WITH FORMAT;
GO
BACKUP LOG '+Name+' TO DISK = ''C:\Databases_Backup\'+Name+'_FullRM.bak'';
GO' as [BACKUP SQL] , N'RESTORE DATABASE '+Name+'
FROM DISK = ''C:\Databases_Backup\'+Name+'_FullRM.bak''
WITH FILE=1,
NORECOVERY; RESTORE LOG AdventureWorks2012
FROM DISK = ''C:\Databases_Backup\'+Name+'_FullRM.bak''
WITH FILE=2,
NORECOVERY; RESTORE LOG AdventureWorks2012
FROM DISK = ''C:\Databases_Backup\'+Name+'_FullRM.bak''
WITH FILE=3,
NORECOVERY;
GO
RESTORE DATABASE '+Name+' WITH RECOVERY;
GO' as [RESTORE SQL] FROM Master..SysDatabases ORDER BY Name
上一篇:学成在线hot模块


下一篇:win10的安装与下载