PowerShell script exchange
Post Reply
mzigadlo
Lurker
Posts: 1
Liked: never
Joined: Aug 18, 2011 11:53 am
Full Name: Mark Zigadlo

How to Update targetdir with Powershell

Post by mzigadlo »

I have been looking for a way to update targetdir in my backup jobs with powershell.

Does anyone know where this can be done?

-Mark
Gostev
Chief Product Officer
Posts: 31460
Liked: 6648 times
Joined: Jan 01, 2006 1:01 am
Location: Baar, Switzerland
Contact:

Re: How to Update targetdir with Powershell

Post by Gostev »

Hi Mark, I am pretty sure this is possible, however you would still have to move backup files manually to the new location, or the job will cease functioning.
Sethbartlett
Veteran
Posts: 282
Liked: 26 times
Joined: Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett
Contact:

Re: How to Update targetdir with Powershell

Post by Sethbartlett »

Here is the ridiculous code to do so, I have created a function called SetDir:

Code: Select all

Function SetDir
{
	param ([string]$JobName, [string]$targetDir)
	$job = Get-VBRJob | where-object {$_.name -eq $JobName}
	$info = [Veeam.Backup.Model.CDbBackupJobInfo]::CreateExisting($job.id, $job.name, $job.description, $job.jobtargettype, $job.gettargethost().id, $targetDir, $job.info.targetfile, $job.info.options, $job.info.scheduleoptions, $job.info.vssoptions, $job.info.postcommandruncount, $job.info.vcbhostid, $job.info.jobsourcetype, $job.info.targettype, $job.info.includedsize, $job.info.excludedsize, $job.info.isdeleted, $job.getlastresult(), $job.isscheduleenabled, $job.info.backupplatform)
	[Veeam.Backup.Core.CBackupJob]::Update($info)
}
To call it, you will want to do SetDir "JobName" "Directory"
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
Sethbartlett
Veteran
Posts: 282
Liked: 26 times
Joined: Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett
Contact:

Re: How to Update targetdir with Powershell

Post by Sethbartlett »

Here is the most up to date code, please do not use the other code:

Code: Select all

Function SetDir
{
   param ([string]$JobName, [string]$targetDir)
   $job = Get-VBRJob | where-object {$_.name -eq $JobName}
   $Manager = [Veeam.Backup.DBManager.CDBManager]::Instance
   $Backup = Get-VBRBackup | ?{$_.jobname -eq "JobName"}
   $Storage = $Manager.Storages.GetStoragesInBackup($Backup.Id)
   $info = [Veeam.Backup.Model.CDbBackupJobInfo]::CreateExisting($job.id, $job.name, $job.description, $job.jobtargettype, $job.gettargethost().id, $targetDir, $job.info.targetfile, $job.info.options, $job.info.scheduleoptions, $job.info.vssoptions, $job.info.postcommandruncount, $job.info.vcbhostid, $job.info.jobsourcetype, $job.info.targettype, $job.info.includedsize, $job.info.excludedsize, $job.info.isdeleted, $job.getlastresult(), $job.isscheduleenabled, $job.info.backupplatform)
   [Veeam.Backup.Core.CBackupJob]::Update($info)
   foreach($value in $Storage)
   {
	$value.FilePath = $targetDir + [regex]::Replace($value.filepath,"(.+\\)(.*)",'$2')
	$Manager.Storages.UpdateStorage($value)	
   }
}
This will update everything properly. As always, I am writing these scripts myself and they are not officially supported by Veeam. :D

To call it, you will want to do SetDir "JobName" "Directory". Example: SetDir "Myjob" "C:\test\" make sure you append the \ at the end(this is very important)
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
pizzim13
Enthusiast
Posts: 94
Liked: 6 times
Joined: Apr 21, 2011 7:37 pm
Contact:

Re: How to Update targetdir with Powershell

Post by pizzim13 »

Thanks for this code Seth. A powerful command line interface is one of the main features that makes this product standout from the rest.
raphael@schitz.net
Enthusiast
Posts: 71
Liked: 2 times
Joined: Jul 25, 2009 12:14 am
Contact:

Re: How to Update targetdir with Powershell

Post by raphael@schitz.net »

Sethbartlett wrote:Here is the most up to date code, please do not use the other code:

Code: Select all

Function SetDir
{
   param ([string]$JobName, [string]$targetDir)
   $job = Get-VBRJob | where-object {$_.name -eq $JobName}
   $Manager = [Veeam.Backup.DBManager.CDBManager]::Instance
   $Backup = Get-VBRBackup | ?{$_.jobname -eq "JobName"}
   $Storage = $Manager.Storages.GetStoragesInBackup($Backup.Id)
   $info = [Veeam.Backup.Model.CDbBackupJobInfo]::CreateExisting($job.id, $job.name, $job.description, $job.jobtargettype, $job.gettargethost().id, $targetDir, $job.info.targetfile, $job.info.options, $job.info.scheduleoptions, $job.info.vssoptions, $job.info.postcommandruncount, $job.info.vcbhostid, $job.info.jobsourcetype, $job.info.targettype, $job.info.includedsize, $job.info.excludedsize, $job.info.isdeleted, $job.getlastresult(), $job.isscheduleenabled, $job.info.backupplatform)
   [Veeam.Backup.Core.CBackupJob]::Update($info)
   foreach($value in $Storage)
   {
	$value.FilePath = $targetDir + [regex]::Replace($value.filepath,"(.+\\)(.*)",'$2')
	$Manager.Storages.UpdateStorage($value)	
   }
}
This will update everything properly. As always, I am writing these scripts myself and they are not officially supported by Veeam. :D

To call it, you will want to do SetDir "JobName" "Directory". Example: SetDir "Myjob" "C:\test\" make sure you append the \ at the end(this is very important)
Hi Seth,
This was exactly what i needed but i noticed a typo:
it should be

Code: Select all

$Backup = Get-VBRBackup | ?{$_.jobname -eq $JobName}
not

Code: Select all

$Backup = Get-VBRBackup | ?{$_.jobname -eq "JobName"}
Beside that, this function rocks :)
Sethbartlett
Veteran
Posts: 282
Liked: 26 times
Joined: Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett
Contact:

Re: How to Update targetdir with Powershell

Post by Sethbartlett »

Yeah I noticed that after I wrote it and forgot to edit it:P
Skype: Sethbartlett88 - Make sure to label who you are and why you want to add me ;)
Twitter: @sethbartlett
If my post was helpful, please like it. Sometimes twitter is quicker to hit me up if you need me.
JST
Lurker
Posts: 1
Liked: never
Joined: Mar 17, 2015 10:43 pm
Full Name: Joseph Anthony
Contact:

Re: How to Update targetdir with Powershell

Post by JST »

Any idea how to get this working for a File Copy Job? Those jobs don't seem to show up under Get-VBRBackup , only under Get-VBRJob. Tried to modify the script a bit, cannot get past the ($Backup.Id) portion.

Basically, I want my File Copy jobs to go into a new folder each time and not overwrite the last copy. My thought is to add the $Date variable when using this script to set the targetdir.

Thoughts? Thank you
stephensmalls
Lurker
Posts: 1
Liked: never
Joined: Mar 29, 2016 7:08 pm
Full Name: Stephen Small
Contact:

Re: How to Update targetdir with Powershell

Post by stephensmalls »

I ran the code above to alter my TragetDir:

Code: Select all

Function SetDir
{
   param ([string]$JobName, [string]$targetDir)
   $job = Get-VBRJob | where-object {$_.name -eq $JobName}
   $Manager = [Veeam.Backup.DBManager.CDBManager]::Instance
   $Backup = Get-VBRBackup | ?{$_.jobname -eq "JobName"}
   $Storage = $Manager.Storages.GetStoragesInBackup($Backup.Id)
   $info = [Veeam.Backup.Model.CDbBackupJobInfo]::CreateExisting($job.id, $job.name, $job.description, $job.jobtargettype, $job.gettargethost().id, $targetDir, $job.info.targetfile, $job.info.options, $job.info.scheduleoptions, $job.info.vssoptions, $job.info.postcommandruncount, $job.info.vcbhostid, $job.info.jobsourcetype, $job.info.targettype, $job.info.includedsize, $job.info.excludedsize, $job.info.isdeleted, $job.getlastresult(), $job.isscheduleenabled, $job.info.backupplatform)
   [Veeam.Backup.Core.CBackupJob]::Update($info)
   foreach($value in $Storage)
   {
   $value.FilePath = $targetDir + [regex]::Replace($value.filepath,"(.+\\)(.*)",'$2')
   $Manager.Storages.UpdateStorage($value)   
   }
}

setdir 'SCCMBackup' 'F:\Backups\'


but get this error:

Code: Select all

Method invocation failed because [Veeam.Backup.DBManager.CStoragesDbScope] does not contain a method named 'GetStoragesInBackup'.
At line:7 char:4
+    $Storage = $Manager.Storages.GetStoragesInBackup($Backup.Id)
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
 
Cannot find an overload for "CreateExisting" and the argument count: "20".
At line:8 char:4
+    $info = [Veeam.Backup.Model.CDbBackupJobInfo]::CreateExisting($job ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
 
Exception calling "Update" with "1" argument(s): "Object reference not set to an instance of an object."
At line:9 char:4
+    [Veeam.Backup.Core.CBackupJob]::Update($info)
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NullReferenceException
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to Update targetdir with Powershell

Post by veremin »

Would it be easier just to clone the job via Copy-VBRJob and set a new repository with -Repository parameter? Thanks.
pizzim13
Enthusiast
Posts: 94
Liked: 6 times
Joined: Apr 21, 2011 7:37 pm
Contact:

Re: How to Update targetdir with Powershell

Post by pizzim13 »

Using Copy-VBRJob this way, does it cause the next job run to create a full or an incremental?
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to Update targetdir with Powershell

Post by veremin »

Full, unless you move the backups to a new repository and map a backup job. The very same behaviour exists in GUI. Thanks.
nsimao
Veeam Software
Posts: 66
Liked: 3 times
Joined: Oct 18, 2011 3:47 am
Full Name: Nelson Simao
Contact:

Re: How to Update targetdir with Powershell

Post by nsimao »

Hi Vladmir,

I've been trying to find a cmdlet or script for mapping an imported backup to a repository, is it possible?
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to Update targetdir with Powershell

Post by veremin »

What exactly do you mean by saying "map imported backup to a repository"? Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests