From 81705dbdd023f896a447a43c8decc64c3aa1e776 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Wed, 17 Jun 2020 04:20:50 +0000
Subject: [PATCH] Added scripts to backup system with borg

---
 system_backup.sh      |   41 +++++++++++++++++++++++++++++++++++++++++
 system_backup.timer   |    9 +++++++++
 system_backup.service |    9 +++++++++
 3 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/system_backup.service b/system_backup.service
new file mode 100644
index 0000000..775c5c8
--- /dev/null
+++ b/system_backup.service
@@ -0,0 +1,9 @@
+[Unit]
+Description=Backup system with Borg
+
+[Service]
+Type=simple
+ExecStart=/root/system_backup.sh
+
+[Install]
+RequiredBy=multi-user.target
\ No newline at end of file
diff --git a/system_backup.sh b/system_backup.sh
new file mode 100755
index 0000000..ef603ac
--- /dev/null
+++ b/system_backup.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+if [ $EUID != 0 ]; then
+	sudo "$0" "$@"
+	exit $?
+fi
+
+export BORG_REPO=/backup/borg
+
+# add --list to view files as they're added to the backup
+borg create \
+	 --verbose \
+	 --stats \
+	 --compression zstd,22 \
+	 --one-file-system \
+	 --exclude-caches \
+	 --exclude /var/cache \
+	 \
+	 ::"$(date +%F)" \
+	 /
+
+BACKUP_EXIT=$?
+
+borg prune \
+	 --list \
+	 --keep-daily 7 \
+	 --keep-monthly 2
+
+PRUNE_EXIT=$?
+
+GLOBAL_EXIT=$(( BACKUP_EXIT > PRUNE_EXIT ? BACKUP_EXIT : PRUNE_EXIT ))
+
+if [ $GLOBAL_EXIT -eq 0 ]; then
+	echo "Backup and Prune finished with successfully"
+elif [ $GLOBAL_EXIT -eq 1 ]; then
+	echo "Backup and/or Prune finshed with warnings"
+else
+	echo "Backup and/or Prune finished with errors"
+fi
+
+exit $GLOBAL_EXIT
diff --git a/system_backup.timer b/system_backup.timer
new file mode 100644
index 0000000..c783813
--- /dev/null
+++ b/system_backup.timer
@@ -0,0 +1,9 @@
+[Unit]
+Description=System backup with Borg Timer
+
+[Timer]
+OnCalendar=Daily
+Persistent=true
+
+[Install]
+WantedBy=timers.target
\ No newline at end of file

--
Gitblit v1.9.3