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