/*
	Set recovery to FULL if needed
*/
ALTER DATABASE [AdventureWorks2016] SET RECOVERY FULL;
GO


/*
	Backup the database
*/
BACKUP DATABASE [AdventureWorks2016] 
	TO  DISK = N'C:\Backups\AW2016_BaseFullTest.bak' 
	WITH NOFORMAT, 
	INIT,   
	COMPRESSION,  
	STATS = 10;
GO

/*
	Backup the log
*/
BACKUP LOG [AdventureWorks2016] 
	TO  DISK = N'C:\Backups\AW2016_TLog1Test.trn' 
	WITH NOFORMAT, 
	NOINIT,  
	NAME = N'AdventureWorks2016-Transaction Log  Backup', 
	SKIP, 
	NOREWIND, 
	NOUNLOAD,  
	STATS = 10;
GO


/*
	Backup the database with COPY ONLY
*/
BACKUP DATABASE [AdventureWorks2016] 
	TO  DISK = N'C:\Backups\AW2016_BaseFullCopyOnly.bak' 
	WITH NOFORMAT,
	COPY_ONLY, 
	INIT,   
	COMPRESSION,  
	STATS = 10;
GO

/*
	restore with a move
*/
USE [master];
GO

RESTORE DATABASE [AdventureWorks2016_CO_Primary] 
	FROM  DISK = N'C:\Backups\AW2016_BaseFullCopyOnly.bak' 
	WITH  FILE = 1,  
	MOVE N'AdventureWorks2016_Data' TO N'C:\Databases\AW_CopyOnly_Restore\AdventureWorks2016_Data.mdf',  
	MOVE N'AdventureWorks2016_Log' TO N'C:\Databases\AW_CopyOnly_Restore\AdventureWorks2016_Log.ldf',  
	NOUNLOAD,  
	REPLACE,  
	STATS = 5
GO

/*
	take a diff of it
*/
BACKUP DATABASE [AdventureWorks2016_CO_Primary] 
	TO  DISK = N'C:\Backups\AW2016_BaseFullCO_Primary_Diff.bak' 
	WITH  DIFFERENTIAL , 
	NOFORMAT, 
	INIT,  
	NAME = N'AdventureWorks2016_CO_Primary-Diff Database Backup', 
	STATS = 10
GO

/*
	restore copy only again, with no recovery!!
*/
USE [master];
GO

RESTORE DATABASE [AdventureWorks2016_CO_Secondary] 
	FROM  DISK = N'C:\Backups\AW2016_BaseFullCopyOnly.bak' 
	WITH  FILE = 1,  
	MOVE N'AdventureWorks2016_Data' TO N'C:\Databases\AW_CopyOnly_Secondary\AdventureWorks2016_Data.mdf',  
	MOVE N'AdventureWorks2016_Log' TO N'C:\Databases\AW_CopyOnly_Secondary\AdventureWorks2016_Log.ldf',  
	NOUNLOAD,  
	REPLACE,  
	NORECOVERY,
	STATS = 5
GO

/*
	restore the diff, with no recovery...
*/
USE [master];
GO

RESTORE DATABASE [AdventureWorks2016_CO_Secondary] 
	FROM  DISK = N'C:\Backups\AW2016_BaseFullCO_Primary_Diff.bak' 
	WITH  FILE = 1,  
	NORECOVERY,
	STATS = 5
GO


RESTORE HEADERONLY 
FROM DISK = N'C:\Backups\AW2016_BaseFullTest.bak';
GO

RESTORE FILELISTONLY
FROM DISK = N'C:\Backups\AW2016_BaseFullTest.bak';
GO

RESTORE HEADERONLY 
FROM DISK = N'C:\Backups\AW2016_BaseFullCopyOnly.bak';
GO

RESTORE FILELISTONLY
FROM DISK = N'C:\Backups\AW2016_BaseFullCopyOnly.bak';
GO

RESTORE HEADERONLY 
FROM DISK = N'C:\Backups\AW2016_BaseFullCO_Primary_Diff.bak';
GO

RESTORE FILELISTONLY
FROM DISK = N'C:\Backups\AW2016_BaseFullCO_Primary_Diff.bak';
GO


DROP DATABASE AdventureWorks2016_CO_Primary;
GO
DROP DATABASE AdventureWorks2016_CO_Secondary;
GO




/*
	restore with a move
*/
USE [master];
GO

RESTORE DATABASE [AdventureWorks2016_CO_Primary] 
	FROM  DISK = N'C:\Backups\AW2016_BaseFullCopyOnly.bak' 
	WITH  FILE = 1,  
	MOVE N'AdventureWorks2016_Data' TO N'C:\Databases\AW_CopyOnly_Restore\AdventureWorks2016_Data.mdf',  
	MOVE N'AdventureWorks2016_Log' TO N'C:\Databases\AW_CopyOnly_Restore\AdventureWorks2016_Log.ldf',  
	NOUNLOAD,  
	REPLACE,  
	STATS = 5
GO


/*
	Back it up
*/
BACKUP DATABASE [AdventureWorks2016_CO_Primary] 
	TO  DISK = N'C:\Backups\AdventureWorks2016_CO_Primary_Full.bak' 
	WITH NOFORMAT, 
	INIT,   
	COMPRESSION,  
	STATS = 10;
GO

/*
	take a diff of it
*/
BACKUP DATABASE [AdventureWorks2016_CO_Primary] 
	TO  DISK = N'C:\Backups\AdventureWorks2016_CO_Primary_Diff.bak' 
	WITH  DIFFERENTIAL , 
	NOFORMAT, 
	INIT,  
	NAME = N'AdventureWorks2016_CO_Primary-Diff Database Backup', 
	STATS = 10
GO


/*
	restore copy only again, with no recovery!!
*/
USE [master];
GO

RESTORE DATABASE [AdventureWorks2016_CO_Secondary] 
	FROM  DISK = N'C:\Backups\AdventureWorks2016_CO_Primary_Full.bak' 
	WITH  FILE = 1,  
	MOVE N'AdventureWorks2016_Data' TO N'C:\Databases\AW_CopyOnly_Secondary\AdventureWorks2016_Data.mdf',  
	MOVE N'AdventureWorks2016_Log' TO N'C:\Databases\AW_CopyOnly_Secondary\AdventureWorks2016_Log.ldf',  
	NOUNLOAD,  
	REPLACE,  
	NORECOVERY,
	STATS = 5
GO

/*
	restore the diff, with no recovery...
*/
USE [master];
GO

RESTORE DATABASE [AdventureWorks2016_CO_Secondary] 
	FROM  DISK = N'C:\Backups\AdventureWorks2016_CO_Primary_Diff.bak' 
	WITH  FILE = 1,  
	NORECOVERY,
	STATS = 5
GO
