PowerShell script exchange
Post Reply
SuperYosh
Influencer
Posts: 12
Liked: never
Joined: Aug 10, 2020 1:55 pm
Full Name: Johan LE HELLOCO
Contact:

Powershell - Rotated Drives - Delete content

Post by SuperYosh »

Hi !
I'm new to this board, but I read a lot allready, thanks !

I have an issue with my VBR10, HyperV cluster, and backupcopy to external drives ...
I need to make a backup copy job every week to several USB HDD (4 to be precise) but these drives just have enough room for one backup copy ... I can't have Increments, and a synthetic full done BEFORE deleting older backups for retention. That's because of budget, I can't move this cursor ...

So, I am working on a script to automatically delete everything on these USB Hardrive as soon as the task begins, by using "script" option.

Here is my script :

Code: Select all

powershell -command "&{ Add-PSSnapin VeeamPSSnapin ; Get-VBRBackup -Name *EXT | Remove-VBRBackup -FromDisk -Confirm:$false }"
As you can see, this script SHOULD work fine, and it is if use it from a command line, BUT it didn't work when launch from backup tasks.
Can anyone help me please ?
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by oleg.feoktistov »

Hi Johan,

Are you using this command directly as a pre-job script or wrap it in a bat file and then select it from pre-job scripts browser?
Have you tried just saving the commands you need to invoke as a ps1 file and select it as pre-job script?

Thanks,
Oleg
SuperYosh
Influencer
Posts: 12
Liked: never
Joined: Aug 10, 2020 1:55 pm
Full Name: Johan LE HELLOCO
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by SuperYosh »

Hi Oleg,
The job is used in a .bat called from the pre-job script window.
As you asked me, did you mean that pre-job script can launch .ps1 files ?

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

Re: Powershell - Rotated Drives - Delete content

Post by oleg.feoktistov »

Yep, that's exactly what I meant. Could be easier to just invoke ps script directly. Thanks!
SuperYosh
Influencer
Posts: 12
Liked: never
Joined: Aug 10, 2020 1:55 pm
Full Name: Johan LE HELLOCO
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by SuperYosh »

Oleg
I tried with .PS1 but it changed nothing ... Old backup are still on the HDD ... :(
jhoughes
Veeam Vanguard
Posts: 279
Liked: 112 times
Joined: Apr 20, 2017 4:19 pm
Full Name: Joe Houghes
Location: Castle Rock, CO
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by jhoughes »

I would suggest setting up your script to run with a transcript to a local file, so you can see exactly what is happening/failing.
Husband, Father, Solutions Architect, Geek Extraordinaire | @DenverVMUG, @AustinVMUG & @ATXPowerShell leader | VMware vExpert | Cisco Champion
SuperYosh
Influencer
Posts: 12
Liked: never
Joined: Aug 10, 2020 1:55 pm
Full Name: Johan LE HELLOCO
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by SuperYosh »

Hi jhoughes !

That's a good idea ... how did you do it ? :)
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by oleg.feoktistov »

Hi,

First of all, make sure VBR finds powershell script and successful execution of pre-job script is logged to a backup session (no exit code 1).
If all is fine but the script itself doesn't seem to work, proceed with some simple logging to a file:

Code: Select all

asnp VeeamPSSnapin
Get-VBRBackup -Name *EXT | Remove-VBRBackup -FromDisk -Confirm:$false *> C:\job.log
Make sure to use precisely *> to redirect all streams, including errors, to one file.
Then, check the output in C:\job.log

Thanks
Oleg
SuperYosh
Influencer
Posts: 12
Liked: never
Joined: Aug 10, 2020 1:55 pm
Full Name: Johan LE HELLOCO
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by SuperYosh »

Hi,

That was that simple ... *facepalm*
Sorry, I begin with Powershell and VBR. I'm going to try that and come back to give you the result. Thanks !
SuperYosh
Influencer
Posts: 12
Liked: never
Joined: Aug 10, 2020 1:55 pm
Full Name: Johan LE HELLOCO
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by SuperYosh »

Got the result, and I dont' understand it :

Code: Select all

CreationTime : 29/04/2021 16:25:16
EndTime      : 29/04/2021 16:25:22
JobId        : 68541dd7-fa52-4a05-9a29-ff2f03d53682
Result       : Failed
State        : Stopped
Id           : 0c071acc-e5bd-4495-9ddb-9c8552d51f57
Can you help me ?

// edit :
When launch from PowerShell manually it works : "Removing backup / Please Wait ..." etc.
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by oleg.feoktistov »

If it was the last backup you attempted to delete, please post the output from the following script:

Code: Select all

$jobType = [Veeam.Backup.Model.EDbJobType]::DeleteBackup
$deletionSession = [Veeam.Backup.Core.CBaseSession]::FindLastByJobType($jobType)
$log = $deletionSession.Logger.GetLog()
$log.UpdatedRecords | select Title, Status 
This script dives into the session logs and outputs some relevant info about it.

Thanks,
Oleg
SuperYosh
Influencer
Posts: 12
Liked: never
Joined: Aug 10, 2020 1:55 pm
Full Name: Johan LE HELLOCO
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by SuperYosh »

Thanks Oleg,

Ok, it seems job runs WHILE it needs to wait after deletion of files :

Code: Select all

Title                                                                                                     St
                                                                                                          at
                                                                                                          us
-----                                                                                                     --
Job finished with error at 30/04/2021 10:18:57                                                            ed
Processed 1 of 1 backups (100% done)                                                                      ed
NAS-ISN-BK-01_USB: 0 deleted, 0 skipped, 0 warned, 1 failed                                               ed
[ISN_BCJPW_EXT] Failed to delete backup Error: Unable to perform this operation while the job is running. ed
Building backup deletion tasks                                                                            ed
Preparing backups for deletion                                                                            ed
Starting backup deletion job                                                                              ed
Sorry for bad formating, I don't know why.
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by oleg.feoktistov »

It actually makes sense. I think, instead you could create a task in Windows Task Scheduler, which would sequentially invoke backup deletion script and then start backup copy job. This task would subscribe to an event of usb drive discovery in event logger, so that task scheduler would trigger it on each event occurrence. Thanks!
SuperYosh
Influencer
Posts: 12
Liked: never
Joined: Aug 10, 2020 1:55 pm
Full Name: Johan LE HELLOCO
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by SuperYosh »

It makes sense ... but don't help me to duplicate this behavior on multiple sites :(

Is it possible to have a task/script that can start the task, BUT wait for the files to be deleted before beginning the BackupCopy ?

I allready have I Windows Task to make sure the External BCJ will run on saturday/sunday ONLY. And stops on monday early morning so I can have mail with 100% jobs done at the benning of a new week.
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by oleg.feoktistov »

By default, Remove-VBRBackup will wait for the backup deletion before returning anything unless you specify -RunAsync parameter.
So, just make sure to run the following sequence:

Code: Select all

asnp VeeamPSSnapin
Get-VBRBackup -Name *EXT | Remove-VBRBackup -FromDisk -Confirm:$false
$job = Get-VBRJob -Name 'Backup Copy Job'
Start-VBRJob -Job $job
Thanks,
Oleg
SuperYosh
Influencer
Posts: 12
Liked: never
Joined: Aug 10, 2020 1:55 pm
Full Name: Johan LE HELLOCO
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by SuperYosh »

Thank you oleg, I'll try this and let you know what happens.
ITP-Stan
Service Provider
Posts: 201
Liked: 55 times
Joined: Feb 18, 2013 10:45 am
Full Name: Stan (IF-IT4U)
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by ITP-Stan »

Another option is to just delete the files on the drive (BATCH or Powershell) and set the option on the repository "rotated drives" so it doesn't freak out if the files are deleted.
SuperYosh
Influencer
Posts: 12
Liked: never
Joined: Aug 10, 2020 1:55 pm
Full Name: Johan LE HELLOCO
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by SuperYosh »

Hi !
oleg.feoktistov wrote: Apr 30, 2021 2:39 pm

Code: Select all

asnp VeeamPSSnapin
Get-VBRBackup -Name *EXT | Remove-VBRBackup -FromDisk -Confirm:$false
$job = Get-VBRJob -Name 'Backup Copy Job'
Start-VBRJob -Job $job
Thanks,
Oleg
Oleg, your sequence works just fine !
But ... I lost visibility on the VBR GUI console for this job : I only have "Running pre-job script" in the VBR window if I want to monitor it.

If you have an alternative, or a way to "show" what's going on in real time ?
Thanks :)

@ITP-Stan that's what we are tyring to FROM the VBR Job. I need to automatize this, and deploy this on multiple site with equivalent configurations. So your BATCH/POWERSHELL only idea is sadly not enough for my needs. But thanks :)
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by oleg.feoktistov »

Since you invoke this script from Task Scheduler, you can just stream execution logs to a file as I showed above. Thanks!
SuperYosh
Influencer
Posts: 12
Liked: never
Joined: Aug 10, 2020 1:55 pm
Full Name: Johan LE HELLOCO
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by SuperYosh »

Hi !

Oleg, sorry I was on other subjects last week so I didn't have the time to do as you said.

Something I didn't tell is I went back to .PS1 script launched directly from BCJ. So no logs from Task Scheduler available.

Last log script tells this :
PS C:\ > $jobType = [Veeam.Backup.Model.EDbJobType]::DeleteBackup
>> $deletionSession = [Veeam.Backup.Core.CBaseSession]::FindLastByJobType($jobType)
>> $log = $deletionSession.Logger.GetLog()
>> $log.UpdatedRecords | select Title, Status

Title Status
----- ------
Job finished at 10/05/2021 10:07:53 ESucceeded
Processed 1 of 1 backups (100% done) ESucceeded
NAS-MES-BK-01_USB: 1 deleted, 0 skipped, 0 warned, 0 failed ESucceeded
[MES_BCJPW_EXT] Backup has been removed successfully ESucceeded
Building backup deletion tasks ESucceeded
Preparing backups for deletion ESucceeded
Starting backup deletion job ESucceeded
There is still something weird : if I launch the PS script from the Veeam Toolkit or the Powershell ISE everything works fine !
No error, HDD drive is deleted as wanted, etc.
But if the PS script is laucnh from the BCJ itself, I have a TimeOut ...
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by oleg.feoktistov »

HI,
But if the PS script is laucnh from the BCJ itself, I have a TimeOut ...
I believe, we discussed this case above and came to the conclusion that backups created by the same job cannot be deleted manually while the job is running.
While if you launch the script outside the job execution time, it is being invoked without issues.

Thanks,
Oleg
SuperYosh
Influencer
Posts: 12
Liked: never
Joined: Aug 10, 2020 1:55 pm
Full Name: Johan LE HELLOCO
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by SuperYosh »

Hi Oleg,
Sorry for the no answer last week, I was on other subjects :)
And yes, I agree with your analyze.
The main issue for me is the industrialisation of this job/script other multiple sites. For people who have no PowerShell competencies ... and the difficulty to maintain something that is not "package" with Veeam product.
That's why I am looking for a Full Veeam Process.

Last week, I had an case opened with one of your teamate about the Regkey : https://www.veeam.com/kb1154
I let you know if this corrected my issue. Here my fear was that this KB would delete ALL repositories, not only the Rotated Ones ...
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Powershell - Rotated Drives - Delete content

Post by oleg.feoktistov »

@SuperYosh, so did this reg key help you?
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests