From d9b189158c72f4c9aac80e8184c03db41a2f9ae0 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sun, 27 Oct 2019 18:36:54 -0500 Subject: [PATCH] Add minecraft backup --- roles/minecraft/defaults/main.yaml | 4 + roles/minecraft/files/minecraft-backup.sh | 114 ++++++++++++++++++ roles/minecraft/tasks/main.yaml | 49 ++++++++ .../minecraft/templates/minecraft.service.j2 | 4 +- roles/minecraft/templates/nginx.conf.j2 | 15 +++ 5 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 roles/minecraft/files/minecraft-backup.sh create mode 100644 roles/minecraft/templates/nginx.conf.j2 diff --git a/roles/minecraft/defaults/main.yaml b/roles/minecraft/defaults/main.yaml index 55b72f6..e61871e 100644 --- a/roles/minecraft/defaults/main.yaml +++ b/roles/minecraft/defaults/main.yaml @@ -6,6 +6,8 @@ minecraft_service_name: minecraft.service minecraft_service_state: started minecraft_service_enabled: yes +minecraft_port: 25565 + minecraft_user: minecraft minecraft_group: minecraft @@ -13,3 +15,5 @@ minecraft_jar_url: https://launcher.mojang.com/v1/objects/3dc3d84a581f14691199cf minecraft_opt_path: /opt/minecraft minecraft_var_path: "{{ minecraft_opt_path }}/var" + +minecraft_syslog_facility: local5 diff --git a/roles/minecraft/files/minecraft-backup.sh b/roles/minecraft/files/minecraft-backup.sh new file mode 100644 index 0000000..b0dd96b --- /dev/null +++ b/roles/minecraft/files/minecraft-backup.sh @@ -0,0 +1,114 @@ +#!/bin/bash + +SERVICE=minecraft.service +BACKUP_DIR=/opt/minecraft/backup +VAR_DIR=/opt/minecraft/var +SCRATCH=$(mktemp -p /var/tmp -d minecraft.XXXXXXXXXX) +NICE=15 +TAR=minecraft-$(date +%Y-%m-%d).tar +WAIT=30 + +finish() { + printf "Cleaning up ...\n" + rm -rf "$SCRATCH" + if ! systemctl -q is-active "$SERVICE"; then + start_server + fi +} +trap finish EXIT + +error_exit() { + printf "%s\n" "$1" + exit 1 +} + +stop_server() { + local unit="${1:-$SERVICE}" + local attempts="${2:-$WAIT}" + + if ! systemctl -q is-active "$unit"; then + printf "%s is already stopped\n" "$unit" + return 0 + fi + + systemctl -q stop "$unit" + + while systemctl -q is-active "$unit"; do + printf "Waiting for %s to stop ... (%s)\n" "$unit" "$attempts" + ((attempts--)) + if ((attempts == 0)); then + return 1 + fi + sleep 1 + done + + return 0 +} + +start_server() { + local unit="${1:-$SERVICE}" + local attempts="${2:-$WAIT}" + + if systemctl -q is-active "$unit"; then + printf "%s is already started\n" "$unit" + return 0 + fi + + systemctl -q start "$unit" + + while ! systemctl -q is-active "$unit"; do + printf "Waiting for %s to start ... (%d)\n" "$unit" "$attempts" + ((attempts--)) + if ((attempts == 0)); then + return 1 + fi + sleep 1 + done + + return 0 +} + + +open_files() { + local dir=${1-$VAR_DIR} + local attempts="${2:-$WAIT}" + + while (($(lsof +D "$dir" | wc -l) > 0)); do + printf "Waiting for open files ... (%d)\n" "$attempts" + ((attempts--)) + if ((attempts == 0)); then + return 1 + fi + sleep 1 + done + + return 0 +} + + +main() { + if ! stop_server; then + error_exit "Failed to stop $SERVICE" + fi + + if ! open_files; then + error_exit "Open files exist in $VAR_DIR" + fi + + if ! tar -cf "$SCRATCH/$TAR" -C "$VAR_DIR" world; then + error_exit "Archive failed" + fi + + if ! start_server; then + error_exit "Failed to start $SERVICE" + fi + + if ! nice -n "$NICE" xz -9 "$SCRATCH/$TAR"; then + error_exit "Failed to compress $SCRATCH/$TAR" + else + mv "$SCRATCH/$TAR.xz" "$BACKUP_DIR" + ln -nfs "$BACKUP_DIR/$TAR.xz" "$BACKUP_DIR/minecraft-latest.tar.xz" + fi +} + +main diff --git a/roles/minecraft/tasks/main.yaml b/roles/minecraft/tasks/main.yaml index 5d8a22a..055b5cc 100644 --- a/roles/minecraft/tasks/main.yaml +++ b/roles/minecraft/tasks/main.yaml @@ -33,6 +33,7 @@ - "{{ minecraft_opt_path }}" - "{{ minecraft_opt_path }}/bin" - "{{ minecraft_opt_path }}/etc" + - "{{ minecraft_opt_path }}/backup" - name: create minecraft var directory file: @@ -92,6 +93,7 @@ owner: "{{ minecraft_user }}" group: "{{ minecraft_group }}" mode: "0644" + force: no notify: restart minecraft - name: configure whitelist @@ -101,4 +103,51 @@ owner: "{{ minecraft_user }}" group: "{{ minecraft_group }}" mode: "0644" + force: no notify: restart minecraft + +- name: install backup script + copy: + src: minecraft-backup.sh + dest: "{{ minecraft_opt_path }}/bin/minecraft-backup" + owner: root + group: root + mode: 0700 + +- name: backup cron job + cron: + name: minecraft backup + minute: "0" + hour: "9" + user: root + job: "{{ minecraft_opt_path }}/bin/minecraft-backup" + +- name: set world download password + htpasswd: + path: "{{ minecraft_opt_path }}/etc/htpasswd" + name: minecraft + password: minecraft + owner: root + group: root + mode: 0644 + when: "'nginx' in ansible_play_role_names" + +- name: configure nginx + template: + src: nginx.conf.j2 + dest: /etc/nginx/sites-available/minecraft + owner: root + group: root + mode: 0644 + when: "'nginx' in ansible_play_role_names" + notify: reload nginx + +- name: activate site + file: + src: /etc/nginx/sites-available/minecraft + dest: /etc/nginx/sites-enabled/minecraft + owner: root + group: root + state: link + when: "'nginx' in ansible_play_role_names" + notify: reload nginx diff --git a/roles/minecraft/templates/minecraft.service.j2 b/roles/minecraft/templates/minecraft.service.j2 index 94b959e..e8e558d 100644 --- a/roles/minecraft/templates/minecraft.service.j2 +++ b/roles/minecraft/templates/minecraft.service.j2 @@ -5,13 +5,15 @@ Description=Minecraft server After=network.target [Service] -ExecStart=/usr/bin/java -Xmx{{ minecraft_java_xmx | default('1024M') }} -Xms{{ minecraft_java_xms | default('1024M') }} -jar {{ minecraft_opt_path }}/bin/server.jar --nogui +ExecStart=/usr/bin/java -Xmx{{ minecraft_java_xmx | default('1024M') }} -Xms{{ minecraft_java_xms | default('1024M') }} -jar {{ minecraft_opt_path }}/bin/server.jar nogui +SuccessExitStatus=143 Type=simple User={{ minecraft_user }} Group={{ minecraft_group }} WorkingDirectory={{ minecraft_var_path }} Restart=on-failure SyslogIdentifier=minecraft +SyslogFacility={{ minecraft_syslog_facility }} [Install] WantedBy=multi-user.target diff --git a/roles/minecraft/templates/nginx.conf.j2 b/roles/minecraft/templates/nginx.conf.j2 new file mode 100644 index 0000000..8db251f --- /dev/null +++ b/roles/minecraft/templates/nginx.conf.j2 @@ -0,0 +1,15 @@ +# {{ ansible_managed }} + +server { + listen 80; + listen [::]:80; + server_name {{ ansible_fqdn }}; + + root {{ minecraft_opt_path }}/backup; + + location / { + autoindex on; + auth_basic "Access Restricted"; + auth_basic_user_file {{ minecraft_opt_path }}/etc/htpasswd; + } +}