A quick and easy Cisco UCS backup script
This is a re-post of an original script I wrote for my old blog at awesomeman.com. In essence it is a Cisco UCS backup script utilising the UCS Powertool CMDlets provided by Cisco. Prior to this, I was unaware of any method for scheduling UCS backups and have been using the script successfully in a production environment ever since.
I contemplated updating it for a while as it is rather old school and uses a few bad habits I’ve since dropped. However, there is also something in the simplicity I like and given that it works perfectly and has done for some time now, I see no reason to change it. Enjoy.
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.
<# .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
FYI…you can actually do automated backups now with 2.1.
http://www.cisco.com/en/US/docs/unified_computing/ucs/sw/gui/config/guide/2.1/b_UCSM_GUI_Configuration_Guide_2_1_chapter_0101001.html#d92034e1532a1635
Thanks for the tip Anthony.
It looks like you are limited to using FTP, TFTP, SCP or SFTP for the scheduled backups though which is slightly annoying.
Still, better than nothing!