0% found this document useful (0 votes)
108 views16 pages

BackUp and Recovery

The document discusses how to take databases offline and online, take backups and restore databases, and change recovery models using both SQL Server Management Studio (MSSMS) and Transact-SQL (T-SQL). Key tasks covered include taking a database offline or online by right clicking in MSSMS or using ALTER DATABASE commands, taking full, differential and log backups to disk with MSSMS or T-SQL, restoring databases from backup and to a point in time, and changing a database's recovery model in MSSMS or with ALTER DATABASE commands.

Uploaded by

Nathnael Mesfin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views16 pages

BackUp and Recovery

The document discusses how to take databases offline and online, take backups and restore databases, and change recovery models using both SQL Server Management Studio (MSSMS) and Transact-SQL (T-SQL). Key tasks covered include taking a database offline or online by right clicking in MSSMS or using ALTER DATABASE commands, taking full, differential and log backups to disk with MSSMS or T-SQL, restoring databases from backup and to a point in time, and changing a database's recovery model in MSSMS or with ALTER DATABASE commands.

Uploaded by

Nathnael Mesfin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Taking the database offline and Bringing online

 Online file : The file is available for all operations


 Offline : The file is not available for access and may not be present on the disk
To take a database offline (MSSMS)
Right click on the database Tasks Take offline select Drop all active connections

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

To bring a database online (T_SQL)


ALTER DATABASE Databasename SET ONLINE
GO
Example

ALTER DATABASE studentgrade SET ONLINE


GO

To View logs that are related to general SQL Server activity(MSSMS)


1. In Object Explorer, expand Management.
Do either of the following:
2. Right-click SQL Server Logs, point to View, and then click either SQL Server
Log or SQL Server and Windows Log.

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

Take a backup (MSSMS)


1. Right-click the  database and select “Tasks”, then choose “Back Up“.

2. Click on Add to select path

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

Take a backup (T-SQL)


to take a Backup of a database
BACKUP DATABASE Database Name TO DISK = 'Path \ name of the backup.bak'
Example
BACKUP DATABASE BCEmployeeDB TO DISK = 'E:\DATABASEBACKUP\
BCEmp.bak'
GO

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

To take log Backup


BACKUP LOG your_database TO DISK = 'log.bak'

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

To view the recovery model of the databases(T-SQL)

Select Name,recovery_model,recovery_model_desc From sys.databases

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.

Change recovery model of a database (T-SQL)

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.

ALTER DATABASE Database name SET RECOVERY SIMPLE ;  

Example

USE master

ALTER DATABASE BCEmployeeDB SET RECOVERY SIMPLE


GO

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:

Restore a database backup(T-SQL)


To recover a database to a point-in-time it is necessary to start from restoring the full
database backup using the following syntax:
RESTORE DATABASE database name FROM DISK = 'path\ backuoname.bak' WITH
NORECOVERY, REPLACE
To recover log backups
RESTORE LOG database name FROM DISK = 'path\logbackupname.bak' WITH
NORECOVERY 

16

You might also like