Backup Your Source Code Daily Using C# Programming

A daily backup without fail is a very hectic job. So I have created an exe file that takes your source code backup automatically.

Use the following procedure to take a daily backup.

To do this you can use one of the following two ways:

  1. Create a Zip file and add all the files into it to reduce disk space used.
  2. Directly copy the complete folder to your destination.

1. Source Code using Zip

Just don't forget to add a reference for Ionic.Zip.dll if you prefer the first way. We can do it without the DLL but for that we need the 4.5 framework. If your source code is more than 1-2gb then use the second method.

  1. string FolderName = ConfigurationManager.AppSettings["FolderName"];    
  2. string DestinationPath = ConfigurationManager.AppSettings["DestinationPath"];    
  3. string SourcePath = ConfigurationManager.AppSettings["SourcePath"];    
  4. string[] filenames = Directory.GetFiles(SourcePath);    
  5. using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())    
  6. {    
  7. string date = DateTime.Now.ToString("d");    
  8. date = date.Replace("/""");    
  9. zip.AddDirectory(SourcePath, date);    
  10. zip.Save(DestinationPath + FolderName + date + ".zip");    
  11. }    
  12. label1.Text = "Done";    
  13. Application.Exit(); 

1. Source Code using Normal copy/paste

  1. string FolderName = ConfigurationManager.AppSettings["FolderName"];    
  2. string DestinationPath = ConfigurationManager.AppSettings["DestinationPath"];    
  3. string SourcePath = ConfigurationManager.AppSettings["SourcePath"];    
  4. string date = DateTime.Now.ToString("d");    
  5. date = date.Replace("/""");    
  6. string NewDestinationPath = DestinationPath + "\\" + FolderName + date;    
  7. Directory.CreateDirectory(NewDestinationPath);    
  8. foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))    
  9. Directory.CreateDirectory(dirPath.Replace(SourcePath, NewDestinationPath));    
  10. foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories))    
  11. File.Copy(newPath, newPath.Replace(SourcePath, NewDestinationPath), true);    
  12. label1.Text = "Done";    
  13. Application.Exit();   

Now the question is, how to execute this exe daily?

Windows provides us the feature of the task scheduler to schedule your task. So use the task scheduler to create a task and add your exe to that.

How to schedule a task in the task scheduler?

Step 1

Create the Task as in the following:

Step 2: Type your task name



Step 2

Set your timings as in the following:



Step 3

Specify your exe path as in the following:



Step 4

Set the settings as per your requirements.



Now your machine will back up your source daily without fail. Only do not move the exe from that path.

And

The exe is taking the source path and Destination path from App.config, So you just use that config as per your requirements.

Thanks for reading, I have attached the source; please download and try it.

Up Next
    Ebook Download
    View all
    Learn
    View all