Cisco UCS Manager Powershell Backup Script

I love Cisco UCS. I’ve been fortunate enough to be involved with the design and setup of a number of UCS clusters over the last few years. As with anything else in this line of work, the need to have backups is critical. Fortunately UCS manager includes a built in methods for backing up via the UCS Manager GUI and CLI. Unfortunately. there is no built in way of scheduling these backups.

From the Horse’s mouth:

Scheduled Backups

You cannot schedule a backup operation. You can, however, create a backup operation in advance and leave the admin state disabled until you are ready to run the backup. Cisco UCS Manager does not run the backup operation, save, or export the configuration file until you set the admin state of the backup operation to enabled.

Brilliant.

A quick Google search will reveal a number of options for automating this but I wasn’t able to find anything satisfactory that used the Cisco UCS Powertool Powershell cmdlets. After a few hours working with almost non existent documentation and a helpful co-worker we managed to come up with a working solution which you can find below. I do intend to update it especially in regards to the error checking, but for now it is perfectly workable.

You can also find this on my Github repo.

<#
.SYNOPSIS
Backup Cisco UCS Manager cluster

.PARAMETER
None yet

.DESCRIPTION
Connect securely to a UCS Manager cluster and perform all four backup types. Backup files are saved via http to a customisable location. Results will then be emailed for successfull and failed backups.

.EXAMPLES
.\run-ucs-backup.ps1
#>

# Things still todo:
# Make email body more meaningful
# Change subject of email if one or more of the jobs has failed
# Add param support for all of the variables

# Hostname or IP for your SMTP server
$SMTPServer = x.x.x.x
# Email recipients go here
$mailto = me@email.com
# Sender addresses
$mailfrom = UCS@email.com

# Hostname or IP for UCS Cluster
$UCSHostName = x.x.x.x
# Username for account with rights to backup UCS
$UCSUser = user

# These paths are used by the backup jobs to store each of the four types in their own folder with a unique name. Customise as required.
$BackupPath = .\UCS\Backups\
$fullstatePath =  "$BackupPath\full-state\$UCSHostName-{0:yyyMMdd-HHmm}.xml" -f (get-date)
$configallPath =  "$BackupPath\config-all\$UCSHostName-{0:yyyMMdd-HHmm}.xml" -f (get-date)
$configlogicalPath =  "$BackupPath\config-logical\$UCSHostName-{0:yyyMMdd-HHmm}.xml" -f (get-date)
$configsystemPath =  "$BackupPath\config-system\$UCSHostName-{0:yyyMMdd-HHmm}.xml" -f (get-date)

# You should store this in a txt file to run the script non interatively. See http://blogs.technet.com/b/robcost/archive/2008/05/01/powershell-tip-storing-and-using-password-credentials.aspx
$pwd = read-host -AsSecureString | ConvertFrom-SecureString
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $UCSUser,$pwd

$result = @()
# Function to test for the presence of a backup file matching the name generated above. If it is present it will return the name, size and creation date in the email body.
function Backup_Check($path){
		$test = test-path $path
		if ($test){
			$jobresults = get-childitem $path | select name,length,creationtime
		} else{
			$jobresults = $path + "backup has failed. File is not present"
		}

		return $jobresults
}

# Connect to UCS Manager and execute the four backup jobs
connect-ucs $UCSHostName -credential $cred
backup-ucs -type full-state -pathpattern $fullstatePath
backup-ucs -type config-all -pathpattern $configallPath
backup-ucs -type config-logical -pathpattern $configlogicalPath
backup-ucs -type config-system -pathpattern $configsystemPath

# Run the Backup Check function and save the results in the array $result
$result += Backup_Check($fullstatePath)
$result += Backup_Check($configallPath)
$result += Backup_Check($configlogicalPath)
$result += Backup_Check($configsystemPath)

# Convert $result to a string so it can be used as the body of send-mail message
$body = $result | out-string

# Email results of backups
send-mailmessage -to $mailto -Subject 'UCS Backup Report' -from $mailfrom -smtpserver $SMTPServer -body $body

8 thoughts on “Cisco UCS Manager Powershell Backup Script

  1. Hi, great job, thanks.
    I just have one Problem.
    After executing the script the UCSM returnes an Error 500 and ends. we use UCSM 2.0.3a

    Jean

      1. Hi,

        problem solved.
        My target was a UNC Path.
        I have another question. Is it possible to add a parameter to preserve identities?

        Regards
        Jean

      2. Great to hear you fixed your problem.

        There is a -preservepooledvalues parameter for the backup-ucs cmdlet that you can add on lines 58 to 61.

    1. Looking into it I thought at first you would be able to use new-psdrive but the Cisco cmdlets do not like that.

      Instead what you can do is map your UNC path to a drive letter using the Wscript.Network COM object. Take the code example below and insert early on in the script. Then just update the target paths for your backups to match the drive letter you map to.

      $objNet = new-object -ComObject WScript.Network
      $objNet.MapNetworkDrive("X:", "\\server\share")

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 )

Facebook photo

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

Connecting to %s