summaryrefslogtreecommitdiff
path: root/playbooks.d/backup-borg/playbook.bash
blob: b2149e0ea9f3ad736501302c8bf6cd9f56c7210b (plain)
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
#!/usr/bin/env bash

playbook_add() {
	# TODO: Abort if backup.key is not set yet

	pkg install borg
	pkg install borgmatic

	# Create a `borg1` for compatability with rsync.net
	ln -s /usr/bin/borg /usr/local/bin/borg1

	playbook_sync

	info "$BASHTARD_PLAYBOOK" "Don't forget to add \`sysbackup\` to your crontab!"
}

playbook_sync() {
	local config

	config="$(getent passwd "$(config "backup.user")" | awk -F: '{ print $6 }')/.config/borgmatic/config.yaml"

	mkdir -pv -- "$(dirname "$config")"

	notice "$BASHTARD_PLAYBOOK" "Installing backup script"
	file_template "backup.bash" \
		> /usr/local/bin/sysbackup
	chmod +x /usr/local/bin/sysbackup

	# This is not going to be pretty, generating a YAML document from
	# straight Bash, but the simple sed-based templating in Bashtard
	# doesn't do loops, so this is the best I can do here.
	notice "$BASHTARD_PLAYBOOK" "Generating borgmatic configuration"
	printf "location:\n" > "$config"
	printf "  source_directories:\n" >> "$config"

	while read -r cpath
	do
		printf '  - "%s"\n' "$(config "$cpath")" >> "$config"
	done < <(config_subkeys "backup.fs.paths")

	printf "  repositories:\n" >> "$config"

	while read -r cremote
	do
		printf '  - "%s"\n' "$(config "$cremote")" >> "$config"
	done < <(config_subkeys "backup.fs.remotes")

	printf "  one_file_system: true\n" >> "$config"
	printf "  remote_path: borg1\n" >> "$config" # rsync.net wont work without this
	printf "storage:\n" >> "$config"
	printf "  encryption_passphrase: \"%s\"\n" "$(config "backup.key")" >> "$config"
	printf "retention:\n" >> "$config"
	printf "  keep_daily: 7\n" >> "$config"
	printf "  keep_weekly: 4\n" >> "$config"
	printf "  keep_monthly: 6\n" >> "$config"
	printf "  keep_yearly: 1\n" >> "$config"

	chmod 600 "$config"
}

playbook_del() {
	pkg uninstall borgmatic
	pkg uninstall borg
	rm -f -- /usr/local/bin/sysbackup
}