From 195e28421e8f59488fe2abcf1b4a2fd64947d93b Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Fri, 24 Dec 2021 22:00:22 -0600 Subject: [PATCH] Added backup function --- CentOS/7-Days-To-Die/7D2D-Manage.bash | 128 ++++++++++++++++++++------ 1 file changed, 99 insertions(+), 29 deletions(-) diff --git a/CentOS/7-Days-To-Die/7D2D-Manage.bash b/CentOS/7-Days-To-Die/7D2D-Manage.bash index 0a10008..1540c58 100644 --- a/CentOS/7-Days-To-Die/7D2D-Manage.bash +++ b/CentOS/7-Days-To-Die/7D2D-Manage.bash @@ -129,14 +129,18 @@ start_server() { fi log "info" "Creating new session for $(important "${server_name}") as session $(important "${server_session_name}")" - tmux new-session -d -s "${server_session_name}" ~/.bin/7D2D-Serv-Handler "${server_directory}" + tmux new-session -d -s "${server_session_name}" + tmux send-keys \ +"until ${server_directory}/startserver.sh -configfile=${server_directory}/serverconfig.xml; do + echo 'Server died with code $?, restarting in 60 seconds...' >&2 + sleep 60; +done" C-m log "info" "Finished starting $(important "${server_name}") on port $(important "${server_port}") as tmux session $(important "${server_session_name}")" } kill_server() { local prefix - local tmux_response local server_id server_id="" @@ -248,7 +252,7 @@ update() { printf "Usage: %s\n" \ "update [OPTIONS] --server | -s - Starts the given server id + Updates the given server id Example: --server 3" @@ -285,11 +289,7 @@ update() { server_directory="${BASE_DIR}/${server_name}" backup_dir="${HOME}/Server-Backups/${server_name}" backup_full_path="${backup_dir}/$(date +%s).tar.gz" - log "info" "Backing up server $(important "${server_name}") to $(important "${backup_full_path}"), this may take a while" - mkdir -p "${backup_dir}" - tar cf - "${server_directory}" -P | pv -s $(du -sb "${server_directory}" | awk '{print $1}') | \ - gzip > "${backup_full_path}" - tar -czf "${server_directory}" "${backup_dir}"/ + backup -s "${server_id}" log "info" "Updating server $(important "${server_name}") located at $(important "${server_directory}")..." steamcmd +force_install_dir "${server_directory}" +login anonymous +app_update 294420 validate +quit @@ -297,6 +297,66 @@ update() { } +backup() { + + local server_id + server_id="" + + while :; do + case ${1} in + -h | -\? | --help) + printf "Usage: %s\n" \ +"backup [OPTIONS] + --server | -s + Backups the given server id + + Example: + --server 3" + + exit + ;; + --) # End of all options. + break + ;; + --server | -s) + shift + server_id="${1}" + [[ -z "${server_id}" ]] && log "error" "No server id passed" && exit 1 + ;; + -?*) + printf 'Unknown option: %s\n' "$1" >&2 + ;; + *) # Default case: No more options, so break out of the loop. + break ;; + esac + shift + done + + [[ -z "${server_id}" ]] && log "error" "No server id passed" && exit 1 + + + # Kill the server to ensure a smooth backup + kill_server -s "${server_id}" >/dev/null 2>&1 + + local backup_dir + local backup_full_path + local server_directory + local server_name + server_name="Server-${server_id}" + server_directory="${BASE_DIR}/${server_name}" + backup_dir="${HOME}/Server-Backups/${server_name}" + backup_full_path="${backup_dir}/$(date +%s).tar.gz" + + [[ ! -d "${server_directory}" ]] && log "info" "The server $(important "${server_name}") had no directory located at $(important "${server_directory}")" && exit 1 + + log "info" "Backing up server $(important "${server_name}") to $(important "${backup_full_path}"), this may take a while" + mkdir -p "${backup_dir}" + set -x + tar cf - "${server_directory}" -P | pv -s "$(du -sb "${server_directory}" | awk '{print $1}')" | \ + gzip > "${backup_full_path}" + tar -czf "${server_directory}" "${backup_dir}"/ +} + list_servers() { local picked_option picked_option="" @@ -340,26 +400,29 @@ list_servers() { log "error" "An option must be passed for list, check list -h" && return 1 - if (( picked_option == 0 )); then - log "debug" "Listing running servers" - local tmux_sessions - tmux_sessions="$(tmux list-sessions)" >/dev/null 2>&1 - if [[ ! "${?}" -eq "0" ]]; then - important "No servers currently running." - fi - while read -r; do - if [[ "${REPLY}" = *"-Server-"* ]]; then - local running_server - running_server="$(echo "${REPLY}" | cut -d ":" -f1)" - important "${running_server}" + case "${picked_option}" in + 1) + log "debug" "Listing running servers" + local tmux_sessions + tmux_sessions="$(tmux list-sessions)" >/dev/null 2>&1 + if [[ ! "${?}" -eq "0" ]]; then + important "No servers currently running." fi - done <<< "${tmux_sessions}" - elif (( picked_option == 1 )); then - log "debug" "Listing installed servers" - while read -r; do - important "${BASE_DIR}/${REPLY}" - done <<< "$(find "${BASE_DIR}" -name "startserver.sh" | cut -d "/" -f5)" - fi + while read -r; do + if [[ "${REPLY}" = *"-Server-"* ]]; then + local running_server + running_server="$(echo "${REPLY}" | cut -d ":" -f1)" + important "${running_server}" + fi + done <<< "${tmux_sessions}" + ;; + 2) + log "debug" "Listing installed servers" + while read -r; do + important "${BASE_DIR}/${REPLY}" + done <<< "$(find "${BASE_DIR}" -name "startserver.sh" | cut -d "/" -f5)" + ;; + esac } usage() { @@ -375,7 +438,7 @@ usage() { # Yes # printf "Usage: %s\n" \ - "$(basename ${0}) -h + "$(basename "${0}") -h start Exposes options to start 7 Days To Die Servers, pass -h to it for details kill @@ -385,7 +448,9 @@ usage() { update Exposes options to update 7 Days To Die Servers, pass -h to it for details list - Exposes options to list 7 Days To Die Servers, pass -h to it for details" + Exposes options to list 7 Days To Die Servers, pass -h to it for details + backup + Exposes options to backup 7 Days To Die Servers, pass -h to it for details" } @@ -437,6 +502,11 @@ main() { list_servers "$@" break ;; + backup | b) + shift + backup "$@" + break + ;; -?*) printf "Unknown option: %s\n" "$1" >&2 usage