4 September 2013

Lync 2013 Backup Script

I have to admit the Lasse' scripting talents (and his willingness to share) has delivered some awesome scripts. Todays blog is evidence of his skills...his work is found here http://tech.rundtomrundt.com/p/lync-scripts.html

Thanks buddy!

<#
.Synopsis
   This is a script to make a backup of your Lync 2013 Enterprise edition server environment.
   It should be run on a windows server 2012, and a 2013 Lync server.
.DESCRIPTION
    A script to backup vital components in a Lync Enterprise Edition deployment
    Created by Lasse Nordvik Wedø - All rights reserved
    Http://tech.rundtomrundt.com

    - This script is for a Enterprise Edition Server.
    - The script has only been tested in single site topology. I suspect adjustments must be made for deployments with more than one site (If anyone would do so, or let me have access to such a deployment, please let me know)
    - This script has been tested with a co location of all databeses. If you require it to backup your Monitoring/archiving databases from seperate SQL servers, you must add these sources to the script.
    - The script should be able to run without any modification or input, unless you want to use other paths than I have entered.
    - The script must be run on a server where Lync PS is available.
    - If the script must be run in a PS3 environment, and will load all nessecary modules automatically
    - My script creates a directory C:\lyncbackup\, this may be edited if you like.
    - Certificates will only be backed up if you allowed for this when requesting and creating certificates.
    - Certificate backup is only done on the machine where the script is run
    - The creation of the zipfile can take a while. The script finishes before the zipfile is finished (If anyone know how to wait for this task before quitting the script, please let me know).
    - I highly recommend you test the script in your Lab, before running in your production environment



    V 1.0 - July 2012 - Created for Standard Edition
    V 1.1 - August 2012 - added a cleanup rutine, and zip rutine
    V 1.2 - October 2012 - Completed for Enterprise Edition
    V 2.0 - March 2013 - Edited for a Lync 2013 environment.
.EXAMPLE
   Backupscript - Enterprise EDT v2 (Lync 2013).ps1
   #>

$date = "{0:yyyy_MM_dd-HH_mm}" -f (get-date)

#################################################################
#
# Getting Lync pool information
#
#################################################################

$sysinfo = Get-WmiObject -Class Win32_ComputerSystem
$fqdnLyncReal = “{0}.{1}” -f $sysinfo.Name, $sysinfo.Domain
$fqdnLyncpool = Get-CsService -CentralManagement | Select-Object PoolFqdn
$fqdnLync = $fqdnLyncpool.PoolFqdn.tolower()

#################################################################
#
# This will store Backup in C:\lyncbackup\
#
# Setting File and share paths
# Defining filenames
#
# Edit these to for automation as you please
#
#################################################################


[system.Console]::ForegroundColor = [System.ConsoleColor]::Yellow
$filepath = "c:\lyncbackup\"
$filepathshare = "c:\lyncbackup"
$fileshare = Get-CsService -FileStore | Select-Object UncPath
$filesharepath = $fileshare.UncPath.tolower()

$filepath1 = $filepath + $date
$filepath2 = $filepath1 + "\FileshareData"
$filepath3 = $filepath1 + "\SQLBU"

$backupfile1 = $filepath1 + "\CsConfiguration.zip"
$backupfile10 = $filepath1 + "\RGSConfiguration.zip"
$backupfile11 = $filepath + "\BACKUP " + $date +".zip"
$backupfile12 = $filepath1 + "\Topology " + $date +".xml"
$backupfile13 = $filepath1 + "\UserData.zip"
$backupfile14 = $filepath1 + "\PersistantChatData.zip"
$backupfile2 = $filepath1 + "\CsLISconfiguration.bak"
$backupfile3 = $filepath1 + "\dbimpexp.xml"
$backupfile4 = $filepath1 + "\DialPlan.xml"
$backupfile5 = $filepath1 + "\VoicePolicy.xml"
$backupfile6 = $filepath1 + "\VoiceRoute.xml"
$backupfile7 = $filepath1 + "\PSTNUsage.xml"
$backupfile8 = $filepath1 + "\VoiceConfiguration.xml"
$backupfile9 = $filepath1 + "\TrunkConfiguration.xml"

$logfile = "c:\Backup_run_" + $date +".log"

$fileshare = "\\" + $fqdnLyncReal + "\lyncbackup"
$backuproot = $fileshare + "\" + $date + "\SQLBU"

Start-Transcript -Path $logfile -Append

Write-Output ("Script started at: " + $date);

New-Item $filepath -type directory -force -Verbose
New-Item $filepath1 -type directory -force -Verbose
New-Item $filepath2 -type directory -force -Verbose
New-Item $filepath3 -type directory -force -Verbose

#################################################################
#
# Creating a fileShare with everyone rigths
# If you already have provisioned a share, where you SQL_service user have full controll over,
# You may skip this.
#
#################################################################

NET SHARE lyncbackup=$filepathshare "/GRANT:Everyone,FULL"

#################################################################
#
# Delete all Files in $filepath older than 5 day(s)
#
#################################################################

$Days = "-5"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Days)
Get-ChildItem $filepath -recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Verbose

#################################################################
#
# Exporting your Microsoft Lync Server 2013 topology, policies, and configuration settings to a file.
#
#################################################################

export-csconfiguration -filename $backupfile1 -Verbose

#################################################################
#
# Creating a backup of your topology as an XML file
#
#################################################################

(Get-CsTopology -AsXml).ToString() > $backupfile12

#################################################################
#
# Exports an Enterprise Voice Enhanced 9-1-1 (E9-1-1) configuration to a file in compressed format for backup purposes.
#
#################################################################

export-cslisconfiguration -filename $backupfile2 -Verbose

#################################################################
#
# Exports RGS configuration to a file in compressed format for backup purposes.
#
#################################################################

Export-CsRgsConfiguration -source ApplicationServer:$fqdnLync -FileName $backupfile10 -Verbose

#################################################################
#
# Export User information
#
#################################################################

Export-CsUserData -PoolFqdn $fqdnLync -FileName $backupfile13 -Verbose

#################################################################
#
# Use Xcopy to create copy from fileshare
#
#################################################################

net use y: $filesharepath
cd y:
Xcopy *.* $filepath2 /E /I /Y /H /C
cd $filepath
net use y: /delete

#################################################################
#
# Backing up some of the vital policies and settings
#
#################################################################

Get-CsDialPlan | Export-Clixml -path $backupfile4 -Verbose
Get-CsVoicePolicy | Export-Clixml -path $backupfile5 -Verbose
Get-CsVoiceRoute | Export-Clixml -path $backupfile6 -Verbose
Get-CsPstnUsage | Export-Clixml -path $backupfile7 -Verbose
Get-CsVoiceConfiguration | Export-Clixml -path $backupfile8 -Verbose
Get-CsTrunkConfiguration | Export-Clixml -path $backupfile9 -Verbose

#################################################################
#
# I ran into some file rights issues when backing up the SQL
# Setting ACL on the target forlder of the SQL Backup
# Should not impose any security threat, as the share is removed in the end
#
#################################################################

$Acl = Get-Acl $filepath3
$Ar = New-Object  system.security.accesscontrol.filesystemaccessrule("Everyone","FullControl","ContainerInherit, ObjectInherit", "None","Allow")
$Acl.SetAccessRule($Ar)
Set-Acl $filepath3 $Acl

#################################################################
#
# Backing up SQL
#
#################################################################

Import-Module SQLPS -DisableNameChecking
$SQLInstance = Get-CsConfigurationStoreLocation
$SQLFQDN = Get-CsService -CentralManagementdatabase | Select-Object PoolFqdn
$InstanceSQL = Get-CsService -CentralManagementDatabase | Select-Object SqlInstanceName
$instancenamesql = $InstanceSQL.SqlInstanceName.toupper()
$SQLServer = $SQLFQDN.PoolFqdn.toupper()
$Server = $SQLInstance;     # SQL Server Instance.
$inst=$null
$Dest = $backuproot;    # Backup path on server (optional).
$ServerName = $sysinfo.Name.tostring()
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.ConnectionInfo');          
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Management.Sdk.Sfc');          
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO');          
# Requiered for SQL Server 2008 (SMO 10.0).          
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMOExtended');

cd SQLSERVER:\SQL\$SQLServer\$instancenamesql\Databases

#start full backups
$cdate = Get-Date -Format MMddyy
foreach($database in (Get-ChildItem -name -force)) {
$dbName = $database
$bakFile = $dest + "\" + $dbName + "_full_" + $cdate + ".bak"
If($dbName -ne "tempdb"){
Backup-SqlDatabase -Database $dbName -BackupFile $bakFile -Initialize -verbose }}

#################################################################
#
# Export Persistant Chat
# This part of the script will be updated once I can verify it
#
#################################################################

#$SQLInstance = Get-CsConfigurationStoreLocation | Select-Object BackEndServer
#$PersistandBU = $SQLInstance.BackEndServer.ToLower()
#Export-CsPersistentChatData -DBInstance $PersistandBU -FileName $backupfile14

#################################################################
#
# Backing up CERT of local computer
#
#################################################################

dir cert:\localmachine\my |
      Where-Object { $_.HasPrivateKey -and $_.PrivateKey.CspKeyContainerInfo.Exportable } |
      Foreach-Object { [system.IO.file]::WriteAllBytes(
               $filepath1 + "\$($_.thumbprint).pfx",
               ($_.Export('PFX', 'secret')) ) }
#################################################################
#            
# Create the final ZIP file
#
#################################################################
 
Set-Content $backupfile11 ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
$File = Get-ChildItem $backupfile11 -ErrorAction SilentlyContinue
$File = (New-Object -COM Shell.Application).Namespace($File.FullName)
$File.CopyHere($filepath1, 4)

c:
cd \
NET SHARE lyncbackup /y /delete

#write-host "Your backupfile is now storing as $backupfile11"
Write-Output ("Finished at: " + (Get-Date -format  yyyy-MM-dd-HH:mm:ss) + "A logfile has been created as " + $logfile);
Stop-Transcript
[system.Console]::ForegroundColor = [System.ConsoleColor]::White

No comments:

Post a comment