lots of fixes, automation for bitfolk
[distro-setup] / bk-backup
1 #!/bin/bash
2
3 # usage: $0 [restore]
4
5 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
6 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
7 set -eE -o pipefail
8 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?. PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
9
10 # need root for rsync pull of file ownership/perms
11 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
12
13 host=bk.b8.nz
14 ret=0
15 source /a/bin/bash_unpublished/source-state
16
17 restore=false
18 case $1 in
19 restore)
20 restore=true
21 ;;
22 esac
23
24 # last checked 2022-03 version 23
25 # https://docs.nextcloud.com/server/latest/admin_manual/maintenance/restore.html
26 if $restore; then
27 set -x
28 for ncdir in /var/www/ncexpertpath /var/www/ncninja; do
29 ncbase=${ncdir##*/}
30 ssh root@$host sudo -u www-data php $ncdir/occ -q maintenance:mode --on ||: # might not be running
31 rsync -ravhi --numeric-ids /p/bkbackup/$ncbase/ root@$host:$ncdir || ret=$?
32 # https://docs.nextcloud.com/server/20/admin_manual/configuration_server/occ_command.html#maintenance-commands-label
33 ssh root@$host sudo -u www-data php $ncdir/occ -q maintenance:data-fingerprint
34 ssh root@$host sudo -u www-data php $ncdir/occ -q maintenance:mode --off
35 done
36 # the dovecot thing is not needed afaik, just a good practice.
37 ssh root@$host systemctl stop dovecot
38 rsync -ravi --numeric-ids /p/bkbackup/m root@$host:/
39 ssh root@$host systemctl start dovecot
40 exit 0
41 fi
42
43 ret=0
44 if [[ $HOSTNAME == $MAIL_HOST ]]; then
45 mkdir -p /p/bkbackup
46 for ncdir in /var/www/ncexpertpath /var/www/ncninja; do
47 ncbase=${ncdir##*/}
48 mkdir -p /p/bkbackup/$ncbase
49 ssh root@$host sudo -u www-data php $ncdir/occ -q maintenance:mode --on
50 rsync --numeric-ids -ra --delete root@$host:$ncdir/{config,data,themes} /p/bkbackup/$ncbase || ret=$?
51 ssh root@$host sudo -u www-data php $ncdir/occ -q maintenance:mode --off
52 if (( ret )); then
53 echo "$0: error: failed rsync $ncdir"
54 ret=1
55 fi
56 done
57 rsync --numeric-ids -ra --delete root@$host:/m /p/bkbackup
58 fi
59 exit $ret