Repository: thijsvanloef/palworld-server-docker
File: scripts/restore.sh (Shell)

Code before:
RESTORE_PATH="/palworld/Pal"

# Copy the save file before restore temporary path
TMP_SAVE_PATH="/palworld/backups/restore-"$(date +"%Y-%m-%d_%H-%M-%S")

# shellcheck disable=SC2317
term_error_handler() {
    echo "An error occurred during server shutdown."
    exit 1
}

# shellcheck disable=SC2317
restore_error_handler() {
    printf "\033[0;31mAn error occurred during restore.\033[0m\n"
    if [ -d "$TMP_SAVE_PATH/Saved" ]; then
        read -rp "I have a backup before recovery can proceed. Do you want to recovery it? (y/n): " RUN_ANSWER
        if [[ $RUN_ANSWER == "y" ]] || [[ $RUN_ANSWER == "Y" ]]; then
            rm -rf "$RESTORE_PATH/Saved"
            mv "$TMP_SAVE_PATH/Saved" "$RESTORE_PATH"
            printf "\e[0;32mRecovery complete.\e[0m\n"
        fi
    fi

    echo "Clean up the temporary directory."
    rm -rf "$TMP_PATH"
    rm -rf "$TMP_SAVE_PATH"

    exit 1
}

if [ "${RCON_ENABLED}" != true ]; then
    echo "RCON is not enabled. Please enable RCON to use this feature."
    exit 1
fi

# Show up backup list
echo "Backup List:"
select BACKUP_FILE in "$BACKUP_DIRECTORY_PATH"/*; do
    if [ -n "$BACKUP_FILE" ]; then
        echo "Selected backup: $BACKUP_FILE"
        break
    else
        echo "Invalid selection. Please try again."
    fi
done

if [ -f "$BACKUP_FILE" ]; then
    echo "Do you want to continue with the command?"
    read -rp "When you run it, the server will be stopped and the recovery will proceed. (y/n): " RUN_ANSWER
    if [[ $RUN_ANSWER == "y" ]] || [[ $RUN_ANSWER == "Y" ]]; then
        printf "\e[0;32m*****STARTING PROCESS*****\e[0m\n"

Review comment (suggestion):
Can be one line
```suggestion
    rm -rf "$TMP_PATH" "$TMP_SAVE_PATH"
```

Code after:
RESTORE_PATH="/palworld/Pal"

# Copy the save file before restore temporary path
TMP_SAVE_PATH="/palworld/backups/restore-"$(date +"%Y-%m-%d_%H-%M-%S")

# shellcheck disable=SC2317
term_error_handler() {
    echo "An error occurred during server shutdown."
    exit 1
}

# shellcheck disable=SC2317
restore_error_handler() {
    printf "\033[0;31mAn error occurred during restore.\033[0m\n"
    if [ -d "$TMP_SAVE_PATH/Saved" ]; then
        read -rp "I have a backup before recovery can proceed. Do you want to recovery it? (y/n): " RUN_ANSWER
        if [[ ${RUN_ANSWER,,} == "y" ]]; then
            rm -rf "$RESTORE_PATH/Saved"
            mv "$TMP_SAVE_PATH/Saved" "$RESTORE_PATH"
            printf "\e[0;32mRecovery complete.\e[0m\n"
        fi
    fi

    echo "Clean up the temporary directory."
    rm -rf "$TMP_PATH" "$TMP_SAVE_PATH"

    exit 1
}

if [ "${RCON_ENABLED}" != true ]; then
    echo "RCON is not enabled. Please enable RCON to use this feature."
    exit 1
fi

# Show up backup list
echo "Backup List:"
mapfile -t BACKUP_FILES < <(find "$BACKUP_DIRECTORY_PATH" -type f -name "*.tar.gz" | sort)
select BACKUP_FILE in "${BACKUP_FILES[@]}"; do
    if [ -n "$BACKUP_FILE" ]; then
        echo "Selected backup: $BACKUP_FILE"
        break
    else
        echo "Invalid selection. Please try again."
    fi
done

if [ -f "$BACKUP_FILE" ]; then
    printf "\033[0;31mThis script has been designed to help you restore; however, I am not responsible for any data loss. It is recommended that you create a backup beforehand, and in the event of script failure, be prepared to restore it manually.\033[0m\n"
    echo "Do you understand the above and would you like to proceed with this command?"
    read -rp "When you run it, the server will be stopped and the recovery will proceed. (y/n): " RUN_ANSWER
    if [[ ${RUN_ANSWER,,} == "y" ]]; then