wip using btrfs send for sync
[distro-setup] / btrbk-run
1 #!/bin/bash -l
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
8 usage() {
9 echo "top of script file:"
10 sed -n '1,/^[# ]*end command line/{p;b};q' "$0"
11 exit $1
12 }
13
14 conf_only=false
15 dry_run=false # mostly for testing
16
17 temp=$(getopt -l help,long-opt hcnt "$@") || usage 1
18 eval set -- "$temp"
19 while true; do
20 case $1 in
21 -c) conf_only=true; shift ;;
22 -n) dry_run=true; dry_run_arg=-n; shift ;;
23 -t) IFS=, targets=($2); shift 2 ;;
24 -h|--help) usage ;;
25 --) shift; break ;;
26 *) echo "$0: Internal error!" ; exit 1 ;;
27 esac
28 done
29 read primary <<<"$@"
30
31 ##### end command line parsing ########
32
33 sed="sed -r --follow-symlinks"
34 last_snaps=()
35
36 target-section() {
37 local root=$1
38 local subvol=$2
39 mountpoint $root &>/dev/null || return
40 cat >>/etc/btrbk.conf <<EOF
41 volume $root
42 subvolume $subvol
43 $remote_target
44
45 EOF
46 }
47
48 rsync-dirs() {
49 local host=$1
50 local path=$2
51 rsync $dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
52 }
53
54 last-snap() {
55 vol=${1##*/}
56 cd /mnt/root
57 last_snap=$(
58 for f in $vol.20*; do
59 printf "%s %s\n" $(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s) $f
60 done | sort -r | head -n 1 | awk '{print $2}'
61 )
62 last_snaps+=($last_snap)
63 }
64
65 # note q is owned by root:1000
66 # note p is owned 1000:1000 and chmod 700
67 mountpoints=(/q)
68 if mountpoint /p; then
69 mountpoints+=(/p)
70 fi
71
72 if [[ ! $targets ]]; then
73 case $HOSTNAME in
74 tp|x2)
75 if ! timeout -s 9 10 ssh frodo :; then
76 targets=($HOME_DOMAIN)
77 fi
78 ;;
79 esac
80 targets=(frodo)
81 fi
82
83
84 # umount first to ensure we don't have any errors
85 # todo: do some kill fuser stuff to make umount more reliable
86 # todo: setup sync systemd timer on $primary, once per hour.
87 # todo: setup lock so that if this is already running, we exit out, so
88 # that manual runs don't interfere with cronjobs.
89 if [[ $primary ]] && ! $dry_run; then
90 for m in ${mountpoints[@]}; do
91 # note, this won't work for /i, due to path being /mnt/iroot
92 # todo: include /i for treetowl/frodo
93 btrfs property set -ts /mnt/root$m ro true
94 ssh root@$primary bash <<EOF
95 set -ex
96 umount $m
97 [[ -e /mnt/root$m ]] || exit 0
98 btrfs sub del /mnt/root$m
99 EOF
100 done
101 fi
102
103 for tg in ${targets[@]}; do
104 cat >/etc/btrbk.conf <<'EOF'
105 ssh_identity /root/.ssh/id_rsa
106 transaction_syslog daemon
107
108 # so we only run one at a time
109 lockfile /var/lock/btrbk.lock
110
111 # default format of short does not accomidate hourly preservation setting
112 timestamp_format long-iso
113
114 # only make a snapshot if things have changed
115 snapshot_create onchange
116 # much less snapshots because I have less space on the
117 # local filesystem.
118 snapshot_preserve 2h 2d
119
120 # so, total backups = ~89
121 target_preserve 48h 14d 8w 24m
122 target_preserve_min 6h
123
124 # if something fails and it's not obvious, try doing
125 # btrbk -l debug -v dryrun
126 EOF
127
128 remote_target="target send-receive ssh://${tg}/mnt/root"
129
130 if [[ $tg == frodo && $HOSTNAME == treetowl ]]; then
131 target-section /mnt/iroot i
132 fi
133 for m in ${mountpoints[@]}; do
134 target-section /mnt/root ${m##*/}
135 done
136 done
137
138 if $conf_only; then
139 exit
140 fi
141
142 if $dry_run; then
143 btrbk -n run
144 else
145 btrbk -q run
146 fi
147
148 # if we have /p, rsync to targets without /p
149 if mountpoint /p; then
150 for tg in ${targets[@]}; do
151 case $tg in
152 tp|li|lk)
153 # todo, test this
154 for x in /p/c/machine_specific/*.hosts; do
155 if grep -qxF $tg $x; then
156 dir=${x%.hosts}
157 rsync-dirs ${dir##*/} $dir
158 fi
159 done
160 ;;
161 esac
162 done
163 fi
164
165 first_root=$(awk '$2 == "/mnt/root" {print $1}' /etc/mtab)
166
167 # make $primary have the rw snapshot
168 if [[ $primary ]] && ! $dry_run; then
169 fstab=()
170 for m in ${mountpoints[@]}; do
171 last-snap $m
172 fstab+=("$first_root $m btrfs noatime,subvol=$last_snap 0 0")
173 done
174
175 printf "%s\n" "${fstab[@]}" | cedit /etc/fstab
176 for d in ${mountpoints[@]}; do
177 mount $d
178 btrfs sub del /mnt/root$d
179 done
180 ssh root@primary bash -s "${mountpoints[*]}" "${last_snaps[*]}" <<'EOF'
181 set -xe
182 mountpoints=($1)
183 last_snaps=($2)
184 first_root=$(awk '$2 == "/mnt/root" {print $1}' /etc/mtab)
185 for ((i=0; i < ${#mountpoints[@]}; i++)); do
186 m=${mountpoints[i]}
187 vol=${m##*/}
188 fstab+=("$first_root $m btrfs noatime,subvol=$vol 0 0")
189 cd /mnt/root
190 btrfs sub snapshot ${last_snaps[i]} $vol
191 mount $m
192 done
193 EOF
194 fi
195
196
197 # background on btrbk timezones. with short/long, timestamps use local time.
198 # for long, if your local time moves backwards, by moving timezones or
199 # for an hour when daylight savings changes it, you will temporarily get
200 # a more aggressive retention policy for the overlapping period, and
201 # vice versa for the opposite timezone move. The alternative is using
202 # long-iso, which puts timezone info into the timestamp, which means
203 # that instead of shifting time, you shift the start of day/week/month
204 # which is used for retention to your new local time, which means for
205 # example, if you moved forward by 8 hours, the daily/weekly/monthly
206 # retention will be 8 hours more aggressive since midnight is at a new
207 # time, unless you fake the timzeone using the TZ env variable.
208 # However, in the short term, there will be no inconsistencies.
209 # I don't see any problem with shifting when the day starts for
210 # retention, so I'm using long-iso.