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