-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoZipBackup.py
149 lines (122 loc) · 4.37 KB
/
AutoZipBackup.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import datetime
import os
import json
from apscheduler.schedulers.background import BackgroundScheduler
from mcdreforged.api.all import *
PLUGIN_ID = 'auto_zip_backup'
PLUGIN_METADATA = {
'id': PLUGIN_ID,
'version': '1.0.0',
'name': 'AutoZipBackup',
'description': '定时自动备份与防熊备份',
'author': [
'Sanluli36li'
],
'link': 'https://github.com/DGBCraft/AutoZipBackup',
'dependencies': {
'mcdreforged': '>=1.2.0',
'minecraft_data_api': '*',
'permanent_backup': '>=1.0.0'
}
}
config = {
'new_player_backup': True, # 当新玩家登录时执行一次备份
'daily_backup': True, # 每日定时执行备份
'daily_backup_time': '02:00:00', # 自动备份时间
'minimum_backup_interval': 600, # 备份最小执行间隔, 默认为10分钟 (仅针对新玩家备份)
'require_player_online': True # 如果上次备份后没有任何玩家在线过,则不再执行备份
}
player_list = []
default_config = config.copy()
CONFIG_FILE = os.path.join('config', PLUGIN_ID, 'config.json')
PLAYER_LIST_FILE = os.path.join('config', PLUGIN_ID, 'players.json')
player_logged = False
last_backup_time = 0
def make_backup(server: ServerInterface, comment: str, boardcast: bool = True):
global last_backup_time, player_logged
if boardcast:
server.say('正在执行备份: {}'.format(comment))
server.execute_command('!!backup make {}'.format(comment))
last_backup_time = datetime.datetime.now().timestamp()
players = server.get_plugin_instance('minecraft_data_api').get_server_player_list()
if players[0] == 0:
player_logged = False
else:
player_logged = True
def daily_backup(server: ServerInterface):
global config, player_logged
if not config['require_player_online'] or player_logged:
make_backup(server, 'daily')
####################
# Scheduler
####################
scheduler = None
def init_scheduler(server: ServerInterface):
global config, scheduler
if scheduler:
scheduler.remove_all_jobs()
scheduler.shutdown()
del scheduler
scheduler = BackgroundScheduler()
hour, min, sec = config['daily_backup_time'].split(':')
scheduler.add_job(daily_backup, 'cron', args = [server], id = 'daily_backup',
hour = int(hour), minute = int(min), second = int(sec))
scheduler.start()
def drop_scheduler():
global scheduler
if scheduler:
scheduler.remove_all_jobs()
scheduler.shutdown()
del scheduler
####################
# MCDR Events
####################
def on_load(server: ServerInterface, old):
global config, players, player_logged, scheduler
load_config(server)
load_players()
player_logged = True
if config['daily_backup']:
init_scheduler(server)
def on_remove(server: ServerInterface):
drop_scheduler()
def on_unloaded(server: ServerInterface):
drop_scheduler()
def on_player_joined(server: ServerInterface, player: str, info: Info):
global config, last_backup_time, player_list, player_logged
player_logged = True
# 新玩家加入游戏
if config['new_player_backup'] and player not in player_list:
player_list.append(player)
save_players()
# 仅超过备份时间间隔才可再次执行备份
if datetime.datetime.now().timestamp() - last_backup_time > config['minimum_backup_interval']:
make_backup(server, 'NEW-PLAYER-' + player, False)
####################
# Config
####################
def load_config(server, source: CommandSource or None = None):
global config
try:
config = {}
with open(CONFIG_FILE) as file:
json_data = json.load(file)
for key in default_config.keys():
config[key] = json_data[key]
except:
server.logger.info('Fail to read config file, using default value')
config = default_config
with open(CONFIG_FILE, 'w') as file:
json.dump(config, file, indent=4)
def load_players():
global player_list
try:
with open(PLAYER_LIST_FILE) as file:
player_list = json.load(file)
except:
with open(PLAYER_LIST_FILE, 'w') as file:
json.dump([], file, indent=4)
def save_players():
global player_list
with open(PLAYER_LIST_FILE, 'w') as file:
json.dump(player_list, file, indent=4)