aboutsummaryrefslogtreecommitdiffstats
path: root/lifeboat
blob: 4ce396c248e16d42d6bcb1e16962a7dedf3be00c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/sh

set -e

LIFEBOAT_ROOT=${LIFEBOAT_ROOT:-"/srv/backup/lifeboat"}

errx() {
	printf "lifeboat: %s\n" "$1" >&2
	exit 1
}

usage() {
	printf "usage: lifeboat repo [run|restic-cmd]\n"
	exit 0
}

run_backup() {
	(
		cd "$LIFEBOAT_ROOT/$repo"

		for i in run retain clean sync; do
			if test -x $i; then
				./$i
			else
				${i}_default;
			fi
		done
	)
}

run_default() {
	restic-priv backup --files-from config/include --exclude-file config/exclude --exclude-caches
}

retain_default() {
	restic forget --keep-daily 7 --keep-weekly 5 --keep-monthly 12 --keep-yearly 2
}

clean_default() {
	restic check
	restic prune
}

sync_default() {
	if ! test -r config/sync; then
		return
	fi

	while read -r target_spec; do
		target=$(echo "$target_spec" | cut -f1)
		limit=$(echo "$target_spec" | cut -sf2)

		if test -n "$limit"; then
			if test "$(du -s "$RESTIC_REPOSITORY" | cut -f1)" -gt "$limit"; then
				printf "Warning: size threshold exceeded, skipping sync to %s\n" "$target/$LIFEBOAT_REPO_NAME"
			else
				sync_single "$target"
			fi
		else
			sync_single "$target"
		fi
	done < config/sync
}

sync_single() {
	rclone sync --transfers 20 --fast-list "$RESTIC_REPOSITORY" "$1/$LIFEBOAT_REPO_NAME"
}

test $# -lt 2 && usage

repo=$1
shift

test -d "$LIFEBOAT_ROOT/$repo" || errx "$repo: no such repository"

export PATH="$HOME/bin:$PATH"
export LIFEBOAT_REPO_NAME=$repo
export RESTIC_REPOSITORY=$LIFEBOAT_ROOT/$repo/repo
export RESTIC_PASSWORD_FILE=$LIFEBOAT_ROOT/$repo/credentials/restic

case "$1" in
	run) run_backup;;
	*) restic "$@";;
esac