How to Update targetdir with Powershell

PowerShell script exchange

How to Update targetdir with Powershell

Veeam Logoby mzigadlo » Thu Aug 18, 2011 11:56 am

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
mzigadlo
Lurker
 
Posts: 1
Liked: never
Joined: Thu Aug 18, 2011 11:53 am
Full Name: Mark Zigadlo

Re: How to Update targetdir with Powershell

Veeam Logoby Gostev » Thu Aug 18, 2011 12:41 pm

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.
Gostev
Veeam Software
 
Posts: 21390
Liked: 2349 times
Joined: Sun Jan 01, 2006 1:01 am
Location: Baar, Switzerland

Re: How to Update targetdir with Powershell

Veeam Logoby Sethbartlett » Thu Aug 18, 2011 3:12 pm

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
Expert
 
Posts: 282
Liked: 25 times
Joined: Wed Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett

Re: How to Update targetdir with Powershell

Veeam Logoby Sethbartlett » Tue Oct 18, 2011 10:45 pm

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.
Sethbartlett
Expert
 
Posts: 282
Liked: 25 times
Joined: Wed Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett

Re: How to Update targetdir with Powershell

Veeam Logoby pizzim13 » Wed Oct 19, 2011 1:52 pm

Thanks for this code Seth. A powerful command line interface is one of the main features that makes this product standout from the rest.
pizzim13
Enthusiast
 
Posts: 86
Liked: 6 times
Joined: Thu Apr 21, 2011 7:37 pm

Re: How to Update targetdir with Powershell

Veeam Logoby raphael@schitz.net » Fri May 11, 2012 5:38 pm

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 :)
raphael@schitz.net
Enthusiast
 
Posts: 66
Liked: 2 times
Joined: Sat Jul 25, 2009 12:14 am

Re: How to Update targetdir with Powershell

Veeam Logoby Sethbartlett » Mon May 14, 2012 6:46 pm

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.
Sethbartlett
Expert
 
Posts: 282
Liked: 25 times
Joined: Wed Nov 10, 2010 6:51 pm
Full Name: Seth Bartlett

Re: How to Update targetdir with Powershell

Veeam Logoby JST » Wed Mar 18, 2015 1:22 am

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
JST
Lurker
 
Posts: 1
Liked: never
Joined: Tue Mar 17, 2015 10:43 pm
Full Name: Joseph Anthony

Re: How to Update targetdir with Powershell

Veeam Logoby stephensmalls » Tue Mar 29, 2016 7:10 pm

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
stephensmalls
Lurker
 
Posts: 1
Liked: never
Joined: Tue Mar 29, 2016 7:08 pm
Full Name: Stephen Small

Re: How to Update targetdir with Powershell

Veeam Logoby v.Eremin » Thu Mar 31, 2016 1:58 pm

Would it be easier just to clone the job via Copy-VBRJob and set a new repository with -Repository parameter? Thanks.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 969 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: How to Update targetdir with Powershell

Veeam Logoby pizzim13 » Tue Apr 12, 2016 3:44 pm

Using Copy-VBRJob this way, does it cause the next job run to create a full or an incremental?
pizzim13
Enthusiast
 
Posts: 86
Liked: 6 times
Joined: Thu Apr 21, 2011 7:37 pm

Re: How to Update targetdir with Powershell

Veeam Logoby v.Eremin » Wed Apr 13, 2016 1:27 pm

Full, unless you move the backups to a new repository and map a backup job. The very same behaviour exists in GUI. Thanks.
v.Eremin
Veeam Software
 
Posts: 13266
Liked: 969 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 6 guests