SQL Maintenance Plans and Backed Up Databases: Pt. 2
June 3, 2002
In the first part of this series, a script was used to query an SQL server for databases that were being backed up as part of the maintenance plans. This allows one to determine if a database is part of a maintenance plan. It would, in most cases, be nice to have the pertinent backup information on hand. The following class will return the relevant backup information from the maintenance plan so it can be viewed in a more user friendly manner.
If the script presented from the first series is combined with this script one would be able to loop through all the databases in the maintenance plans and return their individual backup information to a user interface. By taking these classes and converting them to ASP scripts, a web page can be created to display the current backup situation on a given SQL server.
Some of these techniques will be presented in upcoming articles. In this article, however, a script to present the backup information is going to be presented.
The code for this article can be found here. The following is an example of the code needed to return the backup information for a given database. By entering the server and database name, one can query to find the last backup for a given database.
There are two message boxes here that return the backup information. The
message boxes demonstrate two ways information can be returned from the class. The
first method is to use
The beginning of the class has an explanation for the properties and methods of the class. This section is not enumerated. The enumerated section of the code starts by initializing the needed variables (lines 1-18). The only code needed in the initialize routine sets the security variable to integrated security by default. The terminate routine closes the connection to the server.
Lines 28-116 are where the let properties are defined. These are the five settings the user has the ability to control. In this case the user can set the SQLServer, the Database, the UserID, the Password, and the Security. When the SQLServer property and the Database properties are set, a check is made to see if both properties have been set (lines 30 and 68). If both properties have been set the rest of the let property routines behave the same for these two propeties. A SQL statement is constructed, a connection is open and a recordset is returned. The record set is checked to make sure it is not empty and the values are read into the variables. When the recordset values are read into the private variables they are then available as properties to the users via the get statements which will be discussed below.
The UserID and Password properties need to be set, as mentioned above, if the server will not be accessible via integrated security. The security setting does not need to be set as it is set to integrated by default. This setting might be used if one wanted to change servers and databases. One server may be able to use integrated security while another needs an SQL login.
The class has eight get properties, which are the properties the user can get once the object has been instantiated. The SQLServer and Database properties should be known so they may not need to be returned. The other six properties (lines 118 - 148) can be used by the user to format the database backup information. StartTime, EndTime and Duration give the user an idea of how long a backup takes. The success property lets the user know if the backup was successful. The plan property lets the user know which database maintenance plan the backup is a member of and the message property lists where physically the backup was written.
Lines 151 - 168 are a private routine to open a connection to the database. Lines 170-172 are a private routine to close the connection to the database. The close routine is called by the terminate routine. The final method is the GetBackuHist. This method returns a string with the same information returned by the individual properties. This method is used mostly for troubleshooting or in a case where a script needs to return information without regards to format.
This routine is used to query maintenance plans for information regarding backups. The routine allows one to draft formatted messages using the properties of the class. The class can be used in conjunction with other routines to create a reporting mechanism for SQL backup procedures. In the next article, both this script and the previous script will be used in conjunction with SQL-DMO to find servers and query the maintenance plans on those servers.