davideaves.com

Live in a world of your own, but always welcome visitors.

View on GitHub
7 September 2016

Backing up your Citrix Netscalers.

by deaves

The following is an older script I wrote to automate the backup of a bunch of Citrix NetScaler appliances. Previously I posted an F5 backup script; which was based on this original script. NetScalers are awesome appliances! Not only are they insanely easy to manage; their configs are very straight forward to backup and restore. Very similar to the F5 backup script, posted earlier, we rely on SSH in this script. Except here I use SSHFS to mount the NS:/nsconfig directory and create an archive of it. The reason why I decided to use SSHFS was originally was that I intended to grep out the configured hostname from the config before creating tarball output; below is an example…

DEST="$BACKUPDIR/$(grep ^"set ns hostName" /tmp/nsbackup/ns.conf | awk '{print "ns-"$NF"__"}' | sed 's/__$/'" [$(echo $NS | cksum | awk '{print $1}')] $(date +%F)"'.tar.xv/')"

Just like the previous script, this can ran automatically from cron…

@weekly [ -f /srv/nsbackup/nsbackup.sh ] && { /srv/nsbackup/nsbackup.sh; } > /dev/null

For further reading please reference the following Citrix Support Documentation:

Feel free to review, modify or use this script however you see fit. Remember you do so at your own risk!

#!/bin/bash
## Backup /nsconfig directories against a list of Citrix Netscalers.
## 2016 (v1.0) - Script from www.davideaves.com

NSHOSTS="ns01 ns02"
NSPW="nsroot"
BACKUPDIR="/srv/nsbackup"

# FUNCTION: End Script if error.
DIE() {
 echo "ERROR: Validate \"$_\" is installed and working on your system."
 exit 0
}

# Validate script requirements are meet.
type -p sshfs > /dev/null || DIE

# Main Loop.
for NS in $(echo $NSHOSTS | tr [:lower:] [:upper:]); do

 # Create backup directory and mount nsconfig using sshfs.
 mkdir /tmp/nsbackup && echo "$NSPW" | sshfs nsroot@$NS:/nsconfig/ /tmp/nsbackup -o password_stdin,StrictHostKeyChecking=no

 if [ -f "/tmp/nsbackup/ns.conf" ]; then

  # Figure out backup destination file.
  DEST="$BACKUPDIR/$NS$(echo $NS | cksum | awk '{print "_"$1}') ($(date +%F)).tar.xv"

  # Delete backup files older than 90 days.
  find "$BACKUPDIR" -maxdepth 1 -type f -name "*$(echo $NS | cksum | awk '{print "_"$1}')\ *.tar.xv" -mtime +90 -exec rm {} \;

  # Create backup file.
  if [ ! -f "$DEST" ]; then
   cd /tmp/nsbackup
   tar cfJ "$DEST" * && sync
   cd ..
  else
   echo "$DEST: Backup already exists..."
  fi

 fi

  # Unmount and remove backup directory.
  [ -d "/tmp/nsbackup" ] && { fusermount -u /tmp/nsbackup; }
  [ -d "/tmp/nsbackup" ] && { rmdir /tmp/nsbackup; }

done
tags: backup - netscaler - script