aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar TheMightyV <themightyv@protonmail.com> 2022-01-02 20:05:55 +0100
committerGravatar TheMightyV <themightyv@protonmail.com> 2022-01-02 20:05:55 +0100
commit4baf9150016177582dd56a1bf3c09a3cad5aa050 (patch)
tree606b944af7b7486402cdef974bf83361f2ed7b07
parent57f56df53bb63f23b5f4447046a8f9f725162ad9 (diff)
downloadminecraft-server-tools-4baf9150016177582dd56a1bf3c09a3cad5aa050.tar.gz
more generic backend selection, added borgbackup backend
-rw-r--r--backends/borg.sh66
-rwxr-xr-xserver.sh13
-rw-r--r--serverconf.sh10
3 files changed, 85 insertions, 4 deletions
diff --git a/backends/borg.sh b/backends/borg.sh
new file mode 100644
index 0000000..d3d9c63
--- /dev/null
+++ b/backends/borg.sh
@@ -0,0 +1,66 @@
+function borg_init() {
+ export BORG_PASSCOMMAND="$BACKUP_PASSCOMMAND"
+ for BACKUP_DIR in ${BACKUP_DIRS[*]}
+ do
+ # borg will check if repo exists
+ borg init --encryption=repokey-blake2 "$BACKUP_DIR"
+ done
+}
+
+function borg_create_backup() {
+ export BORG_PASSCOMMAND="$BACKUP_PASSCOMMAND"
+ RETCODE=255
+ for BACKUP_DIR in ${BACKUP_DIRS[*]}
+ do
+ export BORG_REPO="$BACKUP_DIR"
+
+ trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
+
+ echo "borg: starting backup to \"$BACKUP_DIR\""
+
+ borg create \
+ "${BACKUP_DIR}::${BACKUP_NAME}_{hostname}_{now}" \
+ "$WORLD_NAME" \
+ --filter AME \
+ --compression lz4 \
+ --exclude-caches \
+
+ backup_exit=$?
+
+ echo "borg: pruning repository at \"$BACKUP_DIR\""
+
+ borg prune \
+ --prefix '{hostname}-' \
+ --keep-minutely 2 \
+ --keep-hourly 24 \
+ --keep-daily 7 \
+ --keep-weekly 4 \
+ --keep-monthly 6 \
+ "$BACKUP_DIR"
+
+ prune_exit=$?
+
+ # use highest exit code as global exit code
+ global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
+ RETCODE=$(( global_exit > RETCODE ? global_exit : RETCODE ))
+
+ if [ ${global_exit} -eq 0 ]; then
+ echo "borg: backup and prune finished successfully"
+ elif [ ${global_exit} -eq 1 ]; then
+ echo "borg: backup and/or prune finished with warnings"
+ else
+ echo "borg: backup and/or prune finished with errors"
+ fi
+ #exit ${global_exit}
+ done
+ return $RETCODE
+}
+
+function borg_ls() {
+ export BORG_PASSCOMMAND="$BACKUP_PASSCOMMAND"
+ for BACKUP_DIR in ${BACKUP_DIRS[*]}
+ do
+ echo "borg: backups in \"$BACKUP_DIR\":"
+ borg list "$BACKUP_DIR"
+ done
+}
diff --git a/server.sh b/server.sh
index 9ed7edf..ee16af7 100755
--- a/server.sh
+++ b/server.sh
@@ -10,6 +10,7 @@ fi
source "backends/tar.sh"
source "backends/bup.sh"
+source "backends/borg.sh"
function backup_hook_example {
bup -d $CUR_BACK_DIR ls -l $BACKUP_NAME/latest/var/minecraft
@@ -121,8 +122,10 @@ function init_backup() {
fi
done
- if [ $USE_BUP = "YES" ]; then
+ if [ $BACKUP_BACKEND = "bup" ]; then
bup_init
+ elif [ $BACKUP_BACKEND = "borg" ]; then
+ borg_init
else
tar_init
fi
@@ -131,8 +134,10 @@ function init_backup() {
function create_backup() {
init_backup
- if [ $USE_BUP = "YES" ]; then
+ if [ $BACKUP_BACKEND = "bup" ]; then
bup_create_backup
+ elif [ $BACKUP_BACKEND = "borg" ]; then
+ borg_create_backup
else
tar_create_backup
fi
@@ -218,8 +223,10 @@ function server_backup() {
}
function ls_backups() {
- if [ $USE_BUP = "YES" ]; then
+ if [ $BACKUP_BACKEND = "bup" ]; then
bup_ls
+ elif [ $BACKUP_BACKEND = "borg" ]; then
+ borg_ls
else
tar_ls
fi
diff --git a/serverconf.sh b/serverconf.sh
index 8625bf3..a73a65d 100644
--- a/serverconf.sh
+++ b/serverconf.sh
@@ -16,9 +16,17 @@ WORLD_NAME="lfja"
BACKUP_NAME="${WORLD_NAME}_backup"
LOGFILE="logs/latest.log"
PIDFILE="server-screen.pid"
-USE_BUP="NO"
+# if not bup or borg, uses tar by default
+#BACKUP_BACKEND="tar"
+#BACKUP_BACKEND="bup"
+BACKUP_BACKEND="borg"
#Constants
CUR_YEAR=`date +"%Y"`
BACKUP_DIRS=(".bak/$CUR_YEAR" "user@backupserver:/path/to/backup/$CUR_YEAR")
+
+# borg repository could be password protected
+# to avoid having to manually type password, borg can run a command that should echo a password
+#BACKUP_PASSCOMMAND="echo superstrongpassword"
+#BACKUP_PASSCOMMAND="pass passwordname"