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