Click here to Skip to main content
Click here to Skip to main content
Go to top

A simple backup maker for your programs' folders

, 5 May 2007
Rate this:
Please Sign up or sign in to vote.
An application for backing up from your programs' folders.

Introduction

This article is for building a simple console application that makes backup of your application folder periodically (daily and/or monthly, for example) by using the Windows Scheduled Tasks. In this application, you can also filter some folders (Bin or images, for example) or some file extensions (.pdb or .user, for example), or yet avoid some files from being backed up.

Background

You can use the Windows Scheduled Tasks for running some tasks periodically or just once. It's available through the Accessories->System Tools menu entry. You can specify an executable file (js file in this project) to run in a periodical manner or just once.

I'll use the js file as an executable file because you can change it simply by using Notepad, this is the known extension for system administrators who make administration scripts with it. I also use the java.util.zip namespace in .NET2.0 for making zipping backups. This namespace is better than System.IO.Compression because of its ability to add new entries to a Zip file.

Using the code

It's just a simple application including two files; one is the ZipClass that wraps the java.util.zip classes and another is the main program class that has some static methods to make backups. The Main method is the starting point of the console application and its pick parameters from the command line (we can pass them through the cmd.exe utility or the way I do in js files with the Execute method of WScript.Shell).

static void Main(string[] args) { 
    string argFilePath; 
    if (args.Length > 0 && !string.IsNullOrEmpty(args[0]) 
                        && File.Exists(args[0]) ) 
    {
        .....
The Main method in this application accepts a parameter that specifies the file path of the running parameter (running configuration). I used the ".prm" for the extension of these files but it's completely optional. The parameter file is an XML file that looks like:
<Parameters>
    <Folders>
        <Source>M:\Mehran</Source>
        <Destination>M:\MonthlyMehranBackup</Destination>
    </Folders>
    <FilterExpersions>
        <Folder>Obj|Debug|Release|Prototype|support</Folder>
        <File></File>
        <Extention>.pdb</Extention>
    </FilterExpersions>
    <Type>RootFolder</Type>
</Parameters>

As you see, this file acts as a configuration file; you specify the source and destination path, and also the filters you want to apply. And finally, you should create a js file for each backup policy with content like this (this is if the previous parameter file is named "MonthlyBackupMaker.prm"):

var oShell = new ActiveXObject("WScript.Shell"); 
oShell.Exec("BackupMaker.exe MonthlyBackupMaker.prm");

Everything is done now; you should simply add a scheduled job to run this js file monthly, for example.

If you specify the type "RootFolder" as in the above example (versus "FileOnly"), then the application test source sub-folder (first level) LastWriteTime will be less than the last backup date.

static bool FolderIsExpired(DirectoryInfo DirInf)
{
    if (DirInf.LastWriteTime >= perviousBackUpDate)
        return true;
    return false;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mehran Farshadmehr
Team Leader
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Mehran Fasrhadmehr is software architecture & developer in Iran.
He is a bachelore of Computer Engineering from yazd university and have deep experiences in C#, T-SQL, ASP.NET, .NET2.0, HTML, AJAX, VC++6, SQLServer2005 and etc.
His favorites are sk8te, swim, Farsi poem, music, film and etc.
You can find more about him in his site (Farsi language) Farshadmehr's personal website.
You can contact to him by sending email to m.farshadmehr@sepanta.com.

Comments and Discussions

 
GeneralError On Page PinmemberMember 300257923-Jun-08 23:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 5 May 2007
Article Copyright 2007 by Mehran Farshadmehr
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid