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;
}