summaryrefslogtreecommitdiff
path: root/playbooks.d/backup-borg/playbook.bash
diff options
context:
space:
mode:
Diffstat (limited to 'playbooks.d/backup-borg/playbook.bash')
-rw-r--r--playbooks.d/backup-borg/playbook.bash63
1 files changed, 63 insertions, 0 deletions
diff --git a/playbooks.d/backup-borg/playbook.bash b/playbooks.d/backup-borg/playbook.bash
new file mode 100644
index 0000000..8f91f8a
--- /dev/null
+++ b/playbooks.d/backup-borg/playbook.bash
@@ -0,0 +1,63 @@
+#!/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"
+}
+
+playbook_del() {
+ pkg uninstall borgmatic
+ pkg uninstall borg
+ rm -f -- /usr/local/bin/sysbackup
+}