In Part 1 , I gave a brief overview of what solutions I have in place for backing up my home network / lab environment.  In this article, I will continue my discussions on backups.

I'll start start the discussion with, how you can back up your Windows servers using Robocopy scripts, and the evolution of the scripts that I use.

If you're not familiar with Robocopy (Robust File Copy), it's a command-line directory and/or file replication command for Microsoft Windows, that's been around for almost 30 years. 

To run robocopy, you'll have to do so from the command line or by creating & executing windows scripts, I'm still using batch scripts, in the future I will be migrating to powershell. To launch the command line press the windows key + R, then type 'cmd'

From within the command prompt, if you type 'robocopy /?' you can see the different options that can be executed. 

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy   

If you click on the link above, you can visit Microsoft's website to see the different options that can be executed within Robocopy. 

Within my windows environment, I use robocopy scripts to backup key windows folders, as of today, I have 3 scripts configured:

  • A daily full that copies all files & folders and any changes to a directory 'daily-full' on a remote NAS 
  • A daily inc that only copies files & folders that have been changed to a directory 'daily-inc' on a remote NAS
  • A monthly full that uses /MIR that copies all files & folders to a directory 'monthly-full' on a remote NAS

These scripts have evolved and been refined over the years, as well as the processes exporting my data. Over the next couple of paragraphs, I'll go over the evolution of my robocopy scripts and how I would export my data. In the early days of using robocopy, I would connect an external USB hard drives to my server, once the drive was mounted, I would run a simple script that would copy key files and directories to this external drive. The script would look similar to the following command

'robocopy c:\src-directory D:\backup-directory'

The advantage of this setup, is it's simplicity, connect the drive and double click the script and wait for it to finish.

The disadvantage of this setup, is there's no logging of success or failures, no revision history of files, no filters ignoring certain files or directories, no file list or reference of what files were backed up or when, etc. 

The next iteration of my scripts was to implement backups to an external server, the benefit of a NAS, was not keeping all my data on my server, not relying immediately and exclusively on external HDD's (which sometimes, I would forget to connect). Using the same command above within my script. 

'robocopy c:\src-directory \\nas\remote-backup-directory'

As time progressed, I moved from running my scripts manually to scheduling a task, within Windows task schedule, if you can automate a task, it's worth automating. However, task scheduler would only tell you, whether the script ran successfully or not, in order to know if the script successfully copied files from the source to destination, logging would be needed. To add logging to robocopy. 

Set yyyy=%DATE:~10,4%
Set mm=%Date:~4,2%
Set dd=%Date:~7,2%
Set DateStamp=%yyyy%%mm%%dd%

robocopy c:\src-dir \\remote-server\remote-share\remote-dir /LOG:\\remote-server\remote-share\log-dir\backuplog-%DateStamp%.log

Make sure the remote log directory exists, once you run the script, you'll have the ability to check the log file to ensure that it ran successfully as well as the file(s) that were copied over. 

As mentioned before, I run 3 different scripts, a daily full, a daily incremental and a monthly full backup. The daily full and monthly full scripts are very similar in how they're structured, the incremental differs the greatest. Let's go over the that script first, as well as put a bit of documentation into the script (always document your work, you'll never know when you'll have to go over what you did and why! )

Daily incremental

rem This takes a backup of several key folders and stores it in a folder titled with todays date/time
Echo on
Set yyyy=%DATE:~10,4%
Set mm=%Date:~4,2%
Set dd=%Date:~7,2%
Set DateStamp=%yyyy%%mm%%dd%

rem ###########################directory to be backed up##########################################
echo directory to be backed up

:: Create a new folder on remote NAS for for each daily backup - only new & modified files & folders to be copied
Set backupFolder=\\remote-server\remote-share\remote-dir\dir-to-be-backup\dir-to-be-backup_%DateStamp%
Set localFolder=nothing
Set localFile=nothing

:: Set robocopy options - See comments (below) for flags that have been set 
Set _what=/COPYALL /E /SEC /timfix /secfix /XO /xd "D:\temp" /xf thumbs.db
:: /COPYALL :: COPY ALL file info
:: /E :: Copy folders even empty ones
:: /SEC :: copy files with SECurity
:: /timfix :: Fixes file times on all files, even skipped ones.
:: /secfix :: Fixes file security on all files, even skipped ones.
:: /XO :: Copy newer files
:: /xd :: exclude directories
:: /xf :: exclude files

:: Set robocopy logging options - See comments (below) for flags that have been set
Set _options=/R:0 /W:0 /LOG:D:\log-folder\dir-to-be-backup-inc-%DateStamp%.log /NFL /NDL /maxage:1
:: /R:n :: number of Retries
:: /W:n :: Wait time between retries
:: /LOG :: Output log file
:: /NFL :: No file logging
:: /NDL :: No dir logging

rem Prep-work: Create the folder to store all backups in
Echo on

md %backupFolder%
If ErrorLevel 1 Goto ErrMsg

rem Backup Local Folder
echo on 

:: Set local folder to be backed up
Set localFolder=D:\local-folder

:: Run robocopy with localfolder to remote folder as well as flags and options set
robocopy %localFolder% %backupFolder% %_what% %_options%

rem ###################end of directories to be backed up############################

Goto Exit
:ErrMsg
  Echo ***SERIOUS ERROR DETECTED***
:Exit 

From the above script there's 4 main pieces to it, variables that have been created for when robocopy runs. The variables cleans things up a bit, instead of having a 1 extremely long command. 

%localFolder% - What folder I want to backup 
%backupFolder% - Where this folder is going to be stored 
%_what% - What robocopy flags I want to use
%_options% - What robocopy options that have been set. 

The incremental backups are only kept for 28 days, I have a separate script that runs on my NAS to find and delete any directory older than 28 days. The question might be why an incremental backup and why not just have a daily full backup? That's a good point and I my mind, I have two reasons to do both.  

  1. It makes keeping track of what files created / added / modified a bit easier, I can search different folders for each day to what was created / modified that day.
  2. It adds revision control to my backups, there are several files that I modify on a daily or at least weekly basis, being able to compare that file from a previous change is beneficial. 

For the incremental backups I use the date stamp variable to mark the individual folders that have been created for each day:
Set DateStamp=%yyyy%%mm%%dd%

Set backupFolder=\\remote-server\remote-share\remote-dir\dir-to-be-backup\dir-to-be-backup_%DateStamp%

For the daily full backups, I don't date stamp the folder, the script copies the local files to my NAS

Set backupFolder=\\remote-server\remote-share\remote-dir\dir-to-be-backup\dir-to-be-backup

With the scripts created, unless you log in daily to run them, they're no good to you. One of the things I've learned as an IT professional is, if you can automated a task, automated it! It'll give you more time to work on more important tasks and/or consume more coffee. 

From the task scheduler, I have configured multiple tasks for each of the scripts that have been configured. 

  • Daily Incremental that runs at 3:00 pm
  • Daily Full that runs at 3:00 am
  • Monthly Full that runs 12:00 pm on the last day of each month

In the next article I will discuss how I export my monthly backups. 

Pin It