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