<?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>Bahramov's Personal Blog &#187; scheduled tasks</title>
	<atom:link href="http://www.bahramov.com/tag/scheduled-tasks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bahramov.com</link>
	<description>Computers, databases, networks and virtualization</description>
	<lastBuildDate>Wed, 28 Apr 2010 12:24:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Creating a compressed SQL Server Database backup</title>
		<link>http://www.bahramov.com/2009/03/19/creating-a-compressed-sql-server-database-backup/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://www.bahramov.com/2009/03/19/creating-a-compressed-sql-server-database-backup/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 18:23:25 +0000</pubDate>
		<dc:creator>Zaur Bahramov</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[batch]]></category>
		<category><![CDATA[scheduled tasks]]></category>

		<guid isPermaLink="false">http://www.bahramov.com/?p=72</guid>
		<description><![CDATA[In this post I will describe how to create a compressed backup of any SQL Server database in Windows. This is especially useful if you have Express or MSDE edition of SQL Server where there&#8217;s no SQL Agent. Let’s look a this example:

First we will create a folder, where we want to keep or database [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will describe how to create a compressed backup of any SQL Server database in Windows. This is especially useful if you have Express or MSDE edition of SQL Server where there&#8217;s no SQL Agent. Let’s look a this example:</p>
<ol>
<li>First we will create a folder, where we want to keep or database backups. Let&#8217;s call it C:\DbBackup.</li>
<li>Now, we’ll create a sql script that will backup our database. I will use Northwind database in this example. The created backup file will have the name Northwind_[current date]. bak Open a Notepad and paste the following T-SQL code:</li>
<pre class="brush: sql">-- Create backup of NorthWind Database
-- add current date to the file name and
-- save it inside C:\DbBackup folder
--
DECLARE @str NVARCHAR(255)
SET @str=N&#039;C:\DbBackup\Northwind_&#039;+CONVERT(VARCHAR, GETDATE(), 105)+&#039;.bak&#039;
BACKUP DATABASE Northwind TO DISK=@str</pre>
<li>Save file as NorthwindBackup.sql</li>
</ol>
<p><span id="more-72"></span><br />
Another thing that we want to do is to compress the backup file so that it takes less space on our hard drive. There’re many software out there both commercial, like <a href="http://www.rarlabs.com" target="_blank">WinRAR</a>, <a href="http://www.winzip.com">WinZIP </a>and free – <a href="http://www.7-zip.org/">7-Zip</a>. Also, you can use the COMPRESS.EXE utility from Windows NT/2000/ZP/2003 Resource Kit available for download from Microsoft site, to create a compressed archive. In this post I will show how to create archive with Compress.exe and Rar.exe utilities.</p>
<p>Create a new text file and write the following code [if using Compress.exe]:</p>
<pre class="brush: php">sqlcmd -E -i NorthwindBackup.sql -o Result.txt
compress.exe -Z -R NorthWind_*.bak NorthWind_*.zip
ren *.ba_ *.zip del *.bak *.zi_</pre>
<p>Save the file as NorthWindBackup.bat in C:\DbBackup.</p>
<p>OR</p>
<p>Create a new text file and write the following code [if using rar.exe]:</p>
<pre class="brush: php">sqlcmd -E -i NorthwindBackup.sql -o Result.txt
rar.exe a -df -inul -ag[dd_mm_yyyy] C:\DbBackup\NorthWind.rar C:\DbBackup\*.bak</pre>
<p>Save the file as NorthWindBackup.bat in C:\DbBackup.</p>
<p>When you run the NorthWindBackup.bat file it will first pass the NorthWindBackup.sql script to ‘<a href="http://technet.microsoft.com/library/ms162773.aspx">sqlcmd.exe</a>’. <a href="http://technet.microsoft.com/library/ms162773.aspx">sqlcmd.exe</a>- is a command line utility of Microsoft SQL Server 2005/2008 and is available in all versions. If you are using a Microsoft SQL Server 2000 then you will have to use <a href="http://technet.microsoft.com/library/ms162806.aspx">osql.exe</a> instead of sqlcmd.exe. The usage syntax of these utilities is very similar and you can read more about their usage in BOL (<a href="http://technet.microsoft.com/library/ms162773.aspx" target="_blank">sqlcmd syntax</a> and <a href="http://technet.microsoft.com/library/ms162806.aspx" target="_blank">osql syntax</a>). In my example utility starts with the default SQL Server parameters and the result is written to Result.txt. After that the backup file is compressed and the uncompressed backup file is deleted.</p>
<p>Further, you will need to create a Scheduled Task that will execute the NorthWindBackup.bat at certain time. For this reason you may use the <a href="http://support.microsoft.com/kb/308569" target="_blank">Windows Scheduled Task</a> from visual interface or create a task directly from the command line interface of  <a href="http://technet.microsoft.com/en-us/library/cc772785.aspx" target="_blank">Schtasks.exe</a> utility.</p>
<p>Here&#8217;s a command that will create a Scheduled Task to be run every day at 8:00 PM from Local System account:</p>
<pre class="brush: plain">schtasks /create /tn &quot;NorthWindBackup&quot; /tr c:\DbBackup\NorthWindBackupR.bat /sc daily /st 20:00:00 /ed 12/12/2010 /ru &quot;NT AUTHORITY\SYSTEM&quot;</pre>
<p>Sample files can be downloaded here: <a href="http://bahramov.com/wp-content/uploads/2009/03/DbBackup.zip"><img src="http://www.bahramov.com/images/icons/winzip7064.png" alt="Zip" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bahramov.com/2009/03/19/creating-a-compressed-sql-server-database-backup/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
