check new version
Some checks failed
Ansible Minecraft Server CI/CD / lint (push) Failing after 21s
Ansible Minecraft Server CI/CD / test (push) Has been skipped
Ansible Minecraft Server CI/CD / deploy (push) Has been skipped

This commit is contained in:
2025-08-27 07:59:19 +02:00
parent 7a2ccb537b
commit 9ea9ac7254
125 changed files with 2696 additions and 1511 deletions

View File

@@ -1,26 +1,11 @@
---
# Backup configuration
backup_base_dir: "{{ minecraft_base_dir }}/backups"
backup_base_dir: /opt/minecraft/backups
backup_daily_dir: "{{ backup_base_dir }}/daily"
backup_weekly_dir: "{{ backup_base_dir }}/weekly"
backup_monthly_dir: "{{ backup_base_dir }}/monthly"
# Retention settings
backup_retention_days: 7
backup_retention_weeks: 4
backup_retention_months: 6
# Source directories to backup
backup_sources:
- "{{ minecraft_server_dir }}"
- "{{ minecraft_base_dir }}/logs"
# Backup schedule
backup_daily_time: "02:00"
backup_weekly_time: "03:00"
backup_weekly_day: "0" # Sunday
backup_monthly_time: "04:00"
backup_monthly_day: "1" # First day of month
# Rsync options
rsync_options: "-avz --delete"
backup_retention_daily: 7
backup_retention_weekly: 4
backup_retention_monthly: 3
backup_time_daily: "03:00"
backup_time_weekly: "04:00"
backup_time_monthly: "05:00"

View File

@@ -1,26 +1,6 @@
---
# Backup configuration
backup_base_dir: "{{ minecraft_base_dir }}/backups"
backup_daily_dir: "{{ backup_base_dir }}/daily"
backup_weekly_dir: "{{ backup_base_dir }}/weekly"
backup_monthly_dir: "{{ backup_base_dir }}/monthly"
# Retention settings
backup_retention_days: 7
backup_retention_weeks: 4
backup_retention_months: 6
# Source directories to backup
backup_sources:
- "{{ minecraft_server_dir }}"
- "{{ minecraft_base_dir }}/logs"
# Backup schedule
backup_daily_time: "02:00"
backup_weekly_time: "03:00"
backup_weekly_day: "0" # Sunday
backup_monthly_time: "04:00"
backup_monthly_day: "1" # First day of month
# Rsync options
rsync_options: "-avz --delete"
- name: reload cron
ansible.builtin.service:
name: cron
state: reloaded
listen: reload cron service

View File

@@ -1,18 +0,0 @@
---
- name: Create backup directories
ansible.builtin.file:
path: "{{ item }}"
state: directory
owner: "{{ minecraft_user }}"
group: "{{ minecraft_group }}"
mode: '0755'
loop:
- "{{ backup_base_dir }}"
- "{{ backup_daily_dir }}"
- "{{ backup_weekly_dir }}"
- "{{ backup_monthly_dir }}"
- name: Install rsync
ansible.builtin.apt:
name: rsync
state: present

View File

@@ -0,0 +1,9 @@
---
- name: Create backup directories
ansible.builtin.file:
path: "{{ item }}"
state: directory
owner: "{{ minecraft_user }}"
group: "{{ minecraft_group }}"
mode: '0755'
loop: "{{ backup_dirs }}"

View File

@@ -0,0 +1,6 @@
---
- name: Install rsync
ansible.builtin.apt:
name: rsync
state: present
when: ansible_os_family == "Debian"

View File

@@ -1,17 +0,0 @@
---
- name: Create daily backup script
ansible.builtin.template:
src: backup-daily.sh.j2
dest: "{{ minecraft_tools_dir }}/backup-daily.sh"
owner: "{{ minecraft_user }}"
group: "{{ minecraft_group }}"
mode: '0755'
- name: Setup daily backup cron job
ansible.builtin.cron:
name: "Minecraft daily backup"
minute: "0"
hour: "{{ backup_daily_time.split(':')[0] }}"
job: "{{ minecraft_tools_dir }}/backup-daily.sh"
user: "{{ minecraft_user }}"
state: present

View File

@@ -0,0 +1,16 @@
---
- name: Create backup script
ansible.builtin.template:
src: backup.sh.j2
dest: "{{ minecraft_base_dir }}/backup.sh"
owner: "{{ minecraft_user }}"
group: "{{ minecraft_group }}"
mode: '0755'
- name: Create restore script
ansible.builtin.template:
src: restore.sh.j2
dest: "{{ minecraft_base_dir }}/restore.sh"
owner: "{{ minecraft_user }}"
group: "{{ minecraft_group }}"
mode: '0755'

View File

@@ -1,18 +0,0 @@
---
- name: Create weekly backup script
ansible.builtin.template:
src: backup-weekly.sh.j2
dest: "{{ minecraft_tools_dir }}/backup-weekly.sh"
owner: "{{ minecraft_user }}"
group: "{{ minecraft_group }}"
mode: '0755'
- name: Setup weekly backup cron job
ansible.builtin.cron:
name: "Minecraft weekly backup"
minute: "0"
hour: "{{ backup_weekly_time.split(':')[0] }}"
weekday: "{{ backup_weekly_day }}"
job: "{{ minecraft_tools_dir }}/backup-weekly.sh"
user: "{{ minecraft_user }}"
state: present

View File

@@ -0,0 +1,29 @@
---
- name: Setup daily backup cron job
ansible.builtin.cron:
name: "Minecraft daily backup"
user: "{{ minecraft_user }}"
hour: "{{ backup_time_daily.split(':')[0] }}"
minute: "{{ backup_time_daily.split(':')[1] }}"
job: "{{ minecraft_base_dir }}/backup.sh daily"
notify: reload cron service
- name: Setup weekly backup cron job
ansible.builtin.cron:
name: "Minecraft weekly backup"
user: "{{ minecraft_user }}"
hour: "{{ backup_time_weekly.split(':')[0] }}"
minute: "{{ backup_time_weekly.split(':')[1] }}"
weekday: "0"
job: "{{ minecraft_base_dir }}/backup.sh weekly"
notify: reload cron service
- name: Setup monthly backup cron job
ansible.builtin.cron:
name: "Minecraft monthly backup"
user: "{{ minecraft_user }}"
hour: "{{ backup_time_monthly.split(':')[0] }}"
minute: "{{ backup_time_monthly.split(':')[1] }}"
day: "1"
job: "{{ minecraft_base_dir }}/backup.sh monthly"
notify: reload cron service

View File

@@ -1,18 +0,0 @@
---
- name: Create monthly backup script
ansible.builtin.template:
src: backup-monthly.sh.j2
dest: "{{ minecraft_tools_dir }}/backup-monthly.sh"
owner: "{{ minecraft_user }}"
group: "{{ minecraft_group }}"
mode: '0755'
- name: Setup monthly backup cron job
ansible.builtin.cron:
name: "Minecraft monthly backup"
minute: "0"
hour: "{{ backup_monthly_time.split(':')[0] }}"
day: "{{ backup_monthly_day }}"
job: "{{ minecraft_tools_dir }}/backup-monthly.sh"
user: "{{ minecraft_user }}"
state: present

View File

@@ -0,0 +1,26 @@
---
- name: Create restore script
ansible.builtin.template:
src: restore.sh.j2
dest: "{{ minecraft_base_dir }}/restore.sh"
owner: "{{ minecraft_user }}"
group: "{{ minecraft_group }}"
mode: '0755'
- name: Stop minecraft server before restore
ansible.builtin.systemd:
name: "{{ minecraft_service_name }}"
state: stopped
when: restore_backup_type is defined
- name: Execute restore
ansible.builtin.command:
cmd: "{{ minecraft_base_dir }}/restore.sh {{ restore_backup_type }} {{ restore_backup_date | default('latest') }}"
become_user: "{{ minecraft_user }}"
when: restore_backup_type is defined
- name: Start minecraft server after restore
ansible.builtin.systemd:
name: "{{ minecraft_service_name }}"
state: started
when: restore_backup_type is defined

View File

@@ -1,38 +0,0 @@
---
- name: Create backup utility script
ansible.builtin.copy:
content: |
#!/bin/bash
# Backup utility functions
# Function to stop Minecraft server safely
stop_minecraft() {
if systemctl is-active --quiet minecraft; then
echo "Stopping Minecraft server..."
/usr/local/bin/mcrcon -H 127.0.0.1 -P {{ rcon_port }} -p "{{ rcon_password }}" "say Server backup starting in 30 seconds..."
sleep 30
/usr/local/bin/mcrcon -H 127.0.0.1 -P {{ rcon_port }} -p "{{ rcon_password }}" stop
sleep 10
fi
}
# Function to start Minecraft server
start_minecraft() {
if ! systemctl is-active --quiet minecraft; then
echo "Starting Minecraft server..."
systemctl start minecraft
fi
}
# Function to clean old backups
clean_old_backups() {
local backup_dir=$1
local retention_days=$2
find "$backup_dir" -type f -mtime +$retention_days -delete
find "$backup_dir" -type d -empty -delete
}
dest: "{{ minecraft_tools_dir }}/backup-functions.sh"
owner: "{{ minecraft_user }}"
group: "{{ minecraft_group }}"
mode: '0755'

View File

@@ -1,8 +0,0 @@
---
- name: Create restore backup script
ansible.builtin.template:
src: restore-backup.sh.j2
dest: "{{ minecraft_tools_dir }}/restore-backup.sh"
owner: "{{ minecraft_user }}"
group: "{{ minecraft_group }}"
mode: '0755'

View File

@@ -1,18 +1,16 @@
---
- name: Include backup directories creation tasks
ansible.builtin.include_tasks: 01-create-backup-directories.yml
- name: Include create backup directories tasks
ansible.builtin.include_tasks: 01-create-backup-dirs.yml
- name: Include daily backup setup tasks
ansible.builtin.include_tasks: 02-setup-daily-backup.yml
- name: Include install rsync tasks
ansible.builtin.include_tasks: 02-install-rsync.yml
- name: Include weekly backup setup tasks
ansible.builtin.include_tasks: 03-setup-weekly-backup.yml
- name: Include configure backup script tasks
ansible.builtin.include_tasks: 03-configure-backup-script.yml
- name: Include monthly backup setup tasks
ansible.builtin.include_tasks: 04-setup-monthly-backup.yml
- name: Include setup cron tasks
ansible.builtin.include_tasks: 04-setup-cron.yml
- name: Include backup scripts setup tasks
ansible.builtin.include_tasks: 05-setup-backup-scripts.yml
- name: Include restore scripts setup tasks
ansible.builtin.include_tasks: 06-setup-restore-scripts.yml
- name: Include restore backup tasks
ansible.builtin.include_tasks: 05-restore-backup.yml
when: restore_backup | default(false)

View File

@@ -1,42 +0,0 @@
#!/bin/bash
# Daily Minecraft Backup Script
# Generated by Ansible
set -e
# Source backup functions
source {{ minecraft_tools_dir }}/backup-functions.sh
# Configuration
BACKUP_DIR="{{ backup_daily_dir }}"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_NAME="daily_backup_$DATE"
LOG_FILE="{{ minecraft_base_dir }}/logs/backup-daily.log"
# Create log entry
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting daily backup..." >> "$LOG_FILE"
# Create backup directory
mkdir -p "$BACKUP_DIR/$BACKUP_NAME"
# Stop Minecraft server for consistent backup
stop_minecraft
# Perform backup
{% for source in backup_sources %}
echo "Backing up {{ source }}..." >> "$LOG_FILE"
rsync {{ rsync_options }} "{{ source }}/" "$BACKUP_DIR/$BACKUP_NAME/$(basename {{ source }})/"
{% endfor %}
# Start Minecraft server
start_minecraft
# Clean old daily backups
clean_old_backups "$BACKUP_DIR" {{ backup_retention_days }}
# Compress backup
cd "$BACKUP_DIR"
tar -czf "${BACKUP_NAME}.tar.gz" "$BACKUP_NAME"
rm -rf "$BACKUP_NAME"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Daily backup completed: ${BACKUP_NAME}.tar.gz" >> "$LOG_FILE"

View File

@@ -1,42 +0,0 @@
#!/bin/bash
# Monthly Minecraft Backup Script
# Generated by Ansible
set -e
# Source backup functions
source {{ minecraft_tools_dir }}/backup-functions.sh
# Configuration
BACKUP_DIR="{{ backup_monthly_dir }}"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_NAME="monthly_backup_$DATE"
LOG_FILE="{{ minecraft_base_dir }}/logs/backup-monthly.log"
# Create log entry
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting monthly backup..." >> "$LOG_FILE"
# Create backup directory
mkdir -p "$BACKUP_DIR/$BACKUP_NAME"
# Stop Minecraft server for consistent backup
stop_minecraft
# Perform backup
{% for source in backup_sources %}
echo "Backing up {{ source }}..." >> "$LOG_FILE"
rsync {{ rsync_options }} "{{ source }}/" "$BACKUP_DIR/$BACKUP_NAME/$(basename {{ source }})/"
{% endfor %}
# Start Minecraft server
start_minecraft
# Clean old monthly backups (convert months to days approximately)
clean_old_backups "$BACKUP_DIR" $(({{ backup_retention_months }} * 30))
# Compress backup
cd "$BACKUP_DIR"
tar -czf "${BACKUP_NAME}.tar.gz" "$BACKUP_NAME"
rm -rf "$BACKUP_NAME"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Monthly backup completed: ${BACKUP_NAME}.tar.gz" >> "$LOG_FILE"

View File

@@ -1,42 +0,0 @@
#!/bin/bash
# Weekly Minecraft Backup Script
# Generated by Ansible
set -e
# Source backup functions
source {{ minecraft_tools_dir }}/backup-functions.sh
# Configuration
BACKUP_DIR="{{ backup_weekly_dir }}"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_NAME="weekly_backup_$DATE"
LOG_FILE="{{ minecraft_base_dir }}/logs/backup-weekly.log"
# Create log entry
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting weekly backup..." >> "$LOG_FILE"
# Create backup directory
mkdir -p "$BACKUP_DIR/$BACKUP_NAME"
# Stop Minecraft server for consistent backup
stop_minecraft
# Perform backup
{% for source in backup_sources %}
echo "Backing up {{ source }}..." >> "$LOG_FILE"
rsync {{ rsync_options }} "{{ source }}/" "$BACKUP_DIR/$BACKUP_NAME/$(basename {{ source }})/"
{% endfor %}
# Start Minecraft server
start_minecraft
# Clean old weekly backups (convert weeks to days)
clean_old_backups "$BACKUP_DIR" $(({{ backup_retention_weeks }} * 7))
# Compress backup
cd "$BACKUP_DIR"
tar -czf "${BACKUP_NAME}.tar.gz" "$BACKUP_NAME"
rm -rf "$BACKUP_NAME"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Weekly backup completed: ${BACKUP_NAME}.tar.gz" >> "$LOG_FILE"

View File

@@ -0,0 +1,69 @@
#!/bin/bash
# {{ ansible_managed }}
BACKUP_TYPE=$1
BACKUP_BASE="{{ backup_base_dir }}"
SERVER_DIR="{{ minecraft_server_dir }}"
TOOLS_DIR="{{ minecraft_tools_dir }}"
RCON_CMD="{{ minecraft_tools_dir }}/mcrcon -H localhost -P {{ rcon_port }} -p {{ rcon_password }}"
DATE=$(date +%Y%m%d_%H%M%S)
# Function to clean old backups
clean_old_backups() {
local dir=$1
local keep=$2
ls -1t $dir/*.tar.gz 2>/dev/null | tail -n +$((keep+1)) | xargs rm -f
}
# Function to perform backup
perform_backup() {
local backup_dir=$1
local retention=$2
echo "Starting $BACKUP_TYPE backup at $(date)"
# Notify players
$RCON_CMD "say Server backup starting in 10 seconds..."
sleep 10
# Save world
$RCON_CMD "save-all"
sleep 5
# Disable auto-save
$RCON_CMD "save-off"
# Create backup
tar -czf "$backup_dir/minecraft_${BACKUP_TYPE}_${DATE}.tar.gz" \
-C "{{ minecraft_base_dir }}" \
server/ sources/ tools/ \
--exclude='*.log' \
--exclude='logs/*'
# Re-enable auto-save
$RCON_CMD "save-on"
# Clean old backups
clean_old_backups "$backup_dir" "$retention"
# Notify players
$RCON_CMD "say Server backup completed!"
echo "Backup completed at $(date)"
}
case "$BACKUP_TYPE" in
daily)
perform_backup "{{ backup_daily_dir }}" "{{ backup_retention_daily }}"
;;
weekly)
perform_backup "{{ backup_weekly_dir }}" "{{ backup_retention_weekly }}"
;;
monthly)
perform_backup "{{ backup_monthly_dir }}" "{{ backup_retention_monthly }}"
;;
*)
echo "Usage: $0 {daily|weekly|monthly}"
exit 1
;;
esac

View File

@@ -1,116 +0,0 @@
#!/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"

View File

@@ -0,0 +1,61 @@
#!/bin/bash
# {{ ansible_managed }}
BACKUP_TYPE=$1
BACKUP_DATE=$2
BACKUP_BASE="{{ backup_base_dir }}"
SERVER_DIR="{{ minecraft_server_dir }}"
# Function to find backup file
find_backup() {
local type=$1
local date=$2
local dir=""
case "$type" in
daily) dir="{{ backup_daily_dir }}" ;;
weekly) dir="{{ backup_weekly_dir }}" ;;
monthly) dir="{{ backup_monthly_dir }}" ;;
*) echo "Invalid backup type"; exit 1 ;;
esac
if [ "$date" = "latest" ]; then
ls -1t $dir/*.tar.gz 2>/dev/null | head -n 1
else
ls $dir/*${date}*.tar.gz 2>/dev/null | head -n 1
fi
}
# Main restore process
BACKUP_FILE=$(find_backup "$BACKUP_TYPE" "$BACKUP_DATE")
if [ -z "$BACKUP_FILE" ]; then
echo "No backup found for type: $BACKUP_TYPE, date: $BACKUP_DATE"
exit 1
fi
echo "Restoring from: $BACKUP_FILE"
# Create restore directory
RESTORE_DIR="{{ minecraft_base_dir }}/restore_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$RESTORE_DIR"
# Extract backup
tar -xzf "$BACKUP_FILE" -C "$RESTORE_DIR"
# Backup current server
echo "Backing up current server state..."
tar -czf "{{ backup_base_dir }}/pre_restore_$(date +%Y%m%d_%H%M%S).tar.gz" \
-C "{{ minecraft_base_dir }}" server/
# Restore files
echo "Restoring server files..."
rsync -av --delete "$RESTORE_DIR/server/" "$SERVER_DIR/"
# Set permissions
chown -R {{ minecraft_user }}:{{ minecraft_group }} "$SERVER_DIR"
# Clean up
rm -rf "$RESTORE_DIR"
echo "Restore completed successfully!"

View File

@@ -1,16 +1,11 @@
---
# Internal backup variables
backup_script_names:
- backup-daily.sh
- backup-weekly.sh
- backup-monthly.sh
- restore-backup.sh
- backup-functions.sh
backup_dirs:
- "{{ backup_base_dir }}"
- "{{ backup_daily_dir }}"
- "{{ backup_weekly_dir }}"
- "{{ backup_monthly_dir }}"
cron_jobs:
- name: "Minecraft daily backup"
script: "backup-daily.sh"
- name: "Minecraft weekly backup"
script: "backup-weekly.sh"
- name: "Minecraft monthly backup"
script: "backup-monthly.sh"
backup_source_dirs:
- "{{ minecraft_server_dir }}"
- "{{ minecraft_sources_dir }}"
- "{{ minecraft_tools_dir }}"