From ab727ce48f549e38884746861cdad01026abe15f Mon Sep 17 00:00:00 2001 From: Mario Alegre Date: Thu, 4 Mar 2021 21:21:51 -0500 Subject: [PATCH] valheim should be finished hopefully --- valheim/Containerfile | 12 +++++- valheim/asssets/bin/maint | 48 ++++++++++++++++++++++ valheim/asssets/bin/steamcmd | 2 + valheim/asssets/bin/vh-backup | 20 +++++++++ valheim/asssets/bin/vh-install | 2 + valheim/asssets/bin/vh-restore | 49 +++++++++++++++++++++++ valheim/asssets/bin/vh-start | 15 +++++++ valheim/asssets/systemd/valheim.service | 25 ++++++++++++ valheim/asssets/systemd/vh-backup.service | 11 +++++ valheim/asssets/systemd/vh-backup.timer | 8 ++++ 10 files changed, 190 insertions(+), 2 deletions(-) create mode 100755 valheim/asssets/bin/maint create mode 100644 valheim/asssets/bin/steamcmd create mode 100755 valheim/asssets/bin/vh-backup create mode 100644 valheim/asssets/bin/vh-install create mode 100755 valheim/asssets/bin/vh-restore create mode 100644 valheim/asssets/bin/vh-start create mode 100644 valheim/asssets/systemd/valheim.service create mode 100644 valheim/asssets/systemd/vh-backup.service create mode 100644 valheim/asssets/systemd/vh-backup.timer diff --git a/valheim/Containerfile b/valheim/Containerfile index 49d3ffd..d0fdbd4 100644 --- a/valheim/Containerfile +++ b/valheim/Containerfile @@ -22,8 +22,8 @@ ARG FILESUID=5000 RUN apt-add-repository contrib && \ apt-add-repository non-free && \ apt update -y && \ -# echo steam steam/question select "I AGREE" | sudo debconf-set-selections && \ -# echo steam steam/license note '' | sudo debconf-set-selections && \ +# echo steam steam/question select "I AGREE" | debconf-set-selections && \ +# echo steam steam/license note '' | debconf-set-selections && \ apt install -y steamcmd libsdl2-2.0-0 # create valheim server user with file owner UID @@ -39,3 +39,11 @@ COPY assets/bin/ /usr/local/bin/ ### WORKDIR /home/vhadmin +# install Valheim +RUN vh-install + +# copy units to systemd +COPY assets/systemd/ /etc/systemd/system/ + +# enable systemd units +RUN systemctl enable valheim.service diff --git a/valheim/asssets/bin/maint b/valheim/asssets/bin/maint new file mode 100755 index 0000000..c66b27f --- /dev/null +++ b/valheim/asssets/bin/maint @@ -0,0 +1,48 @@ +#!/bin/bash + +rm_ife() { + if [[ -e "$1" ]]; then + rm -r "$1" + fi +} + +# Check if server is started +status=$(systemctl show valheim -p ActiveState --value) +if [[ $status != active ]]; then + echo "valheim.service is inactive. Exiting." + exit 1 +fi + +# get time +hour=$(date +%H) +day=$(date +%d) +month=$(date +%m) +year=$(date +%Y) + +# hour at which to do daily tasks +dailyhour=4 + +# cd to backup dir +cd /vol/data/backup + +if [[ 10#$hour -eq $dailyhour ]]; then + systemctl stop valheim + sleep 1 +fi + +# hourly backup +rm_ife worlds-hourly-$hour +vh-backup worlds-hourly-$hour + +if [[ 10#$hour -eq $dailyhour ]]; then + systemctl start valheim + + # save daily and longterm backups + rm_ife worlds-daily-$day.zip + zip -rT worlds-daily-$day.zip worlds-hourly-$hour + chown "$(stat -c '%U:%G' worlds-hourly-$hour)" worlds-daily-$day.zip + touch -d "$(stat -c '%y' worlds-hourly-$hour)" worlds-daily-$day.zip + if [[ 10#$day -eq 1 ]]; then + cp -a worlds-daily-$day.zip worlds-$year-$month-$day.zip + fi +fi diff --git a/valheim/asssets/bin/steamcmd b/valheim/asssets/bin/steamcmd new file mode 100644 index 0000000..2b18dd3 --- /dev/null +++ b/valheim/asssets/bin/steamcmd @@ -0,0 +1,2 @@ +#!/bin/sh +sudo -u vhadmin /usr/games/steamcmd "$@" diff --git a/valheim/asssets/bin/vh-backup b/valheim/asssets/bin/vh-backup new file mode 100755 index 0000000..8e19255 --- /dev/null +++ b/valheim/asssets/bin/vh-backup @@ -0,0 +1,20 @@ +#!/bin/bash + +worlds_dir="/home/vhadmin/.config/unity3d/IronGate/Valheim/worlds" + +if [[ -z $1 ]]; then + echo "Usage: $0 backup_dir" + exit 2 +fi +backup_dir="$1" + +# Check if backup dir already exists +if [[ -e "$backup_dir" ]]; then + echo "Error: backup dir already exists. Delete directory before proceeding or use another directory name." + exit 1 +fi +echo "Creating directory \"$backup_dir\"..." +mkdir -p "$backup_dir" + +# copy files +rsync -aSH $worlds_dir/ "$backup_dir/" diff --git a/valheim/asssets/bin/vh-install b/valheim/asssets/bin/vh-install new file mode 100644 index 0000000..1ce8ef3 --- /dev/null +++ b/valheim/asssets/bin/vh-install @@ -0,0 +1,2 @@ +#!/bin/bash +steamcmd +login anonymous +force_install_dir /home/vhadmin/ +app_update 896660 validate +exit \ No newline at end of file diff --git a/valheim/asssets/bin/vh-restore b/valheim/asssets/bin/vh-restore new file mode 100755 index 0000000..99e7d21 --- /dev/null +++ b/valheim/asssets/bin/vh-restore @@ -0,0 +1,49 @@ +#!/bin/bash + +worlds_dir="/home/vhadmin/.config/unity3d/IronGate/Valheim/worlds" + +if [[ -z $1 ]]; then + echo "Usage: $0 backup_dir" + exit 2 +fi +backup_dir="$1" + +# Check if directory exists +if [[ ! -d "$backup_dir" ]]; then + echo "Error: directory \"$backup_dir\" not found." + exit 1 +fi + +# Check if server is active +status=$(systemctl show valheim -p ActiveState --value) +# shutdown server if it is active +if [[ $status == active ]]; then + echo "Server is active: executing shutdown..." + systemctl stop valheim + sleep 1 +fi + +worlds_dir=/home/mcadmin/worlds +# if worlds dir is non-empty, save it as worlds.bak +if [[ ! -z $(ls $worlds_dir) ]]; then + echo "Worlds directory is nonempty: saving backup as $worlds_dir.bak" + if [[ -e $worlds_dir.bak ]]; then + rm -r $worlds_dir.bak + fi + mv $worlds_dir $worlds_dir.bak + mkdir $worlds_dir + chown $(stat -c %U:%G $worlds_dir.bak) $worlds_dir +fi + +# copy backup to worlds_dir +echo "Copying worlds from backup directory..." +rsync -aSH "$backup_dir/" $worlds_dir/ + +# start server back up if it was active +if [[ $status == active ]]; then + echo "Server was active: starting server again..." + systemctl start valheim + sleep 5 +fi + +echo "Done!" diff --git a/valheim/asssets/bin/vh-start b/valheim/asssets/bin/vh-start new file mode 100644 index 0000000..f0eca06 --- /dev/null +++ b/valheim/asssets/bin/vh-start @@ -0,0 +1,15 @@ +#!/bin/bash +export templdpath=$LD_LIBRARY_PATH +export LD_LIBRARY_PATH=./linux64:$LD_LIBRARY_PATH +export SteamAppId=892970 + +echo "Starting server" + +# Load Config +. /vol/data/config + +# NOTE: Minimum password length is 5 characters & Password cant be in the server name. +# NOTE: You need to make sure the ports 2456-2458 is being forwarded to your server through your local router & firewall. +./valheim_server.x86_64 -name "$vh_name" -port 2456 -world "$vh_world" -password "$vh_pass" + +export LD_LIBRARY_PATH=$templdpath diff --git a/valheim/asssets/systemd/valheim.service b/valheim/asssets/systemd/valheim.service new file mode 100644 index 0000000..210e410 --- /dev/null +++ b/valheim/asssets/systemd/valheim.service @@ -0,0 +1,25 @@ +[Unit] +# Implicit needs are explicitly needed to survive shutdown till stop finishes +After=network.target +Description=Valheim server + +[Service] +ExecStart=/usr/local/bin/vh-start +ExecReload=/bin/kill -s HUP $MAINPID +KillSignal=SIGINT +User=vhadmin +WorkingDirectory=/home/vhadmin +Restart=on-failure +StandardOutput=journal +StandardError=journal +Type=simple +# security +PrivateUsers=true +ProtectHome=true +ProtectControlGroups=true +ProtectKernelModules=true +ProtectKernelTunables=true +ProtectSystem=full + +[Install] +WantedBy=multi-user.target diff --git a/valheim/asssets/systemd/vh-backup.service b/valheim/asssets/systemd/vh-backup.service new file mode 100644 index 0000000..9cc7368 --- /dev/null +++ b/valheim/asssets/systemd/vh-backup.service @@ -0,0 +1,11 @@ +[Unit] +After=valheim.service +Description=Valheim Server Auto-backup +Requisite=valheim.service + +[Service] +ExecStart=/usr/local/bin/maint +StandardOutput=journal +StandardError=journal +KillMode=none +Type=oneshot diff --git a/valheim/asssets/systemd/vh-backup.timer b/valheim/asssets/systemd/vh-backup.timer new file mode 100644 index 0000000..cbf037d --- /dev/null +++ b/valheim/asssets/systemd/vh-backup.timer @@ -0,0 +1,8 @@ +[Unit] +Description=Backup Valheim Server every hour + +[Timer] +OnCalendar=0/1:00:00 + +[Install] +WantedBy=timers.target