Wednesday, February 5, 2014

Backup Lync 2010 Remotely from Orchestrator

Backup Lync 2010 Remotely from Orchestrator


Prerequisites: Lync 2010 Cmdlets installed on Orchestrator Server.
Files needed: DBImpExp.exe


So I needed a way to backup Lync 2010 configuration. Since I have snap shots of the VM I can have a restored Lync environment up in a few hours. But all of my configuration changes will be missing.

This is where Orchestrator Runbook comes into play.

Simple layout
Monitor Date Time
Check Schedule
Export DBImpExp
Sleep (Note Runbook is faster than my script so I need to slow it down)
Script-Export


Now for the Code:

Export DBImpExp:
cd \
cd "C:\Program Files\Common Files\Microsoft Lync Server 2010\Support"
start-process dbimpexp.bat


dbimpexp.bat


@ECHO OFF
REM CD "C:\Program Files\Common Files\Microsoft Lync Server 2010\Support"
dbimpexp.exe /hrxmlfile:"E:\masterbackup\Lync-Backups\BackupFiles\DBImpExp.xml" /sqlserver:nlkvsqldba04\lync_prod
 

Sleep:
PING 127.0.0.1 -n 50 (I do a simple ping, always works!)

Export DBImpExp: once you get all of this data I like to package it into a zip file.


############# NEW-ZIP ############################
function New-Zip
{
  param(
 [Parameter(Mandatory=$true,
  Position=0,
  ValueFromPipeline=$true)]
 [String]
 $Path,
 
 [Switch]
 $PassThru,
 
 [Switch]
 $Force
 )
 process {

   if (Test-Path $path)
   {
    if (-not $Force) { 
     return
    }
   }
   Set-Content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
   $item = Get-Item $path
   $item.IsReadOnly = $false 
   if ($passThru) { $item } 
  
 }
}

Here is the Function to Copy to Zip file

########## Copy-ToZip ###########################
function Copy-ToZip
{
param(
  [Parameter(Mandatory=$true,
    Position=0,
    ValueFromPipelineByPropertyName=$true)]
  [Alias('FullName')]
  [String]$File,

  [Parameter(Mandatory=$true,Position=1)]
  [String]$ZipFile,
  
  [Switch]$HideProgress,

  [Switch]$Force
  )
  begin {

      $ShellApplication = New-Object -ComObject Shell.Application
      if (-not (Test-Path $ZipFile)) {
        New-Zip $ZipFile
      }
      $Path = Resolve-Path $ZipFile
      $ZipPackage =$ShellApplication.Namespace("$Path")
    
  }
  process {

      $RealFile = Get-Item $File
      if (-not $RealFile) { return }    
      if (-not $hideProgress) {
        $perc +=5 
        if ($perc -gt 100) { $perc = 0 } 
        Write-Progress "Copying to $ZipFile" $RealFile.FullName -PercentComplete $perc
      }
      $Flags = 0
      if ($force) {
        $flags = 16 -bor 1024 -bor 64 -bor 512
      } 
      Write-Verbose $realFile.Fullname
      $ZipPackage.CopyHere($realFile.Fullname, $flags)
      Start-Sleep -Milliseconds 500
    
  }
}
Here is where I move the XML Files into the Zip file
I also call 4 other Lync Cmdlets
export-csconfiguration
export-cslisconfiguration
Get-CSVoicePolicy
Get-CSDialPlan

############ Loads funcion to Zip up XML files #############################

# Loads funcion to Zip up XML files
##		This script will purge old files, right now it is set for 10 days

$Now = Get-Date
$Days = "10"
$TargetFolder = "E:\masterbackup\Lync-Backups\BackupFiles\"
$Extension = "*.zip"
$LastWrite = $Now.AddDays(-$Days)
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
	    {
	    if ($File -ne $NULL)
	        {
	        Remove-Item $File.FullName | out-null
	        }
	    else
	        {
	        }
	    }
##		Create two Zip Files for backups
$filename = "E:\masterbackup\Lync-Backups\BackupFiles\{0:yyyy.MM.dd-HH.mm}-CSconfig.zip" -f (Get-Date)
$filename1 = "E:\masterbackup\Lync-Backups\BackupFiles\{0:yyyy.MM.dd-HH.mm}-LISconfig.zip" -f (Get-Date)
$filename2 = "E:\masterbackup\Lync-Backups\BackupFiles\{0:yyyy.MM.dd-HH.mm}-Rgsconfig.zip" -f (Get-Date)
$filename3 = "E:\masterbackup\Lync-Backups\BackupFiles\{0:yyyy.MM.dd-HH.mm}-CS.zip" -f (Get-Date)
##		Commands Run the Export Features
export-csconfiguration -Filename $filename -Force:$True
export-cslisconfiguration -Filename $filename1
#Export-CsRgsConfiguration  -Filename $filename2
Get-CSVoicePolicy | export-clixml –path E:\masterbackup\Lync-Backups\BackupFiles\VoicePolicy.xml
Get-CSDialPlan | export-clixml –path E:\masterbackup\Lync-Backups\BackupFiles\Dialplan.xml



##		Change Directories
Cd \
Cd \
cd E:
cd E:\masterbackup\Lync-Backups\BackupFiles
##		Create Zip file to move XML's
new-zip $filename3
PING 127.0.0.1 -n 10
dir .\DBImpExp.xml | copy-toZip -zipfile $filename3
PING 127.0.0.1 -n 10
dir .\VoicePolicy.xml | copy-toZip -zipfile $filename3
PING 127.0.0.1 -n 5
dir .\Dialplan.xml | copy-toZip -zipfile $filename3
PING 127.0.0.1 -n 5
Del *.xml




When the Runbook has completed you should have several Zip files you Configurations files.