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