In order to follow the 3-2-1 backup rule I recently developed a small PowerShell script to copy backup files (I used it for VEEAM but it works for any backup files) to a local USB drive. The goal is to make the copy accessible by any users.
The problem is as follows : copying backup files from a remote server to a USB drive connected to a local workstation that doesn't have pre-recorded credentials server.
Let's see, in details, how it works…
The goal here is to automatically get the drive letter of our USB device. To do so, we will define a specific label for this drive and our script will get the letter associated to this label.
PS C:\ > $(Get-WmiObject Win32_LogicalDisk | Where-Object { $_.VolumeName -match "USB_BACKUP" }).DeviceID.ToString()
Now we will generate a Window box thanks to Get-Credential command, to allow the user to enter the server credentials.
PS C:\ > $cred = Get-Credential
PS C:\ > $netcred = $cred.GetNetworkCredential()
PS C:\ > $pass = $netcred.Password
We will use the net use command with the previously retrieved credentials in order to connect to our backup server.
PS C:\ > net use \\BACKUP_SERVER_IP $netcred.Password /USER:$($cred.GetNetworkCredential().UserName)
The copy will be made by the robocopy command. Let's review the selected options.
PS C:\ > robocopy /MIR /R:0 /W:0 \\BACKUP_SERVER_IP\d$\Backup "$usb_drive\VEEAM"
Replace BACKUP_SERVER_IP with your current IP.
###########################
# author : shebangthedolphins.net
# version : 1.0
# date : 2021.03
# role : backup the backups to USB drive backup
# other : Tested on Windows 2019 Server
# updates :
# - 1.0 (2021/03) : First Version
#Get drive letter, quit if not found
$usb_drive = try { $(Get-WmiObject Win32_LogicalDisk | Where-Object { $_.VolumeName -match "USB_BACKUP" }).DeviceID.ToString() } catch { exit 1 }
#get credential window
$cred = Get-Credential
#get user and password credentials for net use command (see : https://stackoverflow.com/questions/612015/copy-item-with-alternate-credentials)
$netcred = $cred.GetNetworkCredential()
$pass = $netcred.Password
#connect to the BACKUP_SERVER_IP network resource
net use \\BACKUP_SERVER_IP $netcred.Password /USER:$($cred.GetNetworkCredential().UserName)
#mirror copy of files from "\\BACKUP_SERVER_IP\\d$\Backup" to "USB_DRIVE\VEEAM" folder
robocopy /MIR /R:0 /W:0 \\BACKUP_SERVER_IP\d$\Backup "$usb_drive\VEEAM"
#cancels the BACKUP_SERVER_IP network connection
net use /delete \\BACKUP_SERVER_IP
Contact :