Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 High Availability (2005)
 Deleting Expired Backupsets From Single .BAK File

Author  Topic 

GaryBiscuit
Starting Member

8 Posts

Posted - 2008-05-28 : 13:22:56
Hi,

I'm new to SQL Server 2005 (up until now I've been an Oracle DBA exclusively) and have been tasked with backing up all (6) of our SQL Server 2005 databases on a regular basis. To accomplish this, I've created individual SQL Server jobs for each database I wish to backup. Each of these jobs executes a T-SQL script like this:


BACKUP DATABASE [MYDATA] TO DISK = N'\\networkshare\mydata.bak' WITH RETAINDAYS = 4,
NOFORMAT,
NOINIT,
NAME = N'MyData-FullBackup',
SKIP,
NOREWIND,
NOUNLOAD,
STATS = 10
GO


My goal in specifying 'RETAINDAYS = 4' was so that there would always be 4 days worth of "backupsets" (forgive my Oracle-ese) kept in mydata.bak. But when I look at the amount of backupsets kept in mydata.bak I noticed that all of them were being kept and none were being deleted.


I researched this problem a little bit and thought I had discovered a solution by specifying 'INIT' instead of 'NOINIT' and 'NOSKIP' instead of 'SKIP', but this deletes the entire contents of mydata.bak only-if all of the backupsets contained in it are past their expiration date. I was hoping it would just delete the expired backupsets and keep the non-expired ones, but this isn't the case.


Does anyone know the simplest way - with T-SQL commands - to accmplish the task of backing up a database to 1 individual .bak file and also only retain x amount of "backupsets" within it? Any help would be greatly appreciated, and the more detailed the better. Thanks.


- Gary

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-28 : 13:25:41
Check out isp_Backup in my database maintenance plans link below in my signature. It does everything that you need.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

Haywood
Posting Yak Master

221 Posts

Posted - 2008-05-28 : 17:17:59
RETAINDAYS is only a setting that prevents the media in the file from being overwritten. Common practice is to backup a database to an individual file on a daily basis. Either overwrite the file, or append a timestamp to the backupfile name....
Go to Top of Page

GaryBiscuit
Starting Member

8 Posts

Posted - 2008-05-29 : 14:28:16
quote:
Originally posted by tkizer

Check out isp_Backup in my database maintenance plans link below in my signature. It does everything that you need.



Hey Tara,

I yanked your script, it looks good and that it'd definitely work for us here, but it looks like it's dependant on these objects:

* master.dbo.xp_backup_database
* master.dbo.xp_backup_database
* master.dbo.xp_backup_log

It doesn't look like our SQL databases have these objects by default... 1) Do I need to get these from somewhere else or 2) Does it matter if they *aren't* there?

Thanks,
Gary
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-29 : 14:49:13
It is not dependent on those. My stored procedure gives you the ability to use native backups or SQL Litespeed backups. If you haven't purchased the LS software, then just pass N to the variable. You will not get an error, but you will get warnings during the creation of the stored procedure. Ignore the warnings.



Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

GaryBiscuit
Starting Member

8 Posts

Posted - 2008-05-29 : 15:21:13
Tara,

Ahh, cool. Just ran your script. Seems to work great! Thanks for doing all the work :) My boss thanks you.

- Gary
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-29 : 15:27:13
You can show some love by clicking on the ad in my blog.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-29 : 15:32:44
I should mention that there'll be a new version of isp_Backup soon, probably in a couple of weeks. You might want to subscribe to my blog through an RSS reader to watch for updates as I periodically add features or fix bugs. Or just check the "Database maintenance routines" link to see if the isp_Backup link has changed. I update that blog anytime I have a new version so that it points to the latest and greatest scripts.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

GaryBiscuit
Starting Member

8 Posts

Posted - 2008-05-30 : 11:33:48
Tara,

I just showed you some love. Clicked the ad for "ApexSQL"... Enjoy the fraction of a milkshake responsible for my contribution :)

I've got isp_Backup stored on all 6 of our SQL instances. It's running great on almost all of the databases, however I did discover one problem; It looks like it gets mixed up on databases that have s p a c e s in their name...

i.e.

One of our SQL instances (named klee) has a bunch of individual databases in it. By sheer coincidence, when sorted alphabetically, the last 3 database names have spaces in their name:

...
pubs
SMS_097
SMS_Client_Health
Software Manager Database
Wise Services Database
Workbench Database

When isp_Backup attempts to run a full backup on all USER databases it gets to the point where it creates a directory named \Software - in an attempt, I'm guessing, to make a directory for the Software Manager Database. Nothing ever gets put into this directory and it looks like the script fails at this point.

Is there anything I could do to modify your script to accomodate for spaces in database names? I'll preemptively agree with you in that database names shouldn't have spaces to begin with, but this is the situation I've got to deal with unfortunately. Thanks in advance.

- Gary
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-30 : 11:50:43
I'll take a look at that. It'll require double quotes around the entire path when doing the DOS commands and will need square brackets around the database name during the backup. I'm already rewriting it for a couple of new features, so I'll be sure to include these bugs.

Stay tuned...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

GaryBiscuit
Starting Member

8 Posts

Posted - 2008-05-30 : 13:36:22
Cool... will do.

- Gary
Go to Top of Page

GaryBiscuit
Starting Member

8 Posts

Posted - 2008-06-16 : 10:00:37
Hey Tara,

Not sure how often you check this... any update on the backup script?

- Gary
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-16 : 18:49:12
I'm subscribed to the thread, so I'll get an email on replies.

I'm working on the script. An easy fix is to put double quotes around anywhere I'm touching path/file for DOS commands.

My script supports both native backups and SQL Litespeed backups, so I've got to get it working for both or put a warning that something isn't supported. It's time consuming to get it modified and tested. Right now, the spaces in the database name are breaking my Litespeed code, so I've got to see if LS even supports this.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-16 : 18:49:50
Here's where I'm at:


DECLARE
@path varchar(100),
@dbType sysname,
@bkpType char(4),
@retention smallint,
@liteSpeed char(1)

SELECT
@path ='E:\Backup\',
@dbType = '-Tara''s Test',
@bkpType = 'Diff',
@retention = 2,
@liteSpeed = 'Y'

SET NOCOUNT ON

DECLARE
@now char(14) -- current date in the form of yyyymmddhhmmss
,@dbName sysname -- database name that is currently being processed
,@cmd nvarchar(4000) -- dynamically created DOS command
,@result int -- result of the dir DOS command
,@rowCnt int -- @@ROWCOUNT
,@fileName varchar(200) -- path and file name of the BAK file
,@rc int -- return code
,@version char(1) -- one digit version number, i.e. 8 (2000) or 9 (2005)
,@fullBackup bit -- does full backup exist in msdb.dbo.backupset
,@edition int -- edition of SQL Server
-- (1 - Personal or Desktop Engine; 2 - Standard; 3 - Developer or Enterprise)

-- log shipping tables have been renamed in 2005
SET @version = CONVERT(char(1), SERVERPROPERTY('ProductVersion'))

IF @version NOT IN ('8', '9')
BEGIN
SET @rc = 1
GOTO EXIT_ROUTINE
END

-- Enterprise and Developer editions have msdb.dbo.log_shipping* tables, other editions do not
SET @edition = CONVERT(int, SERVERPROPERTY('EngineEdition'))

-- validate input parameters
IF @dbType IS NOT NULL AND @dbType NOT IN ('All', 'System', 'User') AND @dbType NOT LIKE '-%'
BEGIN
SET @rc = 2
GOTO EXIT_ROUTINE
END

IF @dbType LIKE '-%' AND @version = '8'
BEGIN
IF NOT EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE [name] = SUBSTRING(@dbType, 2, DATALENGTH(@dbType)))
BEGIN
SET @rc = 3
GOTO EXIT_ROUTINE
END
END
ELSE IF @dbType LIKE '-%' AND @version = '9'
BEGIN
IF NOT EXISTS (SELECT * FROM master.sys.databases WHERE [name] = SUBSTRING(@dbType, 2, DATALENGTH(@dbType)))
BEGIN
SET @rc = 3
GOTO EXIT_ROUTINE
END
END

IF @bkpType IS NOT NULL AND @bkpType NOT IN ('Full', 'TLog', 'Diff')
BEGIN
SET @rc = 4
GOTO EXIT_ROUTINE
END

IF @dbType = 'System' AND @bkpType <> 'Full'
BEGIN
SET @rc = 5
GOTO EXIT_ROUTINE
END

IF @liteSpeed IS NOT NULL AND @liteSpeed NOT IN ('N', 'Y')
BEGIN
SET @rc = 6
GOTO EXIT_ROUTINE
END

-- use the default backup directory if @path is null
IF @path IS NULL
EXEC master.dbo.xp_instance_regread
N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\MSSQLServer',N'BackupDirectory',
@path OUTPUT,
'no_output'

-- we need the backslash after the path, so add it if it wasn't provided in the input parameter
IF RIGHT(@path, 1) <> '\'
SET @path = @path + '\'

CREATE TABLE #WhichDatabase(dbName sysname NOT NULL)

-- put the databases to be backed up into temp table
IF @dbType LIKE '-%'
BEGIN
IF @bkpType = 'TLog' AND
DATABASEPROPERTYEX(SUBSTRING(@dbType, 2, DATALENGTH(@dbType)), 'RECOVERY') = 'SIMPLE'
BEGIN
SET @rc = 7
GOTO EXIT_ROUTINE
END

IF @edition = 3
BEGIN
IF @version = '8'
BEGIN
IF EXISTS
(
SELECT *
FROM msdb.dbo.log_shipping_databases
WHERE database_name = SUBSTRING(@dbType, 2, DATALENGTH(@dbType))
)
BEGIN
SET @rc = 8
GOTO EXIT_ROUTINE
END
END
ELSE IF @version = '9'
BEGIN
IF EXISTS
(
SELECT *
FROM msdb.dbo.log_shipping_primary_databases
WHERE primary_database = SUBSTRING(@dbType, 2, DATALENGTH(@dbType))
)
BEGIN
SET @rc = 8
GOTO EXIT_ROUTINE
END
END
END

IF @version = '9'
BEGIN
IF EXISTS
(
SELECT *
FROM master.sys.databases
WHERE [name] = SUBSTRING(@dbType, 2, DATALENGTH(@dbType)) AND source_database_id IS NOT NULL
)
BEGIN
SET @rc = 11
GOTO EXIT_ROUTINE
END
END

INSERT INTO #WhichDatabase(dbName)
VALUES(SUBSTRING(@dbType, 2, DATALENGTH(@dbType)))
END
ELSE IF @dbType = 'All'
BEGIN
IF @edition = 3 AND @version = '8'
INSERT INTO #WhichDatabase (dbName)
SELECT [name]
FROM master.dbo.sysdatabases
WHERE
[name] NOT IN ('tempdb', 'ReportServerTempDB') AND
[name] NOT IN (SELECT database_name FROM msdb.dbo.log_shipping_databases) AND
DATABASEPROPERTYEX([name], 'IsInStandBy') = 0 AND
DATABASEPROPERTYEX([name], 'Status') = 'ONLINE'
ORDER BY [name]
ELSE IF @edition = 3 AND @version = '9'
INSERT INTO #WhichDatabase (dbName)
SELECT [name]
FROM master.sys.databases
WHERE
[name] NOT IN ('tempdb', 'ReportServerTempDB') AND
[name] NOT IN (SELECT primary_database FROM msdb.dbo.log_shipping_primary_databases) AND
DATABASEPROPERTYEX([name], 'IsInStandBy') = 0 AND
DATABASEPROPERTYEX([name], 'Status') = 'ONLINE'
ORDER BY [name]
ELSE IF @version = '8'
INSERT INTO #WhichDatabase (dbName)
SELECT [name]
FROM master.dbo.sysdatabases
WHERE
[name] NOT IN ('tempdb', 'ReportServerTempDB') AND
DATABASEPROPERTYEX([name], 'IsInStandBy') = 0 AND
DATABASEPROPERTYEX([name], 'Status') = 'ONLINE'
ORDER BY [name]
ELSE -- version is 9
INSERT INTO #WhichDatabase (dbName)
SELECT [name]
FROM master.sys.databases
WHERE
[name] NOT IN ('tempdb', 'ReportServerTempDB') AND
DATABASEPROPERTYEX([name], 'IsInStandBy') = 0 AND
DATABASEPROPERTYEX([name], 'Status') = 'ONLINE'
ORDER BY [name]
END
ELSE IF @dbType = 'System'
BEGIN
IF @version = '8'
INSERT INTO #WhichDatabase (dbName)
SELECT [name]
FROM master.dbo.sysdatabases
WHERE [name] IN ('master', 'model', 'msdb')
ORDER BY [name]
ELSE
INSERT INTO #WhichDatabase (dbName)
SELECT [name]
FROM master.sys.databases
WHERE [name] IN ('master', 'model', 'msdb')
ORDER BY [name]
END
ELSE IF @dbType = 'User'
BEGIN
IF @edition = 3 AND @version = '8'
INSERT INTO #WhichDatabase (dbName)
SELECT [name]
FROM master.dbo.sysdatabases
WHERE
[name] NOT IN ('master', 'model', 'msdb', 'tempdb', 'ReportServerTempDB') AND
[name] NOT IN (SELECT database_name FROM msdb.dbo.log_shipping_databases) AND
DATABASEPROPERTYEX([name], 'IsInStandBy') = 0 AND
DATABASEPROPERTYEX([name], 'Status') = 'ONLINE'
ORDER BY [name]
ELSE IF @edition = 3 AND @version = '9'
INSERT INTO #WhichDatabase (dbName)
SELECT [name]
FROM master.sys.databases
WHERE
[name] NOT IN ('master', 'model', 'msdb', 'tempdb', 'ReportServerTempDB') AND
[name] NOT IN (SELECT primary_database FROM msdb.dbo.log_shipping_primary_databases) AND
DATABASEPROPERTYEX([name], 'IsInStandBy') = 0 AND
DATABASEPROPERTYEX([name], 'Status') = 'ONLINE'
ORDER BY [name]
ELSE IF @version = '8'
INSERT INTO #WhichDatabase (dbName)
SELECT [name]
FROM master.dbo.sysdatabases
WHERE
[name] NOT IN ('master', 'model', 'msdb', 'tempdb', 'ReportServerTempDB') AND
DATABASEPROPERTYEX([name], 'IsInStandBy') = 0 AND
DATABASEPROPERTYEX([name], 'Status') = 'ONLINE'
ORDER BY [name]
ELSE
INSERT INTO #WhichDatabase (dbName)
SELECT [name]
FROM master.sys.databases
WHERE
[name] NOT IN ('master', 'model', 'msdb', 'tempdb', 'ReportServerTempDB') AND
DATABASEPROPERTYEX([name], 'IsInStandBy') = 0 AND
DATABASEPROPERTYEX([name], 'Status') = 'ONLINE'
ORDER BY [name]
END
ELSE -- no databases to be backed up
BEGIN
SET @rc = 9
GOTO EXIT_ROUTINE
END

-- Remove snapshots
IF @version = '9'
DELETE t
FROM #WhichDatabase t
INNER JOIN master.sys.databases d
ON t.dbName = d.[name]
WHERE d.source_database_id IS NOT NULL

-- Get the database to be backed up
SELECT TOP 1 @dbName = dbName
FROM #WhichDatabase

SET @rowCnt = @@ROWCOUNT

-- Iterate throught the temp table until no more databases need to be backed up
WHILE @rowCnt <> 0
BEGIN
IF @bkpType = 'TLog' AND @dbType IN ('All', 'User') AND DATABASEPROPERTYEX(@dbName, 'RECOVERY') = 'SIMPLE'
PRINT 'Skipping transaction log backup of ' + @dbName
ELSE IF @bkpType = 'Diff' AND @dbName IN ('master', 'model', 'msdb')
PRINT 'Skipping differential backup of ' + @dbName
ELSE
BEGIN
-- Build the dir command that will check to see if the directory exists
SET @cmd = 'dir "' + @path + @dbName + '"'

-- Run the dir command, put output of xp_cmdshell into @result
EXEC @result = master..xp_cmdshell @cmd, NO_OUTPUT

-- If the directory does not exist, we must create it
IF @result <> 0
BEGIN
-- Build the mkdir command
SET @cmd = 'mkdir "' + @path + @dbName + '"'

-- Create the directory
EXEC master..xp_cmdshell @cmd, NO_OUTPUT

IF @@ERROR <> 0
BEGIN
SET @rc = 10
GOTO EXIT_ROUTINE
END
END
-- The directory exists, so let's delete files older than two days
ELSE IF @retention <> -1
BEGIN
-- Stores the name of the file to be deleted
DECLARE @whichFile VARCHAR(1000)

CREATE TABLE #DeleteOldFiles(DirInfo VARCHAR(7000))

-- Build the command that will list out all of the files in a directory
SELECT @cmd = 'dir "' + @path + @dbName + + '" /OD'

-- Run the dir command and put the results into a temp table
INSERT INTO #DeleteOldFiles
EXEC master..xp_cmdshell @cmd

-- Delete all rows from the temp table except the ones that correspond to the files to be deleted
DELETE FROM #DeleteOldFiles
WHERE
ISDATE(SUBSTRING(DirInfo, 1, 10)) = 0 OR
DirInfo LIKE '%<DIR>%' OR
SUBSTRING(DirInfo, 1, 10) >= GETDATE() - @retention

-- Get the file name portion of the row that corresponds to the file to be deleted
SELECT TOP 1 @whichFile =
SUBSTRING(DirInfo, LEN(DirInfo) - PATINDEX('% %', REVERSE(DirInfo)) + 2, LEN(DirInfo))
FROM #DeleteOldFiles

SET @rowCnt = @@ROWCOUNT

-- Interate through the temp table until there are no more files to delete
WHILE @rowCnt <> 0
BEGIN
-- Build the del command
SELECT @cmd = 'del "' + @path + + @dbName + '\' + @whichFile + + '" /Q /F'

-- Delete the file
EXEC master..xp_cmdshell @cmd, NO_OUTPUT

-- To move to the next file, the current file name needs to be deleted from the temp table
DELETE FROM #DeleteOldFiles
WHERE SUBSTRING(DirInfo, LEN(DirInfo) - PATINDEX('% %', REVERSE(DirInfo)) + 2, LEN(DirInfo)) = @whichFile

-- Get the file name portion of the row that corresponds to the file to be deleted
SELECT TOP 1 @whichFile =
SUBSTRING(DirInfo, LEN(DirInfo) - PATINDEX('% %', REVERSE(DirInfo)) + 2, LEN(DirInfo))
FROM #DeleteOldFiles

SET @rowCnt = @@ROWCOUNT
END
DROP TABLE #DeleteOldFiles
END
-- Get the current date using style 120, remove all dashes, spaces, and colons
SET @now = REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(50), GETDATE(), 120), '-', ''), ' ', ''), ':', '')

-- check for missing full backup
IF EXISTS
(
SELECT *
FROM [msdb].[dbo].[backupset]
WHERE database_name = 'QHOSMaster' AND backup_finish_date IS NOT NULL
)
SET @fullBackup = 1
ELSE
SET @fullBackup = 0

-- Build the backup path and file name, backup the database
IF @liteSpeed = 'N'
BEGIN
IF @bkpType = 'Full' OR @fullBackup = 0
BEGIN
SET @fileName = @path + @dbName + '\' + @dbName + '_' + @now + '.BAK'

BACKUP DATABASE @dbName
TO DISK = @filename
WITH INIT
END

IF @bkpType = 'Diff'
BEGIN
SET @fileName = @path + @dbName + '\' + @dbName + '_' + @now + '.DIF'

BACKUP DATABASE @dbName
TO DISK = @filename
WITH INIT, DIFFERENTIAL
END
ELSE IF @bkpType = 'TLog'
BEGIN
SET @fileName = @path + @dbName + '\' + @dbName + '_' + @now + '.TRN'

BACKUP LOG @dbName
TO DISK = @filename
WITH INIT
END
END
ELSE
BEGIN
DECLARE @regOutput varchar(20) -- stores the output from the registry
DECLARE @numProcs int -- stores the number of processors that the server has registered

-- Get the number of processors that the server has
EXEC master..xp_regread
@rootkey = 'HKEY_LOCAL_MACHINE',
@key = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment\',
@value_name = 'NUMBER_OF_PROCESSORS',
@value = @regOutput OUTPUT

-- We want n - 1 threads, where n is the number of processors
SET @numProcs = CONVERT(int, @regOutput) - 1

IF @bkpType = 'Full' OR @fullBackup = 0
BEGIN
SET @fileName = @path + @dbName + '\' + @dbName + '_LS_' + @now + '.BAK'

EXEC master.dbo.xp_backup_database
@database = @dbName,
@filename = @fileName,
@threads = @numProcs,
@init = 1
END

IF @bkpType = 'Diff'
BEGIN
SET @fileName = @path + @dbName + '\' + @dbName + '_LS_' + @now + '.DIF'

EXEC master.dbo.xp_backup_database
@database = @dbName,
@filename = @fileName,
@threads = @numProcs,
@init = 1,
@with = 'DIFFERENTIAL'
END
ELSE IF @bkpType = 'TLog'
BEGIN
SET @fileName = @path + @dbName + '\' + @dbName + '_LS_' + @now + '.TRN'

EXEC master.dbo.xp_backup_log
@database = @dbName,
@filename = @fileName,
@threads = @numProcs,
@init = 1
END
END
END
-- To move onto the next database, the current database name needs to be deleted from the temp table
DELETE FROM #WhichDatabase
WHERE dbName = @dbName

-- Get the database to be backed up
SELECT TOP 1 @dbName = dbName
FROM #WhichDatabase

SET @rowCnt = @@ROWCOUNT

-- Let the system rest for 5 seconds before starting on the next backup
WAITFOR DELAY '00:00:05'
END

SET @rc = 0

EXIT_ROUTINE:

IF @rc <> 0
BEGIN
DECLARE @rm varchar(500)
DECLARE @error table (returnCode int PRIMARY KEY CLUSTERED, returnMessage varchar(500))

INSERT INTO @error(returnCode, returnMessage)
SELECT 1, 'Version is not 2000 or 2005' UNION ALL
SELECT 2, 'Invalid option passed to @dbType' UNION ALL
SELECT 3, 'Database passed to @dbType does not exist' UNION ALL
SELECT 4, 'Invalid option passed to @bkpType' UNION ALL
SELECT 5, 'Only full backups are allowed on system databases' UNION ALL
SELECT 6, 'Invalid option passed to @liteSpeed' UNION ALL
SELECT 7, 'Can not backup tlog when using SIMPLE recovery model' UNION ALL
SELECT 8, 'Will not backup the tlog on a log shipped database' UNION ALL
SELECT 9, 'No databases to be backed up' UNION ALL
SELECT 10, 'Unable to create directory' UNION ALL
SELECT 11, 'Can not backup database snapshots'

SELECT @rm = returnMessage
FROM @error
WHERE returnCode = @rc

RAISERROR(@rm, 16, 1)
END

--RETURN @rc

DROP TABLE #WhichDatabase


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

GaryBiscuit
Starting Member

8 Posts

Posted - 2008-06-17 : 09:44:20
Tara,

Awesome... thanks for keeping me in the loop. Since I'm not using your script with LiteSpeed (@liteSpeed = 'N') will the updated code posted above work with spaces in the DB name?

- Gary
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-17 : 11:11:36
It should, but I've only done minimal testing on it. You'll notice that the code I posted below isn't wrapped into a stored procedure. For testing purposes, I run it as an ad-hoc script. I haven't yet re-wrapped it. But feel free to wrap it and test it out. Let me know how it goes. I'll get to the LS testing soon.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

GaryBiscuit
Starting Member

8 Posts

Posted - 2008-06-23 : 13:53:43
Tara,

I dropped your updated code into our SQL Server databases (after 'wrapping' it) and it seems to be working like a charm! I ran it in the one particular instance that had spaces in a few of the database names and it handled them fine. I'll let you know if anything acts up, but it seems to work great! Thanks again.

- Gary
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-23 : 13:59:33
No problem, glad it worked with my minimal testing. Let me know if you run into any other issues.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-19 : 17:15:13
I finally blogged my new code:
http://weblogs.sqlteam.com/tarad/archive/2008/08/19/Backup-SQL-Server-Databases.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -