Creating a compressed SQL Server Database backup
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’s no SQL Agent. Let’s look a this example:
- First we will create a folder, where we want to keep or database backups. Let’s call it C:\DbBackup.
- 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:
- Save file as NorthwindBackup.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'C:\DbBackup\Northwind_'+CONVERT(VARCHAR, GETDATE(), 105)+'.bak' BACKUP DATABASE Northwind TO DISK=@str
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 WinRAR, WinZIP and free – 7-Zip. 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.
Create a new text file and write the following code [if using Compress.exe]:
sqlcmd -E -i NorthwindBackup.sql -o Result.txt compress.exe -Z -R NorthWind_*.bak NorthWind_*.zip ren *.ba_ *.zip del *.bak *.zi_
Save the file as NorthWindBackup.bat in C:\DbBackup.
OR
Create a new text file and write the following code [if using rar.exe]:
sqlcmd -E -i NorthwindBackup.sql -o Result.txt rar.exe a -df -inul -ag[dd_mm_yyyy] C:\DbBackup\NorthWind.rar C:\DbBackup\*.bak
Save the file as NorthWindBackup.bat in C:\DbBackup.
When you run the NorthWindBackup.bat file it will first pass the NorthWindBackup.sql script to ‘sqlcmd.exe’. sqlcmd.exe- 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 osql.exe instead of sqlcmd.exe. The usage syntax of these utilities is very similar and you can read more about their usage in BOL (sqlcmd syntax and osql syntax). 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.
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 Windows Scheduled Task from visual interface or create a task directly from the command line interface of Schtasks.exe utility.
Here’s a command that will create a Scheduled Task to be run every day at 8:00 PM from Local System account:
schtasks /create /tn "NorthWindBackup" /tr c:\DbBackup\NorthWindBackupR.bat /sc daily /st 20:00:00 /ed 12/12/2010 /ru "NT AUTHORITY\SYSTEM"

Thank you. Your post was very useful for what I needed.