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

Code before:
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"
    # Shutdown server
    trap 'term_error_handler' ERR

    printf "\e[0;32m*****SHUTDOWN SERVER*****\e[0m\n"
    if [ "${RCON_ENABLED}" = true ]; then
        rcon-cli -c /home/steam/server/rcon.yaml save
        rcon-cli -c /home/steam/server/rcon.yaml "shutdown 1"
    else
        echo "RCON is not enabled. Please enable RCON to use this feature."
    fi
    printf "\e[0;32mShutdown complete.\e[0m\n"

    trap - ERR

    trap 'restore_error_handler' ERR
    
    printf "\e[0;32m*****START RESTORE*****\e[0m\n"

    # Recheck the backup file
    if [ -f "$BACKUP_FILE" ]; then
      # Copy the save file before restore
        if [ -d "$RESTORE_PATH/Saved" ]; then
        echo "Saves the current state before the restore proceeds."
        rm -rf "./tmp_save"
        mkdir -p "./tmp_save"
        \cp -rf "$RESTORE_PATH/Saved" "./tmp_save/Saved"

        while [ ! -d "./tmp_save/Saved" ]; do
          sleep 1
        done

        printf "\e[0;32mSave complete.\e[0m\n"
      fi
      
      # Create tmp directory
      rm -rf "./tmp"

Review comment (suggestion):
You should exit the script if RCON is not enabled.
Maybe move the shutdown print statement so it is not displayed when you RCON is not enabled.
```suggestion
    if [ "${RCON_ENABLED}" = true ]; then
        printf "\e[0;32m*****SHUTDOWN SERVER*****\e[0m\n"
        rcon-cli -c /home/steam/server/rcon.yaml save
        rcon-cli -c /home/steam/server/rcon.yaml "shutdown 1"
    else
        echo "RCON is not enabled. Please enable RCON to use this feature. Unable to restore backup."
        exit 1
    fi
    printf "\e[0;32mShutdown complete.\e[0m\n"
```

Code after:
    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
        printf "\e[0;32m*****STARTING PROCESS*****\e[0m\n"
        # Shutdown server
        trap 'term_error_handler' ERR

        if [ "${RCON_ENABLED}" = true ]; then
            printf "\e[0;32m*****SHUTDOWN SERVER*****\e[0m\n"
            rcon-cli -c /home/steam/server/rcon.yaml save
            rcon-cli -c /home/steam/server/rcon.yaml "shutdown 1"
        else
            echo "RCON is not enabled. Please enable RCON to use this feature. Unable to restore backup."
            exit 1
        fi
        printf "\e[0;32mShutdown complete.\e[0m\n"

        trap - ERR

        trap 'restore_error_handler' ERR
        
        printf "\e[0;32m*****START RESTORE*****\e[0m\n"

        # Recheck the backup file
        if [ -f "$BACKUP_FILE" ]; then
            # Copy the save file before restore
            if [ -d "$RESTORE_PATH/Saved" ]; then
                echo "Saves the current state before the restore proceeds."
                echo "$TMP_SAVE_PATH"
                mkdir -p "$TMP_SAVE_PATH"
                if [ "$(id -u)" -eq 0 ]; then
                    chown steam:steam "$TMP_SAVE_PATH"
                fi