Hints And Tips research knowledge archive
Tip# 130
Sponsored Links
 
Subject: Backup all databases and truncate logs on SQL Server 2000
Updated: Aug-31-2004
Rating: Not Rated
By: Rocky Moore - Member #: 461
Location: Klamath Falls, Oregon USA
Website: www.ReflectedThought.com
Category: Computers > Programming > Databases
Rate This Tip (10=Great):  1  2  3  4  5  6  7  8  9  10
I do not remember where I picked up the parts to this, but have been using it for some time.  It is a simple procedure that I have a scheduled job setup to backup all my databases ever three days and truncate all the logs.  Notice, there is a hard coded path in the procedure where it stores the backups which you will have to change.

CREATE    PROCEDURE BackupAllDatabases AS

SET QUOTED_IDENTIFIER off
select getdate() 'Start Time'
set nocount on

declare @dbname varchar(36),@cmd varchar(255),@logname varchar(2048)
declare dbname_cursor cursor
    for select d.name, rtrim(f.name)
     from master..sysdatabases d left join master..sysaltfiles f 
          on d.dbid = f.dbid and f.groupid = 0
     where d.name != 'tempdb'
     order by d.name

open  dbname_cursor
fetch dbname_cursor into @dbname, @logname

while @@fetch_status = 0
begin
if  DATABASEPROPERTYEX(@dbname,'Status') = 'ONLINE'
  begin
        select @cmd ='use ['+@dbname+'] backup database ['+@dbname+'] to DISK=''c:\MSSql\Backups\'+@dbname+'-'+convert(varchar(50),GetDate(),106)+'.bak'' with init'
           print @cmd
          execute (@cmd)

        select @cmd ='use ['+@dbname+'] BACKUP LOG ['+@dbname+'] WITH TRUNCATE_ONLY'
           print @cmd
          execute (@cmd)

      select @cmd ='use ['+@dbname+'] DBCC SHRINKFILE(['+@logname+'] , 255)'
         print @cmd
        execute (@cmd)
  end

fetch dbname_cursor into  @dbname, @logname
end

close dbname_cursor
deallocate dbname_cursor
select getdate() "End Time"


USE AT YOUR OWN RISK ....

[Submit DIGG for this tip]


-----------
Note: Use the tips posted on this site at your own risk. The tips are posted by the public and as such may or may not be valid.

Sponsored Links
     
Categories
hints and tips gold divider
Sponsor Sites To Visit
www.SaintsInLight.com
Thoughts on the Christian walk
along various Christian links.

CICK HERE
www.BringingInTheHarvest.com
Christian:  The harvest is ripe and
the day is short, we need to bring in
the harvest while there is time!

CLICK HERE
XML RSS News Feed For Recently Posted Hints And Tips  RSS Feed
You can now get updates to the Recent Tips section by using RSS. The address is:

HintsAndTips.com/Rss.aspx

* Posting Tips *
To post a Tip, Recommendation or Tips Wanted, simply browse to the category you feel is a best fit for your post (click on the Recommention or Tips Wanted tab if fitting) and then click on "New Post" option.
(C) Copyright 1998-2010 All Rights Reserved