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 2000 Forums
 SQL Server Administration (2000)
 Log Backup Between Re-Indexes

Author  Topic 

TSQLMan
Posting Yak Master

160 Posts

Posted - 2004-03-12 : 10:36:08
I am writing scripts for DBCC REINDEX. Because of temporary Disk Space concerns, I have to Reindex a a couple larger indexes and then Backup the log to keep the logs to a manageable size. Should I do this in the same SPROC, or should I seperate them and manage them by scheduled run times.

Does anyone have any backup log scripts to share, or at least an example that will backup a log, and give it 1 day to live.

Thanks,

nr
SQLTeam MVY

12543 Posts

Posted - 2004-03-12 : 11:30:10
I've a thing that does backups and deletes them after a time
http://www.nigelrivett.net/BackupAllDatabases.html
But it's probably more than you need.

I would keep the backups in a single SP and call it from your re-index SP if necessary.
You can then schedule the backup SP and if you find you need to add a call in the re-index without upsetting the encapsulation.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-03-12 : 12:35:38
I have this:



SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO



----------------------------------------------------------------------------------------------------
-- OBJECT NAME : isp_FullBackup_UserDBs
--
-- AUTHOR : Tara Duggan
-- DATE : December 18, 2003
--
-- INPUTS : @Path - location of the backups
-- OUTPUTS : None
-- DEPENDENCIES : None
--
-- DESCRIPTION : This stored procedure performs a full backup on all of the user databases
--
-- EXAMPLES (optional) : EXEC isp_FullBackup_UserDBs @Path = 'G:\MSSQL\Backup\'
--
-- MODIFICATION HISTORY :
----------------------------------------------------------------------------------------------------
--
----------------------------------------------------------------------------------------------------
ALTER PROC isp_FullBackup_UserDBs
(@Path VARCHAR(100))
AS

SET NOCOUNT ON

DECLARE @Now CHAR(14) -- current date in the form of yyyymmddhhmmss
DECLARE @DBName SYSNAME -- stores the database name that is currently being processed
DECLARE @SQL VARCHAR(7000) -- stores the dynamically created xp_backup_database command
DECLARE @cmd SYSNAME -- stores the dynamically created DOS command
DECLARE @Result INT -- stores the result of the dir DOS command
DECLARE @NumProcs INT -- stores the number of processors that the server has registered

-- Get the list of the databases to be backed up, does not include master, model, msdb, tempdb, Northwind, or pubs
SELECT name
INTO #WhichDatabase
FROM master.dbo.sysdatabases
WHERE name NOT IN ('master', 'model', 'msdb', 'pubs', 'tempdb', 'Northwind', 'DTTS_PANIC_distribution')
ORDER BY name

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

-- We want n - 1 threads, where n is the number of processors
SELECT @NumProcs = @NumProcs - 1

-- Iterate throught the temp table until no more databases need to be backed up
WHILE (SELECT COUNT(*) FROM #WhichDatabase) <> 0
BEGIN

-- Get the current date using style 120, remove all dashes, spaces, and colons
SELECT @Now = REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(50), GETDATE(), 120), '-', ''), ' ', ''), ':', '')

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

-- Build the dir command that will check to see if the directory exists
SELECT @cmd = 'dir ' + @Path + @DBName

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

-- If the directory does not exist, we must create it
IF @result <> 0
BEGIN

-- Build the mkdir command
SELECT @cmd = 'mkdir ' + @Path + @DBName

-- Create the directory
EXEC master.dbo.xp_cmdshell @cmd

END
-- The directory exists, so let's delete files older than two days
ELSE
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.dbo.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() - 2

-- Interate through the temp table until there are no more files to delete
WHILE (SELECT COUNT(*) FROM #DeleteOldFiles) <> 0
BEGIN

-- 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

-- Build the del command
SELECT @cmd = 'del ' + @Path + + @DBName + '\' + @WhichFile + ' /Q /F'

-- Delete the file
EXEC master.dbo.xp_cmdshell @cmd

-- 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

END

DROP TABLE #DeleteOldFiles

END

-- Build the xp_backup_database command dynamically
SELECT @SQL = ''
SELECT @SQL = @SQL + 'EXEC master.dbo.xp_backup_database ' + CHAR(10)
SELECT @SQL = @SQL + '@database = ''' + @DBName + ''', ' + CHAR(10)
SELECT @SQL = @SQL + '@filename = ''' + @Path + @DBName + '\' + @DBName + '_LS_Full_' + @Now + '.bkp'', ' + CHAR(10)
SELECT @SQL = @SQL + '@backupname = ''' + @DBName + ''', ' + CHAR(10)
SELECT @SQL = @SQL + '@desc = ''Full backup of ' + @DBName + ' - ' + CONVERT(VARCHAR(50), GETDATE()) + ''', ' + CHAR(10)
SELECT @SQL = @SQL + '@init = 1, ' + CHAR(10)
SELECT @SQL = @SQL + '@threads = ' + CONVERT(CHAR(1), @NumProcs) + CHAR(10)

-- Backup the database using xp_backup_database
EXEC (@SQL)

-- To move onto the next database, the current database name needs to be deleted from the temp table
DELETE
FROM #WhichDatabase
WHERE name = @DBName

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

END

DROP TABLE #WhichDatabase

SET NOCOUNT OFF

RETURN

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO





It was written for SQL Litespeed, but it can easily be rewritten for native BACKUP commands. Just ignore the processor registry stuff, it's for Litespeed as well.

It deletes files that are older than two days as well as create a directory for any new databases since the last time it ran.

Tara
Go to Top of Page

TSQLMan
Posting Yak Master

160 Posts

Posted - 2004-03-12 : 12:47:58
Thank you! I appreciate it.
Go to Top of Page

TSQLMan
Posting Yak Master

160 Posts

Posted - 2004-03-12 : 13:20:50
Tara

Looking through your query reminded me of something. I do a lot of long queries for reports, I write them all in caps, I know that all COMMANDS should be caps, and most else lowercase or properly Capitalized. Do you just type it that way and hold the shift key while you type the COMMANDS, or do you have another technique?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-03-12 : 13:23:46
Yeah I just type that way. I got in the habit about a year ago of typing the commands in upper case as I go. I used to write them in lower case, then go back and use CTRL+SHIFT+U to put them in upper case.

Tara
Go to Top of Page

TSQLMan
Posting Yak Master

160 Posts

Posted - 2004-03-12 : 13:26:20
I type about 74 words a minute using one case, and about 40 words a minute if I have to hold the shift key while I type. Old habits die hard I guess.

Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2004-03-13 : 11:16:31
I write all SQL in lower case.
Object names I will try to use the defined case but don't always succeed on case insenitive servers.
Object names I will define with a capital first letter for each word.
e.g. UserName

I find it more readable that way.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2004-03-14 : 03:37:13
nice script tara.
Go to Top of Page
   

- Advertisement -