#!/usr/bin/env bash # # This script is mostly included "as is", I did this quite a while ago and (also see todo remarks) # I'm not happy yet, but it works fine and the core mechanisms are swell. # # AUTHOR: coding@lirion.de # LICENCE: LGPL-3.0-only # # shellcheck disable=SC1091 source /etc/lirion/nextcloud.conf || exit 1 # shellcheck disable=SC1091 source /usr/lib/lirion/ln-initfunctions || exit 2 # TODO: Parametrise file directory and pw acquisition - better design and more secure BDBDIR="${NCDBBKPDIR}/files" PWFL="${NCDBBKPDIR}/pw" TIME="$(date +"%Y-%m-%dT%H:%M:%S%z")" ######### # MYSQL # ######### DUMPOPTS=( "--add-drop-table" "--add-locks" ) DUMPOPTS+=( "--complete-insert" "--create-options" "--lock-tables" ) printf "File timestamp: %s\n" "$TIME" DBARR=( "$NCDBNAME" ) for i in "${DBARR[@]}";do lnbegin "DB backup: $i" sleep 0.1337 lnprog "dump" if ! mysqldump "${DUMPOPTS[@]}" -u root --password="$(cat "$PWFL")" --databases \ "$i" > "$BDBDIR/$i-$TIME.sql" 2>/dev/null; then lnfail exit 120 fi sleep 0.1337 lnprog "compression" if ! xz -T4 "$BDBDIR/$i-$TIME.sql" > /dev/null 2>&1 && chmod 0600 \ "$BDBDIR/$i-$TIME.sql.xz" 2>/dev/null; then lnfail exit 121 fi sleep 0.1337 lnok done NCVER="$(/usr/bin/sudo -u "$NCUSER" "$NCPHPBIN" "${NCAPPLDIR}/occ" 'status')" || exit 122 NCVER="$(/usr/bin/printf '%b' "$NCVER" | /usr/bin/grep 'version:' | /usr/bin/awk '{print $NF}')" || exit 123 /usr/bin/printf 'Current version: %b\n' "$NCVER" if [ "$(printf '%b' "$NCVER" | wc -l)" -gt 0 ]; then printf 'Not a single line: %b\n' "$NCVER" >&2 exit 124 elif [ "$(printf '%b' "$NCVER" | wc -w)" -ne 1 ]; then printf 'Not a single word: %b\n' "$NCVER" >&2 exit 125 fi lnbegin "File backup: Nextcloud" sudo rm -f "${NCAPPLDIR}/../nextcloud-$(date -I)-${NCVER}.tar" \ "${NCAPPLDIR}/../nextcloud-$(date -I)-${NCVER}.tar.xz" || exit 126 sleep 0.1337 lnprog 'dump' if ! tar -C "${NCAPPLDIR}/.." --checkpoint=32768 --checkpoint-action='.' -cf \ "${NCAPPLDIR}/../nextcloud-$(date -I)-${NCVER}.tar" "$NCAPPLDIR" 2>/dev/null then lnfail exit 127 fi lnprog 'compression' if ! xz -T4 "${NCAPPLDIR}/../nextcloud-$(date -I)-${NCVER}.tar" > /dev/null 2>&1 && \ chmod 0600 "${NCAPPLDIR}/../nextcloud-$(date -I)-${NCVER}.tar.xz" 2>/dev/null then lnfail exit 128 fi lnok