BackUp and Recovery
BackUp and Recovery
1
To bring a database online (MSSMS)
Right click on the database Tasks Bring online
2
To take a database offline (T_SQL)
ALTER DATABASE [Database-Name]SET OFFLINE
WITH ROLLBACK IMMEDIATE
GO
Note: If the rollback termination option is not specified, this statement wait for all the
transactions to complete and then takes the database offline. In case if there is a
transaction which is neither committed or roll-backed, then the statement wait for it
indefinitely.
Example
ALTER DATABASE studentgrade SET OFFLINE WITH
ROLLBACK IMMEDIATE
GO
3
Or Expand SQL Server Logs, right-click any log file, and then click View SQL
Server Log. (Or double-click any log file.) As shown below, it has the current log
and six archive logs ( Archive#1 to Archive #6).
To View logs that are related to general SQL Server activity
Select * FROM sys.fn_dblog(NULL,NULL)
SELECT COUNT(*)
FROM fn_dblog(null,null)
GO
To see for specific database
use STUDENTGRADE
Select * FROM sys.fn_dblog(null,null)
GO
4
3. From the select Backup Destination Click …
5
4. In the destination folder write file name of the backup, select file type(.bak) and
Click ok
5. The path and the file name of the backup will be shown in the select Backup
destination window if it is correct click ok
6
6. In the “Back Up Database” window make all necessary settings and press “OK“.
7
8
To take a Differential Back up
BACKUP DATABASE Database Name TO DISK = ''Path \ name of the diff backup.bak'
WITH DIFFERENTIAL
Example
BACKUP DATABASE BCEmployeeDB TO DISK = 'E:\DATABASEBACKUP\
BCEmpdiff1.bak' WITH DIFFERENTIAL
GO
Example
BACKUP LOG BCEmployeeDB TO DISK = 'E:\DATABASEBACKUP\BCEmplog.bak'
9
To view the recovery model of the databases(MSSMS)
From object explorer select database and View Object explorer details
10
Change recovery model of a database (MSSMS)
1. Connect to the SQL instance in the Object Explorer, expand Databases, select the
desired database
2. Right-click the selected database, go to Properties
3. In the database properties window, choose Options
4. The Recovery model list box highlights the current recovery model
5. To change the recovery model, select the desired recovery model: Full, Bulk-logged,
or Simple from the drop-down list
6.
To change the recovery model, execute the alter database statement with set recovery option.
For example, the recovery model of the database is set to SIMPLE using.
Example
USE master
11
Restore a full database backup(MSSMS)
1. In Object Explorer, connect to an instance of the SQL Server Database Engine and
then expand that instance.
2. Right-click Databases and select Restore Database
3. On the General page, use the Source section to specify the source and location of the
backup sets to restore.
Select Database from the drop down list if the back up is taken from the same sever
or Device otherwise
click the browse button to open the Select backup devices dialog box.
Select backup devices dialog box
12
Select media type (File ) Add Locate Backup file window will open
Select the drive and select the backup file and click Ok
4. In the Backup sets to restore grid, select the backups to restore. This grid displays the
backups available for the specified location.
13
Click OK.
Read about Restore options and Recovery States
14
To restore for a specific time
1. Right click on the database that should be restored from the list, Tasks Restore
Database
15
2. In the window that appeared Click Timeline button to set up the time for database
restoration (to access the Backup Timeline dialog box.)
3. In the Restore to section, click Specific date and time.
4. Use either the Date and Time boxes or the slider bar to specify a specific date and
time to where the restore should stop. Click OK.
5. When the restoration process is completed, the following message will appear on the
screen:
16