Browse Source

valheim should be finished hopefully

feature/startup-from-labels
Mario Alegre 4 years ago
parent
commit
ab727ce48f
  1. 12
      valheim/Containerfile
  2. 48
      valheim/asssets/bin/maint
  3. 2
      valheim/asssets/bin/steamcmd
  4. 20
      valheim/asssets/bin/vh-backup
  5. 2
      valheim/asssets/bin/vh-install
  6. 49
      valheim/asssets/bin/vh-restore
  7. 15
      valheim/asssets/bin/vh-start
  8. 25
      valheim/asssets/systemd/valheim.service
  9. 11
      valheim/asssets/systemd/vh-backup.service
  10. 8
      valheim/asssets/systemd/vh-backup.timer

12
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

48
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

2
valheim/asssets/bin/steamcmd

@ -0,0 +1,2 @@
#!/bin/sh
sudo -u vhadmin /usr/games/steamcmd "$@"

20
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/"

2
valheim/asssets/bin/vh-install

@ -0,0 +1,2 @@
#!/bin/bash
steamcmd +login anonymous +force_install_dir /home/vhadmin/ +app_update 896660 validate +exit

49
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!"

15
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

25
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

11
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

8
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
Loading…
Cancel
Save