A
Admincraftā€¢2y ago
drepk

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
...
#!/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.
2 Replies
Admincraft Meta
Admincraft Metaā€¢2y ago
Thanks for asking your question!
Once you have finished, please close your thread. Make sure to provide as much helpful information as possible such as logs/what you tried and what your exact issue is
command to close
/close !close !solved
Requested by drepk#0
drepk
drepkOPā€¢2y ago
thank you! will be checking it now the third link really makes some sense, first two shuts down the server before backing up šŸ˜¢ I've script almost ready, but can't guarantee for reliability, cuz in past sometimes, after running save hold and save query, I compressed the directory using tar but when i later, tried using that world, the server said there's some error with the world, trying to fix ..., and it reverted the world to like my last backup hrs or progress were lost, Thanks for the suggestion, it'll really be helpful šŸ˜ I'll post the solution, then i'll close it's okay, if you /close it rn as well, no problem!!

Did you find this page helpful?