various fixes, add emacs wrappers
[distro-setup] / btrbk-run
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 set -eE -o pipefail
17 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
18
19 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
20 usage() {
21 echo "top of script file:"
22 sed -n '1,/^[# ]*end command line/{p;b};q' "$0"
23 exit $1
24 }
25
26 script_dir=$(dirname $(readlink -f "$BASH_SOURCE"))
27
28 # todo: finish figuring out fai / distro-setup
29 # initial fstab / subvol setup.
30
31 conf_only=false
32 dry_run=false # mostly for testing
33 resume_arg=
34
35 temp=$(getopt -l help hcnprt: "$@") || usage 1
36 eval set -- "$temp"
37 while true; do
38 case $1 in
39 -c) conf_only=true; shift ;;
40 -n) dry_run=true; dry_run_arg=-n; shift ;;
41 -p) progress_arg="--progress"; shift ;;
42 # btrbk arg: Resume only. Skips snapshot creation.
43 -r) resume_arg=-r; shift ;;
44 -t) IFS=, targets=($2); unset IFS; shift 2 ;;
45 -h|--help) usage ;;
46 --) shift; break ;;
47 *) echo "$0: Internal error!" ; exit 1 ;;
48 esac
49 done
50 read primary <<<"$@"
51
52 ##### end command line parsing ########
53
54 rsync-dirs() {
55 local host=$1
56 local path=$2
57 m rsync $dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
58 }
59
60 vol-conf() {
61 cat >>/etc/btrbk.conf <<EOF
62 volume $vol
63 EOF
64 }
65 sub-conf() {
66 cat >>/etc/btrbk.conf <<EOF
67 subvolume $sub
68 EOF
69 }
70 tg-conf() {
71 cat >>/etc/btrbk.conf <<EOF
72 target send-receive ssh://$tg$vol/btrbk
73 EOF
74 }
75 m() { printf "%s: %s\n" "${0##*/}" "$*"; "$@"; }
76
77
78 if ! which btrbk &>/dev/null; then
79 echo "$0: error: no btrbk binary found"
80 fi
81
82 cat >/etc/btrbk.conf <<'EOF'
83 ssh_identity /root/.ssh/id_rsa
84 # Just a guess that local7 is a good facility to pick.
85 # It's a bit odd that the transaction log has to be logged to
86 # a file or syslog, while other output is sent to std out.
87 # The man does not mention a way for them to be together, but
88 # I dunno if setting a log level like warn might also output
89 # transaction info.
90 transaction_syslog local7
91
92 # so we only run one at a time
93 lockfile /var/lock/btrbk.lock
94
95 # default format of short does not accomidate hourly preservation setting
96 timestamp_format long-iso
97
98 # only make a snapshot if things have changed
99 snapshot_create onchange
100 # much less snapshots because I have less space on the
101 # local filesystem.
102 #snapshot_preserve 2h 2d
103 # for now, keeping them equal for simplicity sake
104 snapshot_preserve 48h 14d 8w 24m
105 snapshot_preserve_min 6h
106 snapshot_dir btrbk
107
108 # so, total backups = ~89
109 target_preserve 48h 14d 8w 24m
110 target_preserve_min 6h
111
112 # if something fails and it's not obvious, try doing
113 # btrbk -l debug -v dryrun
114 EOF
115
116 # note q is owned by root:1000
117 # note p is owned 1000:1000 and chmod 700
118 mountpoints=(/a)
119 qmnt=/q
120 if awk '{print $2}' /etc/fstab | grep -xF $qmnt &>/dev/null; then
121 mountpoints+=($qmnt)
122 fi
123
124 # if our mountpoints are from stale snapshots,
125 # it doesn't make sense to do a backup.
126 check-subvol-stale ${mountpoints[@]} || exit 1
127
128 if [[ ! $targets ]]; then
129 case $HOSTNAME in
130 tp|x2)
131 if ! timeout -s 9 10 ssh frodo :; then
132 targets=($HOME_DOMAIN)
133 fi
134 ;;
135 treetowl)
136 targets=(frodo)
137 if timeout -s 9 10 ssh x2 :; then
138 targets+=(x2)
139 fi
140 ;;
141 *)
142 targets=(frodo)
143 ;;
144 esac
145 echo "targets: ${targets[*]}"
146 fi
147
148
149 # for i, we just do a 1 way sync from master to backup,
150 # and manually manage any changes to that.
151 do_i=false
152 for tg in ${targets[@]}; do
153 # for an initial run, btrbk requires the dir to exist
154 ssh root@$tg mkdir -p /mnt/root/btrbk
155 if [[ $tg == frodo && $HOSTNAME == treetowl ]]; then
156 do_i=true
157 fi
158 done
159
160
161 vol=/mnt/root
162 vol-conf
163 for m in ${mountpoints[@]}; do
164 sub=${m##*/}
165 sub-conf
166 for tg in ${targets[@]}; do
167 tg-conf
168 done
169 done
170
171 if $do_i; then
172 vol=/mnt/iroot
173 vol-conf
174 sub=i
175 sub-conf
176 tg=frodo
177 vol=/mnt/root
178 tg-conf
179 fi
180
181
182
183 # todo: umount first to ensure we don't have any errors
184 # todo: do some kill fuser stuff to make umount more reliable
185 # todo: run this on a systemd timer on $primary, once per hour,
186 # and if primary is, change that timer over to primary, and make
187 # sure we mount the latest
188
189
190
191 if $conf_only; then
192 exit
193 fi
194
195 if $dry_run; then
196 m btrbk -n $resume_arg run
197 else
198 # -q and just using the syslog option seemed nice,
199 # but it doesn't show when a send has a parent and when it doesn't.
200 m btrbk $progress_arg $resume_arg run
201 fi
202
203 # if we have /p, rsync to targets without /p
204 if mountpoint /p >/dev/null; then
205 for tg in ${targets[@]}; do
206 case $tg in
207 tp|li|lk)
208 for x in /p/c/machine_specific/*.hosts; do
209 if grep -qxF $tg $x; then
210 dir=${x%.hosts}
211 rsync-dirs $tg $dir
212 fi
213 done
214 ;;
215 esac
216 done
217 fi
218
219 if ! $dry_run; then
220 m $script_dir/mount-latest-remote ${targets[@]}
221 fi
222
223
224 # todo: move variable data we don't care about backing up
225 # to /nocow and symlink it.
226
227
228 # background on btrbk timezones. with short/long, timestamps use local time.
229 # for long, if your local time moves backwards, by moving timezones or
230 # for an hour when daylight savings changes it, you will temporarily get
231 # a more aggressive retention policy for the overlapping period, and
232 # vice versa for the opposite timezone move. The alternative is using
233 # long-iso, which puts timezone info into the timestamp, which means
234 # that instead of shifting time, you shift the start of day/week/month
235 # which is used for retention to your new local time, which means for
236 # example, if you moved forward by 8 hours, the daily/weekly/monthly
237 # retention will be 8 hours more aggressive since midnight is at a new
238 # time, unless you fake the timzeone using the TZ env variable.
239 # However, in the short term, there will be no inconsistencies.
240 # I don't see any problem with shifting when the day starts for
241 # retention, so I'm using long-iso.
242
243 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z