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

Code before:
        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 
            echo "The selected backup file does not exist."
            exit 1
        fi
    else
        echo "Abort the recovery."
        exit 1
    fi
else
    echo "The selected backup file does not exist."
    exit 1
fi

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
            TMP_PATH=$(mktemp -d -p "/palworld/backups")
            if [ "$(id -u)" -eq 0 ]; then
                chown steam:steam "$TMP_PATH"
            fi
```

Code after:

        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."
            rm -rf "$TMP_PATH" "$TMP_SAVE_PATH"

            printf "\e[0;32mRestore complete!!!! Please restart the Docker container\e[0m\n"
            
            exit 0
        else 
            echo "The selected backup file does not exist."
            exit 1
        fi
    else
        echo "Abort the recovery."
        exit 1
    fi
else