《幻兽帕鲁》服务器维护

《幻兽帕鲁》Linux 服务器维护。

cover

服务器搭建见 Pal World 技术指南

配置文件

路径:Pal/Saved/Config/LinuxServer/PalWorldSettings.ini

运维脚本

1
2
3
4
5
6
7
8
# 环境变量
export PAL_SERVER_HOME=/home/steam/Steam/steamapps/common/PalServer
export PAL_SERVER_LOG=/home/steam/app_log/PalWorld

# 创建目录
mkdir -p $PAL_SERVER_LOG/run
mkdir -p $PAL_SERVER_LOG/health_check
mkdir -p $PAL_SERVER_LOG/backup

启动脚本

启动脚本:PalServerStart.sh

1
2
#!/bin/bash
nohup $PAL_SERVER_HOME/PalServer.sh > $PAL_SERVER_LOG/run/run_$(date +\%Y\%m\%d_\%H\%M\%S).log 2>&1 &
1
2
#!/bin/bash
nohup $PAL_SERVER_HOME/PalServer.sh -useperfthreads -NoAsyncLoadingThread -UseMultithreadForDS > $PAL_SERVER_LOG/run/run_$(date +\%Y\%m\%d_\%H\%M\%S).log 2>&1 &

定时备份存档

定时备份脚本:PalServerAutoBackup.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash

echo "[$(date "+%Y%m%d %H:%M:%S")] Start Backup"

folderName=SaveGames_$(date +\%Y\%m\%d)
fileName=SaveGames_$(date +\%Y\%m\%d_\%H\%M\%S).tar.gz

fullPath=$PAL_SERVER_LOG/PalServerBackup/$folderName

mkdir -p $fullPath

/usr/bin/tar -czf $fullPath/$fileName $PAL_SERVER_HOME/Pal/Saved

echo "[$(date "+%Y%m%d %H:%M:%S")] Backup complete. Folder: $folderName, File: $fileName"

echo "[$(date "+%Y%m%d %H:%M:%S")] End Backup"

定时任务 crontab -e

1
2
3
4
5
# 30min
*/30 * * * * ~/PalServerAutoBackup.sh >> $PAL_SERVER_LOG/backup/backup_$(date +\%Y\%m\%d).log 2>&1

# 1h
0 * * * * ~/PalServerAutoBackup.sh >> $PAL_SERVER_LOG/backup/backup_$(date +\%Y\%m\%d).log 2>&1

定时检查运行状态并自动启动

运行状态检查脚本:PalServerRunCheck.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash

echo "[$(date "+%Y%m%d %H:%M:%S")] Start Check"

# 检查进程是否存在
process=$(pgrep -f "PalServer-Linux")

if [ -z "$process" ]; then
echo "[$(date "+%Y%m%d %H:%M:%S")] 进程 PalServer-Linux 不存在,开始启动..."
# 运行启动命令
~/PalServerStart.sh

# 等待 10 秒,确保进程已经启动
sleep 10

process=$(pgrep -f "PalServer-Linux")
echo "[$(date "+%Y%m%d %H:%M:%S")] 进程 PalServer-Linux 已经启动。 pid: $process"
else
echo "[$(date "+%Y%m%d %H:%M:%S")] 进程 PalServer-Linux 已经在运行中,无需重启。 pid: $process"
fi

echo "[$(date "+%Y%m%d %H:%M:%S")] End Check"
1
2
3
4
5
# 10 min 检查一次
*/10 * * * * ~/PalServerRunCheck.sh >> $PAL_SERVER_LOG/health_check/health_$(date +\%Y\%m\%d).log 2>&1

# 5 min 检查一次
*/5 * * * * ~/PalServerRunCheck.sh >> $PAL_SERVER_LOG/health_check/health_$(date +\%Y\%m\%d).log 2>&1

其它常用命令

下载存档

1
2
3
mkdir -p ~/PalWorld/SavedBackup/

scp -i ~/.ssh/secret.pem user@ip:/home/steam/app_log/PalWorld/PalServerBackup/SaveGames_20240131/SaveGames_20240131_190001.tar.gz ~/PalWorld/SavedBackup/

解压存档

1
2
# 解压
tar -zxvf SaveGames_20240121_170712.tar.gz