Scripting and Automating Backups on Windows

03/31/2022

Part One: Hardware


Today is World Backup Day and I thought it would be a perfect opportunity to share an easy backup solution for Windows workstations. I discovered this trick years ago when I worked as a desktop technician for a school corporation. At the time a department director wanted an external hard drive plugged into their computer and back ups to be run daily so they would not have to worry about losing any files. I was assigned this task but was also asked if I can find a way to do this without purchasing any special software. I did some Googling and found a simple way to do this with a batch script and Windows task scheduler. If you don’t know anything about scripting that is okay. I did not know anything about it either when I first tried this.


I have attempted to make this tutorial as beginner friendly as possible but if something does not make sense please reach out to me. Not much technical experience is needed. If you have an understanding of Windows file system (specifically files, folders and file paths), know what the Start button is, and can plug in a USB cable then you are smart enough to do this.


First you will need somewhere to save your backups. In order for a backup to be effective it has to be on a separate device than your computer’s primary hard drive. Another copy of the files on the same storage device will not prevent loss of data if the device fails. The easiest option will be to buy an external hard drive. You can purchase one from your favorite tech retailer. If you are unsure of what kind to get I have found Western Digital and Seagate to be reputable brands. I would also recommend getting a desktop external hard drive and not a portable external hard drive. In my experience the portable ones fail way more frequently and are easier to misplace. How much storage space needed will vary depending on how many files and the size of the files on your computer. Some people might need less than a gigabyte while others might need multiple terabytes. I would recommend going as large as possible to reduce the need to buy another larger hard drive in the future because you have run out of space for your back ups.

Example of an external hard drive by Seagate

Examples of external hard drives.

Example of an external hard drive by Western Digital.

An external hard drive is not the only option for this. You could add another hard drive inside your computer if your computer has the space for it. If you have a server or NAS available you can set them up as a mapped drive to utilize the storage on those devices. You could also use a flash drive if you had to but I would not personally recommend that. For the purpose of keeping this as simple as possible I will assume you are doing this on an external hard drive but the steps won’t change much if you choose another method.


When you have your backup storage solution figured out you will want to get it set up and determine the drive letter it will be using. If your external drive is set up properly it should be displayed in “This PC”. You can navigate to “This PC” by clicking on the Start button and typing “this pc” and then clicking on that option in the Start menu. On my workstation it is the (D:) drive but it could be a different letter for you. Once you know the drive letter you can start creating the script.

Part Two: Scripting with Batch Files

You will want to open your notepad app to create a text file but we will be saving it as a batch file which uses a .bat extension. Simply click on the start menu and type “notepad” to search for the Notepad app. Click on Notepad to open it.

Once open, let's immediately save a file. Click “File” > “Save As…” to bring up the “Save As” window. For my example I am going to save this directly in my user folder “C:\Users\InfosecMiles” as “backup-demo.bat”. Name your batch file whatever you feel is appropriate. Make sure you change the “Save as type:” from “Text Documents (*.txt)” to “All Files (*.*)” because Notepad will add a .txt to the end of your file name and it will not work properly. Click the “Save” button to finish saving this file.

You now have a blank file saved under the name you gave it. Mine is “backup-demo.bat”. The first thing you will add is


@echo off


The “echo off” command technically is not needed for the script to work but it makes the command prompt window look nicer while running your back up. It hides the prompt and batch file contents from being displayed from any command that comes after it while showing only the output of those commands. The “@” is to let the script know to hide the “echo off” command as well. I have linked a Microsoft doc in the sources section below that gives more details on the echo command.


Every line after this will be using the robocopy command. First you will type robocopy followed by the file path to the folder you want to back up (source) in quotes and then followed by the file path of the folder you want to send the back up to (Destination) in quotes and then finally followed by the appropriate options. For my example I will use the Documents folder (located at “C:\Users\InfosecMiles”) as my source and I will be putting my backup in “D:\Backup-Demo” as my destination which is currently empty. My second line will look like this:


robocopy "C:\Users\InfosecMiles\Documents" "D:\Backup-Demo\Documents"


I will note that currently there is not a Documents folder in the Backup-Demo folder. This is ok. The script will create it when you test it later. Also you can use whatever you want as your source. It is not limited to just folders in your user profile. If you had files saved in a folder directly under C: such as "C:\RandomFolder" you could back this up as well. The source doesn't need to be from the C: drive either. If you wanted to back up the contents of a portable external hard drive labeled (G:) to a desktop external hard drive labeled (H:) you could just write this line like so:


robocopy "G:\files" "H:\backup"


The point is that you can back up any folder you like. For the purposes of this tutorial however I am just demonstrating using folders under the user profile: Documents, Desktop and Downloads.


Now we need to add the appropriate options. There are lot of options that allow you to modify your backup in dozens of ways. I will keep it simple for this tutorial. We want to copy all files in the directory and subdirectories from the source to the destination. We also want to delete files from the destination that no longer exist in the source. There is an option for both of these: /e and /purge. The /e option is allows us to copy everything including empty directories. The /purge option allows files to be deleted from the destination when they no longer exist at the source. We want to delete those files because if you didn't need it anymore in your files then you can save some space on your backup hard drive. Conveniently another option exists that combines both of these into a single option: /mir. all options can just be added to the end of the line. My two lines now looks like this:


@echo off

robocopy "C:\Users\InfosecMiles\Documents" "D:\Backup-Demo\Documents" /mir


Other options include copying file properties, unbuffered I/O for larger files, restartable mode and more. Check under the Sources section below for more documentation about the robocopy command and its' many options.


This will be a good point to test the script. Save the file in Notepad. Navigate to the folder where you have saved it and verify that the file ends in .bat and not .bat.txt or this will not work. You could try to double-click to run the script but there is a chance you will run into errors. Instead right-click the batch file and from the menu click "Run as administrator". This will allow it to run without running into a permissions issue. You may get a User Account Control prompt asking if you want to allow this. Go ahead and click "Yes". You will notice a command prompt appear on your screen that shows all your files being copied. This prompt will go away as soon as the script is done running.


If you do run into any errors or issues while running the script you can simply press "Ctrl + C" in the command prompt to get an option to end the script. It will ask "Terminate batch job (Y/N)?" and you just have to type "y" for yes or "n" for no.

Go ahead and check your destination folder and verify the files have successfully been copied. If everything is working so far then this is the perfect time to do another test for modified files and deleted files. While in your destination file add a random file. It does not matter what kind of file it is as long as it is not something in the source. Also go back to the source folder and make a change to an existing file. Doesn't have to be big change. Add an extra period to a word file or change the color of a pixel in an image. Run the batch file again (as administrator of course). It should copy the modified file into the destination, replacing the original. Also it should remove that random file you added in the destination. Now we know for sure it will completely mirror the contents of your source folder to the destination folder.


Now we can add in additional lines for the other folders you want to add to the back up. I will also backup Downloads and Desktop on this script. Make every robocopy line following the same instructions I already explained above. My batch script now looks like this:


@echo off

robocopy "C:\Users\InfosecMiles\Documents" "D:\Backup-Demo\Documents" /mir

robocopy "C:\Users\InfosecMiles\Desktop" "D:\Backup-Demo\Desktop" /mir

robocopy "C:\Users\InfosecMiles\Downloads" "D:\Backup-Demo\Downloads" /mir


When you have everything you need in your script make sure you save it and test it again. If you don't have time to do a full back up at the moment you can always terminate the script with "Ctrl + C" like I mentioned earlier. Congratulations! You have created the script you will use for backups and it works. Now we just need to automate it.


Optional: If you would like to review everything displayed in the command prompt without it going away you can add one final line with the word "pause". I don't normally do this but it could be educational to check out the command prompt. Check under the Sources section below for more documentation about the pause command.

Part Three: Automation with Task Scheduler

For this final part you will need to open the Task Scheduler. To get to this you will click on the Start button and type "Control Panel" and select the Control Panel from the menu. Inside the Control Panel look for the "View by:" option and change it from "Category" to "Small icons". Then click on "Administrative Tools" and then double-click on "Task Scheduler". The Task Scheduler window will be open now. On the right side click "Create Basic Task..." under Actions.

In the "Create Basic Task" window you will be asked for a Name and Description. You may put whatever you feel is appropriate here. I am just naming mine "backup-demo". Click "Next".

You will then be asked how frequently you want this script to be run. Make whatever decision is appropriate for you. I am leaving mine set to "Daily". Click "Next".

On this screen it wants to know when you want to start this task and how often it will recur. I'll set the date and time for 5 a.m. the next day since I'm typically not using my computer at this time. I am also leaving the Recur setting at "1". Click "Next".

The next screen is asking for the action this task will perform. Select "Start a program" and click "Next".

Now you will need to add the batch script. Select the Browse button and navigate to where you have saved the batch script. Click on the file and then click "Open". You can leave the rest of the options blank. Click "Next".

On the final screen, select the "Open the Properties dialog for this task when I click Finish" and click "Finish".

The Properties dialog will now be open. Select "Run with highest privileges". This will make sure the script is run as an administrator. There are a lot of other options in the Properties dialog window. Feel free to explore them, research them and make appropriate changes if you feel they make sense for you. I personally have not made any further changes than what I have already described. Click "OK" when you are done making changes.

All you can do now is wait for the next day to verify that it worked. If that worked then congrats on making it all the way through this.

Sources:

Microsoft Doc for the echo command
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/echo

Microsoft Doc for the robocopy command
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy

Microsoft Doc for the pause command
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/pause