todo comment
[distro-setup] / btrbkr2
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4 # SPDX-License-Identifier: GPL-3.0-or-later
5 # SPDX-License-Identifier: Apache-2.0
6
7 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
8 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
9 set -eE -o pipefail
10 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
11 # alternatively, using https://iankelling.org/git/?p=errhandle;a=tree
12 # source /path/errhandle/err
13 # on my machine
14 source /a/bin/errhandle/err
15
16 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
17
18 usage() {
19 cat <<EOF
20 Usage: ${0##*/} TARGET_HOST
21 Send btrbk for root2
22
23
24 -h|--help Print help and exit.
25
26 Note: Uses util-linux getopt option parsing: spaces between args and
27 options, short options can be combined, options before args.
28 EOF
29 exit $1
30 }
31
32 ##### begin command line parsing ########
33
34 # ensure we can handle args with spaces or empty.
35 ret=0; getopt -T || ret=$?
36 [[ $ret == 4 ]] || { echo "Install util-linux for enhanced getopt" >&2; exit 1; }
37
38 temp=$(getopt -l help h "$@") || usage 1
39 eval set -- "$temp"
40 while true; do
41 case $1 in
42 -h|--help) usage ;;
43 --) shift; break ;;
44 *) echo "$0: unexpected args: $*" >&2 ; usage 1 ;;
45 esac
46 shift
47 done
48
49 read -r target <<<"$@"
50
51 if [[ ! $target ]]; then
52 echo $0: error: specify target
53 exit 1
54 fi
55
56 cat >/etc/btrbk/root2.conf <<EOF
57
58 ssh_identity /root/.ssh/home
59 # Just a guess that local7 is a good facility to pick.
60 # It's a bit odd that the transaction log has to be logged to
61 # a file or syslog, while other output is sent to std out.
62 # The man does not mention a way for them to be together, but
63 # I dunno if setting a log level like warn might also output
64 # transaction info.
65 transaction_syslog local7
66
67 # trying this out
68 stream_compress zstd
69
70 archive_preserve_min latest
71
72 # so we only run one at a time
73 lockfile /var/lock/btrbkroot2.lock
74
75 # default format of short does not accomidate hourly preservation setting
76 timestamp_format long-iso
77
78 # dont make new snapshot, we only receive new snapshots
79 snapshot_create no
80
81 # if something fails and it's not obvious, try doing
82 # btrbk -l debug -v dryrun
83
84 rate_limit no
85 volume /mnt/r7/amy
86 subvolume root_ubuntubionic
87 target send-receive ssh://$target/mnt/root2/btrbk
88 volume /mnt/r7/amy
89 subvolume boot_ubuntubionic
90 target send-receive ssh://$target/mnt/boot2/btrbk
91
92 EOF
93
94 tmpconf=$(mktemp -d)/b.conf
95
96 timeout -s 9 6 ssh root@$target mkdir -p /mnt/{b,r}oot2/btrbk
97
98 exclude=root
99 for base in boot root; do
100 cat - /etc/btrbk/root2.conf >$tmpconf <<<"archive_exclude ${exclude}_ubuntubionic"
101 btrbk -c $tmpconf archive /mnt/r7/amy/btrbk ssh://$target/mnt/${base}2/btrbk
102 # swaps the vars
103 exclude=$base
104 done