PowerShell script exchange
Post Reply
wi_
Lurker
Posts: 1
Liked: never
Joined: Oct 23, 2023 12:18 pm
Full Name: wi_
Contact:

Automatic USB HDD plug/unplug

Post by wi_ »

Hi,
I am using Veeam Backup and Replication.
I have only one physical machine automatically fully backed up thanks to a Backup Job and Veeam Agent on the target machine.
I want to make copy of this backup to an external USB HDD.
To do so, I created a Backup Copy (repository marked as "backed with rotated drives").
The job workds fine, the backup is well copied to the HDD.

Now I want to initiate the Backup Copy to USB HDD when the USB HDD is plugged and mounted by Windows (on the Veeam backup server).
Launch the copy and automatically unmount/unplug the USB HDD when it's done.
I read on this forum that it's only doable by using PowerShell script.
Does any one have already done this? A script example?
Or maybe another way?
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Automatic USB HDD plug/unplug

Post by david.domask » 1 person likes this post

Hi @wi_,

The idea is fine, and likely you'll want to have a script that just runs in the background on your VBR server looking for when the USB device is mounted.

The cmdlet you want is Sync-VBRBackupCopyJob, and you'll just need to pass it either the name of the Backup Copy job on the -Job parameter, or a VBRBackupCopyJob object returned by Get-VBRBackupCopyJob.

For detecting the USB device, that you'll need to do outside of the Veeam PS cmdlets.

I'll share some sample-code I wrote a long time back for detecting removable drives on Windows and setting a dedicated drive letter based on a simple CSV-based "database"; this is not a script to solve what you need, but you can see how I find the drives with Veeam backups; it's lazy way, but very simple to implement without a lot of work, so use it as inspiration for your script.

Code: Select all

#Make a CSV Before hand and put it in a safe/static stop
#CSV should have three columns: DriveFName = Drive Friendly name here only for readability, RDLetter = Drive Letter to assign, Identifier = Reference File Name on the Drive. 
#Identifier should be formatted as "rotated_drive_01" and incrementing for each drive (e.g., first drive is rotated_drive_01, second is rotated_drive_02
#Identifier file must have no extension and sit at the root of the drive
#Currently the script loops continuously watching the drive letters. Tthere is a commented out sleep command which can be used, but you can also add a condition to the while-loop or remove the while-loop and create some triggered action in Task Scheduler. That will be beyond the scope of this script.

#Note to wi_ : probably you don't need the send-WarningEmail function, but maybe you do for another reason; main logic you want is in the while loop for finding the USB device on the VBR server.
#note to others who might want the script for its original purpose: I wrote this many many years ago when learning PS, it is not optimized at all :D but general idea should work

function Send-WarningEmail {
$smtp = "127.0.0.1"
$from = "test@test.com"
$to = "test@test.com"
[System.Net.ServicePointManager]::SecurityProtocol = 'TLS12' #Allowed values Ssl3, Tls, Tls11, Tls12
$body= "There is a drive letter conflict for the rotated drives which requires your attention. Drive $($WhichDrive.DriveFName) has been inserted, but the required Drive Letter $($WhichDrive.RDLetter) is occupied"
Send-MailMessage -SmtpServer $smtp  -To $to -From $from -Subject "Rotated Drive Intervention Needed" -Body $body
}

	while ($active -eq $null){
		$DriveList = Import-Csv -Path "C:\temp\drivelist.csv"
		$Volumes = Get-Volume | ?{$_.DriveLetter -ne $null -and $_.DriveType -ne "CD-ROM"}
		foreach ($volume in $volumes){
			$driveletter = $volume.DriveLetter
			$CheckIdentifier = Get-item -Path ($driveletter + "`:\rotated_drive_*")
			if ($CheckIdentifier){
				$IdentifierName  = $CheckIdentifier.Name
				$WhichDrive = $Drivelist | ? {$_.Identifier -eq $IdentifierName}
				If ($WhichDrive.RDLetter -ne $driveletter){
					if($Volumes.DriveLetter -notcontains $WhichDrive.RDLetter){
						Set-Partition -DriveLetter $driveletter -NewDriveLetter $WhichDrive.RDLetter
				} else {
					Send-WarningEmail
				}
			}	
		}
	#Start-Sleep 1
	}		
}
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests