Backup Information for a Database

How do I find the backup information about a particular database ?

Some one asked me this question in the past and since it is being repeatedly asked in forums and to me personally , I decided to write this.

Having the backup information of a database is very important for a database administrator. In case of disaster, the DBA has to be aware when was the last time that database was backed up.  If a SQL Server is inherited from another Admin and the schedule of the backups and the backup types performed is not known it is easy to find by querying the backup related tables in MSDB.

One of those tables is msdb.dbo.backupset. It has the backup information related to all databases on that instance. information such as backup size, backup type and the start and end time of the backup to name a few.

The following script is one such script that gives the backup info of a specific database with the most recent one first.

The value in bkup_type column indicates the type of backup performed. D indicates FULL backup, I indicates Differential and L indicates Log backup.

Use the following Script.

DECLARE @DBNAME VARCHAR(50)

SET @DBNAME = 'LSWB'

SELECT CEILING(((backup_size / 1024)/1024)/1024)
Bkup_Size_In_GB,
CEILING(((backup_size / 1024)/1024)) Bkup_Size_In_MB,
[type] Bkup_Type,
backup_start_date StartTime,backup_finish_date
FROM msdb.dbo.backupset
WHERE DATABASE_NAME = @DBNAME
ORDER BY backup_start_date DESC

— Bru Medishetty

Finding Shared Drives on a Cluster

When working in a clustered environment, it is a common practice to open the Cluster Administrator to look at the shared drives for the SQL Server instance.  This can be performed from one of the participating nodes in the cluster. Using SQL Server Dynamic Management View sys.dm_io_cluster_shared_drives we can query and retrieve the shared drives for the SQL Server.

Note that the DMV returns only one column DriveName. The script returns no values when run on a standalone installation of SQL Server.

For the convenience of users, the script can be copied from the text at the end of the post.

In the above image, there are 7 rows indicating the 7 shared drives for the SQL Server fail-over cluster.

select * from sys.dm_io_cluster_shared_drives

— Bru Medishetty

Scripting database objects in SQL Server 2008

In this article, we would go through the steps to script the objects in a database using SQL Server 2008. This might be required when we need to setup a database similar in structure to your existing database and move the data at a later time. Situations like setting up a new database on another server when performing a side-by-side upgrade or setting up development or training system. The steps explained here serve the task of setting up the initial database and SSIS or other tools can be used to move data.

Let’s take a look at the steps.

Right click the database which contains the objects to be scripted, from the context menu, choose Tasks and Generate Scripts. (Picture below)

The script wizard dialog box is started and Select Database step is displayed. (Picture below). The database on which we right clicked is selected by default in this step, you can alternately switch to a different database by selecting the database name in the list. I would like to script the reportServer database on my named instance LEARNSQLWITHBRU, hence the database name ReportServer$LEARNSQLWITHBRU. Click Next button to move to the next step.

In this example, I chose Script all objects in the selected database check box at the bottom in order to script all the objects in a sigle step, alternately if you wish to script a selected list of objects, you may want to click next without selecting  the check box.

Since this blog does not cover the other option, I shall give a brief description of what additonal steps would come up at each subsequent steps.

The following step, Choose Scripts Options (picture below) is the next step in the wizard, no matter what option you choose (all objects to script or individual objects). There are several options that you can choose when the objects are scripted. I am using SSMS in SQL 2008 and there are few additional options and changes in default values for few options, when compared to that of SQL 2005. For example, the option Script Indexes is set to True in SQL 2008 where as in SQL 2005 it is false and Script Data and Script Data Compression Options are some of the new options added in SQL 2008.

All of the options are Boolean values and you can change to True or False depending on your choice.  I am changing the Script Data option to True in the picture below.

Click next to continue to the Output Options (picture below). Remember if you have chosen not to script all objects in the database, there are few additional steps that require you to choose the Object types you want to script, and based on the different object types you selected (Schema, Stored Procedures, Tables etc), you get a step for each object type, where you have a choice to select desired objects individually or select all of the objects in that type. After choosing the objects in each object type you would finally come to the next step, Output Option.

There are 3 different destination options you can choose for the output, the default option is the Script to New Query Window when selected, would script the objects and open a new query in the Management Studio and displays the scripts. You can also choose to place the scripts in the clipboard for pasting into a text editor or a separate file that you many have opened, but remember to paste the script as soon as the wizard is completed, since the clipboard contents are not persisted, it may be overwritten if you happen to copy something else into the clipboard.

The Script to file option enables us to store the script to a file in the file system and here is another new addition in SQL Server 2008.   You have a choice to store the entire script in single file or each object script in an individual file; Unicode text format is an additional option available to store the scripts and it is the default in SQL Server 2008. Click Next in the dialog box to continue.

All the required input from the user is available and the wizard continues to the next step, it generates the scripts for all the objects of the database and also generates the scripts for indexes, constraints for the tables. In this example since we have chosen to script the data for the tables, the Insert statements will also be generated by the wizard, due to this it may take several minutes depending on the size of the tables. The image displayed below is the wizard at work in generating the Objects.

Once the wizard completes the scripting, a new query window is opened (depending on our choice). The notable enhancement in SQL Server 2008 is that the Insert statements are also generated as script, which is readily available to run.

One important point to be noted is that this feature is available when the Management Studio is SQL Server 2008 and it does not necessarily need that the database we are scripting should also be of SQL Server 2008. For this demonstration, I used a SQL Server 2005 database scripted using SSMS 2008.

— Bru Medishetty