How to Backup and Restore a SQL Server

How to

To complete this quickstart, you will need the following:

Create a test database

  1. Launch SQL Server Management Studio (SSMS) and connect to your SQL Server instance.
  2. Open a New Query window.
  3. Run the following Transact-SQL (T-SQL) code to create your test database. Refresh the Databases node in Object Explorer to see your new database.

SQL

USE [master]
GO

CREATE DATABASE [SQLTestDB]
GO

USE [SQLTestDB]
GO
CREATE TABLE SQLTest (
	ID INT NOT NULL PRIMARY KEY,
	c1 VARCHAR(100) NOT NULL,
	dt1 DATETIME NOT NULL DEFAULT getdate()
)
GO


USE [SQLTestDB]
GO

INSERT INTO SQLTest (ID, c1) VALUES (1, 'test1')
INSERT INTO SQLTest (ID, c1) VALUES (2, 'test2')
INSERT INTO SQLTest (ID, c1) VALUES (3, 'test3')
INSERT INTO SQLTest (ID, c1) VALUES (4, 'test4')
INSERT INTO SQLTest (ID, c1) VALUES (5, 'test5')
GO

SELECT * FROM SQLTest
GO

Take a backup

To take a backup of your database, do the following:

  1. Launch SQL Server Management Studio (SSMS) and connect to your SQL Server instance.
  2. Expand the Databases node in Object Explorer.
  3. Right-click the database, hover over Tasks, and select Back up….
  4. Under Destination, confirm the path for your backup is correct. If you need to change this, select Remove to remove the existing path, and then Add to type in a new path. You can use the ellipses to navigate to a specific file.
  5. Select OK to take a backup of your database.

Take SQL backup

Alternatively, you can run the following Transact- SQL command to back up your database:

SQL

BACKUP DATABASE [SQLTestDB] 
TO DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\SQLTestDB.bak' 
WITH NOFORMAT, NOINIT,  
NAME = N'SQLTestDB-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO

Restore a backup

To restore your database, do the following:

  1. Launch SQL Server Management Studio (SSMS) and connect to your SQL Server instance.
  2. Right-click the Databases node in Object Explorer and select Restore Database….Restore a database
  3. Choose Device:, and then select the ellipses (…) to locate your backup file.
  4. Select Add and navigate to where your .bak file is located. Select the .bak file and then select OK.
  5. Choose OK to close the Select backup devices dialog box.
  6. Select OK to restore the backup of your database.Restore the database

Alternatively, you can run the following Transact-SQL script to restore your database:

SQL
USE [master]
RESTORE DATABASE [SQLTestDB] 
FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\SQLTestDB.bak' WITH  FILE = 1,  NOUNLOAD,  STATS = 5
GO

Clean up resources

To delete the database you established, as well as its backup history in the MSDB database, run the following Transact-SQL command:

SQL
EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'SQLTestDB'
GO

USE [master]
DROP DATABASE [SQLTestDB]

Click here to read more useful and interesting articles.

X-Soft

X-Soft is an established player in the IT market committed to providing excellent solutions for Web/ Mobile (iOS, Android, Web) around the globe. Namely, we are a team of professional web and mobile developers with 10+ years of experience.

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *