This is a basic instructional script for backing up files with Robocopy onto my local NAS(Network Attached storage). Before proceeding, You should have enough general experience of folder navigation creation and Powershell enough to understand what this script does and doesn't do for you. For reasons being, especially when being security conscious, I tend to lean on not relying on apps and other toolsets to do the same things. A clean and focused environment means you can easily keep track of the scope of your work while still being able to have a good night's sleep knowing some app or other piece of software didn't leak your work or information without you aware of it. Backup tools are powerful, but only by your responsible use of it.
This scenario is for a local computer that I generally access all the time and very simply want to ensure my NAS has a second copy of files for when I humanly mess up. The NAS I have will take a snapshot of the share daily, an immutable snapshot, and then that snapshot is sent via rsync to an external backup in read-only and encrypted(That framework completes an entire triple redundancy for solo work whether it's programming, webdesign, artwork, CAD work and etc). I left the details out of this process intentionally and won't go over the nuances of Powershell Set-ExecutionPolicy (an important security Framework for Windows script safety) so we can focus on only the first universal stage of backing up files in Windows.
We are going to be running this script at startup/logon and executing it to backup only modified files to the destination we specify in the script. The first complete backup will take longer depending on the file sizes and amount.
IMPORTANT: Before running this script, we must change the execution policy in PowerShell from its default state. Run PowerShell as Administrator, then run the following entry:
Set-ExecutionPolicy RemoteSigned
You will be warned with "Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at https:/go.microsoft.com/fwlink/?LinkID=135170". Which in our use case is for one personal computer that is not an enterprise environment. We are setting this because A) We want to run local scripts and B) It Doesn't require digital signatures on scripts that are written on the local computer and not downloaded from the internet. So, select A as Yes to all and that's it.
Let's now get our good ole' Notepad open and copy and paste this script in. Replace "C:\FOLDER_WITH_FILES_TO_BACKUP\" with the folder you want to backup, and "B:\OUR_BACKUP\DESTINATION" with the folder you want to back up to. Save this file to a memorable location like a new folder named scripts in your documents folder or C:/ directory.
# Define the source and destination paths(remember to edit these for your directories)
$sourcePath = "C:\FOLDER_WITH_FILES_TO_BACKUP\"
$destinationPath = "B:\OUR_BACKUP\DESTINATION"
# Check if the destination directory exists; if not, create it
if (!(Test-Path -Path $destinationPath)) {
New-Item -ItemType Directory -Path $destinationPath -Force
}
# Perform backup, copying only new and modified files
Robocopy $sourcePath $destinationPath /MIR /Z /XA:H /XD "System Volume Information" /R:3 /W:5
# Log the backup completion time
$logPath = "$destinationPath\backup_log.txt"
$timeStamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path $logPath -Value "Backup completed on $timeStamp"
Backup Command Robocopy is used to copy files with the following flags:
Now to get that script running on startup. Let's open Task Scheduler and start by creating a basic task and ensure to add the script's directory as the argument as indicated in the last image.
And that's it! Now you have a templated script to back up additional directories later. I recommend naming the scripts as the directory/folder you want to backup as separate scripts so can refer back to them easily.