Add minecraft backup

This commit is contained in:
Ryan Cavicchioni 2019-10-27 18:36:54 -05:00
parent babccb4991
commit d9b189158c
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
5 changed files with 185 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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;
}
}