Automatic daily Backup procedure
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
- Create a Zip file and add all the files into it to reduce disk space used.
- Directly copy the complete folder to your destination.
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-2 GB then use the second method.
string FolderName = ConfigurationManager.AppSettings["FolderName"];
string DestinationPath = ConfigurationManager.AppSettings["DestinationPath"];
string SourcePath = ConfigurationManager.AppSettings["SourcePath"];
string[] filenames = Directory.GetFiles(SourcePath);
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
{
string date = DateTime.Now.ToString("d");
date = date.Replace("/", "");
zip.AddDirectory(SourcePath, date);
zip.Save(DestinationPath + FolderName + date + ".zip");
}
label1.Text = "Done";
Application.Exit();
Source code using normal copy/paste
string FolderName = ConfigurationManager.AppSettings["FolderName"];
string DestinationPath = ConfigurationManager.AppSettings["DestinationPath"];
string SourcePath = ConfigurationManager.AppSettings["SourcePath"];
string date = DateTime.Now.ToString("d");
date = date.Replace("/", "");
string NewDestinationPath = DestinationPath + "\\" + FolderName + date;
Directory.CreateDirectory(NewDestinationPath);
foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(SourcePath, NewDestinationPath));
foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(SourcePath, NewDestinationPath), true);
label1.Text = "Done";
Application.Exit();
How to execute this Exe daily?
Now the question is, how to execute this exe daily?
Windows provides us with 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
Create the Task as in the following
Step 2. Type your task name
Step 3. Set timings
Set your timings as in the following
Step 4. Specify Exe path
Specify your exe path as in the following
Step 5. Adjust settings
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.