Added backup function

This commit is contained in:
Price Hiller 2021-12-24 22:00:22 -06:00
parent 94d0c59955
commit 195e28421e

View File

@ -129,14 +129,18 @@ start_server() {
fi fi
log "info" "Creating new session for $(important "${server_name}") as session $(important "${server_session_name}")" 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}")" log "info" "Finished starting $(important "${server_name}") on port $(important "${server_port}") as tmux session $(important "${server_session_name}")"
} }
kill_server() { kill_server() {
local prefix local prefix
local tmux_response
local server_id local server_id
server_id="" server_id=""
@ -248,7 +252,7 @@ update() {
printf "Usage: %s\n" \ printf "Usage: %s\n" \
"update [OPTIONS] "update [OPTIONS]
--server <server id: int> | -s <server id: int> --server <server id: int> | -s <server id: int>
Starts the given server id Updates the given server id
Example: Example:
--server 3" --server 3"
@ -285,11 +289,7 @@ update() {
server_directory="${BASE_DIR}/${server_name}" server_directory="${BASE_DIR}/${server_name}"
backup_dir="${HOME}/Server-Backups/${server_name}" backup_dir="${HOME}/Server-Backups/${server_name}"
backup_full_path="${backup_dir}/$(date +%s).tar.gz" 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" backup -s "${server_id}"
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}"/
log "info" "Updating server $(important "${server_name}") located at $(important "${server_directory}")..." 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 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 <server id: int> | -s <server id: int>
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() { list_servers() {
local picked_option local picked_option
picked_option="" picked_option=""
@ -340,26 +400,29 @@ list_servers() {
log "error" "An option must be passed for list, check list -h" && log "error" "An option must be passed for list, check list -h" &&
return 1 return 1
if (( picked_option == 0 )); then case "${picked_option}" in
log "debug" "Listing running servers" 1)
local tmux_sessions log "debug" "Listing running servers"
tmux_sessions="$(tmux list-sessions)" >/dev/null 2>&1 local tmux_sessions
if [[ ! "${?}" -eq "0" ]]; then tmux_sessions="$(tmux list-sessions)" >/dev/null 2>&1
important "No servers currently running." if [[ ! "${?}" -eq "0" ]]; then
fi important "No servers currently running."
while read -r; do
if [[ "${REPLY}" = *"-Server-"* ]]; then
local running_server
running_server="$(echo "${REPLY}" | cut -d ":" -f1)"
important "${running_server}"
fi fi
done <<< "${tmux_sessions}" while read -r; do
elif (( picked_option == 1 )); then if [[ "${REPLY}" = *"-Server-"* ]]; then
log "debug" "Listing installed servers" local running_server
while read -r; do running_server="$(echo "${REPLY}" | cut -d ":" -f1)"
important "${BASE_DIR}/${REPLY}" important "${running_server}"
done <<< "$(find "${BASE_DIR}" -name "startserver.sh" | cut -d "/" -f5)" fi
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() { usage() {
@ -375,7 +438,7 @@ usage() {
# Yes # Yes
# #
printf "Usage: %s\n" \ printf "Usage: %s\n" \
"$(basename ${0}) -h "$(basename "${0}") -h
start start
Exposes options to start 7 Days To Die Servers, pass -h to it for details Exposes options to start 7 Days To Die Servers, pass -h to it for details
kill kill
@ -385,7 +448,9 @@ usage() {
update update
Exposes options to update 7 Days To Die Servers, pass -h to it for details Exposes options to update 7 Days To Die Servers, pass -h to it for details
list 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 "$@" list_servers "$@"
break break
;; ;;
backup | b)
shift
backup "$@"
break
;;
-?*) -?*)
printf "Unknown option: %s\n" "$1" >&2 printf "Unknown option: %s\n" "$1" >&2
usage usage