Skip to the main content.
Downloads Try Thriftly
Downloads Try Thriftly
Group 762

Migrate and run DataFlex applications with Oracle, MS SQL Server, PostgreSQL, MySQL &  MariaDB.

flex2Crystal

Stuck in Crystal XI?  Upgrade and use the latest versions of Crystal Reports with DataFlex applications. 

BTR2SQL

Convert from Btrieve / P.SQL / Actian transactional engines to Oracle, MS SQL Server, and PostgreSQL

thriftly-1

Quickly build multi-protocol web services with the same API. Supports JSON-RPC, REST, SOAP,  Thrift, and gRPC.

 Group 671-1

 

Why Mertech?

3 min read

MS SQL Backup and Restore Strategies

Introduction

One of the most significant benefits of running MS SQL is the database recovery model. Your company’s information is its life-blood, and there are myriad methods to backup and preserve it. We will be discussing some basic techniques to preserve your data in a simple and efficient manner. We will not delve into Mirroring, Transaction Log Shipping or Replication; merely the actual backup of individual databases. The backup operations are dependent on the type of backup recovery model used.

The MS SQL default is Full recovery model, which is the model we will be discussing in this article as this is the most commonly used model. We will be using the MS AdventureWorks2008 database as our test database in this article. It is available for download from a Microsoft partner website AdventureWorks sample databases on github. Other backup strategies such as backing up full-text search catalogs and very large databases are not discussed in this article.


Types of Recovery Models

Simple Recovery Model
Full Recovery Model
Bulk-Logged Recovery Model


Types of backups

Full
Differential
Transaction Log


Creating a Full Backup

Creating a Full backup using T-SQL scripts

Using sqlcmd or SQL Query Analyzer in the SQL Query Management Studio tool, enter the following code to create a Full backup device and backup file:

USE MASTER
EXEC sp_addumpdevice 'disk', 'AdventureWorks2008 Full Test', 'C:Program FilesMicrosoft SQL ServerMSSQL10.SQL2008MSSQLBackupAdventureWorks2008Full.dat'
USE MASTER
BACKUP DATABASE AdventureWorks2008 TO 'AdventureWorks2008 Full Test'
WITH DESCRIPTION='Full Adventureworks 2008 Backup' 


Creating a Full Backup using SQL management Studio
  1. Highlight the database you wish to backup

  2. Right-click the database and select “Tasks”, “Back Up…”

  3. If you would like the backup to expire, enter an integer value.
    1. For general usage enter an integer between 7 and 30 days inclusive

  4. The default device is a disk file with a “bak” file extension.
    1. If you have an off-line or on-line backup repository specify this now.

  5. Click the <OK> button to create the backup.


Restoring a Full Recovery Model Backup Step-by-Step

Below we will give an example of a specific type of restore operation involving restoring the AdventureWorks2008 database using the Full recovery model from a Full backup, Differential backup and a transaction log backup.


Step one – Perform a tail-log backup

As mentioned above, immediately prior to our Restore operation we need to backup any uncommitted transactions that may still be in the log. From the SQL Query window in the Management Studio logged into the correct SQL Server instance or from a SQLCMD command prompt logged into the correct SQL Server instance type (Note the filename and pathname are user defined; just make sure the directory path exists):

BACKUP LOG AdventureWorks2008 TO
DISK='c:tempAdventureWorks2008EmergencyLog.bak' WITH NORECOVERY


Step two – Perform Full Database Restore

Next we need to restore the last good full backup. Make sure you use the NORECOVERY option. We cannot fully recover our database and allow users back in until we have applied all the files necessary to complete the Restore operation. Note also that since we have two files included in our Full backup we need to restore the second file (unless we are just initiated our backup job less than two weeks prior). In our example enter the following (referring to the files we created in our Backup Strategies session):

RESTORE DATABASE AdventureWorks2008 FROM "AdventureWorks2008 Full Test" WITH File = 1, NORECOVERY


Step three – Perform Differential Backup Restore

The next step is to restore the last good differential backup. For our example we can assume we have a single Differential backup file to restore. Our command should be as follows:

RESTORE DATABASE AdventureWorks2008 FROM "AdventureWorks2008 Diff Test" WITH NORECOVERY


Step four – Perform Log File Restore

To restore the log file, run the following command:

RESTORE LOG AdventureWorks2008 FROM "AdventureWorks2008 Log1 Test" WITH NORECOVERY


Step five – Perform the Final Database Restore

The last SQL command verifies and activates the database for use:

RESTORE DATABASE AdventureWorks2008 WITH RECOVERY


Creating and scheduling automatic Backups

Using sqlcmd and the Task Scheduler for SQL Backups

We will need to schedule each of our backup jobs using the task scheduler. We will be running a single command, sqlcmd with the appropriate parameters. A typical command would look like the following:


Sqlcmd /S <server nameinstance>/E /Q "EXEC sp_BackupAdventureWorksTest @backupType='F'"


Caveat running sqlcmd:
sqlcmd is particular about the syntax and text case. Make sure you use single-quotes around T-SQL string parameters and double-quotes around the T-SQL query command we are running. The stored procedure parameter text case must match the variable declaration exactly. This is a pain in the neck, but much worse if you make an error and have to track it down! Although you can use a hyphen (“-“) in front of the sqlcmd parameters, I have had better success using a “/” character. Watch out, because this may change in the future.


Scheduling our Task

Use the “Task Scheduler” Windows utility to create a backup schedule for your database. If you do not know how to configure, please refer to this MSDN knowledgebase articles Windows XP Tasks and Windows Vista Tasks.

 

Legacy Application Modernization: Key Steps, Benefits & Best Practices

Legacy Application Modernization: Key Steps, Benefits & Best Practices

This blog post was co-authored with Riaz Merchant, President and CEO at Mertech. In the fast-paced software world, 'legacy' often signals a warning.

Read More
Hybrid Cloud Migration: Plan, Process and Advantages

Hybrid Cloud Migration: Plan, Process and Advantages

This post was co-authored with Riaz Merchant, President/CEO at Mertech Data Systems, Inc.

Read More
Financial Benefits of Cloud Migration & Hybrid Cloud Applications

Financial Benefits of Cloud Migration & Hybrid Cloud Applications

Shifting from your traditional legacy systems to the Cloud can be a game changer, as the benefits of cloud migration are numerous. Cloud computing...

Read More