Comprehensive data protection for all workloads
Post Reply
andreas
Influencer
Posts: 10
Liked: never
Joined: Jan 25, 2012 1:55 pm
Full Name: andreas
Contact:

Offsite backupscript

Post by andreas » Jan 25, 2012 2:44 pm

Hi,

I have used Veeam B&R since version 5.0 and i must first say that i'm very impressed with the product. We have experimented with different methods to get our local Veeam backups to an offsite target in a effective way. I have tested a lot of various solutions and programs such as xcopy, robocopy, ftp, rSync but none of these met our requirements or performed satisfying. I would like to recommend a smart and effeicient program called FastCopy http://ipmsg.org/tools/fastcopy.html.en. This handy litte program does a very god job in copying files over a WAN link to a CIFS/SMB target offsite. You can run FastCopy in commandline mode but to get the most out of this little beauty i wrote this vbscript that i now would like to share.

The task is to copy our local Veeam backups from disk to a remote site over the WAN (we have a site-to-site VPN to the remote site) using FastCopy.exe.

Here are som functionallity that the script has:
  • * Test the connectivity to the remote host by ping. If no answer it will report this by email.
    * Calculates the executiontime of the offsite backup job.
    * Email reporting including the logfile from FastCopy.exe
    * Ensures that no earlier FastCopy process is active until it starts.
How to use:
  • 1. Copy this script and save it as "scriptname.vbs".
    2. Install and add fastcopy.exe directory path to the "PATH" variable on your backupserver.
    3. Specify this script as an post process for your Veeam backupjob so it runs automaticly when Veeam is finished with the backup.
Feel free to use this script and don't forget to edit the settings so it fits your environment. Please give me feedback on how i can improve or make this more efficient.

Best Regards,
Andreas

Code: Select all

'*******************************************************************************
'# Name:          start-offsite-backup.vbs
'# Author:        Andreas
'# E-mail:        andreas@resolutor.se
'# Date:          2012-01-24
'# Version:       1.0
'# Description: Script that utilizes FastCopy.exe to sync files from a local source to remote site using SMB/CIFS. Report is sent by email.
'*******************************************************************************

'# Resume on any error
'*******************************************************************************
On Error Resume Next
'*******************************************************************************

'# Declare variables
'*******************************************************************************
Dim sourceDir, targetDir, targetHost, logFileDir, logFileName, exeProcess  

Dim SMTPServer, Recipient, From, Subject, BackupReport

Dim WshShell, objFSO, objNetwork, startTime, stopTime, totalTime

Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("Wscript.Network")
'*******************************************************************************

'# Main - Config
'*******************************************************************************
sourceDir = "D:\veeambackupsource\"

targetHost = "192.168.xxx.xxx"
targetDir = "\\192.168.xxx.xxx\offsitebackuptarget\"

exeProcess = "fastcopy.exe"

logFileDir = "C:\FastCopy\Log\"
logFileName = "offsite-backup-_" & DATE & ".log"
'*******************************************************************************

'# Email - Config
'*******************************************************************************
SMTPServer = "YOUR_SMTP_SERVER"
Recipient = "YOUR_EMAIL_ADRESS"
From = "YOUR_SENDER_ADRESS"
Subject = "VeeamOffsiteBackup - Report"
'*******************************************************************************

'# Start the backup if no other instance of FastCopy.exe is already running
'*******************************************************************************
If processRunning(exeProcess) = False Then 

'# Run the backup
StartBackup

End If
'*******************************************************************************

'# Backup Script
'*******************************************************************************
Sub StartBackup

'# Ping the target host to validate connectivity
If Not PingHost(targetHost) Then

'# If no response from target host then Exit sub and email error report.
BackupReport = "OffsiteBackup - Report " & Date & "" &vbcrlf&vbcrlf& "!!! ERROR !!!" &vbcrlf&vbcrlf& "No response from offsite backup host." &vbcrlf& "Backup host: " & targetHost &vbcrlf&vbcrlf& "Verify backup host connectivity."

'MsgBox "No response from offsite target host!"

'# Send email reporting the host is unreachable
GenericSendmail SMTPserver, From, Recipient, Subject, BackupReport, False

Exit Sub

End If

'# Set start time
startTime = Now

'# Execute FastCopy.exe to sync Veeam Backups to Offsite SMB/CIFS target
WshShell.Run "fastcopy.exe /cmd=sync /speed=full /disk_mode=diff /auto_close /log /filelog=" & logFileName & " """ & sourceDir & """" & " /to=""" & targetDir & """",0,True

'# Set stop time
stopTime = Now

'# Set total time
totalTime = timeSpan(startTime, stopTime)

'# Format mailreport including the FastCopy logfile
BackupReport = "OffsiteBackup - Report " & Date & "" &vbcrlf&vbcrlf& "Start: " & startTime & "" &vbcrlf& "" & "Stop: " & stopTime & "" &vbcrlf& "" & "Duration: " & totalTime & "" &vbcrlf&vbcrlf& "" & "Logfile: " & logFileName & "" &vbcrlf&vbcrlf& "<----START-LOG---->" &vbcrlf&vbcrlf& "" & readLogFile(logFileDir & logFileName) & "<----STOP-LOG---->"

'# Send FastCopy report by mail
GenericSendmail SMTPserver, From, Recipient, Subject, BackupReport, False

End Sub
'*******************************************************************************

'# Function to check if a given process is running. Returns True or False
'*******************************************************************************
Function processRunning(ByVal aProcessName)

Dim objWMIService, colProcesses, strQuery, isRunning

isRunning = False

Set objWMIService = GetObject("winmgmts:root\cimv2")

strQuery = "Select * from win32_process where name='" & aProcessName & "'"

Set colProcesses = objWMIService.ExecQuery(strQuery)

If (colProcesses.Count) > 0 Then

isRunning = True

End If

processRunning = isRunning

End Function
'*******************************************************************************

'# Function to check if av given hostname/ip-adress answers to PING. Return True or False
'*******************************************************************************
Function PingHost(strComputer)
 On Error Resume Next
 
  Dim wmiQuery, objWMIService, objPing, objStatus
  
  wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"
  
  Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  Set objPing = objWMIService.ExecQuery(wmiQuery)
  
  For Each objStatus in objPing
      If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
          PingHost = False 'if computer is unreacable, return false
      Else
          PingHost = True 'if computer is reachable, return true
      End If
  Next
End Function
'*******************************************************************************

'# Generic Send Mail function
'*******************************************************************************
Sub GenericSendmail (SMTPserver, From, Recipient, Subject, Message, attLogFile)

Dim msg

set msg = WScript.CreateObject("CDO.Message")
msg.From = From
msg.To = Recipient
msg.Subject = Subject
msg.TextBody = Message

'#Attach the FastCopy logfile if arg 'attLogFile' is true
If attLogFile = True Then
msg.AddAttachment logFileDir & logFileName
End If

msg.Configuration.Fields ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
msg.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

msg.Configuration.Fields.Update

msg.Send

End Sub
'*******************************************************************************

'# Function thats reads the specified logfile and returns the content as text
'*******************************************************************************
Function readLogFile(logFile)

Const ForReading = 1

Set objTextFile = objFSO.OpenTextFile(logFile, ForReading)

strText = objTextFile.ReadAll

objTextFile.Close

readLogFile = strText

End Function
'*******************************************************************************

'# Function takes two dates as parameters and returns the time diff string containing hours:minutes:seconds.
'*******************************************************************************
Function TimeSpan(dt1, dt2) 
        If (isDate(dt1) And IsDate(dt2)) = false Then 
            TimeSpan = "00:00:00" 
            Exit Function 
        End If 
 
        seconds = Abs(DateDiff("S", dt1, dt2)) 
        minutes = seconds \ 60 
        hours = minutes \ 60 
        minutes = minutes mod 60 
        seconds = seconds mod 60 
 
        if len(hours) = 1 then hours = "0" & hours 
 
        TimeSpan = hours & ":" & _ 
            RIGHT("00" & minutes, 2) & ":" & _ 
            RIGHT("00" & seconds, 2) 
End Function
'*******************************************************************************

'# Clean up
'*******************************************************************************
Set WshShell = Nothing
Set objFSO = Nothing
Set objNetwork = Nothing 
'*******************************************************************************

Gostev
SVP, Product Management
Posts: 24810
Liked: 3571 times
Joined: Jan 01, 2006 1:01 am
Location: Baar, Switzerland
Contact:

Re: Offsite backupscript

Post by Gostev » Jan 25, 2012 10:23 pm

Hi Andreas, thanks for sharing this excellent script with the community. I can see how this can be easily repurposed to rsync for example - really useful script this is! Cheers.

Post Reply

Who is online

Users browsing this forum: Google [Bot], nathang_pid, nd39475, Roxana Dediu, smuzzu and 66 guests