build 65d0a3ad | content blog-content@c8490fa · 338 posts | profiles 20 · corpus 228 | 0 skipped |
Scripting · 2017-07-20

VCSA Backup & Restore automatisieren

In den VMware Communities hatte jemand Mühe, die Datenbank der VCSA zu sichern. Hier ist das Wrapper-Script, das ich um die beiden Python-Scripts von VMware herum geschrieben habe.

2017-07-20Datum
Dario DörflingerAutor
7Min. Lesezeit
1354words
no translation reviewed
Themen capabilities · idf weight Automation automation 1.80
Hersteller vendors · idf weight VMware vmware 0.89

Dieser Beitrag ist von 2017. Er bleibt online, weil er nach wie vor nachgefragt wird, beschreibt aber einen Produktstand von damals.

Ich bin kürzlich über einen Thread in den VMware Communities gestolpert, in dem jemand Mühe hatte, die Datenbank der VCSA zu sichern. Er nutzte das Script von jemand anderem, deshalb teile ich hier das Wrapper-Script, das ich um die beiden Python-Scripts herum geschrieben habe, die VMware in KB 2091961 veröffentlicht hat.

Update: Dieses Script liegt inzwischen auf GitHub.

Das Script

#!/bin/bash

#***************************************************************
# Get Options from the command line
#***************************************************************
while getopts "b:r:h" options
do
case $options in
                b ) opt_b=$OPTARG;;
                r ) opt_r=$OPTARG;;
                h ) opt_h=1;;
                \? ) opt_h=1;;
esac
done

###############################################
# bkp_rst.sh
#=============================================
# Name: bkp_rst.sh
# Datum: 24.07.2017
# Ziel: Mache ein Backup der vCenter Appliance Datenbank, Restore die Datenbank
#
# Autor: Dario aka virtualFrog
version="1.5"
#
# Changelog:
# 0.1                   dario           Initial-Setup
# 1.0                   dario           Testing, Testing, Testing
# 1.1                   dario           Added dynamic filename & cleanUp Function
# 1.2                   dario           Added local cleanup
# 1.3                   dario           Changed local cleanup to delete all but most recent 3 files
# 1.4                   dario           Added checkForErrors function and writeMail function
# 1.5                   dario           Added CheckForErrors before every exit statement

# Variablen
# ---------

#log-Variablen
log=/var/log/vmware_backup_restore.log

#Mail variabeln
recipients="test@gmail.com,test3@gmail.com"

#NFS-Parameter
nfsmount="vcsa"
nfsserver=""
nfsoptions="nfs4"

# Funktionen
# ----------

function print_banner
{
        echo "

___  __ ____   ___________
\  \/ // ___\ /  ___/\__  \

 \   /\  \___ \___ \  / __ \_
  \_/  \___  >____  >(____  /
           \/     \/      \/
                            "
echo "v$version"
}

function get_parameter
{
        echo "GET_PARAMETER"

        if [ $opt_h ]; then print_help
        fi

        #--------------------------------------
        # Check to see if -b was passed in
        #--------------------------------------
        if [ $opt_b ]; then
                backup_string=$opt_b
                echo -e "\tBackup: $backup_string"
                echo "Backup: $backup_string" >> $log
                echo ""
                validate_backup_path
                filename=`hostname`-`date +%Y-%d-%m:%H:%M:%S`.bak
                todo="backup"
                echo ""
        elif [ $opt_r ]; then
                restore_string=$opt_r
                echo -e "\tRestore: $restore_string"
                echo "Restore: $restore_string" >> $log
                echo ""
                validate_restore_path
                todo="restore"
                echo ""
        else
                echo "warning: no parameters supplied." >> $log
                echo -e "\tKeine Parameter gefunden."
                print_help
        fi

}

function print_help
{
        echo "Gueltige Parameter: -b  -r "
        echo ""
        echo "To restore you should mount the volume ($nfsmount) on ($nfsserver) which this script copies the backups to"
        exit 0
}

function init_log
{

                echo `date` > $log
                echo "fzag_bkp_rst.sh in der Version $version auf `hostname -f`" >> $log
                echo "____________________________________________" >> $log

                #echo "Nun startet das Script in der Version $version"
                echo ""
}
function validate_backup_path
{
        echo "VALIDATE BACKUP PATH"
        echo -e "\tChecking Backup Path: $backup_string"
        if [ ! -d "$backup_string" ]; then
                echo -e "\tDirectory did not exist. Creating.."
                mkdir -p $backup_string
                echo -e "\tDirectory created."
        else
                echo -e "\tDirectory does exist."
        fi
        echo ""
}

function validate_restore_path
{
        echo "VALIDATE RESTORE PATH"
        echo -e "\tChecking Backup Path: $restore_string"
        if [ -f $restore_string ];
        then
                echo -e "\tFile $restore_string exists."
                echo "File $restore_string exists." >> $log
        else
                echo -e "\tFile $restore_string does not exist. Exiting"
                echo "error: $restore_string does not exist." >> $log
                checkForErrors
                exit 1
        fi
        echo ""
}
function check_python_scripts
{
        echo "CHECK PYTHON SCRIPTS"
        echo -e "\tChecking if Python Scripts are available"
        DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
        if [ -f "$DIR/backup_lin.py" ]; then
                echo -e "\tBackup Script found, continue.."
                echo "script for backup found" >> $log
                if [ -f "$DIR/restore_lin.py" ]; then
                        echo -e "\tRestore Script found, continue.."
                        echo "script for restore found" >> $log
                else
                        echo -e "\tNo restore script found, exit"
                        echo "error: no python restore script found" >> $log
                        checkForErrors
                        exit 1
                fi
        else
                echo -e "\tNo backup script found, exit"
                echo "error: no python backup scripts found" >> $log
                checkForErrors
                exit 1
        fi
        echo ""
}

function create_backup
{
        echo "CREATE BACKUP"
        echo -e "\tCreating the Backup in the file $backup_string/bk.bak:"
        DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

        echo `python $DIR/backup_lin.py -f $backup_string/$filename` >> $log
        if [ $? -ne 0 ]; then
                echo -e "\tError: Return code from backup job was not 0"
                echo "error: return code from backup job was not 0" >> $log
                checkForErrors
                exit 1
        else
                if [ -f "$backup_string/$filename" ]; then
                        echo -e "\tSuccess! Backup created."
                        echo "success: backup created" >> $log
                fi
        fi
        echo ""
}

function restore_db
{
        echo "RESTORE DB"
        echo -e "\tRestoring Database with supplied Backup-File: $restore_string"
        DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
        echo `python $DIR/restore_lin.py -f $restore_string` >> $log
        if [ $? -ne 0 ]; then
                echo -e "\tError: Return code from restore job was not 0"
                echo "error: return code from restore job was not 0" >> $log
        fi
        echo ""

}

function mountNFS
{
        echo "MOUNT NFS"
        echo -e "\tMounting the given NFS Volume ($nfsmount) on server ($nfsserver)"
        if [[ ! -d /mnt/bkp ]]; then
                mkdir -p /mnt/bkp
        fi

        mount -t $nfsoptions $nfsserver:$nfsmount /mnt/bkp
        if [[ $? -ne 0 ]]; then
                echo -e "\tMount was not successful. Abort the operation"
                echo "mount was not successful" >> $log
                checkForErrors
                exit 1
        fi
        echo ""
        return 0
}

function copyBackup
{
        echo "COPY BACKUP"
        echo -e "\tCopying Backup to NFS Mount"
        cp $backup_string/$filename /mnt/bkp/
        if [[ $? -ne 0 ]]; then
                echo -e "\tCopy Operation was not successful. Abort the operation"
                echo "copy was not successful" >> $log
                checkForErrors
                exit 1
        fi
        echo ""
}

function unMountNFS
{
        echo "UMOUNT NFS"
        echo -e "\tUnmounting the NFS Mount"
        umount /mnt/bkp
        if [[ $? -ne 0 ]]; then
                echo -e "\tumount Operation was not successful."
                echo "umount was not successful" >> $log
        fi
        echo ""
}

function cleanUp
{
        echo "CLEAN UP"
        echo -e "\tCleaning up Backups older than 4 days and all local files but the 3 most recent"

        find /mnt/bkp -mtime +4 -exec rm -rf {} \;
        if [[ $? -ne 0 ]]; then
                echo -e "\tNFS cleanUp Operation was not successful."
                echo "NFS cleanUp was not successful" >> $log
        fi

        cd $backup_string
        ls -t1 $backup_string |tail -n +3 | xargs rm
        if [[ $? -ne 0 ]]; then
                echo -e "\tLocal cleanUp Operation was not successful."
                echo "local cleanUp was not successful" >> $log
        fi
        echo ""
}

function writeEmail
{
        echo "WRITE EMAIL"
        echo -e "\tSending the logfile as a mail to $recipients"
        subject="VCSA Backup has run into a error"
        from="root@`hostname -f`"
        body=`cat $log`

        /usr/sbin/sendmail "$recipients" <> $log
        service vmware-vdcs stop >> $log
        restore_db
        service-control --start vmware-vpxd >> $log
        service-control --start vmware-vdcs >> $log
        unMountNFS
        checkForErrors
fi

Voraussetzungen

Das Script erwartet die Python-Scripts aus KB 2091961 im selben Verzeichnis. Fehlen sie, bricht es ab.

Verwendung

Auch wenn ich mir die Mühe gemacht habe, eine Syntax-Hilfe einzubauen, das Script zeigt dir also ohne Parameter, welche du verwenden sollst, sage ich hier genau, wie du es benutzt.

Als Erstes trägst du die Variablen für den NFS Server und den Mount ein, auf dem das Backup landen soll:

#NFS-Parameter
nfsmount="vcsa"
nfsserver="192.168.1.1"
nfsoptions="nfs4"

Backup

./bkp_rst_1.2.sh -b /tmp/backup

Das legt das Verzeichnis an, falls es nicht existiert, und behält einige Backups lokal. Eine Aufräumfunktion löscht alles, was älter als vier Tage ist.

Restore

./bkp_rst_1.2.sh -r /tmp/backup/bk.bak

Für einen Restore gibst du die Datei an, aus der wiederhergestellt werden soll. Liegt sie auf dem NFS Server, sagt dir das Script ohne Parameter, welcher NFS Server und welcher Mount hinterlegt sind.

Crontab

Das Script ist als Cronjob gedacht. Nimm also «crontab -e» und füge diese Zeile hinzu:

0 * * * * /bin/bash /usr/bin/bkp_rst_1.2.sh -b /tmp/backup

Achtung bei vSphere 6.5:
In der VCSA 6.5 GA gibt es einen Fehler, der Cron am Ausführen von Scripts hindert.
Du musst die Datei «/etc/pam.d/crond» anpassen: Ändere die drei Verweise auf «password-auth» in «system-auth», dann läuft es. Ich hoffe, das wird in Update 1 behoben.

Update: In vSphere 6.5 Update 1 GA ist es nicht behoben, vielleicht kommt der Fix in einem Patch.

Wenn du dabei Hilfe brauchst, melde dich auf Twitter oder schreib einen Kommentar.

So sieht die Ausgabe aus, wenn du das Script von Hand startest:
Screen Shot 2017-07-20 at 10.18.06

Und so sieht sie ohne Parameter aus:
Screen Shot 2017-07-20 at 10.18.24

Update

Ich habe das Script eben auf Version 1.5 gehoben. Es räumt lokal besser auf, behält also nur die drei neuesten Dateien, prüft auf Fehler und schickt eine Mail, wenn im Log ein Fehler steht.

Passt ausserdem