Skip to content

Commit e25776e

Browse files
committed
perf: 修改默认根目录增加备份文件夹
1 parent 32a910a commit e25776e

File tree

4 files changed

+45
-32
lines changed

4 files changed

+45
-32
lines changed

Diff for: src/backup.py

+27-18
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,45 @@ class Backup:
1414
def __init__(self):
1515
self.conf = read_conf.read_config()
1616
self.appName = 'PalServer-Win64-Test-Cmd.exe'
17+
self.backup_source = os.path.join(self.conf['main_directory'], 'Pal', 'Saved')
1718

1819
# 备份任务
1920
def backup_task(self):
2021
# 在当前目录下创建名为Backup的文件夹
21-
backup_dir = 'Backup'
22-
if not os.path.exists(backup_dir):
23-
os.makedirs(backup_dir)
22+
if self.conf['backup_dir'] == '':
23+
backup_dir = 'Backup'
24+
if not os.path.exists(backup_dir):
25+
os.makedirs(backup_dir)
26+
else:
27+
backup_dir = self.conf['backup_dir']
28+
if not os.path.exists(backup_dir):
29+
os.makedirs(backup_dir)
2430

2531
# 如果备份间隔不为空,则执行备份
2632
if self.conf['backup_interval']:
2733

2834
INFO.logger.info("自动备份已开启,正在进行备份......")
2935
print("\n自动备份已开启,正在进行备份......")
36+
try:
37+
while True:
38+
datetime_now = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
39+
40+
# 备份文件
41+
shutil.copytree(self.backup_source, os.path.join(backup_dir, f"Saved_{datetime_now}"))
3042

31-
while True:
32-
datetime_now = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
33-
34-
# 备份文件
35-
shutil.copytree(self.conf['backup_source'], os.path.join(backup_dir, f"Saved_{datetime_now}"))
36-
37-
time.sleep(1)
38-
INFO.logger.info("备份成功,文件名为:Saved_" + datetime_now)
39-
print("备份成功,文件名为:Saved_" + datetime_now)
40-
# 存档备份位置
41-
backup_path = os.path.join(os.getcwd(), backup_dir)
42-
print(f"存档备份位置:{backup_path}")
43-
# 显示倒计时并等待指定的备份间隔
44-
for i in range(int(self.conf['backup_interval']), 0, -1):
45-
print(f'\r下一次备份将在 {i} 秒后开始...', end='')
4643
time.sleep(1)
44+
INFO.logger.info("备份成功,文件名为:Saved_" + datetime_now)
45+
print("备份成功,文件名为:Saved_" + datetime_now)
46+
# 存档备份位置
47+
backup_path = os.path.join(os.getcwd(), backup_dir)
48+
print(f"存档备份位置:{backup_path}")
49+
# 显示倒计时并等待指定的备份间隔
50+
for i in range(int(self.conf['backup_interval']), 0, -1):
51+
print(f'\r下一次备份将在 {i} 秒后开始...', end='')
52+
time.sleep(1)
53+
except FileNotFoundError as e:
54+
INFO.logger.error(f"备份失败,{e}")
55+
print(f"备份失败,{e}")
4756

4857
# 备份时间必须大于等于60秒
4958
elif int(self.conf['backup_interval']) < 60:

Diff for: src/config.ini

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[Settings]
22
# *PalServer程序路径
3-
program_path = D:\Steam\steamapps\common\PalServer\PalServer.exe
3+
main_directory = D:\Steam\steamapps\common\PalServer
44

5-
# *存档备份源目录(默认自动在当前目录创建Backup存档目录)
6-
backup_source = D:\Steam\steamapps\common\PalServer\Pal\Saved
5+
# 需要备份到的目录(为空的话则会默认在当前目录创建Backup存档目录)
6+
backup_dir =
77

88
# 启动参数(从启动参数自定义端口和玩家数量,例如: arguments = -port=21424 -players=32)
99
# 可以为空

Diff for: src/read_conf.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
import codecs
55
import configparser
66
import json
7-
7+
import os
88

99
def read_config():
1010
# 读取配置文件
1111
config = configparser.ConfigParser()
12-
with codecs.open('config.ini', 'r', encoding='utf-8-sig') as f:
12+
config_path = os.path.join(os.path.dirname(__file__), 'config.ini')
13+
with codecs.open(config_path, 'r', encoding='utf-8-sig') as f:
1314
config.read_file(f)
14-
program_path = config.get('Settings', 'program_path')
15-
backup_source = config.get('Settings', 'backup_source')
15+
main_directory = config.get('Settings', 'main_directory')
16+
backup_dir = config.get('Settings', 'backup_dir')
1617
arguments = config.get('Settings', 'arguments')
1718
use_multicore_options = config.getboolean('Settings', 'use_multicore_options')
1819
backup_interval_hours = config.getint('Settings', 'backup_interval_hours')
@@ -36,8 +37,8 @@ def read_config():
3637
backup_interval = (backup_interval_hours * 60 + backup_interval_minutes) * 60
3738

3839
return {
39-
'program_path': program_path,
40-
'backup_source': backup_source,
40+
'main_directory': main_directory,
41+
'backup_dir': backup_dir,
4142
'arguments': arguments,
4243
'use_multicore_options': use_multicore_options,
4344
'rcon_enabled': rcon_enabled,

Diff for: src/task_scheduler.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@
55
import subprocess
66
import time
77
import psutil
8-
import rcon
9-
from src import read_conf
8+
import os
9+
import read_conf
1010
import threading
11-
from src.utils.log_control import INFO
11+
from utils.log_control import INFO
1212
from rcon.source import Client
1313
from rcon.source.proto import Packet
14+
from rcon.exceptions import WrongPassword
1415

1516

1617
class TaskScheduler:
1718
def __init__(self):
1819
self.conf = read_conf.read_config()
1920
self.appName = 'PalServer-Win64-Test-Cmd.exe'
21+
self.program_path = os.path.join(self.conf['main_directory'], 'PalServer.exe')
2022
self.host = self.conf['rcon_host'],
2123
self.port = self.conf['rcon_port'],
2224
self.passwd = self.conf['rcon_password'],
@@ -57,7 +59,8 @@ def check_rcon(self):
5759
time.sleep(1)
5860
else:
5961
return False
60-
except rcon.exceptions.WrongPassword:
62+
63+
except WrongPassword:
6164
INFO.logger.error("[ RCON ] RCON密码错误,请检查相关设置")
6265
print("[ RCON ] RCON密码错误,请检查相关设置")
6366
time.sleep(2)
@@ -67,7 +70,7 @@ def check_rcon(self):
6770
def start_program(self):
6871
INFO.logger.info("[ 启动任务 ] 正在启动程序......")
6972
print("[ 启动任务 ] 正在启动程序......")
70-
program_args = [self.conf['program_path']]
73+
program_args = self.program_path
7174

7275
if self.conf['arguments']:
7376
INFO.logger.info("[ 启动任务 ] 已配置额外参数")

0 commit comments

Comments
 (0)