Compare commits

...

5 Commits

6 changed files with 152 additions and 6 deletions

View File

@ -1,2 +1,4 @@
---
rclone_package_url: https://downloads.rclone.org/v1.50.1/rclone-v1.50.1-linux-amd64.deb
rclone_arch: amd64
rclone_version: 1.50.2
rclone_package_url: "https://downloads.rclone.org/v{{ rclone_version }}/rclone-v{{ rclone_version }}-linux-{{ rclone_arch }}.deb"

View File

@ -0,0 +1,96 @@
#!/bin/bash
SERVICE=minecraft.service
VAR_DIR=/opt/minecraft/var
WAIT=30
VERBOSE=${VERBOSE:-4}
error_exit() {
printf "%s\n" "$1" >&2
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 [ "$1" == "pre" ]; then
printf "stopping %s\n" $SERVICE
if ! stop_server $SERVICE; then
error_exit "Failed to stop $SERVICE"
fi
printf "checking for open files\n"
if ! open_files $VAR_DIR; then
error_exit "Open files exist in $VAR_DIR"
fi
elif [ "$1" == "post" ]; then
printf "starting %s\n" $SERVICE
if ! start_server $SERVICE; then
error_exit "Failed to start $SERVICE"
fi
fi
}
main "$@"

15
roles/restic/files/restic-job.sh Normal file → Executable file
View File

@ -20,6 +20,7 @@ fi
JOB_PATH="${RESTIC_ETC_PATH}/jobs/${JOB}"
JOB_ENV="${JOB_PATH}/env.sh"
HOOKS_PATH="${JOB_PATH}/hooks.d"
if [ ! -r "$JOB_ENV" ]; then
error_exit "${JOB_ENV} does not exist"
@ -48,6 +49,13 @@ if [ -z "${PATHS+x}" ]; then
error_exit "\$PATHS is not set"
fi
printf "job '%s' started\n" "$JOB"
if [ -d "${HOOKS_PATH}" ]; then
printf "running '%s' job pre-hooks\n" "$JOB"
run-parts -v -a pre "${HOOKS_PATH}"
fi
counter=0
sleep=1
rc=1
@ -72,4 +80,11 @@ done
if [ $rc -ne 0 ] && [ $counter -eq "$MAX_ATTEMPTS" ]; then
printf "restic job timed out, exiting\n"
else
printf "job '%s' complete\n" "$JOB"
fi
if [ -d "${HOOKS_PATH}" ]; then
printf "running '%s' job post-hooks\n" "$JOB"
run-parts -v -a post "${HOOKS_PATH}"
fi

16
roles/restic/files/restic-tidy.sh Normal file → Executable file
View File

@ -10,8 +10,10 @@ error_exit() {
RESTIC_ETC_PATH=${RESTIC_ETC_PATH:-/etc/restic}
RESTIC_PATH=${RESTIC_PATH:-/usr/local/bin/restic}
# initial sleep
MAX_ATTEMPTS=60
MAX_SLEEP=43200 # 12 hours
SLEEP="$(((RANDOM % MAX_SLEEP) + 1))s"
REPO=$1
if [ -z "$REPO" ]; then
@ -34,8 +36,16 @@ KEEP_WEEKLY=${KEEP_WEEKLY:-5}
KEEP_MONTHLY=${KEEP_MONTHLY:-12}
KEEP_YEARLY=${KEEP_YEARLY:-10}
# initial sleep
sleep "$(((RANDOM % MAX_SLEEP) + 1))s"
printf "started, keep hourly:%d daily:%d weekly:%d monthly:%d year:%d\n" \
"$KEEP_HOURLY" \
"$KEEP_DAILY" \
"$KEEP_WEEKLY" \
"$KEEP_MONTHLY" \
"$KEEP_YEARLY"
printf "sleeping for %s (initial)\n" $SLEEP
sleep $SLEEP
counter=0
sleep=1
@ -65,4 +75,6 @@ done
if [ $rc -ne 0 ] && [ $counter -eq "$MAX_ATTEMPTS" ]; then
printf "tidy timed out, exiting\n"
else
printf "complete\n"
fi

View File

@ -7,7 +7,19 @@
mode: 0755
state: directory
- name: create repo environment helper
- name: create job hooks
copy:
src: "hooks/{{ hook_item }}"
dest: "{{ restic_etc_path }}/jobs/{{ item.name }}/hooks.d/{{ hook_item | basename | splitext | first }}"
owner: root
group: root
mode: 0755
loop: "{{ item.hooks | default([]) }}"
loop_control:
loop_var: hook_item
when: item.hooks is defined
- name: create job environment helper
template:
src: job-env.sh.j2
dest: "{{ restic_etc_path }}/jobs/{{ item.name }}/env.sh"
@ -15,6 +27,15 @@
group: root
mode: 0400
- name: install hooks
file:
path: "{{ restic_etc_path }}/jobs/{{ item.name }}/hooks.d"
owner: root
group: root
mode: 0755
state: directory
when: item.hooks is defined and item.hooks | length
- name: create job exclude file
template:
src: exclude.txt.j2
@ -33,4 +54,4 @@
weekday: "{{ item.cron.weekday | default(omit) }}"
user: "{{ item.cron.user | default('root') }}"
state: "{{ item.cron.state | default('present') }}"
job: "{{ restic_bin_path }}/restic-job {{ item.name }}"
job: "( {{ restic_bin_path }}/restic-job {{ item.name }} | logger --id=$$ -t restic-job -p user.info ) 2>&1 | logger --id=$$ -t restic-job -p user.err"

View File

@ -60,4 +60,4 @@
minute: "{{ 60 | random(seed=inventory_hostname) }}"
user: root
state: present
job: "{{ restic_bin_path }}/restic-tidy {{ item.name }}"
job: "( {{ restic_bin_path }}/restic-tidy {{ item.name }} | logger --id=$$ -t restic-tidy -p user.info ) 2>&1 | logger --id=$$ -t restic-tidy -p user.err"