cancel
Showing results for 
Search instead for 
Did you mean: 
Stephen_Roux_79
Nimbostratus
Nimbostratus

Problem this snippet solves:

This PowerShell application performs archiving and restoring of your BIG-IP configuration.

Let's face it, we like to believe that disaster will never happen, but how many times have you accidentally deleted a document you have been working on for a presentation without a working backup? Odds are more than once. If it can happen on your desktop, then it can happen on the network and imagine the situation where your network configuration is running the bread and butter for your company. Wouldn't it be nice to have the knowledge that your network configuration is securely archived for easy recovery? Well, this application performs configuration archiving for a BIG-IP device by allowing for the creation of configuration restore points as well as remote file upload/download capabilities.

Code :

#----------------------------------------------------------------------------
# The contents of this file are subject to the "END USER LICENSE AGREEMENT FOR F5
# Software Development Kit for iControl"; you may not use this file except in
# compliance with the License. The License is included in the iControl
# Software Development Kit.
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is iControl Code and related documentation
# distributed by F5.
#
# The Initial Developer of the Original Code is F5 Networks,
# Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2007 F5 Networks,
# Inc. All Rights Reserved.  iControl (TM) is a registered trademark of F5 Networks, Inc.
#
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public License (the "GPL"), in which case the
# provisions of GPL are applicable instead of those above.  If you wish
# to allow use of your version of this file only under the terms of the
# GPL and not to allow others to use your version of this file under the
# License, indicate your decision by deleting the provisions above and
# replace them with the notice and other provisions required by the GPL.
# If you do not delete the provisions above, a recipient may use your
# version of this file under either the License or the GPL.
#----------------------------------------------------------------------------
param (
  $g_bigip = $null,
  $g_uid = $null,
  $g_pwd = $null,
  $g_cmd = $null,
  $g_name = $null
);

$DEFAULT_CHUNK_SIZE = (64*1024);

Set-PSDebug -strict;

#-------------------------------------------------------------------------
# function Write-Usage
#-------------------------------------------------------------------------
function Write-Usage()
{
  Write-Host "Usage: ConfigArchive.ps1 host uid pwd [list|save|delete|download|install|rollback|upload [name]]";
  exit;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Generate-ConfigName()
{
  $now = [DateTime]::Now;
  $year = $now.year;
  $month = $now.month; if ($month -lt 10) { $month = "0${month}" }
  $day = $now.day; if ( $day -lt 10 ) { $day = "0${day}" }
  $hour = $now.hour; if ( $hour -lt 10 ) { $hour = "0${hour}" }
  $minute = $now.minute; if ( $minute -lt 10 ) { $minute = "0${minute}" }
  $second = $now.second; if ( $second -lt 10 ) { $second = "0${second}" }
  $config_name = "${g_bigip}-${year}${month}${day}-${hour}${minute}${second}.ucs"
  return $config_name;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Get-ConfigList()
{
  $ConfigFileEntryList = (Get-F5.iControl).SystemConfigSync.get_configuration_list();
  Write-Host "Available Configuration Files";
  Write-Host "-----------------------------";
  foreach ($ConfigFileEntry in $ConfigFileEntryList)
  {
    $file_name = $ConfigFileEntry.file_name;
    $file_datetime = $ConfigFileEntry.file_datetime;
    
    Write-Host "-> $file_name ($file_datetime)";
  }
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Save-Configuration()
{
  param($config_name = $null);
  if ( $config_name -eq $null )
  {
    $config_name = Generate-ConfigName;
  }
  Write-Host "Saving Configuration to file $config_name"
  (Get-F5.iControl).SystemConfigSync.save_configuration($config_name, "SAVE_FULL");
  
  Get-ConfigList;
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Delete-Configuration()
{
  param($config_name);
  if ( $config_name -eq $null )
  {
    Write-Usage;
  }
  else
  {
    Write-Host "Deleting configuration $config_name";
    (Get-F5.iControl).SystemConfigSync.delete_configuration($config_name);
    
    Get-ConfigList;
  }
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Download-Configuration()
{
  param($config_name);
  
  Write-Host "Downloading Configuration file $config_name"
  
  $loc = Get-Location
  $local_file = "$loc\$config_name";
  
  $ctx = New-Object -TypeName iControl.SystemConfigSyncFileTransferContext;
  $chunk_size = $DEFAULT_CHUNK_SIZE;
  $file_offset = 0;
  $bContinue = 1;
  $mode = [System.IO.FileMode]::CreateNew;
  if ( Test-Path $local_file )
  {
    $mode = [System.IO.FileMode]::Truncate;
  }
  
  $fs = New-Object -TypeName System.IO.FileStream -argumentList ($local_file, $mode);
  $w = New-Object -TypeName System.IO.BinaryWriter -argumentList ($fs);
  
  while($bContinue -eq 1)
  {
    $ctx = (Get-F5.iControl).SystemConfigSync.download_configuration($config_name, $chunk_size, [ref]$file_offset);
    $w.Write($ctx.file_data, 0, $ctx.file_data.Length);

    Write-Host "Bytes Transferred: $file_offset";
    if ( ($ctx.chain_type -eq "FILE_LAST") -or ($ctx.chain_type -eq "FILE_FIRST_AND_LAST") )
    {
      $bContinue = 0;
    }
  }
  $w.Close()
  $fs.Close()
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Install-Configuration()
{
  param($config_name);
  if ( $config_name -eq $null )
  {
    Write-Usage;
  }
  else
  {
    Write-Host "Installing Configuration $config_name";
    (Get-F5.iControl).SystemConfigSync.install_configuration($config_name);
    Write-Host "Configuration successfully installed.";
  }
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Rollback-Configuration()
{
  Write-Host "Rolling back configuration"
  (Get-F5.iControl).SystemConfigSync.rollback_configuration();
  Write-Host "Configuration Rollback successful"
}

#-------------------------------------------------------------------------
#
#-------------------------------------------------------------------------
function Upload-Configuration()
{
  param($config_name);

  $loc = Get-Location
  $local_file = "$loc\$config_name";
  
  Write-Host "Uploading file $local_file";
  
  $ctx = New-Object -TypeName iControl.SystemConfigSyncFileTransferContext;
  $bContinue = 1;
  $ctx.chain_type = "FILE_FIRST";
  $chunk_size = $DEFAULT_CHUNK_SIZE;
  $total_bytes = 0;
  $fs = New-Object -TypeName System.IO.FileStream -argumentList ($local_file, [System.IO.FileMode]::Open);
  $r = New-Object -TypeName System.IO.BinaryReader -argumentList ($fs)

  while ($bContinue -eq 1)
  {
    $ctx.file_data = $r.ReadBytes($chunk_size);
    if ( $ctx.file_data.Length -ne $chunk_size )
    {
      # At the end, check to see if it is the first request
      if ( $total_bytes -eq 0 )
      {
        $ctx.chain_type = "FILE_FIRST_AND_LAST";
      }
      else
      {
        $ctx.chain_type = "FILE_LAST";
      }
      $bContinue = 0;
    }
    $total_bytes = $total_bytes + $ctx.file_data.Length;
    
    # Upload bytes
    (Get-F5.iControl).SystemConfigSync.upload_configuration($config_name, $ctx);
    
    # move to middle
    $ctx.chain_type = "FILE_MIDDLE";
    
    Write-Host "Uploaded $total_bytes bytes";
  }
  
  $r.Close();
  $fs.Close();
}

#-------------------------------------------------------------------------
# Do-Initialize
#-------------------------------------------------------------------------
function Do-Initialize()
{
  if ( (Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn"}) -eq $null )
  {
    Add-PSSnapIn iControlSnapIn
  }
  $success = Initialize-F5.iControl -HostName $g_bigip -Username $g_uid -Password $g_pwd;
  
  return $success;
}

#-------------------------------------------------------------------------
# Main Application Logic
#-------------------------------------------------------------------------
if ( ($g_bigip -eq $null) -or ($g_uid -eq $null) -or ($g_pwd -eq $null) -or ($g_cmd -eq $null) )
{
  Write-Usage;
}

if ( Do-Initialize )
{
  switch ($g_cmd.ToLower())
  {
    "list" {
      Get-ConfigList;
    }
    "save" {
      Save-Configuration $g_name;
    }
    "delete" {
      Delete-Configuration $g_name;
    }
    "download" {
      Download-Configuration $g_name;
    }
    "install" {
      Install-Configuration $g_name;
    }
    "rollback" {
      Rollback-Configuration;
    }
    "upload" {
      Upload-Configuration $g_name;
    }
    default {
      Write-Usage;
    }
  }
}
else
{
  Write-Error "ERROR: iControl subsystem not initialized"
}
Version history
Last update:
‎09-Mar-2015 11:52
Updated by:
Contributors