How to make bedrock server (world) backup while server is up?

I run a bedrock server, for backups I've to turn off the server first then copy the files, but it's very unintuitive, I came across the documentation it has three commands basically save hold, save query and save resume. I understand how the commands work, but I'm looking for a Linux script to do this, my bedrock server runs in a separate screen session.

I tried writing down the script myself, here's my progress.

#!/bin/bash

# Define variables
SCREEN_NAME="be"
BACKUP_DIR="/home/ec2-user/backups"  # Replace with the actual backup directory on your server
S3_BUCKET_NAME="worlds"
TMP_FILE="/home/ec2-user/query_output.txt"

# Function to print logs/errors
print_log() {
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}

# Function to handle errors
handle_error() {
    print_log "Error: $1"
    exit 1
}

# Enter the screen session and execute backup commands
print_log "running save hold"
screen -S "$SCREEN_NAME" -X stuff "save hold\n"
sleep 1  # Wait for the server to prepare for backup
print_log "running save query"
screen -S "$SCREEN_NAME" -X stuff "save query\n"
sleep 5
screen -S "$SCREEN_NAME" -X hardcopy $TMP_FILE

# Wait for the backup to be ready
while true; do
    query_output=$(cat $TMP_FILE)
    if [[ $query_output == *"Data saved. Files are now ready to be copied."* ]]; then
        file_list=$(echo "$query_output" | grep -oP 'world7/.*?\:\d+')
        break
    fi
    sleep 1
done

print_log "Backup files: $file_list"
# Copy and truncate the backup files
for file in $file_list; do
    file_path=$(echo "$file" | cut -d':' -f1)
    file_size=$(echo "$file" | cut -d':' -f2)
    cp "$file_path" "$BACKUP_DIR/$file_path.tmp" || handle_error "Failed to copy $file_path"
    truncate -s "$file_size" "$BACKUP_DIR/$file_path.tmp" || handle_error "Failed to truncate $file_path"
done

# Compress the backup files
...

but I can't even copy the read the list of files from the screen, plz help.
Was this page helpful?