Click here to Skip to main content
15,892,697 members
Articles / Programming Languages / C#
Article

A simple backup maker for your programs' folders

Rate me:
Please Sign up or sign in to vote.
4.50/5 (3 votes)
5 May 2007CPOL2 min read 20K   321   12   1
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).

C#
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:
XML
<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"):

C#
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.

C#
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)


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralError On Page Pin
Member 300257923-Jun-08 23:35
Member 300257923-Jun-08 23:35 

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

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