PowerShell script exchange
Post Reply
ccatlett1984
Enthusiast
Posts: 83
Liked: 9 times
Joined: Oct 31, 2013 5:11 pm
Full Name: Chris Catlett
Contact:

post-job script need to delete files that were written.

Post by ccatlett1984 »

I am looking for a way to run a post job script, that will let me delete files off a drive once they have been successfully written to tape.

Do any cmdlets exist that would let me list the files written to tape for the current run of the job?
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: post-job script need to delete files that were written.

Post by veremin »

Do you want to delete backup files (.vbk, .vrb, .vib) or just ordinary files that have been just archived to tapes with the use of files to tape job?
ccatlett1984
Enthusiast
Posts: 83
Liked: 9 times
Joined: Oct 31, 2013 5:11 pm
Full Name: Chris Catlett
Contact:

Re: post-job script need to delete files that were written.

Post by ccatlett1984 »

For some reason I never got the notification of your response.

The files to tape job in question, backs up trans log files from a network share, I need to remove them once they have been written to tape ( and are more than 3 days old, but I can script that check).
I need the file path that is backed up for each file eg: s:\folder\file.trans
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: post-job script need to delete files that were written.

Post by veremin »

From the top of my head the script being scheduled as a post-job activity would do something like the following:

- Get a Tape Job

Code: Select all

$TapeJob = Get-VBRTapeJob -name "Name of your tape job"
- Check whether the latest job result was successful

Code: Select all

$TapeJob.LastResult -eq "Success"
- In case of positive answer get the latest job session

Code: Select all

$Session = Get-VBRSession -Job $TapeJob -Last
- Get the session log

Code: Select all

$Log = $Session.log 
- Check among title entities whether all source files were backed up successfully, and none was skipped. (I don't have a files to tape job at hand, so, can't check the exact wording

- In case of positive answer delete the files in given directory (you've said that this part is not a problem for you)

Thanks.
ccatlett1984
Enthusiast
Posts: 83
Liked: 9 times
Joined: Oct 31, 2013 5:11 pm
Full Name: Chris Catlett
Contact:

Re: post-job script need to delete files that were written.

Post by ccatlett1984 »

$log gives useless data.

Code: Select all

11Job started at 6/10/2015 4:00:00 PM6/10/2015 4:00:04 PM6/10/2015 4:00:04 PMSucceeded
23Building file tree6/10/2015 4:00:09 PM6/10/2015 4:00:06 PMSucceeded
35Preparing next server for processing6/10/2015 4:00:09 PM6/10/2015 4:00:09 PMSucceeded
47Processing MILIVVBACKUP016/11/2015 8:55:05 AM6/10/2015 4:00:10 PMSucceeded
59All hosts have been queued for processing6/11/2015 8:55:07 AM6/11/2015 8:55:07 AMSucceeded
611Unloading tape OZL534L4 from Drive 1 (Drive ID: Tape0) to Slot 216/11/2015 8:58:21 AM6/11/2015 8:55:08 AMSucceeded
7122 directories and 57 files backed up successfully6/11/2015 8:58:21 AM6/11/2015 8:58:21 AMSucceeded
813Load: Source 4% > Proxy 3% > Network 5% > Target 99%6/11/2015 8:58:26 AM6/11/2015 8:58:26 AMSucceeded
914Primary bottleneck: Target6/11/2015 8:58:26 AM6/11/2015 8:58:26 AMSucceeded
1015Job finished at 6/11/2015 8:58:26 AM6/11/2015 8:58:27 AM6/11/2015 8:58:27 AMSucceeded

It doesn't give me the files that were written to tape.
Also it seems to transpose the number of files and directories that are backed up.
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: post-job script need to delete files that were written.

Post by veremin »

If the file is skipped by files to tape job (either if it doesn't already exist or if it is just locked by some process), there will be a warning in the said log, describing which file was skipped.

Having this information should be enough to understand whether the latest session was fully successful or not and which files might be deleted.

Thanks.
ccatlett1984
Enthusiast
Posts: 83
Liked: 9 times
Joined: Oct 31, 2013 5:11 pm
Full Name: Chris Catlett
Contact:

Re: post-job script need to delete files that were written.

Post by ccatlett1984 »

Not in my case, since i'm constantly replicating files to this target (nas to nas file replication)
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: post-job script need to delete files that were written.

Post by veremin »

Hmm, you want to archive files that will be placed to the source directory during the job's execution? If so, it's not possible, as tree of files to copy is built during job's start.
ccatlett1984
Enthusiast
Posts: 83
Liked: 9 times
Joined: Oct 31, 2013 5:11 pm
Full Name: Chris Catlett
Contact:

Re: post-job script need to delete files that were written.

Post by ccatlett1984 »

Let me explain in more detail.

1. SQL trans logs go to primary storage (NAS @ colo)
2. NAS replicates to secondary storage (NAS @ main office)
3. Veeam backs up files from secondary storage to tape. (mapped drive on tape server, using system account to map as local letter)

Since Veeam is not doing the replication, files are still being copied to the NAS while others are written to tape. I only care about what has actually been written to tape, since new files will be caught on the next run.
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: post-job script need to delete files that were written.

Post by veremin »

In this case, the first step would be indeed to create a list saying which files were copied and when. Something like the following:

Code: Select all

Asnp VeeamPSSNapin
foreach ($TapeCatalog in Find-VBRTapeCatalog)
{
$Info = ($TapeCatalog | select Name, Versions), ((Get-VBRTapeMedium | where {$_.mediafamilyitem.id -eq $TapeCatalog.Versions.Parts.backupset.MediaFamilyId}) | select barcode) 
$Info | Format-Table -Property Name, Barcode, {$_.Versions.creationtime}
}
Thanks.
ccatlett1984
Enthusiast
Posts: 83
Liked: 9 times
Joined: Oct 31, 2013 5:11 pm
Full Name: Chris Catlett
Contact:

Re: post-job script need to delete files that were written.

Post by ccatlett1984 »

Code: Select all

Find-VBRTapeCatalog : No files found in the backup set 9eb7e38c-92e2-4820-b38b-20000a013dc8
At line:2 char:26
+ foreach ($TapeCatalog in Find-VBRTapeCatalog)
+                          ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Find-VBRTapeCatalog], Exception
    + FullyQualifiedErrorId : System.Exception,Veeam.Backup.PowerShell.Command.FindVBRTapeCatalog
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: post-job script need to delete files that were written.

Post by veremin »

Does the same happen if you input the same command outside of the cycle:

Code: Select all

Find-VBRTapeCatalog
Or specify error action:

Code: Select all

Find-VBRTapeCatalog -ErrorAction SilentlyContinue
Thanks.
ccatlett1984
Enthusiast
Posts: 83
Liked: 9 times
Joined: Oct 31, 2013 5:11 pm
Full Name: Chris Catlett
Contact:

Re: post-job script need to delete files that were written.

Post by ccatlett1984 »

Tried that, didn't work.

Code: Select all

PS C:\Windows\system32> Find-VBRTapeCatalog -ErrorAction SilentlyContinue
WARNING: This cmdlet is obsolete and no longer supported
Find-VBRTapeCatalog : No files found in the backup set 9eb7e38c-92e2-4820-b38b-20000a013dc8
At line:1 char:1
+ Find-VBRTapeCatalog -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Find-VBRTapeCatalog], Exception
    + FullyQualifiedErrorId : System.Exception,Veeam.Backup.PowerShell.Command.FindVBRTapeCatalog
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: post-job script need to delete files that were written.

Post by veremin »

It seems that you happen to have backupset containing no files within it. Thus, the error.

Either try to check other error actions or query tape catalog so that the corresponding backup set is not returned.

Code: Select all

Find-VBRTapeCatalog | where {$_.Versions.backupsetId -ne "9eb7e38c-92e2-4820-b38b-20000a013dc8"}
Thanks.
ccatlett1984
Enthusiast
Posts: 83
Liked: 9 times
Joined: Oct 31, 2013 5:11 pm
Full Name: Chris Catlett
Contact:

Re: post-job script need to delete files that were written.

Post by ccatlett1984 »

Same error....

Using a where clause wont work, since that's filtering AFTER the find-vbrtapecatalog cmdlet returns its data.

Code: Select all

PS C:\Windows\system32> Find-VBRTapeCatalog | where {$_.Versions.backupsetId -ne "9eb7e38c-92e2-4820-b38b-20000a013dc8"}
WARNING: This cmdlet is obsolete and no longer supported
Find-VBRTapeCatalog : No files found in the backup set 9eb7e38c-92e2-4820-b38b-20000a013dc8
At line:1 char:1
+ Find-VBRTapeCatalog | where {$_.Versions.backupsetId -ne "9eb7e38c-92e2-4820-b38 ...
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Find-VBRTapeCatalog], Exception
    + FullyQualifiedErrorId : System.Exception,Veeam.Backup.PowerShell.Command.FindVBRTapeCatalog
ccatlett1984
Enthusiast
Posts: 83
Liked: 9 times
Joined: Oct 31, 2013 5:11 pm
Full Name: Chris Catlett
Contact:

Re: post-job script need to delete files that were written.

Post by ccatlett1984 »

Should I open a support case to remove the backup sets that contain no files?

I really need to get this working.
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: post-job script need to delete files that were written.

Post by veremin »

I think it's a way to go, as I'm not aware of any way to delete them via PowerShell. Moreover, such corrections should be better conducted under direct supervision of competent engineer. Thanks.
ccatlett1984
Enthusiast
Posts: 83
Liked: 9 times
Joined: Oct 31, 2013 5:11 pm
Full Name: Chris Catlett
Contact:

Re: post-job script need to delete files that were written.

Post by ccatlett1984 »

I ended up attacking this problem a whole different way.

I am directly querying SQL for files that were written to tape, and then looping through them and removing them off the storage.
(Not the most efficient, but it works well and isn't dependent on cmdlets.)

Code: Select all

#Connect to SQL
$dataSource = “BACKUP01\VEEAMSQL2012”
$database = “VeeamBackup”
$connectionString = “Server=$dataSource;Database=$database;Integrated Security=True;”
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

#Get files Veeam wrote to Tape, excluding "Backup" files (Since we only want the sql files we backed up.)
$connection.Open()
$query = “SELECT * FROM [VeeamBackup].[dbo].[Tape.report_file_changes] WHERE name NOT LIKE '%Backup%'”
$command = $connection.CreateCommand()
$command.CommandText = $query

#Pull the result of the query into a powershell object
$result = $command.ExecuteReader()
$table = new-object “System.Data.DataTable”
$table.Load($result)
$table = $table | sort name

#Select only the files that came from the FINSQL folder
$FINSQL_Files = $table | Where-Object {$_.directory_id -eq "20"}
#Select only the files that came from the NTSQL1 folder
$NTSQL1_Files = $table | Where-Object {$_.directory_id -eq "21"}

#Remove files from the Southfied NAS, That have successfully gone to tape.
Foreach ($file in $FINSQL_Files)
{
$file = $file.name
    IF(Test-Path \\nas01\sqlbackup\FINSQL\$file)
    {
    Remove-Item \\nas01\sqlbackup\FINSQL\$file -Force -verbose
    }
}

#Remove files from the Southfied NAS, That have successfully gone to tape.
Foreach ($file in $NTSQL1_Files)
{
$file = $file.name
    IF(Test-Path \\nas01\sqlbackup\NTSQL1\$file)
    {
    Remove-Item \\nas01\sqlbackup\NTSQL1\$file -Force -verbose
    }
}
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests