initial btrbk backup script
[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 conf_only=false
9 if [[ $1 == -c ]]; then
10 # mostly for testing
11 conf_only=true
12 fi
13
14 target_host=frodo
15 cat >/etc/btrbk.conf <<'EOF'
16 ssh_identity /root/.ssh/id_rsa
17 transaction_syslog daemon
18
19 # so we only run one at a time
20 lockfile /var/lock/btrbk.lock
21
22 # default format of short does not accomidate hourly preservation setting
23 timestamp_format long-iso
24 # background on timezones. with short/long, timestamps use local time.
25 # for long, if your local time moves backwards, by moving timezones or
26 # for an hour when daylight savings changes it, you will temporarily get
27 # a more aggressive retention policy for the overlapping period, and
28 # vice versa for the opposite timezone move. The alternative is using
29 # long-iso, which puts timezone info into the timestamp, which means
30 # that instead of shifting time, you shift the start of day/week/month
31 # which is used for retention to your new local time, which means for
32 # example, if you moved forward by 8 hours, the daily/weekly/monthly
33 # retention will be 8 hours more aggressive since midnight is at a new
34 # time, unless you fake the timzeone using the TZ env variable.
35 # However, in the short term, there will be no inconsistencies.
36 # I don't see any problem with shifting when the day starts for
37 # retention, so I'm using long-iso.
38
39 # only make a snapshot if things have changed
40 snapshot_create onchange
41 # much less snapshots because I have less space on the
42 # local filesystem.
43 snapshot_preserve 2h 2d
44
45 # so, total backups = ~89
46 target_preserve 48h 14d 8w 24m
47 target_preserve_min 6h
48
49 # if something fails and it's not obvious, try doing
50 # btrbk -l debug -v dryrun
51 EOF
52
53
54 case $HOSTNAME in
55 tp|x2)
56 if ! timeout -s 9 10 ssh frodo :; then
57 target_host=$HOME_DOMAIN
58 cat >>/etc/btrbk.conf <<EOF
59 ssh_port 2222
60 EOF
61 fi
62 ;;
63 esac
64
65 if [[ $HOSTNAME != frodo ]]; then
66 remote_target="target send-receive ssh://${target_host}/mnt/root/${HOSTNAME}-btrbk"
67 fi
68
69 target-section() {
70 root=$1
71 subvol=$2
72 mountpoint $root &>/dev/null || return
73 cat >>/etc/btrbk.conf <<EOF
74 volume $root
75 subvolume $subvol
76 $remote_target
77
78 EOF
79 }
80
81 target-section /mnt/iroot i
82 target-section /mnt/root q
83
84 if $conf_only; then
85 exit
86 fi
87
88 btrbk -q run