PowerShell | Offline backup, script to turn your hard drive off and on to backup to stay safe
I was procastinating one day when I was backing up a hard drive and I really wanted the Hard drive to be offline after it had done its job as if you got some sort of ransomeware it has a very small window to get onto the backup hard drive. Lets fire up the sHeLl!
Below is the basic script if you want to copy it into a file and save it as say "main.ps1"
# Setup
# run as admin
# need to get $Disk_UniqueId
# Get-Disk
# Get-Disk -Number '' | Select-Object UniqueId
# disk id
$Disk_UniqueId = ""
# Test to make sure driver/folder is online
function DriveOnline {
param ()
$FolderTest = test-path "W:\"
if ($FolderTest -eq $True) {
Write-Host "Drive Online"
} elseif ($FolderTest -eq $False) {
Write-Host "Drive Offline"
Set-Disk -UniqueId $Disk_UniqueId -IsOffline $False
Start-Sleep -Seconds 5
DriveOnline
} else {
Write-Host "Something went wrong"
}
}
DriveOnline
# mirror contents
$source = 'F:\'
$destination = 'W:\vms & backup'
robocopy $source $destination /mir /MT:4 /A-:SH # Multi Thread, System and Hidden Files
# onedrive copy
$source = 'E:\My Documentss\OneDrive'
$destination = 'W:\OneDrive'
robocopy $source $destination /mir /MT:4 /A-:SH # Multi Thread, System and Hidden Files
# set disk to offline
Set-Disk -UniqueId $Disk_UniqueId -IsOffline $True
Write-Host "Completed"
And it’s that simple, super fun happy time
Link to robocopy switches
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy