Shellscript config file

Vor einiger Zeit benötigte ich die Möglichkeit für ein rsync Backupscript eine Konfiguration zu hinterlegen. Leider bietet die Shell da wenig Möglichkeiten also muss man selbst den Handwerker Sysadmin raushängen lassen.

/opt/backup/config.cfg

system-a {
  IP="10.8.0.1"
  FOLDERS=( \
    /etc \
    /opt \
    /var/www \
  )
  EXCLUDES=( \
    /opt/backup \
  )
}

/opt/backup/doBackup.sh

#!/usr/bin/env bash

# Sections in Configfile to Backup
BACKUPS=(
 system-a \
)

function readconf() {
    match=0
    while read line; do
        # skip comments
        [[ ${line:0:1} == "#" ]] && continue

        # skip empty lines
        [[ -z "$line" ]] && continue

        # still no match? lets check again
        if [ $match == 0 ]; then

            # do we have an opening tag ?
            if [[ ${line:$((${#line}-1))} == "{" ]]; then

                # strip "{"
                group=${line:0:$((${#line}-1))}
                # strip whitespace
                group=${group// /}

                # do we have a match ?
                if [[ "$group" == "$1" ]]; then
                    match=1
                    continue
                fi

                continue
            fi

        # found closing tag after config was read - exit loop
        elif [[ ${line:0} == "}" && $match == 1 ]]; then
            break

        # got a config line eval it
        else
            eval $line
        fi

    done < "$CONFIG"
}


CONFIG="/opt/backup/config.cfg"
for SERVER in ${BACKUPS[@]}
do
    readconf $SERVER
    EXCLUDE=""
    if [ ! -d "./servers/$IP" ]; then
        mkdir -p "./servers/$IP"
    fi
    for EX in ${EXCLUDES[@]}
    do
       EXCLUDE="${EXCLUDE} --exclude=${EX}"
    done

    for DIR in ${FOLDERS[@]}
    do
	rsync $EXCLUDE -avbuR $IP:${DIR} /opt/backup/servers/$IP
    done
done

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.