Sunday, March 6, 2011

DB Backup, Restore, Shrink

Backup Database to Disk

USE TESTDB;
GO
BACKUP DATABASE TESTDB
TO DISK = 'D:\SQLServerBackups\TESTDB201101241156.Bak'
GO


Restore Database
----Make Database to single user Mode
ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
----Restore Database
RESTORE DATABASE YourDB
FROM DISK = 'D:BackUpYourBaackUpFile.bak'
WITH MOVE 'YourMDFLogicalName' TO 'D:DataYourMDFFile.mdf',
MOVE 'YourLDFLogicalName' TO 'D:DataYourLDFFile.ldf'
/*If there is no error in statement before database will be in multiuser
mode.
If error occurs please execute following command it will convert
database in multi user.*/
ALTER DATABASE YourDB SET MULTI_USER
GO


Shrinking a database and specifying a percentage of free space
DBCC SHRINKDATABASE (TESTDB, 10);
GO


Truncating a database
DBCC SHRINKDATABASE (TESTDB, TRUNCATEONLY);

0 comments:

Post a Comment