aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar TheMightyV <themightyv@protonmail.com> 2022-01-02 21:18:02 +0100
committerGravatar TheMightyV <themightyv@protonmail.com> 2022-01-02 21:18:02 +0100
commit8974233a537d2890d75d398bdbdf5ca73152b23b (patch)
treea6af6f42786c9a9ce590da798f000f8001246318
parent023a18b1d150fc9c09bf6ac135663f7ddce3b757 (diff)
downloadminecraft-server-tools-8974233a537d2890d75d398bdbdf5ca73152b23b.tar.gz
moved from dmenu to bash select, minor cosmetics
-rw-r--r--backends/borg.sh7
-rw-r--r--backends/bup.sh5
-rw-r--r--backends/tar.sh5
-rwxr-xr-xserver.sh61
4 files changed, 48 insertions, 30 deletions
diff --git a/backends/borg.sh b/backends/borg.sh
index 999067f..f5930c8 100644
--- a/backends/borg.sh
+++ b/backends/borg.sh
@@ -56,17 +56,18 @@ function borg_create_backup() {
return $RETCODE
}
-function borg_ls_remote() {
+# server_restore relies on output format of this function
+function borg_ls_dir() {
export BORG_PASSCOMMAND="$BACKUP_PASSCOMMAND"
borg list "$1" | cut -d' ' -f1
}
-function borg_ls() {
+function borg_ls_all() {
export BORG_PASSCOMMAND="$BACKUP_PASSCOMMAND"
for BACKUP_DIR in ${BACKUP_DIRS[*]}
do
echo "borg: backups in \"$BACKUP_DIR\":"
- borg list "$BACKUP_DIR"
+ borg list "$BACKUP_DIR" | cut -d' ' -f1
done
}
diff --git a/backends/bup.sh b/backends/bup.sh
index ab27825..a5ddb14 100644
--- a/backends/bup.sh
+++ b/backends/bup.sh
@@ -54,11 +54,12 @@ function bup_create_backup() {
return $RETCODE
}
-function bup_ls_remote() {
+# server_restore relies on output format of this function
+function bup_ls_dir() {
bup -d "$(bup_local)" ls -r "$BACKUP_DIR" "$BACKUP_NAME"
}
-function bup_ls() {
+function bup_ls_all() {
for BACKUP_DIR in ${BACKUP_DIRS[*]}
do
echo "bup: backups in \"$BACKUP_DIR\":"
diff --git a/backends/tar.sh b/backends/tar.sh
index b556798..1265992 100644
--- a/backends/tar.sh
+++ b/backends/tar.sh
@@ -40,7 +40,8 @@ function tar_create_backup() {
return $RETCODE
}
-function tar_ls_remote() {
+# server_restore relies on output format of this function
+function tar_ls_dir() {
BACKUP_DIR="$1"
if [[ $BACKUP_DIR == *:* ]]; then
REMOTE="$(echo "$BACKUP_DIR" | cut -d: -f1)"
@@ -51,7 +52,7 @@ function tar_ls_remote() {
fi
}
-function tar_ls() {
+function tar_ls_all() {
for BACKUP_DIR in ${BACKUP_DIRS[*]}
do
echo "Backups in $BACKUP_DIR:"
diff --git a/server.sh b/server.sh
index d144053..46156af 100755
--- a/server.sh
+++ b/server.sh
@@ -224,53 +224,68 @@ function server_backup() {
function ls_backups() {
if [ $BACKUP_BACKEND = "bup" ]; then
- bup_ls
+ bup_ls_all
elif [ $BACKUP_BACKEND = "borg" ]; then
- borg_ls
+ borg_ls_all
else
- tar_ls
+ tar_ls_all
fi
}
-# TODO: replace dmenu with terminal-only option
+function choose_from() {
+ local items=("$@")
+ select item in "${items[@]}"; do
+ echo "$item"
+ return
+ done
+ echo ""
+}
+
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"
+ echo "Select from where get the snapshot"
+ BACKUP_DIR=$(choose_from "${BACKUP_DIRS[@]}")
+ if [[ ! ${BACKUP_DIRS[*]} =~ (^|[[:space:]])"$BACKUP_DIR"($|[[:space:]]) ]]; then
+ echo "No valid backup directory 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
+ if [ $BACKUP_BACKEND = "bup" ]; then
+ bup_ls_dir "$BACKUP_DIR"
+ elif [ $BACKUP_BACKEND = "borg" ]; then
+ borg_ls_dir "$BACKUP_DIR"
+ else
+ tar_ls_dir "$BACKUP_DIR"
+ fi
)
- SNAPSHOT=$(echo "$SNAPSHOTS" | dmenu -l 30 -p "Select a snapshot to recover")
- if [[ ! "$SNAPSHOTS" == *"${SNAPSHOT}"* ]] || [ ! "$SNAPSHOT" ] ; then
- echo "No snapshot selected, abort"
+ if [ -z "$SNAPSHOTS" ]; then
+ echo "No snapshots found, abort"
+ return 1
+ fi
+ # convert multiline string to bash array
+ SNAPSHOTS=($(echo "$SNAPSHOTS"))
+ echo "Select a snapshot to restore"
+ SNAPSHOT=$(choose_from "${SNAPSHOTS[@]}")
+ if [[ ! ${SNAPSHOTS[*]} =~ (^|[[:space:]])"$SNAPSHOT"($|[[:space:]]) ]]; then
+ echo "No valid snapshot selected, abort"
return 1
fi
- echo "Restoring snapshot \"$SNAPSHOT\" from remote \"$REMOTE\""
+ echo "Restoring snapshot \"$SNAPSHOT\" from \"$BACKUP_DIR\""
OLDWORLD_NAME=""
if [[ -d "$WORLD_NAME" ]]; then
- echo "bup: Saving old world, just in case"
+ echo "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"
+ bup_restore "$BACKUP_DIR" "$SNAPSHOT"
elif [ $BACKUP_BACKEND = "borg" ]; then
- borg_restore "$REMOTE" "$SNAPSHOT"
+ borg_restore "$BACKUP_DIR" "$SNAPSHOT"
else
- tar_restore "$REMOTE" "$SNAPSHOT"
+ tar_restore "$BACKUP_DIR" "$SNAPSHOT"
fi
if [ ! $? -eq 0 ]; then