asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Automated Backup with NAnt
By Liam McLennan
Rating: 3.6 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article


    For those who don't know, NAnt is a .NET build scripting tool. It is a lot like the Java build tool Ant, so everything in this article is probably also applicable for Ant. This article assumes a intermediate knowledge of the NAnt tool.

    Though the purists will probably disagree, NAnt is essentially a high-level programming language, optimized for automating common build tasks, such as source control operations, compiling, moving files and deleting files. Because of the automation it allows, NAnt has become very popular for use in continuous integration. But what else might NAnt be good for?

    The rest of this article will work through a difficult backup problem that I solved with Nant. I will demonstrate how to use NAnt to create a customizable, heterogeneous and effective backup system. This article assumes basic familiarity with NAnt. If you would like to learn more about this tool, lots of information is available on the NAnt site (http://nant.sourceforge.net).

    The server being backed up in this article runs Windows 2003, .NET, SQL, MySQL, MSDE, PHP, and a mail server. There are ninety-five clients hosted on the server and they have a variety of backup requirements. Some clients have never considered backups, some prefer to manage it themselves, while some are paranoid that they will lose everything and require a formal, reliable backup process. Due to the variety of different requirements, combined with a limited budget, commercial tools are not a viable solution to this bakcup problem.

    I categorized the various backup types that I needed as follows: IIS metadata backup, MySQL data backup, MS SQL data backup, and backup of each of the sites files. Looking at this list it seemed that perhaps NAnt might be capable of fulfilling my needs. There was also the question of what to do with the backups. The options that I could see were: zip the files and store them on the server, zip the files and store them on a network backup drive, zip the files and send them with email. Once again, different clients needed different solutions.

    Other design goals for my new backup system were that it should be scheduled, and adding new clients to the server should not result in an excessive amount of work configuring my backup process.

    Backup Challenge 1: Backup the IIS Metabase

    Backup Challenge 2: Backup the Site Files

    Backup Challenge 3: Backup the server databases

    Summary

    Backup Challenge 1: Backup the IIS Metabase

    The IIS metabase stores the configuration of IIS. This combined with the site data is enough to restore a web server in the event of some dramatic failure. One way to backup the IIS metabase is to right-click the computer to backup in the IIS snap-in and select 'Backup/Restore Configuration...' from the 'All Tasks' menu item. Fortunately, this functionality is also available with scripting.

    In Windows 2000 or Windows XP a backup can be made with the following command line call:

    cscript metaback.vbs

    For Windows 2003, use this command line call:

    cscript iisback.vbs /backup /b <backup name>


    Executing these commands creates an IIS backup that can later be used to restore the IIS configuration. Because they are simple command line calls it is simple to include them in a NAnt script, such as this one:

    (NB: In my NAnt scripts, the values listed in the <!-- Parameters --> section at the start of the script are values that the script expects to have been set as properties by the calling script.)


    <?xml version="1.0"?>
    <project name="IIS" default="CreateIISBackup">

    <!-- Parameters
         backupName
         scriptDirectory
    -->

    <target name="CreateIISBackup" >
         <exec basedir="${ scriptDirectory }" program="cscript" commandline="iisback.vbs /backup /b ${backupName}" />
    </target>

    </project>

    Save this script as IIS.build. Because this script requires a property to be set before it is called, it cannot be run directly from the command line. A script to run the IIS script looks like this:

    <?xml version="1.0"?>
    <project name="BackupIIS" default="Backup">

    <property name="backupName" value="IISBackup" />
    <!-- For this script to work, this must be set to the location of the script (metaback.vbs or IISback.vbs) -->
    <property name=" scriptDirectory" value="IISBackup" />

    <target name="Backup" >
         
    <nant buildfile="IIS.build" inheritall="true" />
    </target>

    </project>

    Save this script as BackupIIS.build. Now when this backup is run it should create an IIS backup called 'IISBackup'. To run the backup make sure the two scripts are in the same directory and that nant.exe is in the system path. Open a command prompt and
    change to the directory that the scripts are in, then type:

    nant -buildfile:BackupIIS.build
    and press enter.

    To check that the backup worked open the IIS snap-in, right click on your computer node, and select 'Backup/Restore Configuration...' from the 'All Tasks' menu. You should see a backup called 'IISBackup' in the Backups grid.

    Backup Challenge 2: Backup the Site Files

    The requirement is as follows; backup all files in some sites. For each site that requires backup, zip the files and email the archive to a secure storage location.

    I started by writing a NAnt script with two targets. The first target compresses a directory, the second target compresses a directory and then sends the archive via email.

    <?xml version="1.0"?>
    <project name="FileBackup" >

    <!--parameters
         baseDirectory This is the parent of the directory to be compressed
         targetDirectory This is the directory to be compressed
         backupDirectory This is the location to store the archives
    -->

    <target name="CompressDirectory">
         
    <zip zipfile="${backupDirectory}${directoryName}.zip" >
              
    <fileset basedir="${baseDirectory}">
              
    <include name="${ targetDirectory}/*" />
                   
    <include name="${targetDirectory }/**/*" />
              
    </fileset>
         
    </zip>
    </target>

    <target name="CompressDirectoryAndEmail" depends="CompressDirectory">
         
    <mail from="backup@anon.com"
              
    tolist="secure@anon.com"
              
    subject="${ targetDirectory} backup"
              
    mailhost="localhost">
         
         <files>
                   
    <include name="${backupDirectory}${ targetDirectory}.zip" />
              </files>
              
    <attachments>
                   
    <include name="${backupDirectory}${ targetDirectory}.zip" />
              
    </attachments>
         
    </mail>
         
    <!-- The backup has be mailed so the file is no longer required. -->
         
    <delete file="${backupDirectory}${targetDirectory}.zip" />
    </target>

    </project>

    Save this file as FileBackup.build. As with the IIS backup before, this script can not be run directly because it requires parameters to be set. Another script is required to set the parameters and specify backup policies for each site that requires backup. For this example, assume that I have all of my websites stored in a directory called C:\inetpub. There is also another directory called C:\backup. I currently have three websites: web1, web2 and web3. They are stored in C:\inetpub\web1, C:\inetpub\web2 and C:\inetpub\web3 respectively. The owner of web1 requires that the site be backed up nightly, and the archive be stored on the server in C:\backup. The owners of web2 and web3 require that their sites be backed up nightly and emailed to a secure location, but they do not require that a copy be stored on the server. These requirements are implemented with the following script:

    (NB: This script sends email using the localhost SMTP server. Your local SMTP server must be configured to allow this.)


    <?xml version="1.0"?>
    <project name="BackupWebsites" default="Backup">

    <!-- These two properties can be declared globally, because they are the same for all sites. -->
    <property name="baseDirectory" value="C:\inetpub\" />
    <property name="backupDirectory" value="C:\backup\" />

    <!-- This target exists only to specify which sites are to be backed up, and in what order. -->
    <target name="Backup" depends="BackupWeb1,BackupWeb2,BackupWeb3" />

    <target name="BackupWeb1">
         <property name="targetDirectory" value="web1" />
         <nant buildfile="FileBackup.build" inheritall="true" target="CompressDirectory" />
    </target>

    <target name="BackupWeb1">
         <property name="targetDirectory" value="web2" />
         <nant buildfile="FileBackup.build" inheritall="true" target="CompressDirectoryAndEmail" />
    </target>

    <target name="BackupWeb1">
         <property name="targetDirectory" value="web3" />
         <nant buildfile="FileBackup.build" inheritall="true" target="CompressDirectoryAndEmail" />
    </target>

    </project>

    Save this script as BackupWebsites.build. To run this backup make sure that FileBackup.build and BackupWebsites.build are in the same directory. Open a command prompt and navigate to this directory. Then run the following command:

    nant -buildfile:BackupWebsites.build

    The result of this script should be that an archive called web1.zip is created in C:\backup, and archives called web2.zip and web3.zip are emailed to secure@anon.com.

    Backup Challenge 3: Backup the Server Databases

    The server currently hosts two databases servers that contain databases that need to be backed up. One is a MySQL 5 database, and one is a MSDE database. The users who want their databases backed up are requesting that the backups be zipped and emailed to a secure location. The first step is to write a NAnt script to backup a single database. The script needs to have a target for backing up a MySQL database, and a target for backing up a MSDE database. For the MySQL backup, I will use the mysqldump utility, executing the following command for the database that is being backed up:

    mysqldump -u <username> -p<password> <databasename>

    I recommend creating a read-only user specifically for backup. To backup the MSDE databases I will use the osql utility, executing the following command to backup a database:

    osql -E -U <username> -P <password> -Q "BACKUP DATABASE <databasename> To DISK = <backupfile>;"

    Once again, it is recommended to create a read-only user specifically for backup.

    (NB: This script sends email using the localhost SMTP server. Your local SMTP server must be configured to allow this.)

    <?xml version="1.0"?>
    <project name="DatabaseBackup">

    <!-- parameters
         mysqlPath (optional) Only required for the MySQLBackup target. This is the path to the mysqldump executable (in the mysql bin directory).
         
    databaseName The name of the database to backup.
         
    username The user account to use for the backup.
         
    password The user's password.
         
    tempDirectory A temporary directory to store the database backup, prior to emailing it.
    -->

    <target name="BackupMySQL">
         <exec basedir="${mysqlPath}" program="mysqldump"
              
    commandline="-u ${username} -p${password} ${databaseName}"
              
    output="${tempDirectory}${databaseName}.sql" />
         
    <zip zipfile="${databaseName}.zip">
         
         <fileset basedir="${tempDirectory}">
                   
    <include name="${tempDirectory}${databaseName}.sql" />
              </fileset>
         
    </zip>
         
    <mail
              
    from="backup@anon.com"
              
    tolist="secure@anon.com"
              
    subject="${databaseName} database backup"
              
    mailhost="localhost">
              
    <files>
                   
    <include name="${tempDirectory}${databaseName}.zip" />
              
    </files>
              
    <attachments>
                   
    <include name="${tempDirectory}${databaseName}.zip" />
              
    </attachments>
         
    </mail>
         
    <delete file="${tempDirectory}${databaseName}.zip" />
    </target>

    <target name="BackupMSSQL">
         
    <exec program="osql"
              
    commandline="-E -U ${username} -P ${password} -Q "BACKUP DATABASE ${databaseName} To DISK = '${tempDirectory}${databaseName}.dat';" />
         
    <zip zipfile="${tempDirectory}${databaseName}.zip">
              
    <fileset basedir="${tempDirectory}">
                   
    <include name="${tempDirectory}${databaseName}.dat" />
              
    </fileset>
         
    </zip>
         
    <mail
              
    from="backup@anon.com"
              
    tolist="secure@anon.com"
              
    subject="${databaseName} database backup"
              
    mailhost="localhost">
              
    <files>
                   
    <include name="${tempDirectory}${databaseName}.zip" />
              
    </files>
              
    <attachments>
                   
    <include name="${tempDirectory}${databaseName}.zip" />
         
         </attachments>
         
    </mail>
         
    <delete file="${tempDirectory}${databaseName}.zip" />
    </target>

    </project>

    Save this script as DatabaseBackup.build. Once again, we need another script to control the database backup. Recall the three websites hosted on the server: web1, web2 and web3. Each of this application has a database. Web1 and web2 use MySQL databases named web1 and web2 respectively. Web3 uses an MSDE database called web3. The owners of each of these databases require their data to be backed up and emailed to a secure location. The script to do this follows:

    <?xml version="1.0"?>
    <project name="BackupDatabases" target="Backup">

         <!-- This property can be declared globally, because it is the same for all databases. -->
         <property name="tempDirectory" value="C:\backup\" />
         <property name="mysqlPath" value="C:\program files\mysql\bin\" />

    <!-- This target exists only to specify which databases are to be backed up, and in what order. -->
    <target name="Backup" depends="BackupWeb1,BackupWeb2,BackupWeb3" />

    <target name="BackupWeb1">
         <property name="databaseName" value="web1" />
         <property name="username" value="web1user" />
         <property name="password" value="P@ssword" />
         <nant buildfile="DatabaseBackup.build" inheritall="true" target="BackupMySQL" />
    </target>

    <target name="BackupWeb2">
         <property name="databaseName" value="web2" />
         <property name="username" value="web2user" />
         <property name="password" value="P@ssword" />
         <nant buildfile="DatabaseBackup.build" inheritall="true" target="BackupMySQL" />
    </target>

    <target name="BackupWeb3">
         <property name="databaseName" value="web3" />
         <property name="username" value="web3user" />
         <property name="password" value="P@ssword" />
         <nant buildfile="DatabaseBackup.build" inheritall="true" target="BackupMSSQL" />
    </target>

    </project>

    Save this script as BackupDatabases.build. To test this script make sure it is saved in the same directory as DatabaseBackup.build. Open a command prompt, navigate to this directory, and run the following command:

    nant -buildfile:BackupDatabases.build

    The result of running this script is that the contents of the three databases will be added to zip archives and emailed to secure@anon.com.

    Summary

    So now we have scripts to backup our IIS metadata, our website files, and the contents of our databases. The only thing left to do is consolidate all of these backup tasks and set them to run on a schedule. The easiest way to consolidate all of the build tasks is to define a batch file, such as this one:

    nant -buildfile:BackupIIS.build
    nant -buildfile:BackupWebsites.build
    nant -buildfile:BackupDatabases.build

    Thats it! With this system in place you get a full backup every night without any manual intervention. When new sites are added to the server it is a trivial matter to add them to the BackupWebsites and BackupDatabases scripts. All of the tools used are free, and reliable.

    The potential of NAnt is far greater than just being a build tool. I have demonstrated how it can be used as a powerful automated backup tool, and there are many other possibilities. Every time you find yourself performing a repetitive administration task, ask yourself, "could NAnt be doing this for me?"

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jul 21, 2005 - N-Tier Web Applications using ASP.NET 2.0 and SQL Server 2005 - Part 1
    While the .NET Framework made building ASP.NET applications easier then it had ever been in the past, .NET 2.0 builds on that foundation in order to take things to the next level. This article shows you to how to construct an N-Tier ASP.NET 2.0 Web application by leveraging the new features of ASP.NET 2.0 and SQL Server 2005.
    [Read This Article]  [Top]
    Apr 28, 2005 - New Files and Folders in ASP.NET 2.0
    With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.NET by introducing a suite of new features and functionalities. As part of this release, ASP.NET 2.0 also comes with a host of new special files and folders that are meant to be used to implement a specific functionality. This article examines these new files and folders in detail and provides examples that demonstrate how to utilize them to create ASP.NET 2.0 applications.
    [Read This Article]  [Top]
    Mar 10, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2, Cont'd
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 9, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 3, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1, Cont'd
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Mar 2, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Feb 16, 2005 - Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
    In ASP.NET 2.0 and Visual Studio 2005, you can quickly program custom authentication pages with the provided Membership Login controls. In this article, Dina Fleet Berry examines the steps involved in using the Login control with a custom SQL Server membership database.
    [Read This Article]  [Top]
    Dec 29, 2004 - ClickOnce Deployment in .NET Framework 2.0
    In this article, Thiru Thangarathinam examines .NET 2.0's new ClickOnce deployment technology that is designed to ease deployment of Windows forms applications. This new technology not only provides an easy application installation mechanism, it also eases deployment of upgrades to existing applications.
    [Read This Article]  [Top]
    Dec 15, 2004 - A Sneak Peek at ASP.NET 2.0's Administrative Tools
    With ASP.NET 2.0, Microsoft has made great strides in increasing developer productivity and has made implementing previously complex solutions relatively easy. Where this version of ASP.NET really shines, however, is in its new administrative tools that allow developers to spend less time managing the configuration of the servers and software and more time developing great code.
    [Read This Article]  [Top]
    Nov 17, 2004 - The ASP.NET 2.0 TreeView Control
    Thiru Thangarathinam introduces ASP.NET 2.0's new TreeView control which provides a seamless way to consume and display information from hierarchical data sources. The article discusses this new control in depth and explains how to use this feature rich control in your ASP.NET applications.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers