-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.py
67 lines (57 loc) · 1.95 KB
/
start.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import time
import psutil
from win10toast import ToastNotifier
import math
import zipfile
toaster = ToastNotifier()
# 目標資料夾
target_folder = "Pal/Saved/SaveGames/0/"
# 備份檔資料夾
backup_files_folder = "Pal/Saved/SaveGames/"
# 備份檔數量
file_count = 5
# 備份間隔(秒)
interval = 300
def tobackup(zip_path, dst_dir, backup_files):
# 生成名稱
zip_name = zipfile.ZipFile(dst_dir + time.strftime("%Y%m%d%H%M%S") + '.zip', 'w', zipfile.ZIP_DEFLATED)
# 生成檔案
for root, dirs, files in os.walk(zip_path):
for file in files:
zip_name.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), os.path.join(zip_path, '..')))
zip_name.close
# list根目錄物件
all_files = sorted(os.listdir(dst_dir))
# 過濾
all_backups = [file for file in all_files if file.endswith('.zip')]
# 大於設定數量 刪除最舊檔案
while len(all_backups) > backup_files:
oldest_backup = all_backups.pop(0)
os.remove(os.path.join(dst_dir, oldest_backup))
def server_health(name='PalServer.exe'):
# 伺服器狀態
for process in psutil.process_iter(['name']):
if process.info['name'] == name:
return True
return False
def shutdown():
toaster.show_toast("偵測到伺服器關閉,程式已停止\n server no longer there, stop backup process")
exit()
while True:
start_time = time.time()
if server_health():
tobackup(target_folder, backup_files_folder, file_count)
else:
shutdown()
for i in range(math.floor(interval/10)):
if time.time() - start_time >= interval:
break
time.sleep(10)
if server_health():
print('Running')
else:
shutdown()
remaining_time = interval - (time.time() - start_time)
if remaining_time > 0:
time.sleep(remaining_time)