Continuing on the backup article I wrote yesterday, I'll show you how I do a daily script backup (.sql) of all the databases on my development machine. Before we take a look at the script, we'll need a couple of utilities (don't worry, they're free!).
Database Publishing Wizard (part of SQL Server Hosting Toolkit)
WinRAR (you probably already got this one installed!)
Now.. let's take a look at the script - it goes something like this:
echo off
cls
echo ***************************************************************************
echo ** Script all objects in databases and save them in 'dd-mm-yyyy' folder **
echo ***************************************************************************
cd C:\temp
C:
md %date%
cd %date%
"C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\sqlpubwiz" script -C "Data Source=.\SQLEXPRESS;Initial Catalog=PlastLogistik;Integrated Security=True" PlastLogistik.sql
"C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\sqlpubwiz" script -C "Data Source=.\SQLEXPRESS;Initial Catalog=SonofonAutomatiseretBrugerhaandtering;Integrated Security=True" SonofonAutomatiseretBrugerhaandtering.sql
"C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\sqlpubwiz" script -C "Data Source=.\SQLEXPRESS;Initial Catalog=aspnethotel;Integrated Security=True" aspnethotel.sql
echo ***************************************************************************
echo ** RAR compress all .sql script files **
echo ***************************************************************************
"C:\Program Files\WinRAR\WinRAR.exe" -ibck a PlastLogistik.rar PlastLogistik.sql
"C:\Program Files\WinRAR\WinRAR.exe" -ibck a SonofonAutomatiseretBrugerhaandtering.rar SonofonAutomatiseretBrugerhaandtering.sql
"C:\Program Files\WinRAR\WinRAR.exe" -ibck a aspnethotel.rar aspnethotel.sql
echo WinRAR has completed execution
echo ***************************************************************************
echo ** Delete all .sql script files **
echo ***************************************************************************
del PlastLogistik.sql
del SonofonAutomatiseretBrugerhaandtering.sql
del aspnethotel.sql
echo .SQL files deleted
It's really simple, actually - Let's break it down:
1) I switch to the default backup directory, in the example "c:\temp"
2) A new directory with todays date is created (if you're using DK locals the format is dd-mm-yyyy)
3) Using the Database Publishing Wizard, I do a script backup of each database (only three in this example, add as many as needed!)
4) Using WinRAR and the switch "-ibck" to disable the GUI, a .rar file is created for each .sql file (you can also compress all .sql files to a single .rar file if you prefer!)
5) Last step is to delete all those nasty .sql files (of course a "del *.sql" could also be used to minimize script if you don't store additional .sql files in the directory!)