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

Code before:
        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"
                \cp -rf "$RESTORE_PATH/Saved" "$TMP_SAVE_PATH/Saved"

                while [ ! -d "$TMP_SAVE_PATH/Saved" ]; do
                    sleep 1
                done

                printf "\e[0;32mSave complete.\e[0m\n"
            fi
            
            # Create tmp directory
            TMP_PATH=$(mktemp -d -p "/palworld/backups")
            
            # Decompress the backup file in tmp directory
            tar -zxvf "$BACKUP_FILE" -C "$TMP_PATH"

            # Move the backup file to the restore directory
            \cp -rf -f "$TMP_PATH/Saved/" "$RESTORE_PATH"

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

            printf "\e[0;32mRestore complete!!!! Please restart the Docker container\e[0m\n"
            
            exit 0
        else 

Review comment (suggestion):
I would add a chmod in so the file is owned by steam not root. 
In the event of a container kill or sigterm on restore it will remain.
While it shouldn't be needed since the container `chown -R steam:steam /palworld /home/steam/` it prevents having a directory owned by root from existing. 

```suggestion
                mkdir -p "$TMP_SAVE_PATH"
                if [ "$(id -u)" -eq 0 ]; then
                    chown steam:steam "$TMP_SAVE_PATH"
                fi
```

Code after:
        # 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
                \cp -rf "$RESTORE_PATH/Saved" "$TMP_SAVE_PATH/Saved"

                while [ ! -d "$TMP_SAVE_PATH/Saved" ]; do
                    sleep 1
                done

                printf "\e[0;32mSave complete.\e[0m\n"
            fi
            
            # Create tmp directory
            TMP_PATH=$(mktemp -d -p "/palworld/backups")
            if [ "$(id -u)" -eq 0 ]; then
                chown steam:steam "$TMP_PATH"
            fi
            
            # Decompress the backup file in tmp directory
            tar -zxvf "$BACKUP_FILE" -C "$TMP_PATH"

            # Move the backup file to the restore directory
            \cp -rf -f "$TMP_PATH/Saved/" "$RESTORE_PATH"

            echo "Clean up the temporary directory."