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