PowerShell script exchange
Post Reply
expert
Enthusiast
Posts: 30
Liked: 3 times
Joined: May 20, 2016 7:05 am
Contact:

"enable-vbrtapeprotection" after backup completes

Post by expert »

Hi,

we are using a HPE Tape Library with 8 slots. I would like to protect the tape (Monday until Saturday) after the backup job completes (run the following script after the job)

How can I manage this that the right/current tape will be protected? Then, on Sunday alle the tape protections (Mo - Sa) should be removed because the tape set will be replaced with a new set.

example:
$tape = Get-VBRTapeMedium | Where-Object {$_.Location.slotaddress -like "2"}
Enable-VBRTapeProtection -Medium $tape -PassThru

kind regards,
soncscy
Veteran
Posts: 643
Liked: 312 times
Joined: Aug 04, 2019 2:57 pm
Full Name: Harvey
Contact:

Re: "enable-vbrtapeprotection" after backup completes

Post by soncscy »

Hi expert,

Decided to hack at this over some morning coffee to wake up a bit :D I want to share some thoughts on logic/approaches. Excuse the huge amount of text :lol:

There's a way to do this with unsupported methods to guarantee that whatever tape(s) were used in the job get protected, but do I get it right you have just a handful of tapes, one tape for each day?

Are you certain your team is disciplined enough to put the correct tape in for each day?

Basically, I see three ways to handle the logic of this:
  • Pull the exact tape used from the Job Session data using unsupported .NET methods, act on the tape
  • Assume the tape name based on the current day and act on the tape
  • Parse on tape LastWriteTime property and act on any tape written same day as the Last Tape Job Session
The first is best to avoid unless you already know how to use Powershell .NET reflections, since each release has a good chance of breaking your code, but it will also be the most precise. I'm going to hold off on this for now but the code is fairly simple.

Second one is probably the least amount of code; if your tapes are named/labelled by Day (e.g., Monday's tape has a display name of "Monday", then you can just do:

Code: Select all

$CurrentDay = (Get-Date).DayofWeek
$Tape = Get-VBRTapeMedium -Name $CurrentDay
Enable-VBRTapeProtection -Medium $Tape
This is very simple, but I actually don't like it personally as it leaves a lot to chance and assumes that you never put the wrong tape in; the logic here is about matching the current day to a tape, but in fact your actual goal is to match the last written tape; there just happens to be overlap. If your team will be very careful about 1 tape for each day, this is all you need.

The third option is a bit more parsing, but it's closer to your real goal of protecting the used tape:

Get your tape job and find its lasts Job Session, then select the tape mediums that have a LastWriteTime the same as the LastEndTime:

Code: Select all

$tJob = Get-VBRTapeJob -Name "Name of Job"
$lastrun = Get-VBRSession -Job $tJob |sort CreationTime -Descending | select -First 1
$TapesUsed = Get-VBRTapeMedium | ?{$_.LastWriteTime.Date -eq $lastrun.endtime.Date}
You'd loop the array of $TapesUsed and just protect each one.

This has a logic downside though as I can see that Date-Time issues on the server might arise and if different job runs end on the same day, you might get unexpected tapes protected, but I think it's closer to what you want. The current form also hard-codes the Tape job which is fine if you just have one, but if you have multiple jobs that maybe use different tapes/media pools, you can fix this by just having the script check for the most recently completed job and it's a fairly safe assumption that any tapes that match on the LastWriteTime are what you want to protect.

So there are a few options and I think these are better than trying to parse on slot location -- most tape backup applications must have the library run in Random mode, so linear slot usage cannot be relied on, and again, you never know when someone puts a tape in the wrong slot on accident and the application needs to use an unexpected slot, so I wouldn't try to parse on library locations.

Hopefully this rambling is useful for you. :)
expert
Enthusiast
Posts: 30
Liked: 3 times
Joined: May 20, 2016 7:05 am
Contact:

Re: "enable-vbrtapeprotection" after backup completes

Post by expert »

Hello soncscy,

many thanks for your detailed description!

we are using 1 tape for each day (Mo - Fri), for Saturday/Sunday we are using another Pool with 2 Tapes.

Group "A" (first week)
- Monday -> "Monday A Tape 01"
- Tuesday -> "Tuesday A Tape 01"
- Wednesday -> "Wednesday A Tape 01"
- .......

Group "B" (second week)
- Monday -> "Monday B Tape 01"
- Tuesday -> "Tuesday B Tape 01"
- Wednesday -> "Wednesday B Tape 01"
- .......

Your suggestion to select tape mediums that have "LastWriteTime", "LastEndTime" sounds good. I'll make some test and let you know the results!

kind regards,
expert
Enthusiast
Posts: 30
Liked: 3 times
Joined: May 20, 2016 7:05 am
Contact:

Re: "enable-vbrtapeprotection" after backup completes

Post by expert »

Hi!

the script it excaclty what we need and works!

I had only do modify a bit because the backup job runs as "when new backup files appears"

Code: Select all

start-sleep -s 120
$tJob = Get-VBRTapeJob -Name "Backup_to_tape_daily"
$lastrun = Get-VBRSession -Job $tJob |sort CreationTime -Descending | select -First 1
$TapesUsed = Get-VBRTapeMedium | ?{$_.LastWriteTime.Date -eq $[b]lastrun.creationtime.Date[/b]}

foreach ($Tape in $TapesUsed){
   Enable-VBRTapeProtection -Medium $Tape
}
On Sunday evening the following script disables the tape protection and on Monday a new set will be placed in the library

Code: Select all

$TapesProtected = Get-VBRTapeMedium  | ?{$_.ProtectedBySoftware}

foreach ($Tape in $TapesProtected) {
   Disable-VBRTapeProtection -Medium $Tape
}
The only problem at the moment that I have is, that the script will not be executed after "job backup completes". I tried the PS-Script and also a batch-file:

Code: Select all

cmd.exe /C start
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -nologo -command "& {C:\_batch\Veeam_tape_protect_daily.ps1}"
Any tips how PS scripts should be executed after job backup completes?

Another possibility can be to execute the scripts daily with scheduled tasks ...

best regards,
soncscy
Veteran
Posts: 643
Liked: 312 times
Joined: Aug 04, 2019 2:57 pm
Full Name: Harvey
Contact:

Re: "enable-vbrtapeprotection" after backup completes

Post by soncscy »

Wonderful! I'm glad I was able to help, and thank you for sharing your results. :)

For the script I can't say what's happening; normally I just place such scripts as a post-job script and it works. If there's an error I guess it's a support case? What is happening even?
expert
Enthusiast
Posts: 30
Liked: 3 times
Joined: May 20, 2016 7:05 am
Contact:

Re: "enable-vbrtapeprotection" after backup completes

Post by expert »

Hi,

i placed the script also as post-job scipt, but the script does not start at all, tried as ps-script and batch-job (see above). Is there anything special to note here? With scheduled task the script runs without problems.

regards,
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: "enable-vbrtapeprotection" after backup completes

Post by oleg.feoktistov »

Had no issues whatsoever running the very same script as a post-job scenario. Is the same account used for a scheduled task and veeam service? Is there anything stated regarding the post-job activity in the job log section in the UI? Thanks!
expert
Enthusiast
Posts: 30
Liked: 3 times
Joined: May 20, 2016 7:05 am
Contact:

Re: "enable-vbrtapeprotection" after backup completes

Post by expert »

Hi,

for the scheduled task I'm using an account with local admin rights, the Veeam services run under local system account. In the job log section I noticed nothing about the post-job activity.

Can you tell me how do you run the post-job script in the job-configuration? Do I have to run it with any special arguments?

best regards,
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: "enable-vbrtapeprotection" after backup completes

Post by oleg.feoktistov »

By nothing you meant that nothing regarding post-job activity is logged at all, or it reads "success" as if everything was fine?
In my environment I use local admin account and configure post-job script as a .ps1 file. Nothing special, no arguments are set.
Since post-job scripts are executed on a backup server, and the cmdlets we are talking about are all server-side, there shouldn't be a problem with the configuration and execution. I would suggest to reach our support team and also share the case id here for further follow-up.
Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests