Files
Ansible-Minecraft-Server/roles/04-backups/templates/restore-backup.sh.j2
hcornet 7a2ccb537b
Some checks failed
Ansible Minecraft CI/CD Pipeline / Ansible Lint Check (push) Successful in 58s
Ansible Minecraft CI/CD Pipeline / Project Structure Validation (push) Failing after 3s
Ansible Minecraft CI/CD Pipeline / Security Scan (push) Successful in 4s
Ansible Minecraft CI/CD Pipeline / Deploy to Staging (push) Has been skipped
Ansible Minecraft CI/CD Pipeline / Deploy to Production (push) Has been skipped
Ansible Minecraft CI/CD Pipeline / Backup System Check (push) Has been skipped
test other version
2025-08-26 21:59:21 +02:00

116 lines
2.8 KiB
Django/Jinja

#!/bin/bash
# Minecraft Backup Restore Script
# Generated by Ansible
set -e
# Usage function
usage() {
echo "Usage: $0 <backup_type> <backup_file>"
echo "backup_type: daily, weekly, or monthly"
echo "backup_file: name of the backup file to restore"
echo ""
echo "Example: $0 daily daily_backup_20241225_120000.tar.gz"
exit 1
}
# Check arguments
if [ $# -ne 2 ]; then
usage
fi
BACKUP_TYPE="$1"
BACKUP_FILE="$2"
LOG_FILE="{{ minecraft_base_dir }}/logs/restore.log"
# Source backup functions
source {{ minecraft_tools_dir }}/backup-functions.sh
# Validate backup type
case "$BACKUP_TYPE" in
daily)
BACKUP_DIR="{{ backup_daily_dir }}"
;;
weekly)
BACKUP_DIR="{{ backup_weekly_dir }}"
;;
monthly)
BACKUP_DIR="{{ backup_monthly_dir }}"
;;
*)
echo "Invalid backup type: $BACKUP_TYPE"
usage
;;
esac
# Check if backup file exists
BACKUP_PATH="$BACKUP_DIR/$BACKUP_FILE"
if [ ! -f "$BACKUP_PATH" ]; then
echo "Backup file not found: $BACKUP_PATH"
exit 1
fi
# Confirmation
echo "WARNING: This will replace the current Minecraft server data!"
echo "Backup file: $BACKUP_PATH"
echo "Type 'yes' to continue:"
read -r confirmation
if [ "$confirmation" != "yes" ]; then
echo "Restore cancelled."
exit 0
fi
# Start restoration process
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting restore from $BACKUP_FILE..." >> "$LOG_FILE"
# Stop Minecraft server
stop_minecraft
# Create temporary restore directory
TEMP_DIR="/tmp/minecraft_restore_$"
mkdir -p "$TEMP_DIR"
# Extract backup
echo "Extracting backup..." >> "$LOG_FILE"
cd "$TEMP_DIR"
tar -xzf "$BACKUP_PATH"
# Find the extracted directory
EXTRACTED_DIR=$(find . -maxdepth 1 -type d -name "*backup*" | head -1)
if [ -z "$EXTRACTED_DIR" ]; then
echo "Could not find extracted backup directory"
rm -rf "$TEMP_DIR"
exit 1
fi
# Backup current data
CURRENT_BACKUP="{{ minecraft_base_dir }}/current_backup_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$CURRENT_BACKUP"
{% for source in backup_sources %}
if [ -d "{{ source }}" ]; then
cp -r "{{ source }}" "$CURRENT_BACKUP/"
fi
{% endfor %}
echo "Current data backed up to: $CURRENT_BACKUP" >> "$LOG_FILE"
# Restore data
echo "Restoring data..." >> "$LOG_FILE"
{% for source in backup_sources %}
SOURCE_NAME=$(basename {{ source }})
if [ -d "$EXTRACTED_DIR/$SOURCE_NAME" ]; then
rm -rf "{{ source }}"
cp -r "$EXTRACTED_DIR/$SOURCE_NAME" "{{ source }}"
chown -R {{ minecraft_user }}:{{ minecraft_group }} "{{ source }}"
fi
{% endfor %}
# Cleanup temporary directory
rm -rf "$TEMP_DIR"
# Start Minecraft server
start_minecraft
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Restore completed successfully" >> "$LOG_FILE"
echo "Restore completed successfully!"
echo "Previous data backed up to: $CURRENT_BACKUP"