Click here to Skip to main content
Click here to Skip to main content

Backup Utility in C#

By , 4 Aug 2003
 

Sample Image - backup.gif

Introduction

During a development project, I realized I needed something to backup my solutions every night, and be tagged with a date stamp. I looked around the Net for some DOS utilities but none really fitted the bill as far as what I was looking for. Eventually, I decided to just code it. Turned out to be super simple in C#.

Goal

My goal was to backup my solution from a source folder (e.g. C:\Data) to a backup folder (e.g. C:\Backup\Data_080403). I also wanted to only keep N number of days of backups and not let my backup folder grow infinitely. So, for instance, I wanted to keep 30 days worth of backups at most. So, whenever, any files older than 30 days were discovered, I wanted the system to delete them automatically.

Solution

I used the System.IO's Directory and DirectoryInfo classes to create folders, delete folders, and wrote a method to recursively copy folders from one location to the next. I utilized the DirectoryInfo class to figure out how old a given folder was, in order to delete it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

rbala
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralNice. Yet it doesn't work twice on the same daymemberYaron Dan-Goor21 Mar '11 - 6:50 
It should use full date format which includes the hour and minute.
GeneralUse Path.DirectorySeparatorCharmembercodekamfung14 Apr '04 - 23:13 
I see there is "\\" inside the code.
I think it is better to use
Path.DirectorySeparatorChar
for portability (Where can .NET code be ported to without change anyway?)
GeneralRe: Use Path.DirectorySeparatorCharsussAnonymous8 Oct '04 - 9:02 
Well, it's always better to look ahead Smile | :)
 
Wired Solutions
GeneralRe: Use Path.DirectorySeparatorCharmemberjustsml12 Dec '04 - 10:34 
"(Where can .NET code be ported to without change anyway?)"
 
LINUX! Poke tongue | ;-P
- Platform.NET
- Mono.Net (www.Mono-Project.com)

GeneralPVCS systemsmemberHåkan Nilsson6 Aug '03 - 4:48 
For single home developer's not sharing code with other's this may be considered. Otherwise there is PVCS systems like MS SourceSafe keeping track of versions and releases.
 
How about creating an old fashion DOS command file instead? Smile | :)
 
Like:
 
mybackup.bat
-----------------------------------------
md c:\mybackup\%1
xcopy c:\mysourcecode\*.* c:\mybackup\%1\*.* /E
 
%1 parameter can be date or day number or whatever. There is also possible to get a date inside the command file and running it without parameter, but this is more flexible. Error checking can be added if needed, so the parameter is right.
 
No need for coding, xcopy does the recursive job for you.

 
/Håkan
 
------------------------------------------------
- Bachelor in systems analysis
- Systems developer, C#, Java, C, VB3-6
- DBA, database design, SQL
------------------------------------------------
GeneralRe: PVCS systemsmemberM C15 Jul '10 - 10:00 
Create a batch file with the following two lines...
 
md %2\%date:~0,3%
xcopy %1 %2\%date:~0,3%\*.* /H /Y /D /E
 
Can be run via...
my_backup.bat "c:\Documents and Settings\Admin\My Documents" T:\My_Backup
 
Will create a folder for the 7 days (text Mon..Sun) of the week and will copy the *changed* contents of the folder listed as the 1st param to the target folder of the 2nd param.
 
If you want to do day of the month (numeric)? change %date:~0,3% for %date:~7,2%
 
If you want to copy *everything* everytime, remove the /D parameter.
GeneralSuggestionsmemberGunmen5 Aug '03 - 23:13 
I understand the main problem, here are some suggestions:
- Microsft has for this problem already build a tool, take a look around for SourceSave.
- It's better to save the date format as YYYY-MM-DD, now you can sort on ASC en DESC.
 
Hope this helps,
Gunmen

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 5 Aug 2003
Article Copyright 2003 by rbala
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid