PowerShell script exchange
Post Reply
tim5700
Enthusiast
Posts: 30
Liked: 1 time
Joined: Apr 17, 2009 11:31 am
Contact:

How to backup to removable storage: RoboCopy and PowerShell

Post by tim5700 » 1 person likes this post

I was asked to post on this after tweeting it last night.

A lot of my clients are used to rotating their backup media. Most of my clients prefer to keep copies of their backups off site for DR purposes. The problem I've into with rotating backup media is that Veeam may want to remove files etc.

To simplify the process, we are using Veeam to backup to a hard drive on the computer running Veeam all the time. Then have another set of disks that will rotated periodically.

Use a batch file with robocopy to mirror your permanent backup drive to the rotated drives for off site storage. As a result you always have a few backups ready and waiting on the Veeam server, but you also have off site backups in case of DR.

Something I quickly found out as I sometimes have non-technical folk trained to swap the removable drives is that they will do it regardless of whether the robocopy job is complete. Solution? Send them an email when it is safe to swap the drives. The result is a batch file and a powershell script. Install robocopy and powershell on your Veeam server. Run the batch file after your backup runs. Here's the script snippets.

Post_Backup_Copy.bat:

robocopy e:\ f:\ /MIR /r:3 /w:1

powershell.exe c:\scripts\RoboCopyEmailAlert.ps1

That's it for the batch file. It runs a robocopy and mirrors everything from drive E:\ (the permanent drive) to drive F:\ (the rotated drives)

It then runs the powershell script to send an alert when the process is complete.

RoboCopyEmailAlert.ps1:

## Define the variables for smtp server, from address, to address, subject and message body

$emailFrom = "administrator@domain.com"

$emailTo = "backupusers@domain.com"

$subject = "Backup Mirroring Operation Complete"

$body = "The robocopy hard drive mirroring operation is complete, it is now safe to change the backup hard drives."

$smtpServer = "x.x.x.x"


## Initiate sending a message to the IT group.


$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$smtp.Send($emailFrom, $emailTo, $subject, $body)


That's it, when the copy is complete an email will go out. I usually, send it to a distro group so that I get it and the responsible people get it.

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

Re: Veeam and removable storage - use RoboCopy and PowerShell.

Post by Gostev »

Thank you - stickied!

johnny@datafant.se
Enthusiast
Posts: 58
Liked: never
Joined: Jan 01, 2006 1:01 am

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by johnny@datafant.se »

Hello,

thanks for this valuable information.

Got one problem though, maybe it is in my environment only:

I have tried putting the email script created with Powershell both within the same and a separate BAT file as the RoboCopy command.
It only runs the Robocopy command but no email is received.

When I double click the BAT file, I receive the email.
Also when scheduling the BAT file with Windows Scheduler, I receive the Powershell created email.
But not when attaching this RoboCopy BAT file to the post command field in VB as described by you.

Any ideas ?

Regards,

Johnny Lundgren

johnny@datafant.se
Enthusiast
Posts: 58
Liked: never
Joined: Jan 01, 2006 1:01 am

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by johnny@datafant.se »

Anton,

concerning my Powershell email problem above; what credentials is used by VB with the post job command possibility ?
Since both the RoboCopy command and the Powershell script reside in the same BAT file, both should work but unfortunately only the RoboCopy command works.

And as I wrote, when scheduling the Powershell command with Windows Scheduler on the same machine(tested with both domain and local credentials) it works fine and I cannot understand why.


Regards,
Johnny

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

Re: How to backup to removeable storage: RoboCopy and PowerShell

Post by Gostev »

Johnny, actually it was team who posted solution, I hope he can comment on your issue. Post job command is started by Veeam Backup service under the same credentials that it runs.

Now, while I am definitely not a big PowerShell guy, I think I remember from early PS days that it has some security settings that define how it can be started (interactive only, or from other apps/services). May be this is what causing your issue?

tim5700
Enthusiast
Posts: 30
Liked: 1 time
Joined: Apr 17, 2009 11:31 am
Contact:

Re: How to backup to removeable storage: RoboCopy and PowerShell

Post by tim5700 »

Hi Johnny,

Sorry for the late response on this.

This could be an issue of the SMTP server not relaying the message. If you are using MS Exchange, there are settings for SMTP relay that can be configured to require authentication for the message to be sent through the Exchange server's SMTP service.

You can specify the service you are using to run Veeam, or you can configure Exchange to accept SMTP from the IP address of your Veeam server.

Thanks,

T

johnny@datafant.se
Enthusiast
Posts: 58
Liked: never
Joined: Jan 01, 2006 1:01 am

Re: How to backup to removeable storage: RoboCopy and PowerShell

Post by johnny@datafant.se »

Hello again,

I still have problems with the

Powershell Email Script

although using the Domain Admin account on the Veeam Service (VB Email always works).
When using the local Administrator account it works fine.
It is correct that I am using an Exchange 2003 Server as the SMTP server and this server relays email correctly when using the Windows Scheduler on the same Veeam Backup computer (both local Admin and domain Admin accounts).
In Windows Scheduled Task I can specifically set the account I want to use, I cannot do this in the Veeam Backup interface.
Powershell executionpolicy is set to unrestricted.

There should be a possiblity to run scheduled postjob scripts without having to backup a vm, just for quick testing.
Can I do this in VB in some way ?

Finally once again, what is the difference between running the VB postscipts with the Domain Admin account on the VB service, which does NOT work, compared to running the Windows Scheduled Task with differents Admin accounts (Local or domain) which DOES work ? On the same machine ?

Regards,

Johnny

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

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by Gostev »

Johnny, does your test work when you use VB service account to run Windows Scheduled Task?

johnny@datafant.se
Enthusiast
Posts: 58
Liked: never
Joined: Jan 01, 2006 1:01 am

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by johnny@datafant.se »

Yes, I am using the domain admin everywhere when testing.

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

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by Gostev »

Johnny, it looks like you misunderstood my question. Can you look up what service account is being used by Veeam Backup service, and perform Windows Scheduled Task test using that account, instead of another domain admin account?

We have seen it many times when the fact that account is domain admin did not really help, and there were still some "access denied" errors due to some local settings. What you can try to do is use services.msc applet to change the Veeam Backup service account to the one with which the script surely works when scheduled using Windows Scheduler.

johnny@datafant.se
Enthusiast
Posts: 58
Liked: never
Joined: Jan 01, 2006 1:01 am

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by johnny@datafant.se »

Anton,

Sorry for not being very clear.

When I use domain admin as VB service account - PS email script does not work.
When I use domain admin as Windows Scheduled Task acount - PS email script works.

When I use local admin as VB service account - PS email script works.
When I use local admin as Windows Scheduled Task account - PS email script works.

Note, the Veeam Backup job SMTP email via same Exchange server works all the time.

You may think this is a minor problem and not even a Veeam concern,
but I need this function and would like to be able to easlily test scheduled VB job and postscript email without having to backup a whole vm.


/Johnny

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

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by Gostev »

Johnny, in this way this issue may looks be related to Veeam Backup, but I wonder why it works fine for original poster. Anyway, it would be best to let our developers invetigate - can you please open support case and send us all logs as well and most importantly your script.

johnny@datafant.se
Enthusiast
Posts: 58
Liked: never
Joined: Jan 01, 2006 1:01 am

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by johnny@datafant.se »

Hello,

I just want to clear out my PowerShell email script problems once and for all:

This problem is NOT Veeam Backup related.

In the end, all I needed to do was to add the VB service account being used ( in my case, the Domain Admin account), to the local Administrators group.
So, even if the Domain Admin account reside in the Domain Admin group, which in turn reside in the local Administrators group, this is not enough to make it work.

I had other problems with my Robocopy/Powershell script which resulted in added confusion, but that is related to errorlevel codes not working correctly with Robocopy version XP026. And that is an issue I have to deal with Microsoft.

In any case, thanks for taking my problem seriously.

Regards,

Johnny Lundgren

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

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by Gostev »

Hello Johnny, I am glad that it works now - and thank you for updating the topic with resolution.

Rich
Novice
Posts: 6
Liked: never
Joined: Nov 27, 2009 9:37 am
Full Name: Richard Main
Contact:

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by Rich »

Thanks for the info - much appreciated. We now have a similar backup strategy in place whereby robocopy mirrors onsite Veeam backups to a pair of rotated 2Tb USB drives for offsite DR purposes. Works nicely.

I've just noticed the 'File Copy' function in Veeam. Just wondering - could this be used as an alternative to robocopy for mirroring of Veeam backups to USB drives?

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

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by Gostev »

Sure, you could absolutely do that.

I think I should also mention the following new feature here in this thread for future readers:
v4.1 Support for rotated media

Rich
Novice
Posts: 6
Liked: never
Joined: Nov 27, 2009 9:37 am
Full Name: Richard Main
Contact:

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by Rich »

Thanks for the prompt response, I'll read up on v4.1 features re rotated media.

I understand every environment is different, however, typically - how will Veeam file copy throughput compare vs standard Windows file copy utilities i.e. Robocopy?

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

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by Gostev »

Windows performance will be similar to other tools... but there are definitely no issues with performance, because this would affect the speed of backups to Windows/CIFS.

Linux file copy performance is where our file copy engine really shines comparing to other existing tools. :D

Rich
Novice
Posts: 6
Liked: never
Joined: Nov 27, 2009 9:37 am
Full Name: Richard Main
Contact:

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by Rich »

Hi Gostev - initial tests show a marked difference between veeam 'File Copy' vs Robocopy performance. Using Veeam to copy a 1Gb file from onboard Veeam server disks to USB hard drive took 3 minutes, vs 40 seconds for Robocopy. I realise this is a crude test, however, wasn't expecting this variation. We'll continue to use Robocopy to copy Veeam backups to cycled USB drives, your thoughts on Veeam 'File Copy' performance would be appreciated though. Thanks again.

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

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by Gostev »

Richard, have you done one test after another? If so, the second test would use Windows system cache, this would explain the difference. Anyhow, I am not saying File Copy for Windows is brilliant because we never ever done performance testing with it, so it may have issues. Our primary use case for file copies is between Windows and Linux/ESX.

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

Re: How to backup to removable storage: RoboCopy and PowerShell

Post by Gostev »

We've done just done a quick clean test with 2 different 1GB VBK files and tried to copy both with Windows Explorer and with File Copy job, and the time was about the same (Windows Explorer was a few seconds faster): around 1 min 20 sec.

broadview
Lurker
Posts: 2
Liked: never
Joined: Mar 01, 2010 5:38 pm
Full Name: Broadview Mortgage
Contact:

Re: How to backup to removable storage: RoboCopy and PowerSh

Post by broadview »

You could also use a .vbs script to send the email without needing to install powershell, I am doing this in many instances on many of my systems.
Seems a little more stable and less dependent on the user credentials running the script.

Here is an example of one script I use (Unrelated) that sends email:

Code: Select all

'Monitoring servers free disk space
'
'Build an array of the computer names to monitor free space on multiple servers
'Query those computers for all local, active hard disks and get their Drive letter and free space in MBs
'Trap errors if any of the computers are not on or unable to be contacted, note error description, continue 
'Authenticate with SMTP Server
'Send email with all gathered information to IT
'

'drive types
Const Unknown = 0
Const Removable = 1
Const FLOPPY = 2
Const HDD = 3
Const NETWORK = 4
Const CD = 5

'Setup mail parameters
Sub SendMail(Sender, Recipient, Subject, Message)
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. 
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). 

Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM

Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = Subject 
objMessage.From = Sender 
objMessage.To = Recipient 
objMessage.TextBody = Message

'==This section provides the configuration information for the remote SMTP server.

objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.domain.com"

'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic

'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user@domain.com"

'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "PASSWORD"

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

objMessage.Configuration.Fields.Update

'==End remote SMTP server configuration section==

objMessage.Send
End Sub

'get current computer name (from system environment variables)
Function GetCurrentComputerName
set oWsh = WScript.CreateObject("WScript.Shell")
set oWshSysEnv = oWsh.Environment("PROCESS")
GetCurrentComputerName = oWshSysEnv("COMPUTERNAME")
End Function

str = ""
Dim strComputer(14)
strComputer(0) = "Altigen"
strComputer(1) = "BACKUP"
strComputer(2) = "COPIERPC"
strComputer(3) = "Corp3"
strComputer(4) = "dc1"
strComputer(5) = "FS1"
strComputer(6) = "FS2"
strComputer(7) = "FS3"
strComputer(8) = "MS1"
strComputer(9) = "MV"
strComputer(10) = "TS1"
strComputer(11) = "QB"
strComputer(12) = "VC1"
strComputer(13) = "VC2"
strComputer(14) = "VCS1"

dim errorstr
Dim size
Dim free
Dim pct
Dim vol
Dim dlet 'Drive Letter
dim voldlet

For Each strComputer in strComputer
  On Error Resume Next
  Err.Clear
  Set objWMIService = GetObject("winmgmts:{impersonationLevel=Impersonate}\\" & strComputer & "\root\cimv2")
  Set colDisks = objWMIService.ExecQuery _
  ("Select * from Win32_LogicalDisk Where DriveType = " & HDD & "")
  If Err > 0 then str = str & vbcrlf & "Server: " & strComputer & " ***Error*** " & Err.Description & vbcrlf
  If Err < 1 then str = str & vbcrlf & "Server: " & strComputer & vbcrlf
    For Each objDisk in colDisks
      if (not isnull(objDisk.FreeSpace)) and (not objDisk.VolumeName = "PAGEFILE") then
        If Err < 1 then 
        size = FormatNumber(CLng(objDisk.Size/1024/1024),0)
        free = FormatNumber(CLng(objDisk.FreeSpace/1024/1024),0)
        pct = FormatNumber((free * 100 / size),2)
        vol = objDisk.VolumeName
        dlet = objDisk.DeviceID
		voldlet = vol & " (" & dlet & ")"
		'if pct < 20 then 
		'	errorstr = "***ERROR"
		'end if
        str = str & vbTab & voldlet
		if len(voldlet) > 24 then
			str = str & vbTab 
		else
			str = str & vbTab & vbTab & vbTab 
		end if
		str = str & "Size = " & size & " MB" & vbTab & "Free = " & free & " MB" & vbTab & "Pct Free = " & pct & "%" & vbcrlf
        end if
      end if
    Next
 Next

str = str & vbcrlf & vbcrlf & "This report was generated by ""V:\Scripts\Server Available Drive Space Report.vbs"" run via scheduled tasks on FS1"

'Send the email
SendMail "sender@domain.com","recip@domain.com", "Server Available Drive Space Report", str

bruc2082
Lurker
Posts: 1
Liked: never
Joined: Feb 04, 2013 6:26 pm
Full Name: Jason Bruce
Contact:

Re: How to backup to removable storage: RoboCopy and PowerSh

Post by bruc2082 »

I need help with rotating media. I have a nice Robocopy script configured, however, I do not know how to configure Veeam to call it up. Anybody know how to set this up? Below are the two Robocopy commands for the two backup jobs I have.

robocopy "F:\01 Local Linux VM Backups" "E:\01 Local Linux VM Backups" /COPYALL /PURGE /E /XO /W:1 /IPG:15 /NP /NDL /R:1 /B

robocopy "F:\01 Local Windows VM Backups" "E:\01 Local Windows VM Backups" /COPYALL /PURGE /E /XO /W:1 /IPG:15 /NP /NDL /R:1 /B

veremin
Product Manager
Posts: 17901
Liked: 1673 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to backup to removable storage: RoboCopy and PowerSh

Post by veremin »

Hi, Jason.

Please, be aware that you can only specify *cmd,*exe or *bat files as the job post-activity. So, you’ll have to either convert your powershell script into a batch file (powershell-script-in-a-bat-file) or just execute your PowerShell script from a batch file, using the following command:

Code: Select all

Powershell.exe -File C:\YourScript.ps1
Hope this helps.
Thanks.

Garry Brown
Lurker
Posts: 1
Liked: never
Joined: Jan 07, 2018 11:45 am
Full Name: Garry Brown
Contact:

Re: How to backup to removable storage: RoboCopy and PowerSh

Post by Garry Brown »

I would like to suggest you, try Long Path Tool program to resolve this issue.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests