Schedule Your Directory For Daily Backup Automatically

This Article is for those developers who want to take backup of their working directory files on a daily basis without doing it manually.

Purpose of this article:

The main purpose of this article is to show you how you can take daily backup of your working directory, automatically. You don't have to worry if you forgot to take backup of your files, the system will do it for you, and without losing any changes. All you need to do is, just setup the automatic backup once.

So, let’s start.

Step 1

I have created a Console application and written the code for copying all the files and directories from Source Directory to Destination Directory, along with the tracking of your task log, either failed or successful, in a text file.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Reflection;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8.   
  9. namespace TakeBackupDaily  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             try  
  16.             {  
  17.                 string Source = @"D:\Source";  
  18.                 string Destination = @"D:\Destination";  
  19.                 CopyAllFiles(Source, Destination);  
  20.             }  
  21.             catch (Exception)  
  22.             {  
  23.                   
  24.                 throw;  
  25.             }  
  26.              
  27.             CreateTextFileLog("Task Completed successfully.");  
  28.         }  
  29.         private static void CopyAllFiles(string Source, string Destination)  
  30.         {  
  31.             try  
  32.             {  
  33.                 // Get the subdirectories for the specified directory.  
  34.                 DirectoryInfo dir = new DirectoryInfo(Source);  
  35.                 DirectoryInfo[] dirs = dir.GetDirectories();  
  36.   
  37.                 if (!dir.Exists)  
  38.                 {  
  39.                     CreateTextFileLog("Source directory does not exist or could not be found: "  
  40.                         + Source);  
  41.   
  42.                 }  
  43.                 // If the destination directory doesn't exist, create it.   
  44.                 if (!Directory.Exists(Destination))  
  45.                 {  
  46.                     Directory.CreateDirectory(Destination);  
  47.                 }  
  48.   
  49.                 // Get the files in the directory and copy them to the new location.  
  50.                 FileInfo[] files = dir.GetFiles();  
  51.                 foreach (FileInfo file in files)  
  52.                 {  
  53.                     string temppath = Path.Combine(Destination, file.Name);  
  54.                     file.CopyTo(temppath, true);  
  55.                 }  
  56.                 foreach (DirectoryInfo subdir in dirs)  
  57.                 {  
  58.                     string temppath = Path.Combine(Destination, subdir.Name);  
  59.                     CopyAllFiles(subdir.FullName, temppath);  
  60.                 }  
  61.   
  62.             }  
  63.             catch (Exception ex)  
  64.             {  
  65.   
  66.                 CreateTextFileLog(ex.ToString());  
  67.             }  
  68.   
  69.   
  70.         }  
  71.         private static void CreateTextFileLog(string Message)  
  72.         {  
  73.             StreamWriter SW;  
  74.             if (!File.Exists(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txt_" + DateTime.Now.ToString("yyyyMMdd") + ".txt")))  
  75.             {  
  76.                 SW = File.CreateText(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txt_" + DateTime.Now.ToString("yyyyMMdd") + ".txt"));  
  77.                 SW.Close();  
  78.             }  
  79.             using (SW = File.AppendText(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txt_" + DateTime.Now.ToString("yyyyMMdd") + ".txt")))  
  80.             {  
  81.                 SW.WriteLine(Message);  
  82.                 SW.Close();  
  83.             }  
  84.         }  
  85.     }  
  86. }  
Step 2 

I am ready with my application. Now, I will set up my application in “Windows  Scheduler”.

Point to remember- You need to have admin rights for this task. Otherwise, you won't be able to do this.

How to open Windows Scheduler?

Go to the Start menu and search for scheduler.

scheduler

After clicking on Task Scheduler, a window will open where you can “Create task”.

Task Scheduler

After clicking on Create Task, a window will open. There, you will see a couple of tabs, like General, Trigger, Action and so on. In the "General Tab”, we need to specify scheduler's name and description. Please see the following screenshot.

General Tab

In the “Trigger Tab”, we need to set date and time for Scheduler execution. Click on New. A new window will open where we will set the time for execution of task.

Trigger Tab

In the below window, you can see a couple of options available, but we set the options as per the requirement. Here, I have set my .exe file to be executed once a day at 8:00PM.

new

In the “Action tab“, we need to set what the scheduler will execute.

In Action tab, click on the New button.

Action

A window will open. In the following screenshot, I have selected Action: “Start a Program”. After that, browse to where your exe file is stored in directory. You can find your exe file in the application bin/debug folder.

Start a Program

Please follow the following screenshot.

Once scheduler is created, it will run at the specified time.

If your code runs properly, it will do the task which is to copy files from one location to another location.

You can see my output here,

output

Also, create a log text file and maintain task execution details for a day.

output