SQL server全备份/还原,差异备份/还原sql语句

备份: 

use test
go

SELECT df.[name],f.[name] AS [filegroup],df.physical_name,df.[size],
df.[growth],f.[is_default]
FROM sys.database_files df
LEFT JOIN sys.filegroups f
ON df.data_space_id = f.data_space_id


--设置完整模式
USE [master]
GO
ALTER DATABASE [test] SET RECOVERY FULL WITH NO_WAIT
GO

--备份主分区- FG_Archive_Id_01
BACKUP DATABASE [test]
FILEGROUP = 'PRIMARY'
TO DISK = 'D:\DBBackup\PRIMARY.bak' WITH FORMAT
GO

--备份分区- test1
BACKUP DATABASE [test]
FILEGROUP = 'test1'
TO DISK = 'D:\DBBackup\test1.bak' WITH FORMAT
GO

--备份分区- test2
BACKUP DATABASE [test]
FILEGROUP = 'test2'
TO DISK = 'D:\DBBackup\test2.bak' WITH FORMAT
GO

--备份日志
BACKUP LOG [test]
TO DISK = 'D:\DBBackup\test_log.bak' WITH FORMAT
GO

------------------------------差异备份----------------------------------------------
--差异备份主分区-
backup database [test] 
FILEGROUP = 'PRIMARY'
to disk='D:\DBBackup\PRIMARYDIF.bak' with 
differential,retaindays=9,noformat,noinit,compression
go

--备份分区- test1
BACKUP DATABASE [test]
FILEGROUP = 'test1'
TO DISK = 'D:\DBBackup\test1DIF.bak' with 
differential,retaindays=9,noformat,noinit,compression;
GO

--备份分区- test2
BACKUP DATABASE [test]
FILEGROUP = 'test2'
TO DISK = 'D:\DBBackup\test2DIF.bak' with 
differential,retaindays=9,noformat,noinit,compression;
GO

--备份日志
BACKUP LOG [test]
TO DISK = 'D:\DBBackup\test_log.bak' WITH FORMAT
GO

还原:


restore filelistonly from disk='D:\DBBackup\PRIMARY.bak'

--还原主分区
RESTORE DATABASE [test]
FILEGROUP = 'PRIMARY'
FROM DISK = 'D:\DBBackup\PRIMARY.bak' WITH FILE = 1, 
MOVE N'test' TO N'F:\DataBase\test.mdf',  
MOVE N'test_log' TO N'F:\DataBase\test_log.ldf',
NORECOVERY,REPLACE,STATS = 10
GO


--还原分区- test1
RESTORE DATABASE [test]
FILEGROUP = 'test1'
FROM DISK = 'D:\DBBackup\test1.bak' WITH FILE = 1, 
MOVE N'1' 
TO N'F:\DataBase\FG_Archive\test1_data.ndf',
NORECOVERY
GO

--还原分区- test2
RESTORE DATABASE [test]
FILEGROUP = 'test2'
FROM DISK = 'D:\DBBackup\test2.bak' WITH FILE = 1, 
MOVE N'2' 
TO N'F:\DataBase\FG_Archive\test2_data.ndf',
NORECOVERY
GO


--还原日志
RESTORE LOG [test]
FROM DISK = 'D:\DBBackup\test_Log.bak'
WITH NORECOVERY
GO

--还原在线
RESTORE DATABASE [test]
WITH RECOVERY
GO


 

你可能感兴趣的:(sqlserver)