diff options
author | TheMightyV <themightyv@protonmail.com> | 2022-01-02 20:12:27 +0100 |
---|---|---|
committer | TheMightyV <themightyv@protonmail.com> | 2022-01-02 20:12:27 +0100 |
commit | de4fc74d50d536f524aa4c65e270123bfe8ef893 (patch) | |
tree | 977cda9219a434d5d4f8618fc7accde7cb7ef7d3 | |
parent | 4baf9150016177582dd56a1bf3c09a3cad5aa050 (diff) | |
download | minecraft-server-tools-de4fc74d50d536f524aa4c65e270123bfe8ef893.tar.gz |
added backup restoration
-rw-r--r-- | backends/borg.sh | 16 | ||||
-rw-r--r-- | backends/bup.sh | 11 | ||||
-rw-r--r-- | backends/tar.sh | 34 | ||||
-rwxr-xr-x | server.sh | 54 |
4 files changed, 105 insertions, 10 deletions
diff --git a/backends/borg.sh b/backends/borg.sh index d3d9c63..999067f 100644 --- a/backends/borg.sh +++ b/backends/borg.sh @@ -19,7 +19,7 @@ function borg_create_backup() { echo "borg: starting backup to \"$BACKUP_DIR\"" borg create \ - "${BACKUP_DIR}::${BACKUP_NAME}_{hostname}_{now}" \ + "${BACKUP_DIR}::${BACKUP_NAME}_$(date +'%F_%H-%M-%S')" \ "$WORLD_NAME" \ --filter AME \ --compression lz4 \ @@ -56,6 +56,11 @@ function borg_create_backup() { return $RETCODE } +function borg_ls_remote() { + export BORG_PASSCOMMAND="$BACKUP_PASSCOMMAND" + borg list "$1" | cut -d' ' -f1 +} + function borg_ls() { export BORG_PASSCOMMAND="$BACKUP_PASSCOMMAND" for BACKUP_DIR in ${BACKUP_DIRS[*]} @@ -64,3 +69,12 @@ function borg_ls() { borg list "$BACKUP_DIR" done } + +function borg_restore() { + export BORG_PASSCOMMAND="$BACKUP_PASSCOMMAND" + REMOTE="$1" + SNAPSHOT="$2" + + export BORG_REPO="$REMOTE" + borg extract "${REMOTE}::${SNAPSHOT}" +} diff --git a/backends/bup.sh b/backends/bup.sh index e51aaf1..ab27825 100644 --- a/backends/bup.sh +++ b/backends/bup.sh @@ -54,6 +54,10 @@ function bup_create_backup() { return $RETCODE } +function bup_ls_remote() { + bup -d "$(bup_local)" ls -r "$BACKUP_DIR" "$BACKUP_NAME" +} + function bup_ls() { for BACKUP_DIR in ${BACKUP_DIRS[*]} do @@ -61,3 +65,10 @@ function bup_ls() { bup -d "$(bup_local)" ls -r "$BACKUP_DIR" --human-readable -l "$BACKUP_NAME" done } + +function bup_restore() { + REMOTE="$1" + SNAPSHOT="$2" + + bup -d "$(bup_local)" restore -r "$REMOTE" "$BACKUP_NAME/$SNAPSHOT/$PWD/." +} diff --git a/backends/tar.sh b/backends/tar.sh index c6012e3..b556798 100644 --- a/backends/tar.sh +++ b/backends/tar.sh @@ -8,7 +8,7 @@ function tar_create_backup() { echo "tar: backing up..." # save world to a temporary archive - ARCHNAME="/tmp/${BACKUP_NAME}_`date +%d-%m-%y-%T`.tar.gz" + ARCHNAME="/tmp/${BACKUP_NAME}_`date +%FT%H%M%S%z`.tar.gz" tar -czf "$ARCHNAME" "./$WORLD_NAME" if [ ! $? -eq 0 ] @@ -40,16 +40,34 @@ function tar_create_backup() { return $RETCODE } +function tar_ls_remote() { + BACKUP_DIR="$1" + if [[ $BACKUP_DIR == *:* ]]; then + REMOTE="$(echo "$BACKUP_DIR" | cut -d: -f1)" + REMOTE_DIR="$(echo "$BACKUP_DIR" | cut -d: -f2)" + ssh "$REMOTE" "ls -1 $REMOTE_DIR" | grep "tar.gz" + else + ls -1 "$BACKUP_DIR" | grep "tar.gz" + fi +} + 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 + tar_ls_remote "$BACKUP_DIR" done } + +function tar_restore() { + REMOTE="$1" + SNAPSHOT="$2" + + scp "$REMOTE/$SNAPSHOT" "/tmp/" + if [ ! $? -eq 0 ]; then + echo "Failed to get archive from \"$REMOTE/$SNAPSHOT\"" + return 1 + fi + + tar -xzf "/tmp/$SNAPSHOT" +} @@ -232,6 +232,56 @@ function ls_backups() { fi } +# TODO: replace dmenu with terminal-only option +function server_restore() { + REMOTES=$( IFS=$'\n'; echo "${BACKUP_DIRS[*]}" ) + REMOTE=$(echo "$REMOTES" | dmenu -l 30 -p "Select a remote to get snapshot from") + if [[ ! "$REMOTES" == *"${REMOTE}"* ]] || [ ! "$REMOTE" ] ; then + echo "No remote selected, abort" + return 1 + fi + + SNAPSHOTS=$( + if [ $BACKUP_BACKEND = "bup" ]; then + bup_ls_remote "$REMOTE" + elif [ $BACKUP_BACKEND = "borg" ]; then + borg_ls_remote "$REMOTE" + else + tar_ls_remote "$REMOTE" + fi + ) + SNAPSHOT=$(echo "$SNAPSHOTS" | dmenu -l 30 -p "Select a snapshot to recover") + if [[ ! "$SNAPSHOTS" == *"${SNAPSHOT}"* ]] || [ ! "$SNAPSHOT" ] ; then + echo "No snapshot selected, abort" + return 1 + fi + + echo "Restoring snapshot \"$SNAPSHOT\" from remote \"$REMOTE\"" + + OLDWORLD_NAME="" + if [[ -d "$WORLD_NAME" ]]; then + echo "bup: Saving old world, just in case" + OLDWORLD_NAME="${WORLD_NAME}.old.$(date +'%F_%H-%M-%S')" + mv -v "$WORLD_NAME" "$OLDWORLD_NAME" + fi + + if [ $BACKUP_BACKEND = "bup" ]; then + bup_restore "$REMOTE" "$SNAPSHOT" + elif [ $BACKUP_BACKEND = "borg" ]; then + borg_restore "$REMOTE" "$SNAPSHOT" + else + tar_restore "$REMOTE" "$SNAPSHOT" + fi + + if [ ! $? -eq 0 ]; then + echo "Restore failed, reverting old world back" + rm -r "$WORLD_NAME" + mv -v "$OLDWORLD_NAME" "$WORLD_NAME" + else + echo "Restore finished" + fi +} + #cd $(dirname $0) case $1 in @@ -247,7 +297,9 @@ case $1 in "backup") server_backup ;; - # TODO: Add restore command + "restore") + server_restore + ;; "status") server_status ;; |