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