Page 1 of 1

How to Update targetdir with Powershell

Posted: Aug 18, 2011 11:56 am
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

Re: How to Update targetdir with Powershell

Posted: Aug 18, 2011 12:41 pm
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.

Re: How to Update targetdir with Powershell

Posted: Aug 18, 2011 3:12 pm
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"

Re: How to Update targetdir with Powershell

Posted: Oct 18, 2011 10:45 pm
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)

Re: How to Update targetdir with Powershell

Posted: Oct 19, 2011 1:52 pm
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.

Re: How to Update targetdir with Powershell

Posted: May 11, 2012 5:38 pm
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 :)

Re: How to Update targetdir with Powershell

Posted: May 14, 2012 6:46 pm
by Sethbartlett
Yeah I noticed that after I wrote it and forgot to edit it:P

Re: How to Update targetdir with Powershell

Posted: Mar 18, 2015 1:22 am
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

Re: How to Update targetdir with Powershell

Posted: Mar 29, 2016 7:10 pm
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

Re: How to Update targetdir with Powershell

Posted: Mar 31, 2016 1:58 pm
by v.Eremin
Would it be easier just to clone the job via Copy-VBRJob and set a new repository with -Repository parameter? Thanks.

Re: How to Update targetdir with Powershell

Posted: Apr 12, 2016 3:44 pm
by pizzim13
Using Copy-VBRJob this way, does it cause the next job run to create a full or an incremental?

Re: How to Update targetdir with Powershell

Posted: Apr 13, 2016 1:27 pm
by v.Eremin
Full, unless you move the backups to a new repository and map a backup job. The very same behaviour exists in GUI. Thanks.

Re: How to Update targetdir with Powershell

Posted: May 17, 2018 1:15 am
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?

Re: How to Update targetdir with Powershell

Posted: May 24, 2018 9:49 pm
by v.Eremin
What exactly do you mean by saying "map imported backup to a repository"? Thanks.