Batch Script Backup to Network NAS Drive

You can never be too paranoid about backing up. Each night I run a simple batch script to transfer my key files onto an external drive, in addition to backing it up on a cloud sever (recommendation: CrashPlan for Small Business).

The batch script is this:

@ECHO OFF
 set dt=%DATE:~0,2%/%DATE:~3,2%/%DATE:~6,4%:%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2%
 set dt=%dt: =0%
 ECHO %dt%>>backuplog.txt
 robocopy <source> <destination> /R:0 /E /W:5 /FFT /TBD /COPY:DT /XO /XD $RECYCLE.BIN "#RECYCLE BIN" .dropbox.cache
echo backed up at %dt%

The first four/five lines append the date and time of the backup to a batchfile, so I know it has completed. The key line is this one:

 robocopy <source> <destination> /R:0 /E /W:5 /FFT /TBD /COPY:DT /XO /XD $RECYCLE.BIN "#RECYCLE BIN" .dropbox.cache 

Obviously, you change source and destination to your own setup. The last three words are files to exclude. As my backup includes my Dropbox folder, I don’t want to transfer the hidden cache files that Dropbox use, nor do I want to transfer the Windows Recycled files. Keep, discard or amend depending on your choice.

The rest of the robocopy spell only copies newer versions of the files already backed up, this way you aren’t transferring masses of data over your internal network each night.

I save this as a file called backup.bat in the User folder, ie C:\Users\<User>

I often make a shortcut and put this on the desktop so I can run the file at a whim, but you probably want to ruin it at the same time each night. As you often won’t be there, I want to introduce you to TASK SCHEDULER

How to create a basic task using Task Scheduler

To create a task using basic settings on Windows 10, use these steps:

  1. Open Start.
  2. Search for Task Scheduler, and click the top result to open the experience.
  3. Right-click the “Task Scheduler Library” branch, and select the New Folder option.
  4. Type a name for the folder. For example, MyTasks. (This step isn’t a requirement, but it’s a recommended step to keep your tasks separate from the system and apps tasks.)
  5. Click the OK button.
  6. Expand the “Task Scheduler Library” branch, and select the MyTasks folder.
  7. Click the Action menu.
  8. Select the Create Basic Task option.
  9. In the “Name” field, type a short descriptive name for the task. For example, “Backup to Drive”.
  10. (Optional) In the “Description” field, create a description for the task.
  11. Click the Next button.
  12. Select the Daily option. Task Scheduler allows you to select from a number of triggers, including on a specific date, during startup, or when you or a particular user signs in. Depending on your requirements, you’ll need to configure additional parameters. In this case, we’ll be selecting the option to run a task every evening
  13. Click the Next button.
  14. Using the “Start” settings, specify when the task should start running and the time (very important).
  15. Use the “Daily” drop-down menu to the time that you want to run the task.
  16. Click the Next button.
  17. Select the Start a program option to launch an app, run a command, or execute a script file. You can select the Send an e-mail or Display a message option, but these are deprecated features, which means that they may or may not work because Microsoft is no longer maintaining them.
    • Send an e-mail: Triggers an email notification with a custom message on schedule, but it requires to specify an email server to work.
    • Display a message: Allows to display a text message on the screen on schedule.
  18. In the “Program/script” field, specify the path for the application: In this case “C:\Users\User\backup.batQuick Tip: If you don’t know the path of the app, click the Browse button to find it.
  19. (Optional) In the “Add arguments” field, you can specify arguments to run the task with special instructions, but in our case, you don’t need that.
  20. (Optional) In the “Start in” field, specify the folder in which the program will start. (Usually, you can leave this setting empty.)
  21. Click the Finish button.

Once you’ve completed the steps, the task will be saved, and it’ll run automatically on the schedule you specified.