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 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 100 insertions(+), 19 deletions(-) (limited to 'backends') 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 } -- cgit v1.2.3