Automate Windows Server Backup using DiskShadow and Rsync …

We have a number of Netgear ReadyNAS devices across our offices. All of these run nightly Rsync backup jobs back to a ReadyNAS 516 in head office. The 516 device also does a daily snapshot of the shares. Note that as most of our offices utilise ADSL, we had to manually transport a copy of the data to head office. Now the backup jobs just synchronise changes. This works reasonably well and provides some rudimentary historical offsite backup for the remote offices.

It proved somewhat more difficult to work out how to achieve the same result with our Windows Servers. These are primarily 2008R2 but we still have 1 x 2003 and 1 x 2012R2.

I had a look at variety of programs such as DeltaCopy and Yintersync but could not seem to get them to talk to the ReadyNAS Rsync server. Or get the ReadyNAS to talk to them. Yintersync looked really interesting but did not seem to suit my set up with the NAS devices. Also, I was not sure how these would work with open files such as Exchange and SQL Server database files.

Eventually, I stumbled across this page:

http://web.aboutmyx.com/app?operation=forum&st=viewOneArticle&id=1223

This seemed to offer an interesting option. Not having done much with shadow copies, i soon discovered that since Server 2008 (i think), DiskShadow is now the command-line tool for working with shadow copies.

http://angrytechnician.wordpress.com/2011/02/23/vssadmin-is-dead-long-live-diskshadow/

http://social.technet.microsoft.com/Forums/windowsserver/en-US/506855d1-21a4-4302-918a-5c9a503be1ae/where-is-a-vshadowexe-for-windows-2008-r2?forum=winservergen

http://technet.microsoft.com/en-us/library/cc772172%28WS.10%29.aspx

Anyhow, to cut a long story short, I ended up with this DiskShadow script, imaginatively called “DiskShadowScript.cmd”:

#DiskShadow script file
set context persistent nowriters
set metadata C:\BW\DiskShadowBackup\example.cab
set verbose on
begin backup
add volume c: alias SystemVolumeShadow
create
expose %SystemVolumeShadow% x:
exec C:\cwRsync\BackupScript.cmd
unexpose x:
end backup
#End of script

You will notice that this calls an rsync script to synchronise the shadow copy with the rsync server. This is the rsync script minus the incriminating details:

C:\cwRsync\rsync.exe -rltv --exclude-from="/cygdrive/C/cwRsync/ExcludePatterns.txt" --log-file="/cygdrive/C/Logs/Rsync/BackupScript.log" "/cygdrive/X/" "ip-address::backup-mel/sp1/C/"

Obviously, we replace ‘ip-address’ with an actual ip address like 192.168.0.254.

You will also see that I am using rsync from cwRsync. This is the free version available here:

https://www.itefix.no/i2/cwrsync

I’m tossing up whether to purchase the other one but not sure if I need the extra features and support etc.

I then wrapped this up in a PowerShell script that sends me an email when finished, with the backup log attached. I schedule this to run daily using the Windows Task Scheduler. Here is the PowerShell script:

Import-Module C:\BW\Powershell\Modules\SendMail
# Create a disk shadow and rsync to Melbourne
DiskShadow /s "C:\BW\DiskShadowBackup\DiskShadowScript.cmd"
#
# Set up email
$dtToday = ((Get-Date).dateTime).tostring()
$strSubject = "SP1 Rsync Report - $dtToday" 
$strBody = "report attached."
$strSender = "noreply@domain"
$strRecipient = "me@domain"
$Attachment = "C:\Logs\Rsync\BackupScript.log"
# Send the email
SendMail $strSubject $strBody $strSender $strRecipient $Attachment

The ‘SendMail’ is a function defined in a module. Here is the module:

Function SendMail($strSubject, $strBody, $strSenderEmail, $strRecipientEmail, $AttachFile)
 {
 $strSMTP = "smtp-server" #Change this to your SMTP/Exchange server
 $MailMessage = New-Object System.Net.Mail.MailMessage
 $SMTPClient = New-Object System.Net.Mail.smtpClient
 $SMTPClient.host = $strSMTP
 $Recipient = New-Object System.Net.Mail.MailAddress($strRecipientemail, "Recipient")
 $Sender = New-Object System.Net.Mail.MailAddress($strSenderemail, "Sender")
 $MailMessage.Sender = $Sender
 $MailMessage.From = $Sender
 $MailMessage.Subject = $strSubject
 $MailMessage.To.add($Recipient)
 $MailMessage.Body = $strBody
 if ($AttachFile -ne $null) {$MailMessage.attachments.add($AttachFile) }
 $SMTPClient.Send($MailMessage)
 }

The PowerShell scripts were basically copied from these pages with some modification:

http://ss64.com/ps/syntax-email.html

http://gallery.technet.microsoft.com/scriptcenter/Simple-Powershell-function-8e826d7c

http://blogs.technet.com/b/heyscriptingguy/archive/2012/08/11/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script.aspx

http://technet.microsoft.com/en-us/library/hh847804.aspx

http://community.spiceworks.com/how_to/show/17736-run-powershell-scripts-from-task-scheduler

Thanks again to all those that take the time to blog or post on the internet.

Advertisements

One comment

  1. It looks like SMB 3.0 supports remote differential compression. I’m not sure how it works in practice, though.

    http://windowsitpro.com/hyper-v/use-dfs-r-replicate-windows-server-2012-hyper-v-virtual-machines

    SMB 3.0 has some very nice features:

    http://searchservervirtualization.techtarget.com/opinion/Drop-everything-and-jump-on-the-SMB-3-bandwagon

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: