 




<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL Server with Mr. Denny &#187; xp_create_subdir</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/sql-server/tag/xp_create_subdir/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/sql-server</link>
	<description></description>
	<lastBuildDate>Fri, 17 May 2013 17:04:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Backup Databases on Creation</title>
		<link>http://itknowledgeexchange.techtarget.com/sql-server/backup-databases-on-creation/</link>
		<comments>http://itknowledgeexchange.techtarget.com/sql-server/backup-databases-on-creation/#comments</comments>
		<pubDate>Sat, 17 Nov 2012 14:00:56 +0000</pubDate>
		<dc:creator>Denny Cherry</dc:creator>
				<category><![CDATA[Backup & recovery]]></category>
		<category><![CDATA[BACKUP DATABASE]]></category>
		<category><![CDATA[CREATE TRIGGER]]></category>
		<category><![CDATA[Data Loss]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[DDL Trigger]]></category>
		<category><![CDATA[Recovery]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2008 R2]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[T/SQL]]></category>
		<category><![CDATA[Transactions]]></category>
		<category><![CDATA[Trigger]]></category>
		<category><![CDATA[xp_create_subdir]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/sql-server/?p=2194</guid>
		<description><![CDATA[One of the companies which I work with has the occasion to create new databases when they do releases of their software.  Normally this isn&#8217;t a problem, except that they are setup to use maintenance plans to handle the backup and pruning of their transaction logs.  As all the new databases are created in the [...]]]></description>
				<content:encoded><![CDATA[<p>One of the companies which I work with has the occasion to create new databases when they do releases of their software.  Normally this isn&#8217;t a problem, except that they are setup to use maintenance plans to handle the backup and pruning of their transaction logs.  As all the new databases are created in the full recovery model this can end up causing some problems for them as within 12 minutes they start getting emails saying that the transaction log backup job has failed.  And these emails will keep coming in, possibly for hours until the full backup job kicks in later that night.</p>
<p>To solve this problem, I added a DDL trigger to the server which will cause the new database to be backed up as soon as the database is created.  The trigger itself is rather simple.  Most of the trigger is setting variables.  Then I make sure that the database isn&#8217;t a database snapshot, as database snapshots can&#8217;t be backed up.  If it isn&#8217;t a snapshot we continue with everything else.</p>
<p>Then I create a subfolder for the backups to be put into (the backups for each database go into their own folder, so as this is a new database the folder needs to be created).  Then I commit the transaction, as database backups can&#8217;t be taken within a transaction.  Then we do the actual database backup.  I then throw a message to the end user using the RAISERROR statement telling them that they can ignore the other error that they are going to get about the transaction being closed before the trigger was complete.  This is just an annoyance of my needing to commit the transaction before taking the backup.  Sure I could have setup a job which takes the backup and emails if there was a failure, but that just seems to complex for something so simple.  The code for my trigger is below.</p>
<blockquote><p>CREATE TRIGGER BackupNewDatabase<br />
ON ALL SERVER<br />
FOR CREATE_DATABASE<br />
AS<br />
declare @database sysname, @event_data XML = EVENTDATA(), @folder nvarchar(4000), @file nvarchar(4000)</p>
<p>SET @database = @event_data.value(&#8216;(/EVENT_INSTANCE/DatabaseName)[1]&#8216;, &#8216;sysname&#8217;)</p>
<p>set @folder = &#8216;X:\Backups\&#8217; + @database</p>
<p>set @file = @folder + &#8216;\&#8217; + @database + &#8216;.bak&#8217;</p>
<p>if exists (select * from sys.databases where name = @database and source_database_id is null)<br />
BEGIN<br />
EXEC master.dbo.xp_create_subdir @folder</p>
<p>COMMIT</p>
<p>BACKUP DATABASE @database to disk=@file</p>
<p>raiserror( &#8216;You can ignore the error message which says that the transaction ended within the trigger.&#8217;, 16,1)<br />
END<br />
GO</p></blockquote>
<p>Hopefully you find this solution helpful if you get into a situation like this,<br />
Denny</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/sql-server/backup-databases-on-creation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
