PowerShell script exchange
Post Reply
rcarazzone
Novice
Posts: 3
Liked: never
Joined: Nov 15, 2021 3:59 pm
Full Name: Reid Carazzone
Contact:

Automatically Run Active Full Backup Copy when Drive is Rotated

Post by rcarazzone »

I have a rotated drive repository that receives backup copies from a Windows Backup Copy job. The job copies workstation backups stored on a static repository. I have retention set fairly low for the backup copy job, to conserve space.

When I rotate the backup copy drive, and the backup copy job runs, the job processes retention and deletes the entire chain of backups. It then proceeds to run a regular full backup copy. The problem is that the regular full backup copy excludes workstation backups regarded as "out of date", so that I get a full backup file that does not contain all workstation backups available on the static repository. (Some workstations do not get backed up frequently, such as laptops that may stay disconnected from the network for a while.) If I right click the job and choose the option "Active Full", then backups for all available workstations are copied.

I've spoken with technical support, and they say that this behavior is expected. They suggested that I write a script to work around the problem. Therefore, my question is this: Can a script be written that could detect when the drive has been rotated, then prompt an active full backup copy, so that backups for all available workstations are copied?
Jesus said, "Come to Me, all who are weary and heavy-laden, and I will give you rest.” (Matthew 11:28)
soncscy
Veteran
Posts: 643
Liked: 312 times
Joined: Aug 04, 2019 2:57 pm
Full Name: Harvey
Contact:

Re: Automatically Run Active Full Backup Copy when Drive is Rotated

Post by soncscy » 1 person likes this post

Hey Reid,

https://helpcenter.veeam.com/docs/backu ... ml?ver=110

This with the -FullBackup parameter should do it for you.

As for detecting how to trigger it, you have a few ways:

1. If you can ensure that the drive will always be assigned the same letter somehow (there are utilities for this), you can simply set up a script that collects the current Disk UUID with Get-Disk, then enters a while loop like (this is very much so pseudo-code so it's something you'd want to explore a little):

Code: Select all

While($BreakScript -eq $false){
$CurrentDiskId = Get-Disk |Where-Object{$_.VolumeName}
If($LastUsedDiskId -ne $CurrentDiskId){
$LastUsedDiskId = $CurrentDiskId #Set up the new disk ID to compare against
Sync-VBRBackupCopyJob -name 'name of your backup copy' -FullBackup 
}
Start-Sleep N #Set N to some reasonable check time you'd expect from rotating the drive
}
There are some drawbacks here of course if the drive letter name changes or is set unexpectedly, so I'm not a fan of this personally.

2. On your different drives, set some "dummy" file that the script watches for to know which disk you have. I like this a lot better than worrying about Windows' disk management and letter assignment to be honest. So you'd just make a list of the filenames somewhere (maybe just make some uuids with New-GUID) and set these on the root of your drives. This simplifies it a lot as your script as you'll just write a logic to check all attached volumes and look to see if the UUID file exists at the root level. I have some code I can share later that you can copy for this, but maybe you'll figure it out yourself :)

Of course the drawback here is if someone removes your file, your detection breaks, but well, I guess make sure no one does that.

I'll try to clean up the above a little later when I'm on a Windows machine to play with Get-Disk a bit; I know it has some nuances that I just don't remember well.
rcarazzone
Novice
Posts: 3
Liked: never
Joined: Nov 15, 2021 3:59 pm
Full Name: Reid Carazzone
Contact:

Re: Automatically Run Active Full Backup Copy when Drive is Rotated

Post by rcarazzone »

Hey Harvey,

Thank you very much for this helpful information. I will start working on it as soon as I can.

One thing I am wondering about: Should I make this a script that runs before the job? Would that prevent the need for the Start-Sleep function?

Your method 2 seems promising. When I posted, I had supposed that there was a native Veeam function that would return the result, "Drive has been rotated". I say this because Veeam reports this information in the output of the job activity whenever I do rotate the drive. Nevertheless, if this function is not available in the Veeam Powershell module, then I will be going with your method 2.
Jesus said, "Come to Me, all who are weary and heavy-laden, and I will give you rest.” (Matthew 11:28)
soncscy
Veteran
Posts: 643
Liked: 312 times
Joined: Aug 04, 2019 2:57 pm
Full Name: Harvey
Contact:

Re: Automatically Run Active Full Backup Copy when Drive is Rotated

Post by soncscy » 1 person likes this post

To be honest, I don't really know how Veeam does it; I tried to check logs, and if I get it right, it just brute forces all drives and looks for the VBM path, similar to what I suggested in my second suggestion; so I guess they don't try to be to clever :) I might be wrong here.

As for the code, here it is as promised:

Code: Select all

$Volumes = Get-Volume | ?{$_.DriveLetter -ne $null -and $_.DriveType -ne "CD-ROM"}
foreach ($volume in $volumes){
		$driveletter = $volume.DriveLetter
		$CheckIdentifier = Get-item -Path ($driveletter + "`:\<replace this with your identifier>")
		
From there, you'll run logic on $CheckIdentifier and make decisions. In my original code, I used more discrete identifiers like "drive_1, drive_2, etc", so the check `:\<replace this with your identifier> was more like "`:\drive_*" (Backtick escapes the : character and we use double-quotes so the shell interprets the wildcard). If you plan to use UUIDs, you can either capture a regex pattern for uuids (^\{?[A-F0-9a-f]{8}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{12}\}? in bash-isms) or you can create a custom extension and capture that, then do some text validation on the file name captured and compare it to your ID list.

You need to plan for:

1. If drive changed
2. If drive is the same
3. If no identifier is found (e.g., drive is disconnected or volume is offline)

I would set this as a general script you just "start" and it immediately enters the loop. Since you're forcing the sync anyways, you don't care about when the job runs, you care about when you rotate the drives, so attaching it to a job start/stop doesn't make a ton of sense to me.

However, if you want to just run it after your last source backup job, then you have two options:

1. Use it as a post job script on the last job to run
2. Add some check to the script with Get-VBRJob and have the script watch for if the job has run that day and completed.

Hope it helps you solve your issue!
rcarazzone
Novice
Posts: 3
Liked: never
Joined: Nov 15, 2021 3:59 pm
Full Name: Reid Carazzone
Contact:

Re: Automatically Run Active Full Backup Copy when Drive is Rotated

Post by rcarazzone »

It seems I have found a solution, with much thanks to your help.

I have set the following code as a pre-job script for the backup copy job in question. The job runs in periodic mode and restarts itself when it detects the drive has been rotated. Thus, the script will run at the appropriate times, prompting the Active Full backup.

Many thanks to you again for your help!

Code: Select all

Import-module -name "C:\Program Files\Veeam\Backup and Replication\Console\Veeam.Backup.PowerShell\Veeam.Backup.PowerShell.psd1"

$IDFile = "C:\Users\Administrator\Documents\Veeam\BACKUP_COPY_DRIVE_ID.txt"
$IDListFile = "C:\Users\Administrator\Documents\Veeam\BACKUP_COPY_DRIVE_ID_LIST.txt"

$Volumes = Get-Volume | ?{$_.DriveLetter -ne $null -and $_.DriveType -ne "CD-ROM"}
$Identifier = get-content $IDFile
$IdentifierList = get-content $IDListFile
$FoundDrive = $false

foreach ($volume in $volumes)
{
    $CheckIdentifier = $volume.FileSystemLabel
    if ($CheckIdentifier -in $IdentifierList)
    {
        $FoundDrive = $true
        $DriveLetter = $volume.DriveLetter
        break
    }
}

if ($FoundDrive -eq $false)
{
    Exit
}

if ($CheckIdentifier -ne $Identifier)
{
    set-content -path $IDFile -value $CheckIdentifier
    $FolderPath = "$DriveLetter`:\Backup Copy\Workstations Copy"
    $Files = Get-ChildItem $FolderPath
    foreach ($File in $Files)
    {
        if ($File.Extension -eq ".vbk" -or $File.Extension -eq ".vib")
        {
            Remove-Item $File.FullName #this may not be necessary because of how the job processes retention
        }
    }
    Get-VBRJob -Name "Workstations Copy" | Sync-VBRBackupCopyJob -FullBackup
}
Jesus said, "Come to Me, all who are weary and heavy-laden, and I will give you rest.” (Matthew 11:28)
soncscy
Veteran
Posts: 643
Liked: 312 times
Joined: Aug 04, 2019 2:57 pm
Full Name: Harvey
Contact:

Re: Automatically Run Active Full Backup Copy when Drive is Rotated

Post by soncscy »

Fantastic! Glad you were able to get it going and make it workable, and definitely thanks for sharing the end result.
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests