Lync server 2013 backup script

$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

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Time limit is exhausted. Please reload the CAPTCHA.

Этот сайт использует Akismet для борьбы со спамом. Узнайте, как обрабатываются ваши данные комментариев.