check new version
This commit is contained in:
69
roles/04-backups/templates/backup.sh.j2
Normal file
69
roles/04-backups/templates/backup.sh.j2
Normal 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
|
Reference in New Issue
Block a user