Ramblings of a DBA

Tara Kizer
posts - 165, comments - 832, trackbacks - 75

My Links

Advertisement

News

Subscribe
Search this Blog

Archives

Post Categories

Work

Backup SQL Server Databases

EDIT: There is a new version of this stored procedure.

I've made several changes to my backup stored procedure, isp_Backup.  I fixed a few things and added some much needed features.  Here are all of my changes:

  1. Fixed the retention code so that only those files that pertain to the type of backup are deleted.  I had previously decided that I wanted all files older than @retention to be deleted, however I've changed my mind due to requests from my blog readers.
  2. Added support for database names with spaces, dashes, single quotes, and other annoying, special characters.
  3. Added support for British date format.
  4. Added feature for SQL Server 2005 to run a full backup if the transaction log chain is broken.  This starts the chain again.  If you run a transaction log backup on a database whose chain is broken, those transaction log backups are useless until a full backup is run.  I am unable to locate the necessary information in SQL Server 2000.  I've been told to locate the information in the msdb backup history tables, however no rows get put into these tables when the chain is broken.
  5. Added feature to run a full backup when a differential or transaction log backup is attempted but no full backup has ever been run.  This situation throws an error, so it's better to just run the full backup rather than having failed jobs.
  6. Added feature to run a full backup when last full backup is older than the create date or restore date of the database.  This is important on SQL Server 2000 instances where restores happen and a database with the same name existed before.  In that situation, no full backup has ever been made on the restored database yet the msdb backup history tables might contain rows pertaining to it due to using the same name. 

The last 3 changes were made to prevent job failures and for recoverability reasons. 

I have not gotten a chance to add code to check if a database is being log shipped by SQL LiteSpeed.  I haven't received any requests for this code nor do I have a situation like this where I work, so I've been avoiding this code change. 

Let me know if you'd like me to add any other features or if you find a bug.  After careful consideration of each, I'll make a decision whether or not to include them in the next version.

In the past I've embedded the code in my blog posts, however I'm now going to provide the script instead so that you get the formatted code.

To download the stored procedure, click here.

If you don't have SQL LiteSpeed installed, ignore the warnings in the output when creating this stored procedure.  They are warnings and not errors.  The stored procedure works with or without SQL LiteSpeed installed. 

Print | posted on Tuesday, August 19, 2008 2:09 PM | Filed Under [ SQL Server - Database Administration ]

Feedback

Gravatar

# re: Backup SQL Server Databases

Awesome. Thanks for sharing, Tara!

Joe
8/20/2008 3:57 AM | Joe Webb
Gravatar

# re: Backup SQL Server Databases

Great script, Tara.

This and the other scripts you have posted will be a big help for me. I did notice a change you might make if you think it is worth your while.

I was using this script today on my local machine and noticed that it backed up my Report Server Temp Database, which has a different name than what was in your script. It looks like the RS installer concatenated the instance name on the end of it. The database name is ReportServer$SQL2005TempDB. My instance name is SQL2005.

Since all of the production servers do not use named instances I can't check to see if this a default behavior.

Thanks.
8/20/2008 12:24 PM | David
Gravatar

# re: Backup SQL Server Databases

David,

You make an excellent point. In my next version, I'm not going to exclude ReportServerTempdb database. You don't need backups of it but since the name isn't constant across servers, I'm going to to just let my script back it up. I don't want to add code to isp_Backup to check if each database is an RS tempdb database. That would get too complex. Hopefully the database is small enough that those extra files won't matter on anyone's file system.
8/20/2008 12:50 PM | Tara
Gravatar

# re: Backup SQL Server Databases

Tara, I've been using your script happily for a long time now. Thanks for writing it!

I just upgraded my development server to SQL Server 2008. The script exits with the error:

Version is not 2000 or 2005 [SQLSTATE 42000] (Error 50000). The step failed.

I figured SQL Server 2008 should be able to do everything that 2005 can do, so I just changed all the checks for

@version = '9'

to:

@version in ('9','1')

(because it's version 10, and as a single character I guess it's '1'). As I was cutting and pasting I realized I could have just forced @version to '9' if it's '1' and I wouldn't have had to change all those other lines but I was almost done by then :)

Anyway, it works pretty well like this.

Do you know any major disasters I could run into off-hand by doing what I did (changing your script to make it work in 2008)?

-Chris
8/25/2008 8:36 AM | Chris
Gravatar

# re: Backup SQL Server Databases

It's a great backup script, thanks for sharing it with us.

We had a full-text catalog on one of the databases that became corrupt and due to that backups fail. Instead of skipping that faulty database the whole job stops with an error. Would it be possible to continue the script with the other databases and only report about the faulty one instead of stopping it completely?

On the other hand it's not really an issue for us atm, cause you can simply fix the error and rerun the job to let it complete for the other databases

9/11/2008 2:31 AM | Jeroen
Gravatar

# re: Backup SQL Server Databases

Jeroen,

I don't think I can control that as it encountered a fatal error and therefore got disconnected. I could be wrong though. If anyone has a query that I check beforehand, I'd be happy to add it to a future version.
9/11/2008 12:55 PM | Tara
Gravatar

# re: Backup SQL Server Databases

I am wondering if an option to change the folder layout would be helpful to others. As is, each backup for a given database is dropped into the sub-folder with the name of the database. The implication is that the backups from Sunday, Monday, Tuesday, etc. are in the same folder.

I have a server where I need to zip up the backups and ftp them to another server. If each day adds to the collective, my zip files would get larger and larger. I think an option to insert a datestamp folder in-between would be a nice option.

So, instead of Backups\db1 and Backups\db2...you can have Backups\yyyyMMdd\db1 an Backups\yyyyMMdd\db2. Then all you need to do is zip up the Backups\yyyyMMdd folder. (Of course I can work around this by setting retension to 1 day but I think the feature would be helpful anyways.)
9/22/2008 9:37 AM | Jason
Gravatar

# re: Backup SQL Server Databases

Bah...retension should be retention...too bad we cannot edit...
9/22/2008 9:56 AM | Jason
Gravatar

# re: Backup SQL Server Databases

Another feature may adding the ability to kick off the compression (zipping) of the backup as part of the isp_Backup stored proc. Since we know the exact name of the file this may not be too difficult.
9/22/2008 11:04 AM | Jason
Gravatar

# Great script!!

Hey Tara,

Just wanted to say thanks for the fantastic code!

I've built some additional stored procedures that dynamically create SQL Agent Jobs based on all server databases, which in turn call your backup proc.

Makes for a very robust backup code set!

let me know if you've any interest in the agent job creation stuff. I'd be more then happy to share!

Thanks again!

jeff
10/8/2008 3:19 PM | jeff
Gravatar

# re: Backup SQL Server Databases

its working.. superb tara...

thank u .. love ur sharing knowledgle things....
10/30/2008 12:04 AM | yohen
Gravatar

# re: Backup SQL Server Databases and Link Server

Hey Tara

fantastic code!

Please .... I need make a backup for a database, but this database are in another server, maybe can I use linkserver ???... but not found....

Thank´s for you answer...

Andrew.
11/13/2008 1:32 PM | ANDREW
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET