8. January 2015

NetScaler Backup by using Powershell

NetScaler Backup by using Powershell

For quite some time it is possible to interact with┬áa NetScaler by using the NITRO API. Since i got some a spare time i took a closer look at this and thought this could be cool way to backup my NetScaler config regularly. On a side note: I do not like running bash scripts started by crontab on my Appliance ­čśë As a result of this I created a little Powershell script which does the following: At first the script connects to the Appliance and creates a session for the following tasks. The script saves the current NetScaler configuration and creates a new full system backup. After that we have to use a third party tool to copy the file from the NetScaler to a desired location in the network. ┬áSadly it is currently not possible to transfer files from or to the NetScaler by using the NITRO API except following file formats: .txt, .cert, .req, .xml, .key.┬á

NetScaler Backup by using Powershell

In the end the script deletes the created system backup file (the NetScaler can only store up to 50 backups at the same time) disconnects the session and sends a mail.

If want to use SSL but haven┬┤t had time yet to configure your certificates right you can disable the SSL check.

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

Citrix also published a nice blog about configuring the NetScaler by using Powershell which can be found here. You can find the NITRO API  documentation in the Citrix eDocs

 

######################################################################################################
#		Purpose:
#				Automatically create and save a NetScaler Backup
#		Scriptname:
#				ns-backup.ps1
#		$ScriptVersion =        "0.1"
#		Prerequisites:
#				- Powershell 3.0
#               - pscp.exe http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
#		Author:
#				Jens Trendelkamp, Jens.Trendelkamp@sepago.de, sepago GmbH
#		Change history:
#				06.01.2015, Jens Trendelkamp, Jens.Trendelkamp@sepago.de
#					- Initial version
#######################################################################################################
# $nsip = NetScaler NS IP Address
# $nsprotocol = HTTP or HTTPS 
# $nsuser = User 
# $nspass = Password
# $filename = How the backup file will be named
# $level = full or basic | http://support.citrix.com/proddocs/topic/ns-system-10-5-map/ns-system-backup1-tsk.html
# $pathtopscp = Where to find pscp.exe 
# $savelocation = Where to save the backup 
# $psemailserver = SMTP Address in case a mail should be send. If not leave this field empty
# $mailto = Mail Recipient
#######################################################################################################
$nsip = "10.0.0.0"
$nsprotocol = "https://"
$nsuser = "nsroot"
$nspass = "nsroot"
$filename = "ns-backup-$(get-date -uformat "%d-%m-%Y-%H-%M")"
$level = "full"
$pathtopscp = "C:\temp"
$savelocation = "\\10.0.0.2\Backups\NetScaler"
$psemailserver = "10.0.0.3"
$mailto = "jens@trendelkamp.net"

$payload = @{"login" = @{"username"=$nsuser;"password"=$nspass;"timeout"=60}}
$payloadjson = ConvertTo-Json $payload
Invoke-RestMethod -Uri "$nsprotocol$nsip/nitro/v1/config/login" -Body $payloadjson -Method POST -SessionVariable saveSession -ContentType application/json | out-null 
$nssession = New-Object -TypeName PSObject
$nssession | Add-Member -NotePropertyName WebSession  -NotePropertyValue $saveSession -TypeName Microsoft.PowerShell.Commands.WebRequestSession
$payload = @{"nsconfig"=@{}}
$payloadjson = ConvertTo-Json $payload
Invoke-RestMethod -Uri "$nsprotocol$nsip/nitro/v1/config/nsconfig?action=save" -Body $payloadjson -Method POST -ContentType application/json -WebSession $nssession.WebSession | out-null 
$payload = @{"systembackup"=@{"level"="$level";"filename"="$filename"}}
$payloadjson = ConvertTo-Json $payload
Invoke-RestMethod -Uri "$nsprotocol$nsip/nitro/v1/config/systembackup?action=create" -Body $payloadjson -Method POST -ContentType application/json -WebSession $nssession.WebSession | out-null 
& $pathtopscp\pscp.exe -pw $NSPASS $nsuser@"$nsip":/var/ns_sys_backup/$filename.tgz $savelocation | out-null 
$payload = @{"systembackup"=@{"filename"="$filename.tgz"}}
$payloadjson = ConvertTo-Json $payload
Invoke-RestMethod -Uri "$nsprotocol$nsip/nitro/v1/config/systembackup?action=rm" -Body $payloadjson -Method POST -ContentType application/json -WebSession $nssession.WebSession | out-null 
$payload = @{"logout"=@{}}
$payloadjson = ConvertTo-Json $payload
Invoke-RestMethod -Uri "$nsprotocol$nsip/nitro/v1/config/logout" -Body $payloadjson -Method POST -ContentType application/json -WebSession $nssession.WebSession | out-null 
$pathnfile = $savelocation + "\" + $filename + ".tgz"
if ($psemailserver) {
if (test-path $pathnfile){
Send-MailMessage -to "$mailto" -from "NetScaler Backup <nsbackup@trendelkamp.net>" -Subject "NetScaler Backup successfull" -body "The scheduled NetScaler Backup has successfully finished and can be found here : $pathnfile"}
else {
Send-MailMessage -to "$mailto" -from "NetScaler Backup <nsbackup@trendelkamp.net>" -Subject "NetScaler Backup failed" -body "The NetScaler Backup has failed!"}}

About Jens

My name is Jens Trendelkamp. I currently work as an IT Consultant at sepago GmbH. My fields of specialty are Application Delivery, SBC\VDI Solutions and Enterprise Mobility based on Products from Microsoft and Citrix.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close