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."
                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"
            mkdir -p "./tmp"
            
            # Decompress the backup file in tmp directory
            tar -zxvf "$BACKUP_FILE" -C "./tmp"

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

            # Remove tmp directory
            rm -rf "./tmp"
            rm -rf "./tmp_save"

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

Review comment (suggestion):
Instead of making a two fixed temporary directory I'd recommend using mktemp to make a parent for them in backups otherwise it's inside wherever you called the script from: `tmp_parent=$(mkdir -d -p /palworld/backup)`. Or do `tmp_parent="/palworld/backup/restore-"$(date +"%Y-%m-%d_%H-%M-%S")`.

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