Today I came across a blog posting that contained a NAnt
task for Redgate SQL Compare. This task would be quite useful if you
planned on continuously upgrading a build server or test database with the latest
development changes. However I was looking to backup our current database and
drop it in a location that was accessible to our client. So I wrote the following
NAnt task:
<target name="BackupDatabase" >
<property name="project.dir" value="C:\Development\Northwind"/>
<property name="target.dir" value="C:\"/>
<property name="server" value="(local)"/>
<property name="databaseName" value="Northwind"/>
<!-- Backup the DB into the project directory -->
<exec program="osql.exe" verbose="true">
<arg line="-E -S ${server} -Q "BACKUP DATABASE ${databaseName} TO DISK = '${project.dir}\${databaseName}.bak'""/>
</exec>
<!-- Zip up the database in the project dir and copy it to the target dir -->
<zip zipfile="${target.dir}${databaseName}Database ${datetime::get-month(datetime::now())}-${datetime::get-day(datetime::now())}-${datetime::get-year(datetime::now())}.zip">
<fileset basedir="${project.dir}">
<include name="${databaseName}.bak" />
</fileset>
</zip>
<!-- Remove the temporary database backup from the project dir -->
<delete>
<fileset basedir="${project.dir}">
<include name="${databaseName}.bak" />
</fileset>
</delete>
</target>