#!/bin/sh
#
# @author Thomas Waser <thomas.waser@libelektra.org>
# @brief Backups the current KDB.
# @date 14.08.2017
# @tags backup, stash, helper

if [ -z "$KDB" ];
then
    KDB=kdb
fi

TS="$(date +%s)"
KDBSYSTEM="$($KDB file system)"
KDBUSER="$($KDB file user)"
KDBSPEC="$($KDB file spec)"
KDBINIT="/etc/kdb/elektra.ecf"

TMPDIR="/var/tmp/elektra_backup/${TS}"

mkdir -p "$TMPDIR" 2>/dev/null
cp -a "$KDBSYSTEM" "${TMPDIR}/system_`basename $KDBSYSTEM`" 2>/dev/null
cp -a "$KDBUSER" "${TMPDIR}/user_`basename $KDBUSER`" 2>/dev/null
cp -a "$KDBSPEC" "${TMPDIR}/spec_`basename $KDBSPEC`" 2>/dev/null
cp -a "$KDBINIT" "${TMPDIR}/elektra.ecf" 2>/dev/null

echo "${KDBINIT}=elektra.ecf" > "${TMPDIR}/info"
echo "${KDBSYSTEM}=system_`basename $KDBSYSTEM`" >> "${TMPDIR}/info"
echo "${KDBUSER}=user_`basename $KDBUSER`" >> "${TMPDIR}/info"
echo "${KDBSPEC}=spec_`basename $KDBSPEC`" >> "${TMPDIR}/info"

echo "restore with \"kdb restore $TS\""
