2021-12-25 21:30:42 -06:00
#!/bin/bash
echo_rgb( ) {
2021-12-26 02:05:44 -06:00
# Echo a colored string to the terminal based on rgb values
#
# Positional Arguments:
#
# message <type: string> <position: 1> <required: true>
# - The message to be printed to stdout
# red <type: int> <position: 2> <required: true>
# - The red value from 0 to 255
# green <type: int> <position: 3> <required: true>
# - The green value from 0 to 255
# blue <type: int> <position: 4> <required: true>
# - The blue value from 0 to 255
#
# Usage:
# echo_rgb "Yep" 10 8 30
#
# POSIX Compliant:
# N/A
#
local red
local green
local blue
local input
input = " ${ 1 } "
red = " ${ 2 } "
green = " ${ 3 } "
blue = " ${ 4 } "
printf "\e[0;38;2;%s;%s;%sm%s\e[m\n" " ${ red } " " ${ green } " " ${ blue } " " ${ input } "
2021-12-25 21:30:42 -06:00
}
log( ) {
2021-12-26 02:05:44 -06:00
# Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc.
#
# Arguments:
# level <type: string> <position: 1> <required: true>
# - The log level, defined within a case check in this function
# message <type: string> <position: 2> <required: true>
# - The info message
# line_number <type: int> <position: 3> <required: false>
# - The line number of the calling function (${LINNO})
#
# Usage:
# log "info" "Could not find that directory"
#
# POSIX Compliant:
# Yes
#
# Set debug status depending if a global debug variable has been set to either 1 or 0
local debug
if [ ${ DEBUG } ] ; then
debug = ${ DEBUG }
else
debug = 0
fi
local FORMAT
FORMAT = " [ $( echo_rgb " $( date +%Y-%m-%dT%H:%M:%S) " 180 140 255) ] "
# Convert the level to uppercase
local level
level = $( echo " ${ 1 } " | tr '[:lower:]' '[:upper:]' )
local message
message = " ${ 2 } "
case " ${ level } " in
2021-12-25 21:30:42 -06:00
INFO)
2021-12-26 02:05:44 -06:00
# Output all info log levels to stdout
printf " ${ FORMAT } [ $( echo_rgb "INFO" 0 140 255) ] %s\n " " ${ message } " >& 1
return 0
; ;
2021-12-25 21:30:42 -06:00
WARN | WARNING)
2021-12-26 02:05:44 -06:00
# Output all warning log levels to stdout
printf " ${ FORMAT } [ $( echo_rgb "WARNING" 255 255 0) ] %s\n " " ${ message } " >& 1
return 0
; ;
2021-12-25 21:30:42 -06:00
DEBUG)
2021-12-26 02:05:44 -06:00
# Output all debug log levels to stdout
if [ " ${ DEBUG } " ] ; then
printf " ${ FORMAT } [ $( echo_rgb "DEBUG" 0 160 110) ] %s\n " " ${ message } " >& 1
fi
return 0
; ;
2021-12-25 21:30:42 -06:00
ERROR)
2021-12-26 02:05:44 -06:00
# Output all error log levels to stderr
printf " ${ FORMAT } [ $( echo_rgb "ERROR" 255 0 0) ] %s\n " " ${ message } " >& 2
return 0
; ;
2021-12-25 21:30:42 -06:00
# Further log levels can be added by extending this switch statement with more comparisons
*) # Default case, no matches
2021-12-26 02:05:44 -06:00
# Returns non-zero code as an improper log option was passed, this helps with using `set -e`
printf " ${ FORMAT } [ERROR] %s\n " " Invalid log level passed, received level \" ${ level } \" with message \" ${ message } \" " >& 2
return 1
; ;
esac
2021-12-25 21:30:42 -06:00
}
confirmation( ) {
2021-12-26 02:05:44 -06:00
# Receive confirmation from user as y, Y, n, or N
# returns 0 when answer is yes and 1 when answer is no
#
# Arguments:
# message <type: string> <position: 1> <required: true>
# - The confirmation prompt sent to the user, for example:
# Would you like to overwrite foobar.txt (y/N)?
#
# Usage:
# confirmation "Some prompt"
# - Sends "Some prompt" to the user and gets their input
#
# POSIX Compliant:
# Yes
#
local message
message = " ${ 1 } "
local choice
while true; do
read -p " ${ message } " -n 1 -r choice
case " $choice " in
y | Y)
echo ""
return 0
; ;
n | N)
echo ""
return 1
; ;
*) echo -e "\nInput must be either y, Y, n, or N" ; ;
esac
done
2021-12-25 21:30:42 -06:00
}
### IMPORTS ###
### CONSTANTS ###
2021-12-26 12:59:35 -06:00
PRIMARY_START_PORT_RANGE = 50000
2021-12-25 21:30:42 -06:00
BASE_DIR = " ${ HOME } /7-Days-To-Die "
### CONSTANTS ###
important( ) {
2021-12-26 02:05:44 -06:00
echo_rgb " ${ 1 } " 135 195 255
2021-12-25 21:30:42 -06:00
}
start_server( ) {
2021-12-26 02:05:44 -06:00
local server_id
local can_kill
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
server_id = ""
can_kill = 1
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
while :; do
case ${ 1 } in
-h | -\? | --help)
printf "Usage: %s\n" \
" start [options]
2021-12-25 21:30:42 -06:00
--server <server id: int> | -s <server id: int>
Starts the given server id
Example:
--server 3
--can-kill | -c
Automatically kills the server if it is running without prompting
Example:
--can-kill"
2021-12-26 02:05:44 -06:00
exit
; ;
--) # End of all options.
break
; ;
--server | -s)
shift
server_id = " ${ 1 } "
[ [ -z " ${ server_id } " ] ] && log "error" "No server id passed" && exit 1
; ;
--can-kill | -c)
can_kill = 0
; ;
-?*)
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
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
local server_directory
local server_config
local server_save_dir
2021-12-26 12:59:35 -06:00
local server_mods_dir
2021-12-26 02:05:44 -06:00
local server_name
local prefix
local server_session_name
server_name = " Server- ${ server_id } "
server_directory = " ${ BASE_DIR } / ${ server_name } "
server_config = " ${ server_directory } /serverconfig.xml "
server_save_dir = " ${ server_directory } /Saves/ "
2022-01-01 17:16:01 -06:00
server_userdata_dir = " ${ server_directory } /UserData/ "
2022-01-01 04:00:21 -06:00
server_mods_dir = " ${ server_directory } /Mods "
2021-12-26 02:05:44 -06:00
prefix = "7D2D"
server_session_name = " ${ prefix } - ${ server_name } "
[ [ ! -d " ${ server_directory } " ] ] &&
log "error" " Unable to find the server directory for $( important " ${ server_name } " ) " &&
return 1
backup_configs " ${ server_directory } "
local server_port
2022-01-01 02:16:27 -06:00
local telnet_port
2022-01-01 18:00:54 -06:00
local web_control_port
2022-01-01 02:16:27 -06:00
# This multiplication gives us 5 available ports for this server, safely allows us to block off ports efficiently
2022-01-01 17:18:34 -06:00
server_port = " $(( PRIMARY_START_PORT_RANGE + $(( " ${ server_id } " * 7 )) )) "
2021-12-27 16:41:34 -06:00
server_port = " ${ SERVER_PORT :- $server_port } "
2021-12-26 12:59:35 -06:00
2022-01-01 17:18:34 -06:00
telnet_port = " $(( server_port + 4 )) "
2022-01-01 18:00:54 -06:00
web_control_port = " $(( server_port + 5 )) "
2021-12-26 02:05:44 -06:00
2021-12-27 16:41:34 -06:00
telnet_port = " ${ TELNET_PORT :- $telnet_port } "
2021-12-26 02:05:44 -06:00
log "info" " Generating a few required directories in $( important " ${ server_directory } " ) "
mkdir -p " ${ server_save_dir } "
mkdir -p " ${ server_userdata_dir } "
2021-12-26 12:59:35 -06:00
mkdir -p " ${ server_mods_dir } "
2021-12-26 02:05:44 -06:00
log "info" " Configuring $( important "serverconfig.xml" ) "
# Overwrite values that we want to control, e.g. server port
while IFS = '' read -r; do
if [ [ " ${ REPLY } " = *"property name=\"ServerPort\"" * ] ] ; then
printf "\t%s\n" " <property name=\"ServerPort\" value=\" ${ server_port } \" /> "
# Override SaveGameFolder, opiniated in that it should exist in the Server Directory
elif [ [ " ${ REPLY } " = *"property name=\"SaveGameFolder\"" * ] ] ; then
printf "\t%s\n" " <property name=\"SaveGameFolder\" value=\" ${ server_save_dir } \" /> "
# Override UserDataFolder, opiniated in that it should exist in the Server Directory
elif [ [ " ${ REPLY } " = *"property name=\"UserDataFolder\"" * ] ] ; then
printf "\t%s\n" " <property name=\"UserDataFolder\" value=\" ${ server_userdata_dir } \" /> "
2021-12-26 12:59:35 -06:00
# Manage the telnet tags
elif [ [ " ${ REPLY } " = *"property name=\"TelnetPort\"" * ] ] ; then
2021-12-31 23:46:55 -06:00
printf "\t%s\n" " <property name=\"TelnetPort\" value=\" ${ telnet_port } \" /> "
2022-01-01 02:16:27 -06:00
elif [ [ " ${ REPLY } " = *"property name=\"ControlPanelPort\"" * ] ] ; then
2022-01-01 18:00:54 -06:00
printf "\t%s\n" " <property name=\"ControlPanelPort\" value=\" ${ web_control_port } \" /> "
2021-12-26 02:05:44 -06:00
else
printf "%s\n" " ${ REPLY } "
fi
done <" ${ server_config } " >"temp-serverconfig.xml"
# Occasionally the closing tag in serverconfig.xml will be missing, this ensures it is there in that scenario
if [ [ ! " $( tail "temp-serverconfig.xml" -n 1) " = *"ServerSettings" * ] ] ; then
printf "</ServerSettings>\n" >>"temp-serverconfig.xml"
fi
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
mv "temp-serverconfig.xml" " ${ server_config } "
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
# xmllint, helps avoiding the random server launch failures for apparently no reason
2022-01-01 02:58:07 -06:00
local lint_dirs
2022-01-01 03:11:17 -06:00
lint_dirs = ( " ${ server_directory } " " ${ server_directory } /Saves " )
2021-12-31 23:56:47 -06:00
if which xmllint >/dev/null 2>& 1; then
2022-01-01 03:11:17 -06:00
for dir in " ${ lint_dirs [@] } " ; do
2022-01-01 03:19:33 -06:00
if ls " ${ dir } " /*.xml >/dev/null 2>& 1; then
for xml_file in " ${ dir } " /*.xml; do
log "info" " Linting $( important " ${ xml_file } " ) "
if ! xmllint " ${ xml_file } " >/dev/null; then
log "error" " Xml parsing error in $( important " ${ xml_file } " ) , resolve the error and attempt to start again "
return 1
fi
done
fi
2022-01-01 02:48:53 -06:00
done
2021-12-31 23:56:47 -06:00
else
log "warning" " $( important "xmllint" ) not installed or not in PATH, skipping lint check "
fi
2021-12-26 02:05:44 -06:00
2022-01-01 18:00:54 -06:00
log "info" " Starting $( important " ${ server_name } " ) located at $( important " ${ server_directory } " ) on game port $( important " ${ server_port } " ) , telnet port $( important " ${ telnet_port } " ) , control panel port $( important " ${ web_control_port } " ) "
2021-12-26 02:05:44 -06:00
if tmux has-session -t " ${ server_session_name } " >/dev/null 2>& 1; then
log "warning" " $( important " ${ server_name } " ) is currently running "
if ( ( can_kill = = 0) ) ; then
log "info" "Been explicitly permitted to kill, bypassing confirmation"
kill_server -s " ${ server_id } "
else
if confirmation " Would you like to kill $( important " ${ server_name } " ) ? (Y/n) " ; then
log "info" " Given answer $( important "yes" ) to kill the server, killing $( important " ${ server_name } " ) "
kill_server -s " ${ server_id } "
else
log "info" " Given answer $( important "no" ) to kill the server, exiting "
exit 0
fi
fi
2021-12-25 21:30:42 -06:00
fi
2021-12-26 02:05:44 -06:00
log "info" " Creating new session for $( important " ${ server_name } " ) as session $( important " ${ server_session_name } " ) "
tmux new-session -d -s " ${ server_session_name } "
tmux send-keys \
" until ${ server_directory } /startserver.sh -configfile= ${ server_directory } /serverconfig.xml; do
2021-12-25 21:30:42 -06:00
echo 'Server died with code $?, restarting in 60 seconds...' >& 2
sleep 60;
done " C-m
2021-12-26 02:05:44 -06:00
log "info" " Finished starting $( important " ${ server_name } " ) on port $( important " ${ server_port } " ) as tmux session $( important " ${ server_session_name } " ) "
2021-12-25 21:30:42 -06:00
}
kill_server( ) {
2021-12-26 02:05:44 -06:00
local prefix
local server_id
2022-01-01 02:16:27 -06:00
local force
local session
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
server_id = ""
prefix = "7D2D"
2022-01-01 02:16:27 -06:00
force = 1
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
while :; do
case ${ 1 } in
-h | -\? | --help)
printf "Usage: %s\n" \
" kill [options]
2021-12-25 21:30:42 -06:00
--server <server id: int> | -s <server id: int>
2022-01-01 02:16:27 -06:00
Stops the server with the given id
2021-12-25 21:30:42 -06:00
Example:
2022-01-01 02:16:27 -06:00
--server 3
2022-01-01 02:18:45 -06:00
2022-01-01 02:16:27 -06:00
--force | -f
Forces the server to close with tmux kill-session
Example:
--force"
2021-12-26 02:05:44 -06:00
exit
; ;
--) # End of all options.
break
; ;
--server | -s)
shift
server_id = " ${ 1 } "
[ [ -z " ${ server_id } " ] ] && log "error" "No server id passed" && exit 1
; ;
2022-01-01 02:16:27 -06:00
--force | -f)
shift
force = 0
; ;
2021-12-26 02:05:44 -06:00
-?*)
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
2022-01-01 02:16:27 -06:00
session = " ${ prefix } -Server- ${ server_id } "
2022-01-01 02:21:51 -06:00
if [ [ " ${ force } " -eq 0 ] ] ; then
2022-01-01 02:16:27 -06:00
if tmux kill-session -t " ${ session } " >/dev/null 2>& 1; then
log "info" " Killed $( important " ${ prefix } -Server- ${ server_id } " ) "
return 0
else
log "error" " $( important " ${ prefix } -Server- ${ server_id } " ) is not running "
return 1
fi
2021-12-26 02:05:44 -06:00
else
2022-01-01 02:25:33 -06:00
local attempts
local max_attempts
attempts = 0
2022-01-01 03:24:49 -06:00
max_attempts = 5
2022-01-01 02:43:35 -06:00
while tmux has-session -t " ${ session } " >/dev/null 2>& 1; do
2022-01-01 03:24:49 -06:00
if [ [ " ${ attempts } " -gt " ${ max_attempts } " ] ] ; then
2022-01-01 02:25:33 -06:00
log "error" " Unable to kill session ${ session } "
return 1
fi
2022-01-01 02:16:27 -06:00
log "info" " Attempting to gracefully kill $( important " ${ session } " ) "
2022-01-01 03:24:49 -06:00
tmux send-keys -t " ${ session } " C-c C-c C-c C-c C-c C-c C-c C-c C-c
log "info" "Sent Exit Request, waiting 5 seconds..."
sleep 5
2022-01-01 02:25:33 -06:00
tmux send-keys -t " ${ session } " C-d
2022-01-01 02:16:27 -06:00
done
log "info" " Successfully stopped $( important " ${ session } " ) "
return 0
2021-12-26 02:05:44 -06:00
fi
2021-12-25 21:30:42 -06:00
}
install( ) {
2021-12-26 02:05:44 -06:00
local server_id
server_id = ""
while :; do
case ${ 1 } in
-h | -\? | --help)
printf "Usage: %s\n" \
" install [OPTIONS]
2021-12-25 21:30:42 -06:00
--server <server id: int> | -s <server id: int>
Installs the server to the given id if it doesn' t exist
Example:
--server 3"
2021-12-26 02:05:44 -06:00
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
local prefix
prefix = "7D2D"
local server_name
local server_directory
local server_admin_xml
server_name = " Server- ${ server_id } "
server_directory = " ${ BASE_DIR } / ${ server_name } "
server_admin_xml = " ${ HOME } /.local/share/7DaysToDie/Saves/serveradmin.xml "
# Ensure that the global mods directory exists
[ [ -d " ${ server_directory } " ] ] &&
log "error" " A server already exists at $( important " ${ server_directory } " ) , delete it and try again " &&
exit 1
log "info" " Installing $( important " ${ server_name } " ) to $( important " ${ server_directory } " ) "
steamcmd +force_install_dir " ${ server_directory } " +login anonymous +app_update 294420 validate +quit
log "info" " Running $( important " ${ server_name } " ) to get a valid $( important "serveradmin.xml" ) "
start_server -s " ${ server_id } " -c
log "info" " Waiting to kill $( important " ${ server_name } " ) "
sleep 5
2022-01-01 16:34:20 -06:00
kill_server -s " ${ server_id } " -f
2021-12-26 02:05:44 -06:00
2022-01-09 21:49:39 -06:00
cp " ${ server_admin_xml } " " ${ server_directory } /Saves " 2>/dev/null
2022-01-01 02:16:27 -06:00
log "info" "Installing Allocs Server Fixes"
local server_fixes_temp_dir
server_fixes_temp_dir = " Server-Fixes- $( date +%s) "
2022-01-01 02:29:27 -06:00
mkdir -p " ${ server_fixes_temp_dir } "
2022-01-01 02:16:27 -06:00
wget http://illy.bz/fi/7dtd/server_fixes.tar.gz >/dev/null 2>& 1
tar xf server_fixes.tar.gz -C " ${ server_fixes_temp_dir } "
2022-01-01 16:39:41 -06:00
cp -r " ${ server_fixes_temp_dir } /Mods/ " * " ${ server_directory } /Mods "
2022-01-01 02:16:27 -06:00
rm -rf " ${ server_fixes_temp_dir } "
2022-01-01 02:36:42 -06:00
rm -rf "server_fixes.tar.gz"
2021-12-26 02:05:44 -06:00
log "info" " Successfully installed $( important " ${ server_name } " ) to $( important " ${ server_directory } " ) "
2021-12-25 21:30:42 -06:00
}
update( ) {
2021-12-26 02:05:44 -06:00
local server_id
server_id = ""
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
while :; do
case ${ 1 } in
-h | -\? | --help)
printf "Usage: %s\n" \
" update [OPTIONS]
2021-12-25 21:30:42 -06:00
--server <server id: int> | -s <server id: int>
Updates the given server id
Example:
--server 3"
2021-12-26 02:05:44 -06:00
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 update
kill_server -s " ${ server_id } " >/dev/null 2>& 1
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
local server_directory
local server_name
local wait_time
server_name = " Server- ${ server_id } "
server_directory = " ${ BASE_DIR } / ${ server_name } "
wait_time = "5"
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
log "info" " Recommend running a $( important "backup" ) operation before running this, waiting $( important " ${ wait_time } " ) seconds before continuing... "
sleep " ${ wait_time } "
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
backup_configs " ${ 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
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
log "info" " Finished updating $( important " ${ server_name } " ) located at $( important " ${ server_directory } " ) "
2021-12-25 21:30:42 -06:00
}
backup_configs( ) {
local server_directory
local config_backup_directory
server_directory = " ${ 1 } "
2021-12-26 01:14:46 -06:00
config_backup_directory = " ${ server_directory } /config-backups/ $( date +%s) / "
2021-12-25 21:30:42 -06:00
log "info" " Backing up $( important ".xml configurations" ) in $( important " ${ server_directory } " ) to $( important " ${ config_backup_directory } " ) "
mkdir -p " ${ config_backup_directory } "
2022-01-01 04:36:36 -06:00
for xml_file in " ${ server_directory } " /{ "" ,"Saves/" } *.xml; do
2022-01-01 16:35:06 -06:00
cp " ${ xml_file } " " ${ config_backup_directory } / " 2>/dev/null
2022-01-01 04:36:36 -06:00
done
2021-12-25 21:30:42 -06:00
}
backup( ) {
2021-12-26 02:05:44 -06:00
local server_id
server_id = ""
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
while :; do
case ${ 1 } in
-h | -\? | --help)
printf "Usage: %s\n" \
" backup [OPTIONS]
2021-12-25 21:30:42 -06:00
--server <server id: int> | -s <server id: int>
Backups the given server id
Example:
--server 3"
2021-12-26 02:05:44 -06:00
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 } /7D2D-Server-Backups/ ${ server_name } "
backup_full_path = " ${ backup_dir } / $( date +%s) .tar.gz "
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
[ [ ! -d " ${ server_directory } " ] ] && log "info" " The server $( important " ${ server_name } " ) had no directory located at $( important " ${ server_directory } " ) " && exit 1
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
log "info" " Backing up server $( important " ${ server_name } " ) to $( important " ${ backup_full_path } " ) , this may take a while "
mkdir -p " ${ backup_dir } "
2021-12-25 21:30:42 -06:00
2021-12-26 02:05:44 -06:00
# Do a check if pv is there, pv is used for showing progress
if which pv >/dev/null 2>& 1; then
tar cf - " ${ server_directory } " -P 2>/dev/null | pv -s " $( du -sb " ${ server_directory } " | awk '{print $1}' ) " | gzip >" ${ backup_full_path } "
else
log "info" " $( important "pv" ) not installed, not showing progress... "
tar czf " ${ server_directory } " " ${ backup_full_path } " 2>/dev/null
fi
2021-12-25 21:30:42 -06:00
}
2022-01-01 04:14:39 -06:00
view( ) {
local server_id
server_id = ""
while :; do
case ${ 1 } in
-h | -\? | --help)
printf "Usage: %s\n" \
" view [OPTIONS]
--server <server id: int> | -s <server id: int>
Views the given server' s latest log file via tail -f
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
local server_directory
2022-01-01 04:19:57 -06:00
local server_log_dir
2022-01-01 04:14:39 -06:00
local server_name
server_name = " Server- ${ server_id } "
server_directory = " ${ BASE_DIR } / ${ server_name } "
2022-01-01 04:21:12 -06:00
server_log_dir = " ${ server_directory } /7DaysToDieServer_Data "
2022-01-01 04:14:39 -06:00
2022-01-01 04:25:07 -06:00
if [ [ ! -d " ${ server_log_dir } " ] ] ; then
2022-01-01 04:26:12 -06:00
log "error" " Could not find a log directory for $( important " ${ server_name } " ) at $( important " ${ server_log_dir } " ) , does the server exist? "
2022-01-01 04:25:07 -06:00
return 1
fi
2022-01-01 04:22:06 -06:00
tail -f " $( ls -t " ${ server_log_dir } " /output_log* | head -1) "
2022-01-01 04:14:39 -06:00
}
2021-12-25 21:30:42 -06:00
list_servers( ) {
2021-12-26 02:05:44 -06:00
local picked_option
picked_option = ""
while :; do
case ${ 1 } in
-h | -\? | --help)
printf "Usage: %s\n" \
" list [OPTIONS]
2021-12-25 21:30:42 -06:00
--running | -r
Lists the currently running 7 Days To Die Servers
Example:
--running
--installed | -i
Lists the currently installed 7 Days To Die Servers
Example:
--installed"
2021-12-26 02:05:44 -06:00
exit
; ;
--) # End of all options.
break
; ;
--running | -r)
picked_option = 0
; ;
--installed | -i)
picked_option = 1
; ;
-?*)
printf 'Unknown option: %s\n' " $1 " >& 2
return 1
; ;
*) # Default case: No more options, so break out of the loop.
break ; ;
esac
shift
done
[ [ -z " ${ picked_option } " ] ] &&
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
2022-01-01 04:42:43 -06:00
tmux_sessions = " $( tmux list-sessions 2>/dev/null) "
2021-12-26 02:05:44 -06:00
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 } "
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) "
2021-12-25 21:30:42 -06:00
fi
}
usage( ) {
2021-12-26 02:05:44 -06:00
# Print out usage instructions for the local script
#
# Arguments:
# None
#
# Usage:
# usage
#
# POSIX Compliant:
# Yes
#
printf "Usage: %s\n" \
" $( basename " ${ 0 } " ) -h
2021-12-25 21:30:42 -06:00
start
Exposes options to start 7 Days To Die Servers, pass -h to it for details
kill
Exposes options to kill 7 Days To Die Servers, pass -h to it for details
install
Exposes options to install 7 Days To Die Servers, pass -h to it for details
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
backup
2022-01-01 04:14:39 -06:00
Exposes options to backup 7 Days To Die Servers, pass -h to it for details
view
Exposes options to view 7 Days To Die Server Logs, pass -h to it for details"
2021-12-25 21:30:42 -06:00
}
main( ) {
2021-12-26 02:05:44 -06:00
# Parse input arguments
#
# Arguments:
# Consult the `usage` function
#
# Usage:
# parse_args "$@"
# - All arguments should be ingested by parse_args first for variable setting
#
# POSIX Compliant:
# Yes
#
while :; do
case ${ 1 } in
-h | -\? | --help)
usage # Display a usage synopsis.
exit
; ;
--) # End of all options.
break
; ;
start | s)
shift
start_server " $@ "
break
; ;
kill | k)
shift
kill_server " $@ "
break
; ;
install | i)
shift
install " $@ "
break
; ;
update | u)
shift
update " $@ "
break
; ;
list | l)
shift
list_servers " $@ "
break
; ;
backup | b)
shift
backup " $@ "
break
; ;
2022-01-01 04:16:16 -06:00
view | v)
2022-01-01 04:17:21 -06:00
shift
view " $@ "
2022-01-01 04:16:16 -06:00
break
; ;
2021-12-26 02:05:44 -06:00
-?*)
printf "Unknown option: %s\n" " $1 " >& 2
usage
exit 1
; ;
*) # Default case: No more options, so break out of the loop.
break ; ;
esac
shift
done
2021-12-25 21:30:42 -06:00
}
main " $@ "