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

Code before:
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"
        # 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"

Review comment (suggestion):
Currently it shows everything in backups including non backup files and directories. It also doesn't go into the subdiretories.

```
1) /palworld/backups/notes.txt
2) /palworld/backups/palworld-save-2024-02-01_17-06-58.tar.gz
3) /palworld/backups/palworld-save-2024-02-01_17-11-15.tar.gz
4) /palworld/backups/palworld-save-2024-02-01_17-11-25.tar.gz
5) /palworld/backups/subfolder
```

Best to change so it only shows compressed archives and goes into the child directories

```suggestion
mapfile -t BACKUP_FILES < <(find "$BACKUP_DIRECTORY_PATH" -type f -name "*.tar.gz" | sort)
select BACKUP_FILE in "${BACKUP_FILES[@]}"; do
```
Added in sort since find doesn't output in order of name
Now the output includes child directories
```
1) /palworld/backups/palworld-save-2024-02-01_17-06-58.tar.gz
2) /palworld/backups/palworld-save-2024-02-01_17-11-15.tar.gz
3) /palworld/backups/palworld-save-2024-02-01_17-11-25.tar.gz
4) /palworld/backups/subfolder/palworld-save-2024-02-01 _17-07-24.tar.gz
```


Code after:
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
        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