#!/bin/bash
# Script zum Aufheben eines borg backup repo-locks

# (c) 2011-2016,2020 Stefan Schaefer -- invis-server.org
# License GPLv3
# Version 0.10

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
# der GNU General Public License, wie von der Free Software Foundation,
# Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren
# veröffentlichten Version, weiterverbreiten und/oder modifizieren.

# Dieses Programm wird in der Hoffnung, dass es nützlich sein wird, aber
# OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
# Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
# Siehe die GNU General Public License für weitere Details.

# Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
# Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>

# Globale Einstellungen
confdir="/etc/invis"
passfile="$confdir/invis-pws.conf"

logfile="/var/log/bbunet.log"
datum=$(date "+%d.%m.%Y - %H:%M")

getconfvalue() {
    cat $3 | grep "$1:" | cut -d ":" -f "$2"
}

usage() {
    echo -e "\tusage:"
    echo -e "\tbbubreaklock net repo"
    echo -e "\toder:"
    echo -e "\tbbubreaklocak disk repo"
}

if (( $# != 2 )); then
    usage
    exit 1
fi

# Was soll bereinigt werden net oder disk
if [[ $1 == "net" || $1 == "disk" ]]; then
    target=$1
else
    usage
    exit 1
fi

# Testen, ob eine Datensicherung konfiguriert ist.
if [[ $target == "net" && ! -f "$confdir/bbunet.conf" ]]; then
    echo -e "\tEs ist keine Sicherung auf einen Backupserver eingerichtet."
    exit 1
elif [[ $target == "net" && -f "$confdir/bbunet.conf" ]]; then
    conffile="$confdir/bbunet.conf"
elif [[ $target == "disk" && ! -f "$confdir/bbudisk.conf" ]]; then
    echo -e "\tEs ist keine Sicherung auf einen Backupserver eingerichtet."
    exit 1
else
    conffile="$confdir/bbudisk.conf"
fi

# Volumes einlesen
sourcevolume=($(getconfvalue "sourceVolume" "2" "$conffile"))

# Pruefen, ob das angegebene Volume zur Sicherung konfiguriert ist
# Wenn ja, gehts weiter...
for vol in ${sourcevolume[@]}; do
    if [[ $2 == $vol ]]; then
	repo=$2
	break
    fi
done

# ...andernfalls nicht
if [[ -z $repo ]];then
    usage
    exit 1
fi

# Borg Repo-Passphrase als Umgebungsvariable exportieren
repopw=$(getconfvalue "borgRepoPass" "2" "$passfile")
# Repo-PW exportieren
export BORG_PASSPHRASE="$repopw"

# Bereinigung eines Netzwerkrepos
if [[ $target == "disk" ]]; then

    # Datensicherung verhindern / hier oder gesondert?
    # udbrestore

    # Dasiplatte mounten
    mount -o acl,user_xattr /dev/backup /mnt/udevsync

    targetdir=$(getconfvalue "targetDirUDEV" "2" "$conffile")

    datum=$(date "+%d.%m.%Y - %H:%M")
    echo "$datum - Blockade von Repository $repo wird aufgehoben." >> $logfile
    # Blockade aufheben
    borg -v break-lock $targetdir/$repo >> $logfile 2>&1

    # Kontrolldatei löschen
    rm -f $controldirectory/restore
    umount /mnt/udevsync
fi

# Bereinigung eines Diskrepos
if [[ $target == "net" ]]; then

    # Konfigurationen lesen
    targetuser=$(getconfvalue "buServerUser" "2" "$conffile")
    targetdir=$(getconfvalue "targetDir" "2" "$conffile")
    targethost=$(getconfvalue "targetHost" "2" "$conffile")
    sshport=$(getconfvalue "thSSHPort" "2" "$conffile")

    datum=$(date "+%d.%m.%Y - %H:%M")
    echo "$datum - Blockade von Repository $repo wird aufgehoben." >> $logfile
    # Blockade aufheben
    borg -v break-lock ssh://$targetuser@$targethost:$sshport/$targetdir/$repo >> $logfile 2>&1

fi
