NETWORK ENGINEER BLOG

Tips and Reviews for Engineers

BIG-IP の UCS ファイルを作成・取得するスクリプト

BIG-IP の UCS(設定ファイル)を作成し・取得するスクリプトになります。
タスクスケジューラを使用する事で自動化が可能です。

使用する引数は下記の 3 つです。

  • list: Archive List の一覧を表示します。
  • save: UCS を任意の名前で作成します。
  • download: 存在する UCS をダウンロードします。
ISE 実行例

.\ConfigBackup.ps1 192.168.1.101 admin password list

> .\ConfigBackup.ps1 192.168.1.101 admin password list
Available Configuration Files
-----------------------------
-> backup.ucs (Tue Apr  9 05:11:28 2013)

.\ConfigBackup.ps1 192.168.1.101 admin password save backup.ucs

> .\ConfigBackup.ps1 192.168.1.101 admin password save backup.ucs
Saving Configuration to file backup.ucs
Available Configuration Files
-----------------------------
-> backup.ucs (Tue Apr  9 05:29:36 2013)

.\ConfigBackup.ps1 192.168.1.101 admin password download backup.ucs

> .\ConfigBackup.ps1 192.168.1.101 admin password download backup.ucs
Downloading Configuration file backup.ucs
Bytes Transferred: 65536
Bytes Transferred: 131072
Bytes Transferred: 196608
Bytes Transferred: 262144
Bytes Transferred: 327680
Bytes Transferred: 345804
Script

ConfigBackup.ps1 の内容は下記のとおりです。

#----------------------------------------------------------------------------
# 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|download [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 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()
}

#-------------------------------------------------------------------------
# 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;
    }
    "download" {
      Download-Configuration $g_name;
    }
    default {
      Write-Usage;
    }
  }
}
else
{
  Write-Error "ERROR: iControl subsystem not initialized"
}

参考:
https://devcentral.f5.com/wiki/icontrol.psconfigarchiving.ashx
https://devcentral.f5.com/tech-tips/articles/icontrol-apps-06-configuration-archiving#.UWKnnFeF-FD

参考書籍

以上