Saturday, December 17, 2011

How To Create A Data Backup Tool With SyncToy & VB Script

How To Create A Data Backup Tool With SyncToy & VB Script:

data backupLast month, I wrote an article about different tools you can use to create backups or images of all of your Windows 7 computer systems. While taking full backups of your entire system is important, you may want to more frequently back up really important directories or files. This is often the case in the field of IT, where you have clients collecting data to a specific directory, and they want to be sure that there are regular daily (or hourly) backups of the data there.

We’ve covered a lot of data backup solutions here at MUO, such as Tina’s article on cloning hard drives, Stefan’s article on file sync tools, or Shankar’s article on syncing files between a PC and your USB drive.  Justin even did one yesterday on Redo. All of these solutions are great, but if you’re working in an environment that is wary about free 3rd party software packages, or companies that want to stick with Microsoft products only, then you may find yourself without a good solution.

In this article, I’m going to show you how you can use a combination of Microsoft’s free SyncToy tool with a very simple scheduled VB Script that will automate the entire data backup process.

Setting Up SyncToy For Automated File Backups

Microsoft SyncToy is a free tool that lets you “pair” up folders for either an echo clone or full synchronization. I’ll explain the difference below. However, the point here is that before you can automate the directory and file backups, you need to set up all of the areas you want to copy and where you want the archived copy to go.

data backup

You do this when you first run the SyncToy by clicking on “Create New Folder Pair” and then defining the left (from) folder, and the right (to) folder. The second step of the sync setup process is to choose the type of synchronization you want.

Synchronize” is a two-way data backup. This means if any new file appears or is updated on the left or the right, those changes will be copied over to the other directory. On the other hand, Echo just mirrors all changes from the left directory onto the right. This is usually what people want to do when they’re backing up a particular directory – they want all changes mirrored on the backup.

data backup system

In the scheduled solution that I’m going to show you how to set up, I’m going to set up four folder pairs. Each pair is a backup that I want to handle during one particular time of the day. In the morning, I’m going to back up one folder. At noon, I will back up another, and so on.

data backup system

Once you have all of the folders set up that you want to perform automated backups for, it’s time to set up the script that will launch SyncToy using the command line feature that Microsoft offers with the tool.

Setting Up The SyncToy Automation Script

The VB Script that I’m going to show you will check the current time of day, and will run the appropriate command to launch the Microsoft SyncToy program and back up the right directory.

It does this by using the name of the paired directory that you set up in the tool above. Copy the script into notepad and save it as something like “databackup.wsf”.

<job>
<script language="VBScript">
Option Explicit
On Error Resume Next

Dim HourNow
Dim strHour
Dim WshShell
Dim strProgFiles

HourNow = Hour(Now())
set WshShell=CreateObject("WScript.Shell")
strProgFiles = WshShell.ExpandEnvironmentStrings("%PROGRAMFILES%")

Select Case HourNow
case HourNow >= 0 and HourNow < 7
WshShell.exec strProgFiles & "\SyncToy 2.1\SyncToyCmd.exe -R MorningFiles"
case HourNow >= 7 and HourNow < 13
WshShell.exec strProgFiles & "\SyncToy 2.1\SyncToyCmd.exe -R NoonFiles"
case HourNow >= 13 and HourNow < 19
WshShell.exec strProgFiles & "\SyncToy 2.1\SyncToyCmd.exe -R MailArchives"
case else
WshShell.exec strProgFiles & "\SyncToy 2.1\SyncToyCmd.exe -R EveningFiles"
End Select
WScript.Quit
</script>
</job>

The script above simply checks the hour right now (based on the PC clock where the script runs), and if it is between midnight and 6:59 a.m., it will sync the “MorningFiles” pair that you set up. Between 7 a.m. and 12:59, the “NoonFiles” pair, and so on.

All you have to do now is set up a Windows scheduled task that will launch the script above four times a day within the four time spans. This is also pretty easy, just go to the Control Panel, Administrative Tools, and open up the Task Scheduler. Click on “Create Task”.

data backup system

Name the task, and then click on the trigger tab. Make sure to select “On a schedule“, Daily, recur every day, start at 3 a.m., and then at the bottom click to repeat the task every 6 hours. This will trigger the task at 0300, 0900, 1500 and 2100 hours.

backup data

Those are all within one of the four time spans that you scheduled into your script. Now click on the Actions tab, and select “Start a program” from the dropdown list and browse to where you stored the script.

backup data

That’s all there is to it! Now, the task scheduler will launch your single script four times a day (no need to mess with multiple tasks). Your script will handle launching SyncToy in command mode by launching “SyncToyCmd.exe -R EveningFiles” – with whatever file pair you named after “-R”.

You can monitor whether your script is running by checking the SyncToy log file at “C:\Users\Owner\AppData\Local\Microsoft\SyncToy\2.0\SyncToyLog.log

data backup

The log updates every time SyncToy is run, and it’ll show you what directory was backed up, when it was done, the file count and the size of the backup.

Does this data backup solution work for you? Do you have any other ways you like to automatically back up important data files and folders? Share your thoughts in the comments section below.

Image Credit: Shutterstock

Similar Stuff



No comments:

Post a Comment

[Please do not advertise, or post irrelevant links. Thank you for your cooperation.]