0


幻兽帕鲁服务器自动重启备份-python

幻兽帕鲁服务器自动重启备份-python

基于python编写的服务器全自动管理工具,能够实现自动定时备份存档,以及在检测到服务器崩溃之后自动重新启动,并且整合了对于frp端口转发工具的自动重启。

我受够这个服务器没完没了的崩溃了,别再整天艾特我开服了

如果对你的部署很有用,欢迎评论和点赞~

1. 前置知识点

幻兽帕鲁开服教程——游戏
架设游戏私服——内网穿透工具frp

需要掌握基本python编程知识,知道怎么部署python环境与修改配置路径。

2. 目录结构

|-pal_server_manage.bat
|-pal_server_manage.py

3. 代码内容

pal_server_manage.bat

这就是一个单纯方便双击启动运行的脚本。

python D:\servers\pal_server_manage.py
pal_server_manage.py

包含了初始化启动、自动备份与自动重启的功能。

import os
import time
import zipfile
import socket
import threading
import psutil
import subprocess

# 参数配置classConfig:# 服务器路径
    server_path =r"D:\servers\steamcmd\steamapps\common\PalServer\PalServer.exe"# 计算服务器应用程序名字
    server_name = os.path.split(server_path)[-1]# frp路径
    frp_path =r"D:\servers\frp\client\frpc.exe"
    frp_config =r"D:\servers\frp\client\frpc.ini"
    frp_name = os.path.split(frp_path)[-1]# 记录正在运行的服务器
    server =None
    frp =None# 是否使用自动重启
    use_auto_restart =True# 检测服务器是否在运行的间隔(秒)
    check_server_run_step =10# 是否启动自动备份
    use_auto_backup =True# 备份的路径
    save_dir_path =r"D:\servers\steamcmd\steamapps\common\PalServer\Pal\Saved"# 备份的时间间隔(秒)
    save_time_step =900# 备份的存档路径
    output_zip_path =r"D:\servers\save_backups\pal_save_backups"
    os.makedirs(os.path.split(output_zip_path)[-1], exist_ok=True)# 将1个文件夹打包压缩为zip文件defzip_file(src_dir, zip_path):
    z = zipfile.ZipFile(zip_path,'w', zipfile.ZIP_DEFLATED)
    count =0for dirpath, _, filenames in os.walk(src_dir):
        fpath = dirpath.replace(src_dir,'')
        fpath = fpath and fpath + os.sep or''
        length =len(filenames)for filename in filenames:
            z.write(os.path.join(dirpath, filename), os.path.split(src_dir)[1]+ fpath + filename)
            count +=1print(f'\rzip file: {count}/{length}', end='')print(f'\nzip {src_dir} success!')
    z.close()# 检测程序是否在运行defis_program_running(program_name):# 扫描所有的进程idfor pid in psutil.pids():try:# 如果进程名与服务器名一致,代表服务器正在运行if psutil.Process(pid).name()== program_name:returnTrueexcept(psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):pass# 扫描所有进程后,未找到服务器returnFalse# 获取当前可视化时间信息defget_local_time():
    local = time.localtime(time.time())
    now =f"{local[0]:04d}_{local[1]:02d}_{local[2]:02d}_{local[3]:02d}_{local[4]:02d}_{local[5]:02d}"return now

# 自动备份线程classauto_server_backup(threading.Thread):def__init__(self, threadID, name):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name

    defrun(self):whileTrue:# 压缩存档文件夹到备份路径
            now = get_local_time()
            zip_path = os.path.join(Config.output_zip_path, now +".zip")
            zip_file(Config.save_dir_path, zip_path)print(f"{now}: zip file from {Config.save_dir_path} to {zip_path}")# 休眠等待
            time.sleep(Config.save_time_step)# 自动重启线程classauto_server_restart(threading.Thread):def__init__(self, threadID, name):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name

    defrun(self):whileTrue:# 如果frp没有启动,就启动它ifnot is_program_running(Config.frp_name):
                now = get_local_time()
                Config.frp = subprocess.Popen(["start", Config.frp_path,"-c", Config.frp_config])print(f"{now}: Restart frp {Config.frp_path}")# 如果服务器没有启动,就启动它ifnot is_program_running(Config.server_name):
                now = get_local_time()
                Config.server = subprocess.Popen([Config.server_path])print(f"{now}: Restart Server {Config.server_path}")# 休眠等待
            time.sleep(Config.check_server_run_step)defmain():# 检查服务器是否启动# 如果frp没有启动,就启动它ifnot is_program_running(Config.frp_name):
        Config.frp = subprocess.Popen([Config.frp_path,"-c", Config.frp_config])print(f"Start frp {Config.frp_path}")# 如果服务器没有启动,就启动它ifnot is_program_running(Config.server_name):
        Config.server = subprocess.Popen([Config.server_path])print(f"Start Server {Config.server_path}")# 自动保存线程if Config.use_auto_backup:
        thread_backup = auto_server_backup(1,"backup")
        thread_backup.start()# 自动重启线程if Config.use_auto_restart:
        thread_restart = auto_server_restart(2,"restart")
        thread_restart.start()# 在子线程结束前不要终止,也就是无限堵塞if Config.use_auto_backup:
        thread_backup.join()if Config.use_auto_restart:
        thread_restart.join()if __name__ =="__main__":
    main()

4. 原理解释

自动备份:定时压缩服务器存档文件夹到备份路径,非常简单粗暴。
注:因为没有在服务器内部运行保存命令,有极小概率可能出现玩家与世界存档不同步的问题,但是因为发生概率太低而且只要加快保存频率就不是什么大问题(其实是懒得整那么麻烦的东西),所以选择性无视了此问题。

自动保存:定时扫描所有进程,检测服务器是否在运行,发现没有在运行,就重新启动~

5. 额外备注

如果你希望主动定时关闭服务器重启,可以用该代码主动关闭服务器:

Config.server.kill()

祝大家玩得开心呀~上班当帕鲁已经够苦了,下班后都开心点吧!
在这里插入图片描述

标签: 服务器 python

本文转载自: https://blog.csdn.net/starvapour/article/details/136006557
版权归原作者 starvapour 所有, 如有侵权,请联系我们删除。

“幻兽帕鲁服务器自动重启备份-python”的评论:

还没有评论