<?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>WinBlogs.... &#187; sql express</title>
	<atom:link href="http://www.winblogs.net/index.php/tag/sql-express/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.winblogs.net</link>
	<description>Just another Windows geek blog...</description>
	<lastBuildDate>Thu, 27 Oct 2011 21:56:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Automating backup of databases in SQL Express.</title>
		<link>http://www.winblogs.net/index.php/2009/11/22/automating-backup-of-databases-in-sql-20052008-express/</link>
		<comments>http://www.winblogs.net/index.php/2009/11/22/automating-backup-of-databases-in-sql-20052008-express/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 15:45:29 +0000</pubDate>
		<dc:creator>Alesandro Slepčević</dc:creator>
				<category><![CDATA[Servers]]></category>
		<category><![CDATA[Windows and Powershell]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[backup sql database]]></category>
		<category><![CDATA[backup sql express database]]></category>
		<category><![CDATA[MSSQL]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[sql express]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows 2008]]></category>
		<category><![CDATA[windows 2008 r2]]></category>
		<guid isPermaLink="false">http://www.winblogs.net/?p=224</guid>
		<description><![CDATA[Microsoft SQL Server 2005/2008 Express is a free edition of SQL Server that is ideal for learning, developing and powering desktop, web and small server applications, and for redistribution by ISVs. Top Features Supports stored procedures, triggers, functions, and views Store all kinds of business data with native support for relational data, XML, FILESTREAM and spatial [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/mssql-logo.jpg"><img class="alignleft size-full wp-image-253" style="margin: 5px;" title="mssql-logo" src="http://www.winblogs.net/wp-content/uploads/2009/11/mssql-logo.jpg" alt="mssql-logo" width="122" height="73" /></a>Microsoft SQL Server 2005/2008 Express is a free edition of SQL Server that is ideal for learning, developing and powering desktop, web and small server applications, and for redistribution by ISVs.</p>
<h3>Top Features</h3>
<div>
<ul>
<li>Supports stored procedures, triggers, functions, and views</li>
<li>Store all kinds of business data with native support for relational data, XML, FILESTREAM and spatial data</li>
<li>Improved performance, usability, visualization, in addition to integration with the Microsoft 2007 Office System in SQL Server Reporting Services</li>
<li>Simplify development efforts by leveraging existing T-SQL skills, ADO.NET Entity Framework and LINQ</li>
<li>Closely integrated with Visual Studio and Visual Web Developer</li>
</ul>
<p>Because there&#8217;s no &#8220;SQL Agent&#8221; in SQL Express Editions, in order to create scheduled backups on SQL server Express edition, we need to user couple of tricks  <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </div>
<div><span id="more-224"></span></div>
<div>First, we need to have a query that will backup all databases to one folder&#8230; Well, here it is <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </div>
<p><code><br />
DECLARE @name VARCHAR(50) -- database name<br />
DECLARE @path VARCHAR(256) -- path for backup files<br />
DECLARE @fileName VARCHAR(256) -- filename for backup<br />
DECLARE @fileDate VARCHAR(20) -- used for file name<br />
SET @path = 'E:\sqlbackups\' --path to the backup folder<br />
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)<br />
DECLARE db_cursor CURSOR FOR<br />
SELECT name<br />
FROM master.dbo.sysdatabases<br />
WHERE name NOT IN ('tempdb')<br />
OPEN db_cursor<br />
FETCH NEXT FROM db_cursor INTO @name<br />
WHILE @@FETCH_STATUS = 0<br />
BEGIN<br />
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'<br />
BACKUP DATABASE @name TO DISK = @fileName<br />
FETCH NEXT FROM db_cursor INTO @name<br />
END<br />
CLOSE db_cursor<br />
DEALLOCATE db_cursor</code></p>
<p>This will backup everything except the &#8220;Temdb&#8221; database. So, lets take a look at the current state of our backup folder.</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/sqlbackup1.PNG"><img class="aligncenter size-medium wp-image-230" title="sqlbackup1" src="http://www.winblogs.net/wp-content/uploads/2009/11/sqlbackup1-300x99.PNG" alt="sqlbackup1" width="300" height="99" /></a></p>
<p>As we can see, only the backups from 21.11.2009 exist. So, what happens when we run the query using SQL Management Studio.</p>
<p>Here&#8217;s the query pasted in &#8220;New query window&#8221;.</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/sqlquery.PNG"><img class="aligncenter size-medium wp-image-232" title="sqlquery" src="http://www.winblogs.net/wp-content/uploads/2009/11/sqlquery-300x288.PNG" alt="sqlquery" width="300" height="288" /></a>Sooo&#8230;if everything went smoothly, we should get a message similar to this one :</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/sqlquery2.PNG"><img class="aligncenter size-medium wp-image-233" title="sqlquery2" src="http://www.winblogs.net/wp-content/uploads/2009/11/sqlquery2-300x141.PNG" alt="sqlquery2" width="300" height="141" /></a>Awesome! <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Now, lets take a look at our backup folder, are there any changes? Sure hope there is, because this post will totally suck if the backup didn&#8217;t complete <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/sqlbackup2.PNG"><img class="aligncenter size-medium wp-image-234" title="sqlbackup2" src="http://www.winblogs.net/wp-content/uploads/2009/11/sqlbackup2-300x132.PNG" alt="sqlbackup2" width="300" height="132" /></a></p>
<p>As we can see, this post will not suck after all <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Notice the new files, created on the 22.11.2009 . IT WORKS! <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  All we have to do now is to automate the backup, so we don&#8217;t have to run the query everyday by hand.</p>
<p>We will do that using Task Scheduler (or Scheduled tasks in Windows 2003). We have to add a task to execute everyday in 3 AM.</p>
<p>The command we have to run is:</p>
<p>&#8220;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Binn\SQLCMD.EXE&#8221; -i &#8220;c:\pathtosqlquery\sqlbackup.sql&#8221;</p>
<p>Where &#8220;sqlbackup.sql&#8221; is the name of the file you have pasted the code mentioned before.</p>
<p>Here are a couple of pictures so you can see how to do it:</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/schedule.PNG"><img class="aligncenter size-medium wp-image-235" title="schedule" src="http://www.winblogs.net/wp-content/uploads/2009/11/schedule-300x177.PNG" alt="schedule" width="300" height="177" /></a></p>
<p>Now, click on &#8220;Create task&#8221;. That will click will start the task wizard. Enter the name of the task ( i.e &#8220;SQL Backups&#8221;.</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/task1.PNG"><img class="aligncenter size-medium wp-image-237" title="task1" src="http://www.winblogs.net/wp-content/uploads/2009/11/task1-300x223.PNG" alt="task1" width="300" height="223" /></a>After entering the name and choosing the option &#8220;Run wheneter user is logged on or not&#8221;, click on &#8220;Triggers&#8221;.</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/TASK2.PNG"><img class="aligncenter size-medium wp-image-238" title="TASK2" src="http://www.winblogs.net/wp-content/uploads/2009/11/TASK2-300x224.PNG" alt="TASK2" width="300" height="224" /></a></p>
<p>Click on &#8220;New&#8221;. It will bring up a new window</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/TASK3.PNG"><img class="aligncenter size-medium wp-image-240" title="TASK3" src="http://www.winblogs.net/wp-content/uploads/2009/11/TASK3-300x256.PNG" alt="TASK3" width="300" height="256" /></a>Choose the &#8220;Daily&#8221; option, and set the time when you want the task to run. I have set it to 3 AM .</p>
<p><strong>ONE IMPORTANT THING!!! Dont forget to check the &#8220;Enable&#8221; checkbox <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  , otherwise, you will end up scratching your head and cursing Microsoft and uncle Bill <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  hehe.</strong></p>
<p>After that, click on &#8220;Actions&#8221;, where we will define the program that should be runned&#8230; Click on &#8220;New&#8221;&#8230;</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/TASK4.PNG"><img class="aligncenter size-medium wp-image-241" title="TASK4" src="http://www.winblogs.net/wp-content/uploads/2009/11/TASK4-300x234.PNG" alt="TASK4" width="300" height="234" /></a></p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/TASK5.PNG"><img class="aligncenter size-medium wp-image-242" title="TASK5" src="http://www.winblogs.net/wp-content/uploads/2009/11/TASK5-276x300.PNG" alt="TASK5" width="276" height="300" /></a>Paste this into the &#8220;Program/Script&#8221; box.</p>
<p><code>“C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Binn\SQLCMD.EXE”</code></p>
<p><strong>NOTE! &#8211; path to the &#8220;SQLCMD.exe&#8221; may be different on your system!!!</strong></p>
<p>All that is left to do is to &#8220;Add arguments&#8221;</p>
<p><code>-i “c:\pathtosqlqueryfile\sqlbackup.sql” </code></p>
<p>Click on &#8220;OK&#8221;, enter the system password and thats it. Enjoy your daily backup.</p>
<p><strong>HINT!!!! If you&#8217;re lazy like I am, you can use a powershell script to delete backup files older than X days. I have also covered that with a blog post. More info about that on this <a href="http://www.winblogs.net/index.php/2009/10/01/delete-files-older-from-in-powershell/">link</a>.</strong></p>
<p>Bye bye! <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="shr-publisher-224"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.winblogs.net/index.php/2009/11/22/automating-backup-of-databases-in-sql-20052008-express/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

