Scripts

Restoring a controller

The Restore.sh script restores a controller from a backup file. This script must have permissions set to 770 and be owned by the sdn user and sdn group:

#!/bin/bash

readonly OPT_ROOT="/opt/sdn"
readonly VAR_LIB_SDN="/var/lib/sdn"
readonly backupDir=${OPT_ROOT}"/backup"
readonly targetDir=${backupDir}"/tmp/com.hp.sdn.adm.backup.impl.BackupRestoreLegacyManager"
readonly configDir=${OPT_ROOT}"/config/"
readonly repoDir=${OPT_ROOT}"/virgo/repository/usr"
readonly backupFile=${backupDir}"/sdn_controller_backup*.zip"
readonly LOG_FILE=${backupDir}"/restore.log"
readonly INFO_FILE=${backupDir}"/info.bin"
readonly metricsDir=${OPT_ROOT}"/virgo/metrics"
WAIT_FOR_STOP=120

function restore_log {
    typeset script_name=${0##*/}

    typeset DATE_FORMAT=${DATE_FORMAT:-"+%b %e %H:%M:%S"}
    typeset LOG_PREFIX="$(whoami)@$(hostname)"

    echo "$(date "$DATE_FORMAT") $LOG_PREFIX $script_name[$$]: $*" >> $LOG_FILE
}

# For restore, clean virgo runtime environment
function clean_virgo_runtime {
    export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
    $OPT_ROOT/virgo/bin/startup.sh -clean -noStart
}

function get_sdnc_pid {
    echo $(ps -ef | grep -w "/bin/bash" | grep -w "/opt/sdn/admin/sdnc.sh" | head -n 1 | awk '{print $2}')
}

is_sdnc_running()
{
    pid='get_sdnc_pid'
    [ "x" = "x$pid" ] && return 1 || return 0
}

function restoreCassandraData {
    # 1. shutdown the node - done by stopping sdnc
    /opt/sdn/cassandra/bin/caServer.sh status
    if [ $? -eq 0 ]; then
        restore_log "Cassandra is still running, attempting stop..."
        sudo -u sdnadmin /opt/sdn/cassandra/bin/caServer.sh stop
        check_stop_and_exit $?
    fi

    #pick the cassandra zip file and unpack
    restore_log "Deleting cassandra configuration"
    if [ -d "$OPT_ROOT/cassandra/conf" ]; then 
        rm -rf "$OPT_ROOT/cassandra/conf/"
        check_and_exit $?
    fi

    restore_log "Unzipping the cassandra configuration"
    unzip -o $2 -d "$OPT_ROOT/cassandra/conf/"
    
     # 2. Clear all files in /var/lib/cassandra/commitlog
    restore_log "Deleting the commitlog directory"
    if [ -d "$OPT_ROOT/cassandra/commitlog/" ]; then
        rm -rf "$OPT_ROOT/cassandra/commitlog/"
        check_and_exit $?
    fi

    # 3. Delete system and other data directories
    restore_log "Deleting cassandra data directory"
    if [ -d "/var/lib/sdn/cassandra/" ]; then 
         rm -rf "/var/lib/sdn/cassandra/"
         check_and_exit $?
    fi

    # 4. pick the cassandra zip file and unpack
    restore_log "Unzipping the cassandra data directory"
    unzip -o $1 -d "/var/lib/sdn/cassandra/"    
    chmod -R g=u /var/lib/sdn/cassandra 
}

function restoreTeamConfig {
   # Restore the teaming config 
   unzip -o $1 -d "$VAR_LIB_SDN"
}

function check_stop_and_exit {
    OUT=$1
    if [[ $OUT -ne 0 && $OUT -ne 1 ]]; then
        restore_log "Stopping Cassandra failed and Restore failed : $OUT"
        rm $INFO_FILE
        exit 1
    fi    
}

function check_and_exit {
    OUT=$1
    if [ $OUT -ne 0 ]; then
        restore_log "Restore failed:$OUT"
        rm $INFO_FILE
        exit 1
    fi
}

function restorePostGre {
    restore_log "Restoring postgre database..."

    # unzip the zip of postgre
    unzip -o postgreSQLdata.zip -d $targetDir

    # delete the sdndb first and re-create it
    dropdb sdndb
    createdb -O sdn sdndb

    # remove the extra things that pg_dump back up
    sed -i '/REVOKE ALL ON SCHEMA public FROM PUBLIC/d' backupPG.sql
    sed -i '/REVOKE ALL ON SCHEMA public FROM postgres/d' backupPG.sql
    sed -i '/GRANT ALL ON SCHEMA public TO postgres/d' backupPG.sql
    sed -i '/GRANT ALL ON SCHEMA public TO PUBLIC/d' backupPG.sql

    # this assumes that sdndb database already exists
    psql sdndb < backupPG.sql
    check_and_exit $?
}

function restoreLicenseLogs {
    if [ -f licenselog.zip ] 
    then
        restore_log "Restoring license history logs..."

        # unzip the license logs
        unzip -o licenselog.zip -d /var/log/sdn/virgo/logs
        check_and_exit $?
    fi
}

function restoreMetricsData {
    if [ -f metricsData.zip ]
    then
        restore_log "Restoring metrics data..."

        # wipe out existing contents
        rm -rf $metricsDir/*

        # unzip the metrics data
        unzip -o metricsData.zip -d "$metricsDir" 
        check_and_exit $?
    fi
}

function command_exists {
    command -v $1 &> /dev/null;
}

function wait_for_sdnc_stop {
    restore_log "Waiting for SDNC to stop..."
    pid='get_sdnc_pid'
    if [[ -z $pid ]]; then return 0; fi

    for tries in 'seq $WAIT_FOR_STOP'; do
        sleep 1
        is_sdnc_running || return 0
    done

    return 1
}

# check if unzip is present or not
if command_exists unzip; then :
else
    restore_log "Unzip is not installed"
    restore_log "Please install unzip utility and try again"
    check_and_exit 1
fi

wait_for_sdnc_stop
check_and_exit $?

# extract the backup archive content
cd $targetDir

for file in 'ls -a *.*'
    do
        case $file in
            config.zip) 
                       restore_log "Restoring config files..."
                       unzip -o $file -d $configDir
                       check_and_exit $?
                       ;;
            teamConfig.zip)
                       restore_log "Restoring teaming Config files..."
                       restoreTeamConfig $file
                       check_and_exit $?
                       ;;
            userrepo.zip)
                       restore_log "Restoring user repository..."
                       rm -rf $repoDir/*
                       unzip -o $file -d $repoDir
                       check_and_exit $?
                       ;;
            cassandradata.zip)
                       restore_log "Restoring cassandra data files..."
                       restoreCassandraData $file cassandraconfig.zip
                       check_and_exit $?
                       ;;
            *)
                       ;;
        esac
    done

# restore postgre sql
restorePostGre

# restore licensing compliance history logs
restoreLicenseLogs

# restore metrics data
restoreMetricsData

# clean up virgo runtime environment
clean_virgo_runtime

# create links to /var/log/sdn/virgo
[ ! -L /opt/sdn/virgo/serviceability ] && ln -s /var/log/sdn/virgo /opt/sdn/virgo/serviceability

# Change permissions in case the user IDs have changed since the backup.
sudo chown -R sdn:sdn /opt/sdn /var/lib/sdn /var/log/sdn/virgo/logs/license-history.log

restore_log "Turning off the restore mode..."

# delete the restore.indicator file
[ -f /opt/sdn/backup/restore.indicator ] && rm /opt/sdn/backup/restore.indicator

restore_log "Restore done..."