Free SQL Server Learning - Making the most out of SQL Server Agent
solving sql server problems for millions of dbas and developers since 2006


SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Tip Categories SQL Server Tutorials SQL Server Webcasts SQL Server Whitepapers SQL Server Tools SQL Server Questions and Answers MSSQLTips Authors About MSSQLTips SQL Server User Groups SQL Server Events I am MSSQLTips MSSQLTips Advertising Options

MSSQLTips Facebook Page MSSQLTips LinkedIn Page MSSQLTips RSS Feed MSSQLTips Pinterest Page MSSQLTips Twitter Page MSSQLTips Google+ Page









SQL Product Highlight

SQL Sentry, Inc. - SQL Sentry Performance Advisor

SQL Sentry Performance Advisor for SQL Server delivers an advanced performance dashboard with relevant Windows and SQL Server metrics in a single view along with detailed insight of heavy SQL, blocking, deadlocks, and disk bottlenecks. Performance Advisor is packed with ground-breaking features that are not found in any other performance monitoring software, all designed with the singular goal of simplifying the process of optimizing your SQL Server performance.

Learn more!






































Automating Transaction Log Backups for All SQL Server Databases

By:   |   Read Comments (1)   |   Related Tips: 1 | 2 | 3 | 4 | More > Backup

Problem
Maintenance plans are a great thing, but sometimes the end results are not what you expect.  The whole idea behind maintenance plans is to simplify repetitive maintenance tasks without you having to write any additional code.  For the most part maintenance plans work without a problem, but every once in awhile things do not go as planned. Two of the biggest uses of maintenance plans are issuing full backups and transaction log backups.  What other approaches are there to issue transaction log backups for all databases without using a maintenance plan? 

Solution
With the use of T-SQL you can generate your transaction log backups and with the use of cursors you can cursor through all of your databases to back them up one by one. With the use of the DATABASEPROPERTYEX function we can also just address databases that are either in the FULL or BULK_LOGGED recovery model since you can not issue transaction log backups against databases in the SIMPLE recovery mode.

Here is the script that will allow you to backup the transaction log for each database within your instance of SQL Server that is either in the FULL or BULK_LOGGED recovery model.

You will need to change the @path to the appropriate backup directory and each backup file will take on the name of "DBname_YYYDDMM_HHMMSS.TRN".

DECLARE @name VARCHAR(50-- database name  
DECLARE @path VARCHAR(256-- path for backup files  
DECLARE @fileName VARCHAR(256-- filename for backup  
DECLARE @fileDate VARCHAR(20-- used for file name 

SET @path 'C:\Backup\'  

SELECT @fileDate CONVERT(VARCHAR(20),GETDATE(),112
   + 
'_' 
   
REPLACE(CONVERT(VARCHAR(20),GETDATE(),108),':','')

DECLARE db_cursor CURSOR FOR  
SELECT 
name 
FROM master.dbo.sysdatabases 
WHERE name NOT IN ('master','model','msdb','tempdb'
   AND 
DATABASEPROPERTYEX(name'Recovery'IN ('FULL','BULK_LOGGED')

OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @name   

WHILE @@FETCH_STATUS 0   
BEGIN   
       SET 
@fileName @path @name '_' @fileDate '.TRN'  
       
BACKUP LOG @name TO DISK = @fileName  

       
FETCH NEXT FROM db_cursor INTO @name   
END   

CLOSE 
db_cursor   
DEALLOCATE db_cursor 

In this script we are bypassing the system databases, but these could easily be included as well. You could also change this into a stored procedure and pass in a database name or if left NULL it backups all databases. Any way you choose to use it, this script gives you the starting point to simply backup all of your databases.

Next Steps

  • Add this script to your toolbox
  • Modify this script and make it a stored procedure to include one or many parameters
  • Create a scheduled task to backup your transaction logs on a set schedule
  • Take a look at this tip that does FULL backups for all databases.
  • Send your improved script to tips@mssqltips.com and we will post it on the site for others to use


Last Update: 8/28/2007

About the author



Print  
Become a paid author


Comments and Feedback:

Thursday, February 05, 2009 - 10:23:11 AM - Netchem Read The Tip

This script offers a big advantage over simply using the maintenance plan GUI to setup transaction log backups: in SQL Server 2005, if you edit a transaction log backup task, all of your settings in that task will clear, and if it encounters a failure, the entire task fails, whereas this script skips the offending DB and moves on to the others.

While this will get around transaction log backups failing due to do databases being set to SIMPLE recovery, it doesn't help with databases that haven't had a full backup created yet. Could someone show me how to modify this script to check for the existance of a full backup, and if it doesn't exist, create a full backup, then proceed with the transaction log backup for that database?

 Thank you!



Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email Notify for updates

Signup for our newsletter


Comments
*Enter Code refresh code


 

Sponsor Information
SQL Server having some performance issues? Idera SQL check. FREE SQL Server enhancement.

Get your SQL Server database under version control now! Find out why...

Is “blocking” a bad word at your company? Contact the Edgewood SQL Server Consultants for the resolution.

Solving SQL Server problems for millions of DBAs and Devs since 2006. Join now.

Optimizing SQL Server performance can be a daunting task. Or is it?


Copyright (c) 2006-2013 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com