I see lot of DBA’s still do not get the concept of Database backups and restore in terms of FULL/DIFF/TRANS Log Backups and Restores. I am not going to define what these mean (GOTO BOL), but rather talk about how these are useful and what to do with these fancy backup strategies.

Consider this example if you Database size is small enough (like 2-10GB) Because a bigger database size would take a diff backup strategy depending upon the time to backup and database size.

Considering your Database is in FULL Recovery Mode. (But Obvious) And I am only talking about a very simple scenario where you have a single file-group and simple backup processes. There are other various scenarios where you have multiple data files/multiple file-groups and if you are really Novice DBA then goto BOL to check for some basic lessons. Again anything presented here is a very generalized view of how to proceed with the restore process under a simple setup.

If you are taking a Daily FULL Database Backup / Differential Backup after every 6 Hours / Transaction Log Backups every 30 min’s.

What if a disaster Strikes (Disk Failure or someone hammers the server Or someone Flies his jet and tries to lands on the server) and you are tasked to recover the database Point in TIME or near about with minimum data Loss. What kind of Restores will a DBA consider a PLAN.

Restore the Latest FULL Backup. (WITH NORECOVERY)

Restore the Latest Differential Backup. (WITH NORECOVERY)

Restore the Transaction Logs Backups taken after the Differential Backup (WITH NORECOVERY) unless you are at the Last Transaction Log, then USE WITH RECOVERY to Bring the Database Online.

To Restore the Data to Point in time use (STOPAT= ‘’) while restoring the last transaction log.

The Latest Differential Backup will contain all changes that occurred since the last full backup, so no-need to restore the in-between differential backups unless there is a need otherwise to restore to other time/date.

The transaction logs will contain all the changes that occurred from the latest differential backup and thereafter each log back contains changes that occurred since last/latest transaction log backup.

So, here it is again:

Restore the Latest Full Backup WITH NORECOVERY
Restore the Latest Differential Backup WITH NORECOVERY
Restore the all the Transaction Log Backups since the Latest Differential Backup. WITH NORECOVERY unless you have reached the last transaction log, then use WITH RECOVERY.

Or if you Forget the WITH RECOVERY While restoring the latest Transaction Log Backup, you can still Bring the Database Online using RESTORE DATABASE <DATABASENAME> WITH RECOVERY;

Here is a situation, i wanted to blog about. Renaming a database physical file. some may ask, why would anybody do it.  Well for the people who work like “alice in wonderland”, everything is streamlined without hiccups, but for a volatile environment like i work in anything is possible.  so back to the question of why would anybody rename the database, well there are certain situations as follows:

We will be taking the example of 2 databases ProjectDatabase_DEV and TEST as shown. one is a development database and the other is a test database as per the SDLC.

image

Developers working on limited resources use shared SQL Server environment and if the Project name has changed, so will be database name too, this is to streamline things a bit. When the objects are moved from _DEV to _TEST to production server, we have to take care of renaming and placing the database files appropriately:

Let us start with our databases: execute this command sp_helpdb ProjectDatabase_DEV

image

Now consider a scenario, where the team has decided to move this database into Production.

Steps you will take are take a most current full backup of the database and restore it on the production system.

(I am not going to talk about the users and logins and dbcc etc. this is just about renaming the db files)

Now we restore the database on the production server as ProjectDatabase.

There are  2 ways to restore the database : SSMS and T-SQL. I will use T-SQL for reference here:

Let us assume the database is restored to production using SSMS or T-SQL as

RESTORE database ProjectDatabase from disk = ‘C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\ProjectDatabase_DEV.bak’

Now check the database on production server with sp_helpdb ProjectDatabase

image

you see the database Logical name still contains the _DEV as well as the Physical File Name.

So what do we do here: Things are simple: using T-SQL we change the logical and physical names as follows:

ALTER DATABASE ProjectDatabase
MODIFY FILE (NAME=’ProjectDatabase_DEV’, NEWNAME=’ProjectDatabase’)

After you execute the above statement, run sp_helpdb again, and here is the Output (see the Logical Name has changed)

image

Now we will change the Physical File name on the right:

ALTER DATABASE ProjectDatabase
MODIFY FILE (NAME=’ProjectDatabase’, FILENAME=’C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\ProjectDatabase.mdf’)

Check again with sp_helpdb and here is the output (see the physical file name has change)

image

But the Physical file residing in the Data Folder have not changed: WHY?. This is because the database file name has been changed in the system tables and not the file itself.

image

After a couple of week of SQL Server running smoothly, the sysadmins re-started the Server and suddently no one was able to access the database anymore. sp_helpdb produced no results and gave permission error etc. This is how it looked:

image

no one was able to see the objects inside the database. Even if you try to check out the properties you see this:

image

so to correct this was to stop SQL Server Service. Rename the Physical mdf and ldf files to the required names as were defined previously. Then start the SQL Server service, and LO!, you are able to access the Database.

The best practise is to check the Files names and paths while getting the RESTORE done either using T-SQL as below or SSMS.

RESTORE Database ProjectDatabase from disk = ‘C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\ProjectDatabase_DEV.bak’
WITH REPLACE, – This is Optional and is only required on existing databases.
MOVE ‘Logical MDF Name’ to ‘Path to Physical FileName’,
MOVE ‘Logical LDF Name’ to ‘Path to Physical FileName’

Lets talk about Migrating a single database from SQL 2000 to 2005/2008 and how easy it is. Here are some of the methods i used for the migration.

This Blog has 2 parts: The First will detail the Database Moving Strategies and Second will discuss the core strategies after the move, so the last part is not left un-read or hidden as it is a vital strategy.

Before we proceed to the examples, the things to remember are ":

  • SQL Server 2000/2005/2008 Compatibility Mode.
  • Database Physical Folder Location.
  • Jobs associated with the database.
  • Logins Associated with the Database.
  • Objects that reference this database(Stored Procedures / Functions etc.) 
  • Linked Servers ( if this database is reference in particular)
  • DTS Packages.
  • Analysis Services Tables Derived from the Database(if any).
  • Health of the Database itself. (We will talk about this later.)

These examples are good if you have the luxury of carrying on a Side by side installation (if you have separate Server for SQL 2005 and not upgrading the SQL Server 2000 to 2005 called in-place upgrade)

PROCEDURE 1:

Backup the database from source (SQL Server 2000) and restore the database Server 2005.

Either use SSMS/SSEM (Enterprise Manager as in SQL 2000) Or use T-SQL for the Backup and restore Process.

image

T-SQL goes something like this:

image 
Wow, T-SQL and how did i derive this T-SQL? This is how i did it from the Backup Up database Dialog box.

Script Action to Clipboard and then paste it in Query window

image

NOW, Let us do a Restore of the Database using either of the methods (SSMS or T-SQL)

For our convince i detached the pubs database with the procedure described below in the DEATCH Database Section.

image

In the Restore Database Dialog, Write the name of the database in “To database” and select From Device (if it is a file).  Click on the “From device” Browse button on the right.

image

Click Add button in the Specify Backup window.

image

Select the Database you want to restore. In this case we will select Pubs.bak

image

After select the pubs.bak file check the box named ‘”Restore”

image

click on Options to check for more interesting restore options.

image

If you are going to restore transaction Logs after the initial Full backup, you need to select the appropriate options. They speak for themselves and need not be mentioned in detail here.

I have checked the “Overwrite the Existing Database” as i might have a existing copy of the pubs database and this option asks SQL Server to overwrite it if required.

once you click on the OK button, the Database will be restored.

image

image

and using T-SQL is as simple as :

image

PROCEDURE 2:

Stop the SQL Server Service and move the .mdf and .ldf files to SQL Server 2005 Server.

There are couple of ways of Stopping the SQL Server service. Either through Start –> Administrative Tools –> Services:

image

or through SQL Server configuration Manager.

image

Once the service is Stopped, Copy the .mdf and .ldf of the required database to the desired location of the new server : (Example we will copy the pubs database from original location to some other folder in c: Drive.)

I have copied the pubs.mdf and ldf files up one folder. instead of mssql.1 it is in the MSSQL Folder.

Next Step is to attach the Database on the new server.

Right click on Server/Databases and click on Attach:

image

In the Attach Dialog, click Add and select the appropriate mdf file.

image

image

Click OK and the database is attached.

PROCEDURE 3:

Detach Database from SQL Server 2000 and Attach it on SQL Server 2005 Server.

image

 

image

The Attach procedure is already described above, so use that procedure.

Check out PART 2 for What to do after the Database has been moved.

© 2010 D B A N A T I O N Suffusion WordPress theme by Sayontan Sinha
Better Tag Cloud