From 57f56df53bb63f23b5f4447046a8f9f725162ad9 Mon Sep 17 00:00:00 2001 From: TheMightyV Date: Sun, 2 Jan 2022 19:58:23 +0100 Subject: multiple backup destinations --- backends/bup.sh | 70 ++++++++++++++++++++++++++++++++++++++++++++------------- backends/tar.sh | 49 ++++++++++++++++++++++++++++++++++++---- server.sh | 50 ++++++++++++++++++++++++++++++++--------- serverconf.sh | 2 ++ 4 files changed, 141 insertions(+), 30 deletions(-) diff --git a/backends/bup.sh b/backends/bup.sh index 83e4e98..e51aaf1 100644 --- a/backends/bup.sh +++ b/backends/bup.sh @@ -1,23 +1,63 @@ -function create_bup_backup() { - BACKUP_DIR="mc-backups" - CUR_BACK_DIR="mc-backups/$CUR_YEAR" - - if [ ! -d "$CUR_BACK_DIR" ]; then - mkdir -p "$CUR_BACK_DIR" - fi +# use first not-remote backup directory as bup's local repository +# defaults to ".bup" +function bup_local() { + for BACKUP_DIR in ${BACKUP_DIRS[*]} + do + if [[ $BACKUP_DIR != *:* ]]; then + echo "$BACKUP_DIR" + return + fi + done + echo ".bup" +} - bup -d "$CUR_BACK_DIR" index "$WORLD_NAME" +function bup_init() { + bup -d "$(bup_local)" index "$WORLD_NAME" status=$? - if [ $status -eq 1 ]; then - bup -d "$CUR_BACK_DIR" init - bup -d "$CUR_BACK_DIR" index "$WORLD_NAME" + if [ $status -ne 0 ]; then + echo "bup: no local repo found, creating..." + bup -d "$(bup_local)" init -r "$(bup_local)" + echo "bup: created local repo at $(bup_local)" fi +} + +function bup_create_backup() { + echo "bup: backup started" + + bup -d "$(bup_local)" index "$WORLD_NAME" - bup -d "$CUR_BACK_DIR" save -n "$BACKUP_NAME" "$WORLD_NAME" + # 0 if saved to at least one non-local repository + RETCODE=1 + for BACKUP_DIR in ${BACKUP_DIRS[*]} + do + echo "bup: backing up to \"$BACKUP_DIR\"" + # try to save to remote + bup -d "$(bup_local)" save -r "$BACKUP_DIR" -n "$BACKUP_NAME" "$WORLD_NAME" + # if failed - reinit remote and try again + if [ ! $? -eq 0 ]; then + echo "bup: failed backing up to \"$BACKUP_DIR\", reinitializing remote..." + bup -d "$(bup_local)" init -r "$BACKUP_DIR" + if [ ! $? -eq 0 ]; then + echo "bup: created remote at \"$BACKUP_DIR\"" + bup -d "$(bup_local)" save -r "$BACKUP_DIR" -n "$BACKUP_NAME" "$WORLD_NAME" + else + echo "bup: failed to make remote at \"$BACKUP_DIR\", moving on" + fi + else + if [ "$BACKUP_DIR" = "$(bup_local)" ]; then + RETCODE=0 + fi + fi + done - echo "Backup using bup to $CUR_BACK_DIR is complete" + echo "bup: backup finished" + return $RETCODE } -function ls_bup() { - bup -d "mc-backups/${CUR_YEAR}" ls "mc-sad-squad/$1" +function bup_ls() { + for BACKUP_DIR in ${BACKUP_DIRS[*]} + do + echo "bup: backups in \"$BACKUP_DIR\":" + bup -d "$(bup_local)" ls -r "$BACKUP_DIR" --human-readable -l "$BACKUP_NAME" + done } diff --git a/backends/tar.sh b/backends/tar.sh index 4d7ff36..c6012e3 100644 --- a/backends/tar.sh +++ b/backends/tar.sh @@ -1,14 +1,55 @@ +function tar_init() { + # nothing to do for tar? + : +} + # TODO: Make default .tar with optional bup function tar_create_backup() { - ARCHNAME="backup/$WORLD_NAME-backup_`date +%d-%m-%y-%T`.tar.gz" + echo "tar: backing up..." + + # save world to a temporary archive + ARCHNAME="/tmp/${BACKUP_NAME}_`date +%d-%m-%y-%T`.tar.gz" tar -czf "$ARCHNAME" "./$WORLD_NAME" if [ ! $? -eq 0 ] then - echo "TAR failed. No Backup created." - rm $ARCHNAME #remove (probably faulty) archive + echo "tar: failed to save the world" + rm "$ARCHNAME" #remove (probably faulty) archive return 1 else - echo $ARCHNAME created. + echo "tar: world saved to $ARCHNAME, pushing it to backup directories..." fi + + RETCODE=2 + for BACKUP_DIR in ${BACKUP_DIRS[*]} + do + echo "tar: pushing to \"$BACKUP_DIR\"" + # scp acts as cp for local destination directories + scp "$ARCHNAME" "$BACKUP_DIR/" + if [ ! $? -eq 0 ]; then + echo "tar: failed pushing to \"$BACKUP_DIR\", moving on" + else + RETCODE=0 + fi + done + + rm "$ARCHNAME" + + echo "tar: backup finished" + + return $RETCODE +} + +function tar_ls() { + for BACKUP_DIR in ${BACKUP_DIRS[*]} + do + echo "Backups in $BACKUP_DIR:" + if [[ $BACKUP_DIR == *:* ]]; then + REMOTE="$(echo "$BACKUP_DIR" | cut -d: -f1)" + REMOTE_DIR="$(echo "$BACKUP_DIR" | cut -d: -f2)" + ssh "$REMOTE" "ls -1sh $REMOTE_DIR" | grep "tar.gz" + else + ls -1sh "$BACKUP_DIR" | grep "tar.gz" + fi + done } diff --git a/server.sh b/server.sh index 0bed9af..9ed7edf 100755 --- a/server.sh +++ b/server.sh @@ -109,6 +109,34 @@ function players_online() { [ `tail -n 3 "$LOGFILE" | grep -c "There are 0"` -lt 1 ] } +function init_backup() { + for BACKUP_DIR in ${BACKUP_DIRS[*]} + do + if [[ $BACKUP_DIR == *:* ]]; then + REMOTE="$(echo "$BACKUP_DIR" | cut -d: -f1)" + REMOTE_DIR="$(echo "$BACKUP_DIR" | cut -d: -f2)" + ssh "$REMOTE" "mkdir -p \"$REMOTE_DIR\"" + else + mkdir -p "$BACKUP_DIR" + fi + done + + if [ $USE_BUP = "YES" ]; then + bup_init + else + tar_init + fi +} + +function create_backup() { + init_backup + + if [ $USE_BUP = "YES" ]; then + bup_create_backup + else + tar_create_backup + fi +} function server_backup_safe() { force=$1 @@ -131,11 +159,7 @@ function server_backup_safe() { sleep 2 echo "Done! starting backup..." - if [ $USE_BUP = "YES" ]; then - create_bup_backup - else - create_backup_archive - fi + create_backup local RET=$? @@ -152,11 +176,7 @@ function server_backup_safe() { function server_backup_unsafe() { echo "No running server detected. Running Backup" - if [ $USE_BUP = "YES" ]; then - create_bup_backup - else - create_backup_archive - fi + create_backup if [ $? -eq 0 ] then @@ -197,6 +217,14 @@ function server_backup() { exit } +function ls_backups() { + if [ $USE_BUP = "YES" ]; then + bup_ls + else + tar_ls + fi +} + #cd $(dirname $0) case $1 in @@ -220,7 +248,7 @@ case $1 in server_backup "true" ;; "ls") - ls_bup $2 + ls_backups ;; *) echo "Usage: $0 start|stop|attach|status|backup" diff --git a/serverconf.sh b/serverconf.sh index 25ba16e..8625bf3 100644 --- a/serverconf.sh +++ b/serverconf.sh @@ -20,3 +20,5 @@ USE_BUP="NO" #Constants CUR_YEAR=`date +"%Y"` + +BACKUP_DIRS=(".bak/$CUR_YEAR" "user@backupserver:/path/to/backup/$CUR_YEAR") -- cgit v1.2.3