How to Install Avorion on Ubuntu Server 20.04 LTS

Post Reply
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

How to Install Avorion on Ubuntu Server 20.04 LTS

Post: # 965Post LHammonds »

------------- WORK-IN-PROGRESS -------------

Table of Contents
=================
Installation of Ubuntu Server (DONE)
Firewall Rules (DONE)
Credentials (DONE)
Install SteamCMD/Avorion (DONE)
Create Your Galaxies (DONE)
Modify Avorion Configuration (DONE)
Start / Stop / Update Avorion Manually (DONE)
Server Logs (DONE)
RCON Utility (DONE)
Script Configuration File (DONE)
Script - Start (DONE)
Script - Stop (DONE)
Script - Update (DONE)
Avorion As A Service (DONE)
Script - Fix Permissions
Script - Backup / Restore / Encrypt / Decrypt
Installing Mods
Script - Might make an "install.sh" script that does everything in this tutorial for you. "might"

Overview

This article will cover how to install and maintain a dedicated Avorion server on Ubuntu.

Bash scripts and an RCON utility will be utilized to manage the server.

Two instances will be configured to demonstrate how to manage multiple instances. Two mods will also be used to demonstrate how to install, use and update mods.

These are the locations that will be utilized:
  • /var/scripts/prod (custom Bash scripts)
  • /opt/avorion/savegame (folder to hold game instances)
  • /opt/avorion/bak (folder to hold backup archives)
  • /etc (holds configuration files)
  • /lib/systemd/system/avservice@ (holds configuration files related to systemctl start/stop commands)
These are the port settings that will be used for each instance. Feel free to use different settings but these should not conflict with anything else or themselves:

Code: Select all

Galaxy   Game Port  Steam Master  Steam Query  Query Port  RCON Port  Seed
-------- ---------  ------------  -----------  ----------  ---------  ---------
galaxy1      27101         27102        27103       27104      27105   11111111
galaxy2      27111         27112        27113       27114      27115   22222222
Tools utilized in this process
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Firewall Rules

Post: # 966Post LHammonds »

Firewall Rules

Here are the rules for running the two example galaxies. The RCON rule is commented out since we will not need to access RCON remotely but I left it in there in case you did. Adjust as necessary.

Code: Select all

echo "Adding Avorion Server rules"
ufw allow proto udp to any port 27101 comment 'Galaxy1 Port' 1>/dev/null 2>&1
ufw allow proto udp to any port 27102 comment 'G1 Steam Master' 1>/dev/null 2>&1
ufw allow proto udp to any port 27103 comment 'G1 Steam Query' 1>/dev/null 2>&1
ufw allow proto udp to any port 27104 comment 'G1 Query' 1>/dev/null 2>&1
#ufw allow proto tcp to any port 27105 comment 'G1 RCON' 1>/dev/null 2>&1

ufw allow proto udp to any port 27111 comment 'Galaxy2 Port' 1>/dev/null 2>&1
ufw allow proto udp to any port 27112 comment 'G2 Steam Master' 1>/dev/null 2>&1
ufw allow proto udp to any port 27113 comment 'G2 Steam Query' 1>/dev/null 2>&1
ufw allow proto udp to any port 27114 comment 'G2 Query' 1>/dev/null 2>&1
#ufw allow proto tcp to any port 27115 comment 'G2 RCON' 1>/dev/null 2>&1
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Credentials

Post: # 967Post LHammonds »

Credentials

There will be 3 levels of user access:
  1. root (system-level access for script schedules, backups and service control)
  2. avserver (owner/update account that has read/write access to all Avorion files and ability to use sudo command)
  3. avservice (runtime account that belongs to the avserver group which has the least permissions possible)
One group will be created called "avserver" and will contain the minimum permissions necessary to run the server (read-only access everywhere except necessary locations such as savegame data)

Let's create the game folders, the user accounts and setup correct access.

Code: Select all

sudo mkdir --parents /opt/avorion/savegame
sudo mkdir --parents /opt/avorion/bak
sudo addgroup avserver
sudo useradd --comment "Avorion" --shell /bin/bash --home /opt/avorion/ --no-create-home --no-user-group --gid avserver avserver
sudo useradd --comment "Avorion Service" --shell /bin/bash --home /opt/avorion/ --no-create-home --no-user-group --gid avserver avservice
sudo usermod -aG sudo avserver
sudo chown --recursive avserver:avserver /opt/avorion
sudo chmod g+s /opt/avorion
sudo chmod 777 /opt/avorion/savegame
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Installation

Post: # 968Post LHammonds »

Install SteamCMD

Code: Select all

sudo apt update
sudo apt install steamcmd
Install Avorion

Download and install standard Avorion

Code: Select all

su --command="steamcmd +login anonymous +force_install_dir /opt/avorion +app_update 565060 validate +exit" avserver
or
Download and install BETA Avorion

Code: Select all

su --command="steamcmd +login anonymous +force_install_dir /opt/avorion +app_update 565060 -beta beta validate +exit" avserver
Create Galaxy #1

Be sure to use your own SteamID64 after the admin option.

Code: Select all

su --command="/opt/avorion/server.sh --galaxy-name galaxy1 --admin 76561197970730192 --port 27101 --datapath /opt/avorion/savegame" avserver
>> Wait for initilization to complete. Look for "Server startup complete." <<

Code: Select all

/save
/stop
Create Galaxy #2

Code: Select all

su --command="/opt/avorion/server.sh --galaxy-name galaxy2 --admin 76561197970730192 --port 27102 --datapath /opt/avorion/savegame" avserver
>> Wait for initilization to complete <<

Code: Select all

/save
/stop
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Avorion Configuration

Post: # 969Post LHammonds »

Avorion Configuration
Reference: Documentation: Server Configuration

NOTE: Only make changes to the config file when the server is NOT running.

The most-basic changes you need to make are setting the RCON password and RCON port (which must be unique and unused)

Code: Select all

vi /opt/avorion/savegame/galaxy1/server.ini

Code: Select all

rconPassword=SET_PASSWORD_HERE
rconPort=27201
name=galaxy1
description=Avorion Server: galaxy1

Code: Select all

vi /opt/avorion/savegame/galaxy2/server.ini

Code: Select all

rconPassword=SET_PASSWORD_HERE
rconPort=27202
name=galaxy2
description=Avorion Server: galaxy2
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Commands

Post: # 970Post LHammonds »

Start Server Manually

Code: Select all

su --command="/opt/avorion/server.sh --galaxy-name galaxy1 --admin 76561197970730192 --datapath /opt/avorion/savegame" avserver
su --command="/opt/avorion/server.sh --galaxy-name galaxy2 --admin 76561197970730192 --datapath /opt/avorion/savegame" avserver
Stop Server Manually

At the console, type /save and then /stop
Or via RCON, use /save and /stop commands.

Update Server Manually

Stop the server cleanly using /save and /stop
Backup galaxy data
Run the same steamcmd used to install the app:

Stable Version

Code: Select all

su --command="steamcmd +login anonymous +force_install_dir /opt/avorion +app_update 565060 validate +exit" avserver
BETA Version

Code: Select all

su --command="steamcmd +login anonymous +force_install_dir /opt/avorion +app_update 565060 -beta beta validate +exit" avserver
Then start the server like normal.

Server Logs

Server logs can be found in locations such as this which is based on galaxy name:
Galaxy1 = /opt/avorion/savegame/galaxy1/serverlog*.txt
Galaxy2 = /opt/avorion/savegame/galaxy2/serverlog*.txt

To edit the most-recent log file:

Code: Select all

read recentfile < <(ls -t /opt/avorion/savegame/galaxy1/serverlog*.txt)&& vi "$recentfile"
read recentfile < <(ls -t /opt/avorion/savegame/galaxy2/serverlog*.txt)&& vi "$recentfile"
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

RCON

Post: # 971Post LHammonds »

RCON Utility

Reference: My Steam Guide: RCON Command-Line Utility

The game instances will be running as a service and thus, the console will not be directly available. This is where RCON comes in handy. Avorion has built-in support for RCON clients sending console commands such as /save, /stop, /status, /players, /version, etc.

Avorion does not ship with an RCON client and neither does Ubuntu.

You can get source code written in C by [ASY]Zyrain that you can compile for use with your server. However, I did not like the fact that his version required the password to be passed as a command-line parameter which exposes it to anyone that is watching the process list. So I modified it to pull the password from a configuration file and a few other minor tweaks documented in the change log. Both versions are on my github so you can easily see what was changed if interested.

All the scripts that interact with the service will be using this RCON utility.

Here are the steps to download and compile my variation of the RCON utility.

Code: Select all

sudo apt install gcc
mkdir --parents /var/script/prod
wget https://raw.githubusercontent.com/LHammonds/rcon/main/rcon.c --output-document /var/scripts/prod/rcon.c
sudo gcc -o /var/scripts/prod/rcon /var/scripts/prod/rcon.c
sudo chown avserver:avserver /var/scripts/prod/rcon
sudo chmod 0750 /var/scripts/prod/rcon
sudo apt remove gcc
(I know, super difficult wasn't it!)

Might as well grab the manual for RCON and copy it where you can reference it anywhere:

Code: Select all

wget https://raw.githubusercontent.com/LHammonds/rcon/main/rcon.man --output-document /var/scripts/prod/rcon.man
sudo mkdir -p /usr/local/share/man/man1
sudo cp /var/scripts/prod/rcon.man /usr/local/share/man/man1/rcon.1
sudo chown root:root /usr/local/share/man/man1/rcon.1
sudo chmod 0644 /usr/local/share/man/man1/rcon.1
Now you can pull up the help page anytime you want:

Code: Select all

man rcon
The scripts that I created look for an rcon config file with a prefix of "rcon-" and then the galaxy name and then a suffix of ".ini"

You can use whatever you want but my scripts expect the use of one rcon config file per galaxy so the name is included in the filename but all the instance details are stored in the file such as password, IP and port number.

Create an RCON configuration file for use with galaxy1.

Code: Select all

sudo touch /etc/rcon-galaxy1.ini
sudo chown avserver:avserver /etc/rcon-galaxy1.ini
sudo chmod 0640 /etc/rcon-galaxy1.ini
cat << EOF > /etc/rcon-galaxy1.ini
[rcon]
password=YOUR_RCON_PASSWORD
ipaddress=127.0.0.1
port=27201
EOF
Create an RCON configuration file for use with galaxy2:

Code: Select all

sudo touch /etc/rcon-galaxy2.ini
sudo chown avserver:avserver /etc/rcon-galaxy2.ini
sudo chmod 0640 /etc/rcon-galaxy2.ini
cat << EOF > /etc/rcon-galaxy2.ini
[rcon]
password=YOUR_RCON_PASSWORD
ipaddress=127.0.0.1
port=27202
EOF
Be sure to edit the RCON config files and match the password to what you will / have set for "rconPassword" in your server.ini file(s).

Usage Examples

Run a Save command using a generic config file for a common password but specify a different IP/port:

Code: Select all

rcon -f "/etc/rcon-all.ini" -a 127.0.0.1 -p 27110 "/save"
Run a Stop command using a pre-defined password, IP address and port in a specific instance config file:

Code: Select all

rcon -f "/etc/rcon-galaxy1.ini" "/stop"
Run a Status command using a pre-defined password, IP address and port in a specific instance config file:

Code: Select all

rcon -f "/etc/rcon-galaxy2.ini" "/status"
Run a Players command using a pre-defined password, IP address and port in a specific instance config file:

Code: Select all

rcon -f "/etc/rcon-galaxy3.ini" "/players"
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Script Configuration File

Post: # 972Post LHammonds »

Script Configuration File

This is a configuration file for use by these custom scripts which contain global variables/settings used by the various scripts.

Make sure each variable matches what you want for your system.

Code: Select all

wget https://raw.githubusercontent.com/LHammonds/avorion-bash/main/avorion.conf --output-document /tmp/avorion.conf
chmod 0640 /tmp/avorion.conf
sudo chown avserver:avserver /tmp/avorion.conf
sudo mv /tmp/avorion.conf /etc/.
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Script - Start

Post: # 980Post LHammonds »

Avorion Start Script

This start script is responsible for starting a specific instance of Avorion. It can be run manually but is designed to be used by the systemd service account.

Code: Select all

wget https://raw.githubusercontent.com/LHammonds/avorion-bash/main/avorion-start.sh --output-document /tmp/avorion-start.sh
chmod 0750 /tmp/avorion-start.sh
sudo chown avserver:avserver /tmp/avorion-start.sh
sudo mv /tmp/avorion-start.sh /var/scripts/prod/.
touch /tmp/avorion-start.log
chmod 0660 /tmp/avorion-start.log
sudo chown avserver:avserver /tmp/avorion-start.log
sudo mv /tmp/avorion-start.log /var/log/.
Here is a copy of the contents of the script (keep in mind that this may become an outdated version of what is on github)

Code: Select all

#!/bin/bash
#############################################################
## Name          : avorion-start.sh
## Version       : 1.0
## Date          : 2021-09-11
## Author        : LHammonds
## Purpose       : Start a specific game instance.
## Compatibility : Verified on Ubuntu Server 20.04 LTS
## Requirements  : Run as root or the specified low-rights user.
## Run Frequency : As needed or when starting the server.
## Parameters    : Game Instance
## Exit Codes    :
##    0 = Success
##    1 = ERROR Missing parameter
##    2 = ERROR Invalid parameter
##    3 = ERROR Invalid user
##    4 = ERROR Invalid configuration
######################## CHANGE LOG #########################
## DATE       VER WHO WHAT WAS CHANGED
## ---------- --- --- ---------------------------------------
## 2021-09-11 1.0 LTH Created script.
#############################################################

## Import standard variables and functions. ##
source /etc/avorion.conf
LogFile="${LogDir}/avorion-start.log"

#######################################
##            FUNCTIONS              ##
#######################################

function f_start()
{
  ## Parameter #1 = Instance Name
  printf "\n[INFO] systemctl start ${1}\n"
  systemctl start ${1}
} ## f_start() ##

function f_showhelp()
{
  printf "`date +%Y-%m-%d_%H:%M:%S` - [ERROR] Missing required parameter(s)\n" | tee -a ${LogFile}
  printf "Syntax : ${0} {InstanceName}\n"
  printf "Example: ${0} galaxy1\n"
  printf "List of existing game instances\n"
  printf "===============================\n"
  for intIndex in "${!arrInstanceName[@]}"
  do
    ## Verify instance is installed ##
    if [ -f "${GameSaveDir}/${arrInstanceName[${intIndex}]}/server.ini" ]; then
      printf "${arrInstanceName[${intIndex}]}\n"
    fi
  done
} ## f_showhelp() ##

function f_sanity_check()
{
  ## Parameter #1 = Variable name. ##
  ## Parameter #2 = Variable contents. ##
  if [ "${2}" == "" ]; then
    printf "`date +%Y-%m-%d_%H:%M:%S` - [ERROR] ${1} could not be matched.\n" | tee -a ${LogFile}
    exit 4
  fi
} ## f_sanity_check() ##

#######################################
##          PREREQUISITES            ##
#######################################

## Check existence of required command-line parameters ##
case "$1" in
  "")
    f_showhelp
    exit 1
    ;;
  --help|-h|-?)
    f_showhelp
    exit 1
    ;;
  *)
    GameInstance=$1
    ;;
esac

## Validate GameInstance ##
if [ ! -f "${GameSaveDir}/${GameInstance}/server.ini" ]; then
  printf "`date +%Y-%m-%d_%H:%M:%S` - [ERROR] Invalid parameter. ${GameSaveDir}/${GameInstance} is not a valid instance.\n" | tee -a ${LogFile}
  exit 2
fi

## Get all options based on instance name ##
for intIndex in "${!arrInstanceName[@]}"
do
  if [ "${GameInstance}" == "${arrInstanceName[${intIndex}]}" ]; then
    Desc=${arrDesc[${intIndex}]}
    GamePort=${arrGamePort[${intIndex}]}
    SteamMasterPort=${arrSteamMasterPort[${intIndex}]}
    SteamQueryPort=${arrSteamQueryPort[${intIndex}]}
    QueryPort=${arrQueryPort[${intIndex}]}
    RCONPort=${arrRCONPort[${intIndex}]}
    Seed=${arrSeed[${intIndex}]}
    break
  fi
done

## Sanity check on imported array variables. ##
f_sanity_check "Desc" ${Desc}
f_sanity_check "GamePort" ${GamePort}
f_sanity_check "SteamMasterPort" ${SteamMasterPort}
f_sanity_check "SteamQueryPort" ${SteamQueryPort}
f_sanity_check "QueryPort" ${QueryPort}
f_sanity_check "RCONPort" ${RCONPort}
f_sanity_check "Seed" ${Seed}

## Build command-line parameter list. ##

CommandParameters="--multiplayer true --listed true --galaxy-name ${GameInstance} --admin ${SteamAdmin} --datapath ${GameSaveDir} --server-name '${Desc}' --ip 127.0.0.1 --port ${GamePort} --steam-master-port ${SteamMasterPort} --steam-query-port ${SteamQueryPort} --query-port ${QueryPort} --rcon-port ${RCONPort} --seed ${Seed} --same-start-sector true --max-players ${MaxPlayers}"

#######################################
##           MAIN PROGRAM            ##
#######################################

## Get the current game version. ##
GameVersion=`${GameRootDir}/bin/AvorionServer --version`

if [ "${USER}" == "${GameService}" ]; then
  ## Already running as the low-rights user, start the instance. ##
  printf "`date +%Y-%m-%d_%H:%M:%S` - [INFO] Start ${GameInstance} on version ${GameVersion}\n" >> ${LogFile}
  f_verbose "Command = ${GameRootDir}/server.sh ${CommandParameters}"
  ${GameRootDir}/server.sh ${CommandParameters}
elif [ "${USER}" == "root" ]; then
  ## Run command using low-rights user ##
  printf "`date +%Y-%m-%d_%H:%M:%S` - [INFO] Start ${GameInstance} on version ${GameVersion}\n" >> ${LogFile}
  f_verbose "Command = su --command='${GameRootDir}/server.sh ${CommandParameters}' ${GameService}"
  su --command="${GameRootDir}/server.sh ${CommandParameters}" ${GameService}
else
  ## Exit script with reason and error code ##
  printf "`date +%Y-%m-%d_%H:%M:%S` - [ERROR] ${GameInstance} service must be started by ${GameService}\n" | tee -a ${LogFile}
  exit 3
fi
exit 0
Here is an example of the log output with verbose setting enabled in the avorion.conf file:

Code: Select all

2021-09-29_06:43:06 - [INFO] Start galaxy1 on version 2.0.6.33972
Command = su --command='/opt/avorion/server.sh --multiplayer true --listed true --galaxy-name 'galaxy1' --admin 76561197970730192 --datapath /opt/avorion/savegame --server-name 'LHammonds galaxy1' --ip 127.0.0.1 --port 27101 --steam-master-port 27102 --steam-query-port 27103 --query-port 27104 --rcon-port 27105 --seed 111111111 --same-start-sector true --max-players 10' avservice
2021-12-09_14:22:46 - [INFO] Start galaxy1 on version 2.0.11.34546
Command = /opt/avorion/server.sh --multiplayer true --listed true --galaxy-name galaxy1 --admin 76561197970730192 --datapath /opt/avorion/savegame --server-name 'LHammonds galaxy1' --ip 127.0.0.1 --port 27101 --steam-master-port 27102 --steam-query-port 27103 --query-port 27104 --rcon-port 27105 --seed 111111111 --same-start-sector true --max-players 10
2021-12-09_15:22:14 - [INFO] Start galaxy2 on version 2.0.11.34546
Command = /opt/avorion/server.sh --multiplayer true --listed true --galaxy-name galaxy2 --admin 76561197970730192 --datapath /opt/avorion/savegame --server-name 'LHammonds galaxy2' --ip 127.0.0.1 --port 27111 --steam-master-port 27112 --steam-query-port 27113 --query-port 27114 --rcon-port 27115 --seed 222222222 --same-start-sector true --max-players 10
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Script - Stop

Post: # 981Post LHammonds »

Avorion Stop Script

This start script is responsible for properly stopping a specific instance of Avorion. It issues a save command and then a stop command. It can be run manually but is designed to be used by the systemd service account.

NOTE: Always ensure a save command is issued before a stop command.

Code: Select all

wget https://raw.githubusercontent.com/LHammonds/avorion-bash/main/avorion-stop.sh --output-document /tmp/avorion-stop.sh
chmod 0750 /tmp/avorion-stop.sh
sudo chown avserver:avserver /tmp/avorion-stop.sh
sudo mv /tmp/avorion-stop.sh /var/scripts/prod/.
touch /tmp/avorion-stop.log
chmod 0660 /tmp/avorion-stop.log
sudo chown avserver:avserver /tmp/avorion-stop.log
sudo mv /tmp/avorion-stop.log /var/log/.
Here is a copy of the contents of the script (keep in mind that this may become an outdated version of what is on github)

Code: Select all

#!/bin/bash
#############################################################
## Name          : avorion-stop.sh
## Version       : 1.0
## Date          : 2021-09-11
## Author        : LHammonds
## Purpose       : Stop a specific game instance.
## Compatibility : Verified on Ubuntu Server 20.04 LTS
## Requirements  : Run as root or the specified low-rights user.
## Run Frequency : As needed or when stoping the server.
## Parameters    : Game Instance
## Exit Codes    :
##    0 = Success
##    1 = ERROR Missing parameter
##    2 = ERROR Invalid parameter
######################## CHANGE LOG #########################
## DATE       VER WHO WHAT WAS CHANGED
## ---------- --- --- ---------------------------------------
## 2021-09-11 1.0 LTH Created script.
#############################################################

## Import standard variables and functions. ##
source /etc/avorion.conf
LogFile="${LogDir}/avorion-stop.log"

#######################################
##            FUNCTIONS              ##
#######################################

function f_showhelp()
{
  printf "`date +%Y-%m-%d_%H:%M:%S` - [ERROR] Missing required parameter(s)\n" | tee -a ${LogFile}
  printf "Syntax : ${0} {InstanceName}\n"
  printf "Example: ${0} galaxy1\n"
  printf "List of existing game instances\n"
  printf "===============================\n"
  ls ${GameSaveDir}
}

#######################################
##          PREREQUISITES            ##
#######################################

## Check existence of required command-line parameters ##
case "$1" in
  "")
    f_showhelp
    exit 1
    ;;
  --help|-h|-?)
    f_showhelp
    exit 1
    ;;
  *)
    GameInstance=$1
    ;;
esac

## Validate GameInstance ##
if [ ! -f "${GameSaveDir}/${GameInstance}/server.ini" ]; then
  printf "`date +%Y-%m-%d_%H:%M:%S` - [ERROR] Invalid parameter. ${GameSaveDir}/${GameInstance} is not a valid instance.\n" | tee -a ${LogFile}
  exit 2
fi

#######################################
##           MAIN PROGRAM            ##
#######################################

## Get the current game version. ##
GameVersion=`${GameRootDir}/bin/AvorionServer --version`

printf "`date +%Y-%m-%d_%H:%M:%S` - Stopping ${GameInstance} on version ${GameVersion}\n" >> ${LogFile}
printf "[INFO] /save ${GameInstance}\n" >> ${LogFile}
${ScriptDir}/rcon -f ${RCONPrefix}${GameInstance}${RCONSuffix} "/save"
sleep 5
printf "[INFO] /stop ${GameInstance}\n" >> ${LogFile}
${ScriptDir}/rcon -f ${RCONPrefix}${GameInstance}${RCONSuffix} "/stop"
printf "`date +%Y-%m-%d_%H:%M:%S` - Stopped ${GameInstance}\n" >> ${LogFile}
exit 0
Here is an example of the log output with verbose setting enabled in the avorion.conf file:

Code: Select all

2021-09-29_06:43:06 - Stopping galaxy1 on version 2.0.6.33972
[INFO] /save galaxy1
[INFO] /stop galaxy1
2021-09-29_06:43:12 - Stopped galaxy1
2021-12-09_14:20:14 - Stopping galaxy1 on version 2.0.11.34546
[INFO] /save galaxy1
[INFO] /stop galaxy1
2021-12-09_14:20:21 - Stopped galaxy1
2021-12-09_14:23:05 - Stopping galaxy2 on version 2.0.11.34546
[INFO] /save galaxy2
[INFO] /stop galaxy2
2021-12-09_14:23:12 - Stopped galaxy2
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Script - Update

Post: # 982Post LHammonds »

Avorion Update Script

This start script is responsible for updating Avorion. I will only perform an update if all instances are offline.

Code: Select all

wget https://raw.githubusercontent.com/LHammonds/avorion-bash/main/avorion-update.sh --output-document /tmp/avorion-update.sh
sudo chmod 0750 /tmp/avorion-update.sh
sudo chown avserver:avserver /tmp/avorion-update.sh
sudo mv /tmp/avorion-update.sh /var/scripts/prod/.
touch /tmp/avorion-update.log
chmod 0660 /tmp/avorion-update.log
sudo chown avserver:avserver /tmp/avorion-update.log
sudo mv /tmp/avorion-update.log /var/log/.
Here is a copy of the contents of the script (keep in mind that this may become an outdated version of what is on github)

Code: Select all

#!/bin/bash
#############################################################
## Name          : avorion-update.sh
## Version       : 1.0
## Date          : 2021-09-11
## Author        : LHammonds
## Purpose       : Update to latest version.
## Compatibility : Verified on Ubuntu Server 20.04 LTS
## Requirements  : Run as root or the specified low-rights user.
## Run Frequency : As needed or when stoping the server.
## Parameters    : None for standard version, /beta for BETA version.
## Exit Codes    :
##    0 = Success
##    1 = ERROR Running instances detected
######################## CHANGE LOG #########################
## DATE       VER WHO WHAT WAS CHANGED
## ---------- --- --- ---------------------------------------
## 2021-09-11 1.0 LTH Created script.
#############################################################

## Import standard variables and functions. ##
source /etc/avorion.conf
LogFile="${LogDir}/avorion-update.log"
SteamOut="${TempDir}/steam.out"
NoUpdate="already up to date"
UpgradeSuccess="fully installed"
Beta="0"

#######################################
##          PREREQUISITES            ##
#######################################

## Check existence of optional command-line parameter ##
case "$1" in
  --beta|-beta|-b)
    Beta="1"
    ;;
esac

## Abort if any instances are running ##
cd ${GameSaveDir}
for folder in *; do
  if [ -d "${folder}" ]; then
    systemctl is-active --quiet avservice@${folder}
    ReturnCode=$?
    if [ "${ReturnCode}" == "0" ]; then
      printf "`date +%Y-%m-%d_%H:%M:%S` [ERROR] ${folder} instance is running. Update aborted.\n" | tee -a ${LogFile}
      exit 1
    fi
  fi
done

#######################################
##           MAIN PROGRAM            ##
#######################################

if [ -f ${SteamOut} ]; then
  ## Remove the temp file before we use it ##
  rm ${SteamOut}
fi
printf "`date +%Y-%m-%d_%H:%M:%S` - Started update.\n" | tee -a ${LogFile}
## Get the pre-update game version. ##
PreUpdateVersion=`${GameRootDir}/bin/AvorionServer --version`
if [ "${Beta}" == "1" ]; then
  ## Install BETA version ##
  printf "[INFO] Beta option enabled.\n" | tee -a ${LogFile}
  f_verbose "su --command='steamcmd +login anonymous +force_install_dir ${GameRootDir} +app_update ${ServerID} -beta beta validate +exit' ${GameUser}"
  su --command="steamcmd +login anonymous +force_install_dir ${GameRootDir} +app_update ${ServerID} -beta beta validate +exit > ${SteamOut}" ${GameUser}
  ReturnCode=$?
else
  ## Install Standard version ##
  f_verbose "su --command='steamcmd +login anonymous +force_install_dir ${GameRootDir} +app_update ${ServerID} validate +exit' ${GameUser}"
  su --command="steamcmd +login anonymous +force_install_dir ${GameRootDir} +app_update ${ServerID} validate +exit > ${SteamOut}" ${GameUser}
  ReturnCode=$?
fi
f_verbose "[INFO] SteamCMD ReturnCode=${ReturnCode}"
if grep -Fq "${NoUpdate}" ${SteamOut}; then
  ## No update found ##
  printf "[INFO] No update found.\n" | tee -a ${LogFile}
else
  if grep -Fq "${UpgradeSuccess}" ${SteamOut}; then
    ## Upgrade peformed and was successful ##
    printf "[INFO] Update performed and was successful.\n" | tee -a ${LogFile}
  else
    ## Other issue (could be error, lack of space, timeout, etc.) ##
    printf "[UNKNOWN] Unknown result...need exact wording.\n" | tee -a ${LogFile}
    printf "[SAVE] Output text saved to ${BackupDir}/`date +%Y-%m-%d_%H-%M-%S`-steam.out\n" | tee -a ${LogFile}
    cp ${SteamOut} ${BackupDir}/`date +%Y-%m-%d_%H-%M-%S`-steam.out
  fi
fi
## Get the post-update game version. ##
PostUpdateVersion=`${GameRootDir}/bin/AvorionServer --version`
printf "[INFO] Old version: ${PreUpdateVersion}\n" | tee -a ${LogFile}
printf "[INFO] New version: ${PostUpdateVersion}\n" | tee -a ${LogFile}
printf "`date +%Y-%m-%d_%H:%M:%S` - Completed update.\n" | tee -a ${LogFile}
exit ${ReturnCode}
Here is an example of the log output with verbose setting enabled in the avorion.conf file:

Code: Select all

2021-12-09_13:31:31 - Started update.
su --command='steamcmd +login anonymous +force_install_dir /opt/avorion +app_update 565060 validate +exit' avserver
[INFO] SteamCMD ReturnCode=0
[INFO] Update performed and was successful.
[INFO] Old version: 2.0.6.33972
[INFO] New version: 2.0.11.34546
2021-12-09_13:32:05 - Completed update.
2021-12-09_13:35:38 - Started update.
su --command='steamcmd +login anonymous +force_install_dir /opt/avorion +app_update 565060 validate +exit' avserver
[INFO] SteamCMD ReturnCode=0
[INFO] Update performed and was successful.
[INFO] Old version: 2.0.11.34546
[INFO] New version: 2.0.11.34546
2021-12-09_13:35:44 - Completed update.
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Script - Fix Permissions

Post: # 983Post LHammonds »

Avorion Fix Permissions Script

This script is responsible for fixing ownership and permission settings on all Avorion-related files.

Code: Select all

wget https://raw.githubusercontent.com/LHammonds/avorion-bash/main/avorion-fixperms.sh --output-document /tmp/avorion-fixperms.sh
chmod 0750 /tmp/avorion-fixperms.sh
sudo chown avserver:avserver /tmp/avorion-fixperms.sh
sudo mv /tmp/avorion-fixperms.sh /var/scripts/prod/.
touch /tmp/avorion-fixperms.log
chmod 0660 /tmp/avorion-fixperms.log
sudo chown avserver:avserver /tmp/avorion-fixperms.log
sudo mv /tmp/avorion-fixperms.log /var/log/.
Here is a copy of the contents of the script (keep in mind that this may become an outdated version of what is on github)

Code: Select all

#!/bin/bash
#############################################################
## Name          : avorion-fixperms.sh
## Version       : 1.0
## Date          : 2021-09-11
## Author        : LHammonds
## Purpose       : Fix ownership/permissions on files/folders.
## Compatibility : Verified on Ubuntu Server 20.04 LTS
## Requirements  : Run as root
## Run Frequency : As needed.
## Parameters    : None
## Exit Codes    :
##    0 = Normal
##    1 = Not run as root
######################## CHANGE LOG #########################
## DATE       VER WHO WHAT WAS CHANGED
## ---------- --- --- ---------------------------------------
## 2021-09-11 1.0 LTH Created script.
#############################################################

## Import standard variables and functions. ##
source /etc/avorion.conf

LogFile="${LogDir}/avorion-fixperms.log"

#######################################
##            FUNCTIONS              ##
#######################################

function f_abort()
{
  printf "`date +%Y-%m-%d_%H:%M:%S` [ABORT] ErrorCode=${1}\n" >> ${LogFile}
  exit ${1}
} ## f_abort()

#######################################
##           MAIN PROGRAM            ##
#######################################
## Requirement Check: Script must run as root user.
if [ "$(id -u)" != "0" ]; then
  ## FATAL ERROR DETECTED: Document problem and terminate script.
  printf "\n[ERROR] Root user required to run this script.\n"
  printf "Type 'sudo $0' to run with root privileges.\n"
  f_abort 1
fi

StartTime="$(date +%s)"

printf "`date +%Y-%m-%d_%H:%M:%S` [INFO] fixperms started.\n" | tee -a ${LogFile}

## Reset ownership on everything ##
f_verbose "`date +%Y-%m-%d_%H:%M:%S` [INFO] Setting ownership."
chown --recursive ${GameUser}:${GameGroup} ${GameRootDir}
chown ${GameUser}:${GameGroup} ${LogDir}/avorion-*.log
chown ${GameUser}:${GameGroup} /etc/avorion.conf

## Set basic permissions for all folders ##
find ${GameRootDir} -type d -exec chmod 0750 {} \;

## Set basic permissions for all files ##
find ${GameRootDir} -type f -exec chmod 0440 {} \;

## Fix savegame permissions ##
find ${GameSaveDir} -type d -exec chmod 0770 {} \;
find ${GameRootDir} -type f -exec chmod 0660 {} \;

## Fix binary execution permissions ##
find ${GameRootDir}/bin -type f -exec chmod 0550 {} \;
find ${GameRootDir}/*.sh -type f -exec chmod 0550 {} \;
find ${GameRootDir}/*.so -type f -exec chmod 0550 {} \;

chmod 0750 ${GameRootDir}/.steam/steamcmd/steamcmd.sh
chmod 0750 ${GameRootDir}/.steam/steamcmd/linux32/steamcmd

chmod 0660 ${LogDir}/avorion-*.log
chmod 0640 /etc/avorion.conf

## Calculate total runtime ##
FinishTime="$(date +%s)"
ElapsedTime="$(expr ${FinishTime} - ${StartTime})"
Hours=$((${ElapsedTime} / 3600))
ElapsedTime=$((${ElapsedTime} - ${Hours} * 3600))
Minutes=$((${ElapsedTime} / 60))
Seconds=$((${ElapsedTime} - ${Minutes} * 60))
printf "  Total runtime: ${Hours} hour(s) ${Minutes} minute(s) ${Seconds} second(s)\n" | tee -a ${LogFile}
printf "`date +%Y-%m-%d_%H:%M:%S` [INFO] fixperms completed.\n" | tee -a ${LogFile}
exit 0
Here is an example of the log output:

Code: Select all

2021-09-23_16:33:21 [INFO] fixperms started.
2021-09-23_16:33:21 [INFO] Setting ownership.
  Total runtime: 0 hour(s) 0 minute(s) 1 second(s)
2021-09-23_16:33:22 [INFO] fixperms completed.
2021-12-09_13:43:50 [INFO] fixperms started.
2021-12-09_13:43:50 [INFO] Setting ownership.
  Total runtime: 0 hour(s) 0 minute(s) 2 second(s)
2021-12-09_13:43:52 [INFO] fixperms completed.
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Avorion as a Service

Post: # 984Post LHammonds »

Avorion as a Service

A generic system service will be created that will work for every instance by passing it the instance name as a parameter. This will allow the operating system to start each service when the server boots up and stop each service upon shutdown or reboot. This also means that the user can start and stop the instances just like any other standard Ubuntu service using the "systemctl" command such as "sudo systemctl stop avservice@galaxy1"

The steps below demonstrate how to create two services so you can see what changes are needed for each instance. Adjust the following commands to fit your needs.

Code: Select all

wget https://raw.githubusercontent.com/LHammonds/avorion-bash/main/avservice.service --output-document /tmp/avservice.service
chmod 0644 /tmp/avservice.service
sudo chown root:root /tmp/avservice.service
sudo mv /tmp/avservice.service /lib/systemd/system/avservice\@.service
Now edit the file:

Code: Select all

sudo vi /lib/systemd/system/avservice\@.service
Make sure the paths to the scripts are correct and that the "User" matches "GameUser" in /etc/avorion.conf and "Group" matches "GameService" in /etc/avorion.conf

Now reload the systemd daemon so it can see the newly created service file.

Code: Select all

sudo systemctl daemon-reload
Now we enable the service(s) so the operating system will start and stop the service automatically whenever the server starts up or shuts down.

Tailor the following commands to suit your environment:

Code: Select all

sudo systemctl enable avservice@galaxy1
sudo systemctl enable avservice@galaxy2
To manually start galaxy1 instance, you can run the following command:

Code: Select all

sudo systemctl start avservice@galaxy1
To check status of galaxy1 instance:

Code: Select all

sudo systemctl status avservice@galaxy1
To stop galaxy1 instance:

Code: Select all

sudo systemctl stop avservice@galaxy1
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Backup and Restore

Post: # 985Post LHammonds »

Backup

------------- WORK-IN-PROGRESS -------------

Reference: Database Backup Script

I will turn these into easy-to-run scripts but for now, this is the meat of what they will do.

Code: Select all

tar -cpf - /opt/avorion/savegame/* | 7z a -si -mx=9 -w/tmp /opt/avorion/bak/2021-09-22-avorion.tar.7z 1>/dev/null 2>&1
Restore

Code: Select all

7z x -so -w/tmp /opt/avorion/bak/2021-09-22-avorion.tar.7z | tar -C /tmp/avorion --strip-components=3 -xf -
Encrypt

Code: Select all

touch /etc/passenc
chown root:root /etc/passenc
chmod 600 /etc/passenc
echo 'CryptPass="abc123"' >> /etc/passenc
gpg --cipher-algo aes256 --output /opt/avorion/bak/2021-09-22-avorion.enc --passphrase-file /etc/passenc --batch --yes --no-tty --symmetric /opt/avorion/bak/2021-09-22-avorion.tar.7z
rm /opt/avorion/bak/2021-09-22-avorion.tar.7z
Decrypt

Code: Select all

gpg --cipher-algo aes256 --output /tmp/avorion/decrypted.tar.7z --passphrase-file /etc/passenc --quiet --batch --yes --no-tty --decrypt /opt/avorion/bak/2021-09-22-avorion.enc
User avatar
LHammonds
Site Admin
Site Admin
Posts: 972
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Installing Mods

Post: # 986Post LHammonds »

Installing Mods

------------- WORK-IN-PROGRESS -------------

Not finished with this section, these are just notes for me to try out and refine.

Code: Select all

sudo touch /opt/avorion/savegame/galaxy1/modconfig.lua
sudo chown avserver:avserver /opt/avorion/savegame/galaxy1/modconfig.lua
sudo chmod 0640 /opt/avorion/savegame/galaxy1/modconfig.lua
cat << EOF > /opt/avorion/savegame/galaxy1/modconfig.lua
--------------------------------
-- Mod Configuration File
-- Version: 1.0
-- Date: 2021-09-11
--------------------------------
-- Location of a mods folder other then the default one of /mods in your galaxy folder.
modLocation = ""
-- It is recommended by the developers of Avorion that force enabling be left turned off.
forceEnabling = false
-- List of Steam Workshop mods to enable.
mods =
{
    {workshopid = "2559722572"},
    {workshopid = "445220"}
}
-- List of Steam Workshop client-side mods allowed while on this server. Same format as mods section.
allowed =
{
}
EOF
Post Reply