Schedule Your Directory for Automatic Daily Backup

This Article for developers who want to take backup of working directory files on the daily basis and don’t want to do itself.
Purpose of This article: This article will show you how you can take daily backup of your working directory automatically. No need to worry about Backup of your files. System will do your work.
Main purpose of this article is if you forgot to take Backup of your files. System will do your task without losing any changes. But you need to setup once.

So let’s start.

Step 1: I have created a Console application and write code for copy all files and directory from Source Directory to Destination Directory also track your task Log either fail or Success in text file.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace TakeBackupDaily
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string Source = @"D:\Source";
                string Destination = @"D:\Destination";
                CopyAllFiles(Source, Destination);
            }
            catch (Exception)
            {
               throw;
            }
          
            CreateTextFileLog("Task Completed successfully.");
        }

        private static void CopyAllFiles(string Source, string Destination)
        {
            try
            {
                // Get the subdirectories for the specified directory.
                DirectoryInfo dir = new DirectoryInfo(Source);
                DirectoryInfo[] dirs = dir.GetDirectories();

                if (!dir.Exists)
                {
                    CreateTextFileLog("Source directory does not exist or could not be found: "
                        + Source);
                 }
                // If the destination directory doesn't exist, create it.
                if (!Directory.Exists(Destination))
                {
                    Directory.CreateDirectory(Destination);
                }
                 // Get the files in the directory and copy them to the new location.
                FileInfo[] files = dir.GetFiles();
                foreach (FileInfo file in files)
                {
                    string temppath = Path.Combine(Destination, file.Name);
                    file.CopyTo(temppath, true);
                }
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(Destination, subdir.Name);
                    CopyAllFiles(subdir.FullName, temppath);
                }
             }
            catch (Exception ex)
            {
                 CreateTextFileLog(ex.ToString());
            }
         }
        private static void CreateTextFileLog(string Message)
        {
            StreamWriter SW;
            if (!File.Exists(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txt_" + DateTime.Now.ToString("yyyyMMdd") + ".txt")))
            {
                SW = File.CreateText(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txt_" + DateTime.Now.ToString("yyyyMMdd") + ".txt"));
                SW.Close();
            }
            using (SW = File.AppendText(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txt_" + DateTime.Now.ToString("yyyyMMdd") + ".txt")))
            {
                SW.WriteLine(Message);
                SW.Close();
            }
        }
    }
}
Step 2: Now I am ready with my application. Now I will setup my application in “Window Scheduler”.
Points to remember- You needs to have Admin rights for this Task. If you have admin rights then you can
Do this.

1-How to open window scheduler?
Go to start menu and search scheduler.

 After clicking on Task Scheduler, a window will open where you can “Create task”.
 After clicking on Create Task, a window will open. Where you will see couple of tabs like General, Trigger,Action and so on.
In the” General Tab” we need to specify Scheduler name and description. Please see following Screenshot.

In the “Trigger Tab”  we need to set date and time for Scheduler execution.
Click on New button. After clicking on New Button, a window will open where we will set time for execution of task.

In below window you can see couple of options available but we can set options as per requirement. Here I have set my.exe file execute once a day at 8:00PM.
 In the “Action tab“we need to set what the scheduler will execute.
In Action tab click on new button.

After clicking on New Button, a window will open. In the following screenshot I have selected Action: “Start a Program” after that browse to where your exe file available on directory .You can find your exe file in application bin/debug folder.
Please follow following screenshot.

Once scheduler is created.it will run at specified time.
If you code run properly it will do my task-Copy File from one location to another location.
You can see my output is here:

 Also create log text file and maintain task execution detail for a day.
Thanks For Reading my Article.
Author

Priti Kumari

I am technical blogger.I blogs at www.c-Sharpcorner.com and https://aspdotnetmyblog.blogspot.in/.

No comments:

Post a Comment