PowerShell script exchange
Post Reply
bg.ranken
Expert
Posts: 121
Liked: 21 times
Joined: Feb 18, 2015 8:13 pm
Full Name: Randall Kender
Contact:

Date of restore point used by SureBackup Job

Post by bg.ranken »

Hi All,

I am trying to figure out how to get the date of the restore point that a SureBackup job is using into a script. I have a custom script I'm running in my SureBackup job that reads a txt file from the VM with the date in it. I want to verify that this date matches the date of the restore point being used by SureBackup.

Is there any way to get this using PowerShell? My script is currently VBS and I haven't found a way to get it easily with that so I'm guessing I'll need to have it launch a PowerShell and grab it like that...if that's even possible. I'm pretty new to scripting in general but I can't seem to find any PowerShell commands that would even give me what I'm looking for so I'm hoping someone might be able to give me some guidance. Not sure if it matters but I can post my VBS script if that might help.
PTide
Product Manager
Posts: 6408
Liked: 724 times
Joined: May 19, 2015 1:46 pm
Contact:

Re: Date of restore point used by SureBackup Job

Post by PTide »

Hi,

Here is another approach which I think should work. Backup files that are being used by SureBackup session are locked by Veeam.Agent. Verification scripts are executed at VBR so you can use simple one-liner that attempts to write $null into a specific backup file. If it returns error then the file is being used by SureBackup job.

Code: Select all

echo $null >> .\path\to\backup_file_to_be_checked\backup.vib
Thanks
bg.ranken
Expert
Posts: 121
Liked: 21 times
Joined: Feb 18, 2015 8:13 pm
Full Name: Randall Kender
Contact:

Re: Date of restore point used by SureBackup Job

Post by bg.ranken »

This might work, but it would be a lot of work and seemingly prone to errors. Best I can think of is feeding the folder of backup files to the script through arguments, then doing a search of all VIB files in that folder and seeing which ones are locked. The problem I see with that though is that, aren't all the VIB files locked? I guess I could pull the date from all locked VIBs and then sort and find the latest one and hope that it's really what Veeam is using.

Is there no powershell command in Veeam to see job details of a SureBackup job that's running that might have information on which files it's using or which restore point it's testing? Perhaps somewhere in the job log itself I could pull the information from?
PTide
Product Manager
Posts: 6408
Liked: 724 times
Joined: May 19, 2015 1:46 pm
Contact:

Re: Date of restore point used by SureBackup Job

Post by PTide »

The problem I see with that though is that, aren't all the VIB files locked?
By default Surebackup should use the most recent restore point. If for some reason SureBackup verifies some other backup than the latest then the latest vib should have no lock on it. So you can run the one-liner against the oldest vib.

Btw I just remembered you other thread. If you want to get the value from a file on a remote machine then you can use this cmdlet:

Code: Select all

Get-Content \\HOSTNAME\Path\date.txt
Example

Get date from date.txt located on surebackup VM:

Code: Select all

$inVM=Get-Content \\SureBackup_VM\date.txt
Compare the value with the date extracted from the backup's name

Code: Select all

$inVM -eq $backupDate
The last line should return either "true" or "false"

Hope that helps.

Thanks
bg.ranken
Expert
Posts: 121
Liked: 21 times
Joined: Feb 18, 2015 8:13 pm
Full Name: Randall Kender
Contact:

Re: Date of restore point used by SureBackup Job

Post by bg.ranken »

By default Surebackup should use the most recent restore point. If for some reason SureBackup verifies some other backup than the latest then the latest vib should have no lock on it. So you can run the one-liner against the oldest vib.
So I'm not really understanding this too much. Why would it be better to run the one-liner against the oldest vib? Wouldn't it be better to just find the latest restore point and make sure that it's locked, and then grab the date from that one?

Maybe it's just my limit in terms of programming (I've never done serious programming, just copy/paste) but this seems like it would be harder than just a few lines of code. Here's what I've been working on so far, I get the feeling I'm missing some much easier way of doing this. I haven't starting building any of the code for grabbing the backup folder yet from arguments and finding the date, but maybe you can tell me if this is even a good start before I spend too much more time on this. I know it's not powershell and that's where this post is, but I initially started building the scripts based off the ones Veeam included for testing SQL so I assumed VBS was better in some way.

SetDate.VBS

Code: Select all

'==========================================================================
'
' VBScript Source File -- SetDate.VBS
'
' NAME: Set Date for Veeam SureBackup Verification
'
'! @AUTHOR: Randall Kender
'! DATE  : 09/30/2016
'!
'! @details Overall purpose of the script is to create a file that has
'! the current date in it to be verified during the SureBackup job.
'!
'==========================================================================
'! @Version 1.0.1
'! @Date 09/30/2016
'!
'! Changed variable names.
'!
'==========================================================================
'! @Version 1.0.0
'! @Date 09/27/2016
'!
'! Initial inception of scrypt.
'!
'==========================================================================
' The Option Explicit declaration requires all variables used in the script are defined
' via a DIM statement prior to their being used within the script.  This helps to prevent
' undesirable or difficult-to-troubleshoot problems from arising through misspellings or
' other typographical errors introduced inadvertantly by the script writer.
'==========================================================================
Option Explicit

'==========================================================================
' Define global variables used in the main body of the script
'==========================================================================
Dim strDataFile '! String variable containing the data file's path and name.
Dim objDataFile '! File system object for interacting with data file.
Dim strDataWrite '! File handle for writing to the data file.

'==========================================================================
' Set the name of the data file for script execution
'==========================================================================
strDataFile = "C:\Windows\Temp\CurrentDate.txt"

'Create data File
Set objDataFile = CreateObject("Scripting.FileSystemObject")   
Set strDataWrite = objDataFile.OpenTextFile(strDataFile, 2, True)

'Write current date into data file.
strDataWrite.WriteLine Date
strDataWrite.Close
ReadDate.VBS

Code: Select all

'==========================================================================
'
' VBScript Source File -- ReadDate.VBS
'
' NAME: Reads Date for Veeam SureBackup Verification
'
'! @AUTHOR: Randall Kender
'! DATE  : 10/04/2016
'!
'! @details Overall purpose of the script is to read a file that has
'! the current date in it to be verified during the SureBackup job. This 
'! file is made by the SetDate.vbs script.
'!
'! Script usage:
'! Without Logging: cscript ReadDate.vbs [server_FQDN] [server_IP]
'! With Logging: cscript ReadDate.vbs [server_FQDN] [server_IP] [logs folder]
'! 
'! Examples:
'! ---------------
'! Inside Veeam
'! Name: ReadDate
'! Path: <Path to script on Veeam server.
'! Arguments: %vm_fqdn% %vm_ip% C:\SureBackup_Script_Logs
'! ---------------
'! Standalone
'! Without Logging: cscript ReadDate.vbs server01.contoso.local 10.1.3.248
'! With Logging: cscript ReadDate.vbs server01.contoso.local 10.1.3.248 C:\script_logs
'!
'! 'Return codes:
'! 0 - Success
'! 1 - Date in data file does not match with current date
'! 2 - CurrentDate.txt data file does not exist
'! 3 - Wrong command line sintax
'! 4 - Unknown error
'!
'==========================================================================
'! @Version 1.0.3
'! @Date 10/04/2016
'!
'! Created functions DataFileDate and MatchingDate and moved all relavent
'! commands into the functions.
'! 
'! Modified logging to reflect changes in script.
'! 
'! Added option to test script against local computer.
'!
'==========================================================================
'! @Version 1.0.2
'! @Date 10/03/2016
'!
'! Moved data file close to before any if statements.
'!
'! Changed strCurrentDate to get date using function instead of just date
'! command to fix issues with variable type being date instead of string
'! and not matching strDateFromDataFile.
'!
'! Added check for data file to prevent script from crashing unexpectedly
'! when no data file exists.
'!
'! Incorporated logging and command line parsing functionality from 
'! Veeam.Backup.SqlChecker.vbs used by SureBackup for SQL testing:
'! Created by: Vyacheslav Kuznetsov
'!
'==========================================================================
'! @Version 1.0.1
'! @Date 09/30/2016
'!
'! Added argument capture.
'!
'! Changed data file target to use UNC path instead of local system path.
'!
'==========================================================================
'! @Version 1.0.0
'! @Date 09/27/2016
'!
'! Initial inception of scrypt.
'!
'==========================================================================
' The Option Explicit declaration requires all variables used in the script are defined
' via a DIM statement prior to their being used within the script.  This helps to prevent
' undesirable or difficult-to-troubleshoot problems from arising through misspellings or
' other typographical errors introduced inadvertantly by the script writer.
'==========================================================================
Option Explicit

'==========================================================================
' Define global variables and constants used in the main body of the script.
'==========================================================================
Dim strServerIP '! Server IP address pushed by Veeam.
Dim strServerFQDN '! Server FQDN pushed by Veeam.
Dim gLog '! For logging current script execution.

'Set to 1 if testing script locally, otherwise leave at 0.
Const LOCAL_TESTING = 1

Const LOG_LEVEL_STANDARD = 1

Const EXIT_CODE_SUCCESS = 0
Const EXIT_CODE_MISMATCHED_DATES = 1
Const EXIT_CODE_MISSING_DATA_FILE = 2
Const EXIT_CODE_WRONG_SYNTAX = 3
Const EXIT_CODE_ERROR_UNKNOWN = 4

'==========================================================================
' Main Body.
'==========================================================================
'Starts logging.
Set gLog = New LogWriter

'Function to get arguments and enable logging if argument exists.
ParseCommandLineOptions
gLog "===================="
gLog "Capturing arguments..."
gLog "Server FQDN: " & strServerFQDN
gLog "Server IP: " & strServerIP

'Check if matching date matches date in data file.
If DataFileDate(LOCAL_TESTING) = MatchingDate("C:\test",1) Then
gLog "Date from data file is a match!"
Quit EXIT_CODE_SUCCESS
Else
Quit EXIT_CODE_MISMATCHED_DATES
End If

'==========================================================================
' Define global functions.
'==========================================================================
Sub ParseCommandLineOptions
Dim sLogsFolder
Select Case WScript.Arguments.Count
Case 1:
gLog.Error "Wrong number of arguments - " & WScript.Arguments.Count
PrintUsage
Quit EXIT_CODE_WRONG_SYNTAX
Case 2:
strServerFQDN=WScript.Arguments(0)
strServerIP = WScript.Arguments(1)
Case 3:
strServerFQDN=WScript.Arguments(0)
strServerIP = WScript.Arguments(1)
sLogsFolder = WScript.Arguments(2)
gLog.GenerateLog sLogsFolder,strServerFQDN
Case Else:
gLog.Error "Wrong number of arguments - " & WScript.Arguments.Count
PrintUsage
Quit EXIT_CODE_WRONG_SYNTAX
End Select
End Sub

'Outputs proper argument usage for script when it detects invalid arguments.
Sub PrintUsage
gLog.Write "Script usage:"
gLog.Write "Without Logging: cscript ReadDate.vbs [server_FQDN] [server_IP]"
gLog.Write "With Logging: cscript ReadDate.vbs [server_FQDN] [server_IP] [logs folder]"
End Sub

'Takes the location of the data file and outputs the first line which contains the date.
Function DataFileDate(LocalTesting)
'Declaring variables
Dim objDataFile '! File system object for interacting with data file.
Dim strDataRead '! File handle for reading the data file.
Dim strDataFileLocation '! String variable containing the data file's path and name.

If LocalTesting = 1 Then
'Set the location of the data file locally.
strDataFileLocation = "C:\Windows\Temp\currentdate.txt"
Else
'Set the location of the data file based on arguments.
strDataFileLocation = "\\" & strServerIP & "\" & "C$\Windows\Temp\CurrentDate.txt"
End If
gLog "Data path used: " & strDataFileLocation

'Creates system object for interacting with data file.
Set objDataFile = CreateObject("Scripting.FileSystemObject")

'Check to see if data file exists and if not, quit with error code.
If Not objDataFile.FileExists(strDataFileLocation) Then
Quit EXIT_CODE_MISSING_DATA_FILE
End If
gLog "Data file exists."

'Opens the data file.
Set strDataRead = objDataFile.OpenTextFile(strDataFileLocation, 1)

'Gets date from data file.
DataFileDate = strDataRead.ReadLine
gLog "Date from data file: " & DataFileDate

'Closes data file.
strDataRead.Close
Set strDataRead = Nothing
End Function


Function MatchingDate(iBackupFolderLocation,iUseSystemDate)
'If function is passed 1 then get current date from system.
If iUseSystemDate = 1 Then

'This function is necessary to ensure that MatchingDate's variable type is a string and not a date.
MatchingDate = month(date) & "/" & day(date) & "/" & year(date)
gLog "System date used."
gLog "Date from system: " & MatchingDate
Else
Quit EXIT_CODE_WRONG_SYNTAX
End If
End Function

'Descriptions of error codes used when outputing to a log file.
Function GetExitCodeDescription(iExitCode)
Select Case iExitCode
Case EXIT_CODE_SUCCESS:
GetExitCodeDescription = "Success"
Case EXIT_CODE_MISMATCHED_DATES:
GetExitCodeDescription = "Date in data file does not match with the date obtained from the system."
Case EXIT_CODE_MISSING_DATA_FILE:
GetExitCodeDescription = "CurrentDate.txt data file does not exist. Please make sure SetDate.vbs has ran correctly."
Case EXIT_CODE_WRONG_SYNTAX:
GetExitCodeDescription = "Wrong command line syntax."
Case EXIT_CODE_ERROR_UNKNOWN:
GetExitCodeDescription = "Unknown error."
End Select
End Function

'Exit function that provides error code, logging, and descriptions.
Sub Quit(iExitCode)
On Error Resume Next
If Not IsNumeric(iExitCode) Then iExitCode = EXIT_CODE_ERROR_UNKNOWN
gLog "Exit code: " & iExitCode & ". " & GetExitCodeDescription(iExitCode)
Set gLog = Nothing
WScript.Quit iExitCode
End Sub

'==========================================================================
' Define global Custom Types.
'==========================================================================
Class LogWriter
'Declaring variables
Private oFSO,oTextFile,sPath,sCurFolder,iLogLevel,bLoggingEnabled
Public Verbose,FileOpenMethod,FileEncoding,AddDate
'Methods 
Private Sub class_initialize
Const FILE_OPEN_WRITE=2
Const FILE_OPEN_APPEND=8
Const ENCODING_ASCII=0
Const ENCODING_UNICODE=-1
Const ENCODING_DEFAULT=-2
iLogLevel=LOG_LEVEL_STANDARD
Set oFSO=WScript.CreateObject("Scripting.FileSystemObject")
Verbose=False
FileOpenMethod=FILE_OPEN_APPEND
FileEncoding=ENCODING_DEFAULT
AddDate=True
sCurFolder=Left(WScript.ScriptFullName,Len(WScript.ScriptFullName)-Len(WScript.ScriptName))
If LCase(Right(WScript.FullName,Len(WScript.FullName)-Len(WScript.Path)-1))="cscript.exe" Then Verbose=True
bLoggingEnabled = False
End Sub
Private Sub class_terminate
If VarType(oTextFile)=9 Then oTextFile.Close
Set oFSO=Nothing
End Sub
Private Sub AssignFile
On Error Resume Next
Set oTextFile=oFSO.OpenTextFile(sPath,FileOpenMethod,True,FileEncoding)
If Err.Number=0 Then
bLoggingEnabled=True
End If
End Sub
Private Sub DoWrite(ByVal sText)
sText=Replace(sText,vbCrLf,"")
If Verbose And iLogLevel>0 Then WScript.Echo sText
If iLogLevel=0 Or Not bLoggingEnabled Then Exit Sub
If VarType(oTextFile)=0 Then AssignFile
If AddDate Then
sText="[" & Now & "] " & sText
End If
oTextFile.WriteLine sText
End Sub

Public Sub GenerateLog(sLogsFolder,ByVal sServer)
Dim iPos
If Not oFSO.FolderExists(sLogsFolder) Then
Exit Sub
End If
If InStrRev(sLogsFolder,"\")<Len(sLogsFolder) Then
sLogsFolder = sLogsFolder & "\"
End If
iPos = InStr(sServer, "\")
If iPos>0 Then
sServer = Left(sServer,iPos-1)
End If
sServer = Replace(sServer,".","_")
sServer = Replace(sServer,":","_")
sPath = sLogsFolder & "ReadDate_" & sServer & ".log"
AssignFile
End Sub
Public Sub Write(sText)
DoWrite sText
End Sub
Public Sub Error(sText)
DoWrite "Error " & sText
End Sub
Public Default Sub Info(sText)
DoWrite "Info " & sText
End Sub
'Properties
Public Property Get CurFolder
CurFolder=sCurFolder
End Property
Public Property Let LogLevel(iValue)
If IsNumeric(iValue) Then
If iValue>=0 And iValue<=2 And VarType(oTextFile)=0 Then
iLogLevel=iValue
End If
End If
End Property
Public Property Get LogLevel
LogLevel=iLogLevel
End Property
Public Property Get Path
Path = sPath
End Property
End Class
PTide
Product Manager
Posts: 6408
Liked: 724 times
Joined: May 19, 2015 1:46 pm
Contact:

Re: Date of restore point used by SureBackup Job

Post by PTide »

So I'm not really understanding this too much. Why would it be better to run the one-liner against the oldest vib? Wouldn't it be better to just find the latest restore point and make sure that it's locked, and then grab the date from that one?
Assuming that you're running backups in forever-forward incremental mode the latest .vib contains the latest restore point. So checking if the latest vib is locked and pulling the date from it should be enough.
Maybe it's just my limit in terms of programming (I've never done serious programming, just copy/paste) but this seems like it would be harder than just a few lines of code.
Maybe I'm misunderstanding something, but I really believe that it can be done with a few strings:

Code: Select all

echo "$(Get-Date)" > date.txt
Find the oldest file in a folder and get its creation time with:

Code: Select all

$Item = Get-ChildItem -Path $FolderName | Sort CreationTime | select -First 1
$time=$item.creationtime
Pull the content from a file

Code: Select all

$inVM=Get-Content \\SureBackup_VM\date.txt
bg.ranken
Expert
Posts: 121
Liked: 21 times
Joined: Feb 18, 2015 8:13 pm
Full Name: Randall Kender
Contact:

Re: Date of restore point used by SureBackup Job

Post by bg.ranken » 1 person likes this post

Maybe it's just my limit in terms of programming (I've never done serious programming, just copy/paste) but this seems like it would be harder than just a few lines of code.
Maybe I'm misunderstanding something, but I really believe that it can be done with a few strings:

Code: Select all

echo "$(Get-Date)" > date.txt
Find the oldest file in a folder and get its creation time with:

Code: Select all

$Item = Get-ChildItem -Path $FolderName | Sort CreationTime | select -First 1
$time=$item.creationtime
Pull the content from a file

Code: Select all

$inVM=Get-Content \\SureBackup_VM\date.txt
[/quote]

So the problem is that many of my jobs are per-vm jobs, which makes things a little more difficult. I also was hoping to get the timestamp on the actual backup file written by Veeam and not the modified date since that could be wrong for a number of reasons, mainly that the vib's modified date is not the same as the date of the snapshot if the job runs past midnight. Also, I'm still not understanding why I might want the oldest time, I would think the latest in the chain of VIB files since that's what's most likely to be in use as it's the latest restore point.

So at the moment, what I'm planning on doing is taking the FQDN that Veeam passes as an argument, cutting off the domain (so I have just the server name), then searching for all files in the folder that include that servername with an extension of .VIB. Then sorting by last modified and taking the latest and testing to see if it's locked like you showed prior. If it is ,then parsing out the date that's listed in the filename and matching that with the date I pulled from the file I had created with my other script. It's a bunch of work but at this point it seems like it would do the trick and be pretty solid.

So at least at this point I think I have an endpoint for where I want to be and it's just a matter of coding it, that's the hard part. Also maybe it would have been easier if I had done the whole thing in powershell, the commands you've presented seem to be a lot easier than what I'm going to have to come up with in VBS, but I already have most of the script working so I'll probably just keep going that route. I was moreso hoping that there would be just a simple command in Veeam like "get-surebackupjob details" that would have the restore point date listed somewhere, but I using the physical files will still work in the end.

Thanks for the help, I'll see how far I can get coding it. I'll probably drop the finalized code in here once I get things working in case anyone else wants to do something similar in the future.
Post Reply

Who is online

Users browsing this forum: oscarm and 17 guests