#!/bin/bash # I, Ian Kelling, follow the GNU license recommendations at # https://www.gnu.org/licenses/license-recommendations.en.html. They # recommend that small programs, < 300 lines, be licensed under the # Apache License 2.0. This file contains or is part of one or more small # programs. If a small program grows beyond 300 lines, I plan to switch # its license to GPL. # Copyright 2024 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # usage: $0 [restore] if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4 set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?. PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR # need root for rsync pull of file ownership/perms [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@" host=bk.b8.nz ret=0 source /a/bin/bash_unpublished/source-state restore=false case $1 in restore) restore=true ;; esac # last checked 2022-03 version 23 # https://docs.nextcloud.com/server/latest/admin_manual/maintenance/restore.html if $restore; then set -x for ncdir in /var/www/ncexpertpath /var/www/ncninja; do ncbase=${ncdir##*/} ssh root@$host sudo -u www-data php $ncdir/occ -q maintenance:mode --on ||: # might not be running rsync -ravhi --numeric-ids /p/bkbackup/$ncbase/ root@$host:$ncdir || ret=$? # https://docs.nextcloud.com/server/20/admin_manual/configuration_server/occ_command.html#maintenance-commands-label ssh root@$host sudo -u www-data php $ncdir/occ -q maintenance:data-fingerprint ssh root@$host sudo -u www-data php $ncdir/occ -q maintenance:mode --off done # the dovecot thing is not needed afaik, just a good practice. ssh root@$host systemctl stop dovecot rsync -ravi --numeric-ids /p/bkbackup/m root@$host:/ ssh root@$host systemctl start dovecot exit 0 fi ret=0 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then mkdir -p /p/bkbackup for ncdir in /var/www/ncexpertpath /var/www/ncninja; do ncbase=${ncdir##*/} mkdir -p /p/bkbackup/$ncbase ssh root@$host sudo -u www-data php $ncdir/occ -q maintenance:mode --on rsync --numeric-ids -ra --delete root@$host:$ncdir/{config,data,themes} /p/bkbackup/$ncbase || ret=$? ssh root@$host sudo -u www-data php $ncdir/occ -q maintenance:mode --off if (( ret )); then echo "$0: error: failed rsync $ncdir" ret=1 fi done rsync --numeric-ids -ra --delete \ --exclude md/expertpathologyreview.com/testignore \ --exclude md/amnimal.ninja/testignore \ root@$host:/m /p/bkbackup fi exit $ret