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