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
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 <server id: int> | -s <server id: int>
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 <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() {
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