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
 
 
 
 
 
 
 
 
 
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# coding: utf-8
# -------------------------------------------------------------------
# MySQL Binlog增量备份系统 - 配置管理模块
# -------------------------------------------------------------------
# Author: miku <miku@bt.cn>
# -------------------------------------------------------------------

import json
import os
import sys
import time
import datetime
import threading
from typing import Dict, List, Any, Optional

if "/www/server/panel/class" not in sys.path:
    sys.path.insert(0, "/www/server/panel/class")

if '/www/server/panel' not in sys.path:
    sys.path.insert(0, '/www/server/panel')

import public

class AdvancedScheduleCalculator:
    """高级调度计算器"""
    
    def __init__(self):
        pass
    
    def calculate_next_full_backup(self, schedule_config: Dict[str, Any], last_execution: str = None) -> str:
        """计算下次全量备份时间"""
        schedule_type = schedule_config.get('type', 'hours')
        
        if schedule_type == 'daily':
            return self._calculate_daily(schedule_config)
        elif schedule_type == 'weekly':
            return self._calculate_weekly(schedule_config)
        elif schedule_type == 'interval':
            return self._calculate_interval(schedule_config, last_execution)
        elif schedule_type == 'hours':
            return self._calculate_hours(schedule_config)
        else:
            # 默认使用小时模式
            return self._calculate_hours(schedule_config)
    
    def _calculate_daily(self, config: Dict[str, Any]) -> str:
        """每天固定时间执行"""
        target_time = config.get('time')
        if not target_time:
            raise ValueError("daily模式缺少 time 配置")
        
        now = datetime.datetime.now()
        today = now.date()
        
        # 解析目标时间
        time_parts = target_time.split(':')
        target_hour = int(time_parts[0])
        target_minute = int(time_parts[1])
        target_second = int(time_parts[2]) if len(time_parts) > 2 else 0
        
        # 今天的目标时间
        target_datetime = datetime.datetime.combine(
            today, 
            datetime.time(target_hour, target_minute, target_second)
        )
        
        # 如果今天的目标时间已过,计算明天的时间
        if now >= target_datetime:
            target_datetime += datetime.timedelta(days=1)
        
        return target_datetime.strftime('%Y-%m-%d %H:%M:%S')
    
    def _calculate_weekly(self, config: Dict[str, Any]) -> str:
        """每周固定时间执行"""
        target_weekday = config.get('weekday')
        target_time = config.get('time')
        
        if target_weekday is None:
            raise ValueError("weekly模式缺少 weekday 配置")
        if not target_time:
            raise ValueError("weekly模式缺少 time 配置")
        
        now = datetime.datetime.now()
        
        # 解析目标时间
        time_parts = target_time.split(':')
        target_hour = int(time_parts[0])
        target_minute = int(time_parts[1])
        target_second = int(time_parts[2]) if len(time_parts) > 2 else 0
        
        # 计算距离目标星期几还有多少天
        current_weekday = now.weekday()
        # Python的weekday(): 0=周一, 6=周日
        # 我们的配置: 0=周日, 1=周一...6=周六
        # 转换: 我们的周日(0) = Python的周日(6)
        python_target_weekday = 6 if target_weekday == 0 else target_weekday - 1
        
        days_ahead = python_target_weekday - current_weekday
        if days_ahead < 0:  # 这周的目标日已过
            days_ahead += 7
        elif days_ahead == 0:  # 今天就是目标日
            target_today = now.replace(hour=target_hour, minute=target_minute, second=target_second, microsecond=0)
            if now >= target_today:  # 今天的目标时间已过
                days_ahead = 7
        
        target_date = now.date() + datetime.timedelta(days=days_ahead)
        target_datetime = datetime.datetime.combine(
            target_date,
            datetime.time(target_hour, target_minute, target_second)
        )
        
        return target_datetime.strftime('%Y-%m-%d %H:%M:%S')
    
    def _calculate_interval(self, config: Dict[str, Any], last_execution: str = None) -> str:
        """固定间隔天数执行"""
        interval_days = config.get('interval_days')
        target_time = config.get('time')
        start_date = config.get('start_date')
        
        if not interval_days:
            raise ValueError("interval模式缺少 interval_days 配置")
        if not target_time:
            raise ValueError("interval模式缺少 time 配置")
        
        # 解析目标时间
        time_parts = target_time.split(':')
        target_hour = int(time_parts[0])
        target_minute = int(time_parts[1])
        target_second = int(time_parts[2]) if len(time_parts) > 2 else 0
        
        if last_execution:
            # 基于上次执行时间计算
            last_date = datetime.datetime.strptime(last_execution, '%Y-%m-%d %H:%M:%S').date()
            next_date = last_date + datetime.timedelta(days=interval_days)
        elif start_date:
            # 基于起始日期计算
            start = datetime.datetime.strptime(start_date, '%Y-%m-%d').date()
            now = datetime.datetime.now()
            
            #如果时间大于未来 直接用未来时间
            if start > now.date():
                next_date = start
            else:
                days_passed = (now.date() - start).days
                intervals_passed = days_passed // interval_days
                next_date = start + datetime.timedelta(days=(intervals_passed + 1) * interval_days)
        else:
            next_date = datetime.datetime.now().date() + datetime.timedelta(days=interval_days)
        
        target_datetime = datetime.datetime.combine(
            next_date,
            datetime.time(target_hour, target_minute, target_second)
        )
        
        return target_datetime.strftime('%Y-%m-%d %H:%M:%S')
    
    def _calculate_hours(self, config: Dict[str, Any]) -> str:
        """小时间隔执行"""
        interval_hours = config.get('interval_hours')
        if not interval_hours:
            raise ValueError("hours模式缺少 interval_hours 配置")
        
        now = datetime.datetime.now()
        next_time = now + datetime.timedelta(hours=interval_hours)
        return next_time.strftime('%Y-%m-%d %H:%M:%S')
    
    def parse_schedule_from_request(self, get_obj) -> Dict[str, Any]:
        """从请求对象解析调度配置"""
        if not hasattr(get_obj, 'schedule_type'):
            raise ValueError("缺少调度类型 schedule_type")
        
        schedule = {
            "type": get_obj.schedule_type
        }
        
        if get_obj.schedule_type == 'daily':
            if not hasattr(get_obj, 'schedule_time'):
                raise ValueError("daily模式需要提供 schedule_time 参数")
            schedule["time"] = get_obj.schedule_time
            
        elif get_obj.schedule_type == 'weekly':
            if not hasattr(get_obj, 'schedule_time'):
                raise ValueError("weekly模式需要提供 schedule_time 参数")
            if not hasattr(get_obj, 'weekday'):
                raise ValueError("weekly模式需要提供 weekday 参数")
            schedule["time"] = get_obj.schedule_time
            schedule["weekday"] = int(get_obj.weekday)
            
        elif get_obj.schedule_type == 'interval':
            if not hasattr(get_obj, 'schedule_time'):
                raise ValueError("interval模式需要提供 schedule_time 参数")
            if not hasattr(get_obj, 'interval_days'):
                raise ValueError("interval模式需要提供 interval_days 参数")
            schedule["time"] = get_obj.schedule_time
            schedule["interval_days"] = int(get_obj.interval_days)
            schedule["start_date"] = getattr(get_obj, 'start_date', None)
            
        elif get_obj.schedule_type == 'hours':
            if not hasattr(get_obj, 'interval_hours'):
                raise ValueError("hours模式需要提供 interval_hours 参数")
            schedule["interval_hours"] = int(get_obj.interval_hours)
        else:
            raise ValueError(f"不支持的调度类型: {get_obj.schedule_type}")
        
        return schedule

class ConfigManager:
    def __init__(self):
        self.base_path = '/www/backup/mysql_binlog_backup'
        self.config_file = os.path.join(self.base_path, 'backup_tasks.json')
        self.lock = threading.Lock()
        self.schedule_calculator = AdvancedScheduleCalculator()
        
        # 确保配置目录存在
        if not os.path.exists(self.base_path):
            os.makedirs(self.base_path, exist_ok=True)
            
        # 初始化配置文件
        if not os.path.exists(self.config_file):
            self._init_config_file()

    def _init_config_file(self):
        """初始化配置文件"""
        initial_config = {
            "version": "1.0",
            "create_time": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
            "tasks": {}
        }
        self._write_config(initial_config)

    def _read_config(self) -> Dict[str, Any]:
        """读取配置文件"""
        try:
            if os.path.exists(self.config_file):
                content = public.ReadFile(self.config_file)
                if content:
                    return json.loads(content)
            return self._get_default_config()
        except Exception as e:
            print(f"读取配置文件失败: {e}")
            return self._get_default_config()

    def _write_config(self, config: Dict[str, Any]) -> bool:
        """写入配置文件"""
        try:
            with self.lock:
                config['update_time'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                content = json.dumps(config, ensure_ascii=False, indent=2)
                public.WriteFile(self.config_file, content)
                return True
        except Exception as e:
            print(f"写入配置文件失败: {e}")
            return False

    def _get_default_config(self) -> Dict[str, Any]:
        """获取默认配置"""
        return {
            "version": "1.0",
            "create_time": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
            "tasks": {}
        }

    def save_backup_task_config(self, task_config: Dict[str, Any]) -> Dict[str, Any]:
        """保存备份任务配置"""
        try:
            config = self._read_config()
            database_name = task_config['database_name']
            
            # 必须包含全量备份调度配置
            if 'full_backup_schedule' not in task_config:
                return {"status": False, "msg": "缺少全量备份调度配置 full_backup_schedule"}
            
            schedule_config = task_config['full_backup_schedule']
            
            # 计算下次全量备份时间
            next_full_backup = self.schedule_calculator.calculate_next_full_backup(schedule_config)
            task_config['next_full_backup'] = next_full_backup
            
            # 增量备份时间计算
            incremental_interval = task_config.get('incremental_backup_interval', 30)
            next_full_time = datetime.datetime.strptime(next_full_backup, '%Y-%m-%d %H:%M:%S')
            next_incremental_time = next_full_time + datetime.timedelta(minutes=incremental_interval)
            task_config['next_incremental_backup'] = next_incremental_time.strftime('%Y-%m-%d %H:%M:%S')
            
            task_config['task_id'] = f"{database_name}_{int(time.time())}"
            
            config['tasks'][database_name] = task_config
            
            if self._write_config(config):
                return {"status": True, "msg": "保存成功", "data": task_config}
            else:
                return {"status": False, "msg": "写入配置文件失败"}
                
        except Exception as e:
            return {"status": False, "msg": f"保存配置失败: {str(e)}"}

    def get_backup_task_list(self) -> List[Dict[str, Any]]:
        """获取备份任务列表"""
        try:
            config = self._read_config()
            tasks = list(config.get('tasks', {}).values())
            
            # 添加状态信息
            for task in tasks:
                task['status'] = self._get_task_status(task)
                
            return tasks
        except Exception as e:
            print(f"获取备份任务列表失败: {e}")
            return []

    def get_backup_task_config(self, database_name: str) -> Optional[Dict[str, Any]]:
        """获取指定数据库的备份任务配置"""
        try:
            config = self._read_config()
            return config.get('tasks', {}).get(database_name)
        except Exception as e:
            print(f"获取备份任务配置失败: {e}")
            return None

    def update_backup_task_config(self, database_name: str, update_data: Dict[str, Any]) -> Dict[str, Any]:
        """更新备份任务配置"""
        try:
            config = self._read_config()
            
            if database_name not in config.get('tasks', {}):
                return {"status": False, "msg": "备份任务不存在"}
            
            task_config = config['tasks'][database_name]
            
            # 更新配置
            for key, value in update_data.items():
                if key in ['full_backup_interval', 'incremental_backup_interval', 'enabled']:
                    task_config[key] = value
            
            # 如果更新了间隔时间,重新计算下次执行时间
            if 'full_backup_interval' in update_data:
                now = datetime.datetime.now()
                task_config['next_full_backup'] = self._calculate_next_time(now, task_config['full_backup_interval'] * 60)
            
            if 'incremental_backup_interval' in update_data:
                now = datetime.datetime.now()
                task_config['next_incremental_backup'] = self._calculate_next_time(now, task_config['incremental_backup_interval'])
                
            config['tasks'][database_name] = task_config
            
            if self._write_config(config):
                return {"status": True, "msg": "更新成功", "data": task_config}
            else:
                return {"status": False, "msg": "写入配置文件失败"}
                
        except Exception as e:
            return {"status": False, "msg": f"更新配置失败: {str(e)}"}

    def update_backup_execution_time(self, database_name: str, backup_type: str, execution_time: str = None) -> bool:
        """更新备份执行时间"""
        try:
            config = self._read_config()
            
            if database_name not in config.get('tasks', {}):
                return False
                
            task_config = config['tasks'][database_name]
            
            if execution_time is None:
                execution_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            
            if backup_type == 'full':
                task_config['last_full_backup'] = execution_time
                
                # 使用调度计算器计算下次全量备份时间
                schedule_config = task_config.get('full_backup_schedule')
                if not schedule_config:
                    return False  # 配置格式错误
                
                next_full_backup = self.schedule_calculator.calculate_next_full_backup(schedule_config, execution_time)
                task_config['next_full_backup'] = next_full_backup
                
                # 全量备份完成后,立即设置增量备份时间
                now = datetime.datetime.now()
                task_config['next_incremental_backup'] = self._calculate_next_time(now, task_config['incremental_backup_interval'])
                
            elif backup_type == 'incremental':
                task_config['last_incremental_backup'] = execution_time
                # 计算下次增量备份时间
                now = datetime.datetime.now()
                task_config['next_incremental_backup'] = self._calculate_next_time(now, task_config['incremental_backup_interval'])
            
            config['tasks'][database_name] = task_config
            return self._write_config(config)
            
        except Exception as e:
            print(f"更新备份执行时间失败: {e}")
            return False

    def delete_backup_task_config(self, database_name: str) -> Dict[str, Any]:
        """删除备份任务配置"""
        try:
            config = self._read_config()
            
            if database_name not in config.get('tasks', {}):
                return {"status": False, "msg": "备份任务不存在"}
            
            del config['tasks'][database_name]
            
            if self._write_config(config):
                return {"status": True, "msg": "删除成功"}
            else:
                return {"status": False, "msg": "写入配置文件失败"}
                
        except Exception as e:
            return {"status": False, "msg": f"删除配置失败: {str(e)}"}

    def get_pending_tasks(self) -> List[Dict[str, Any]]:
        """获取待执行的任务"""
        try:
            config = self._read_config()
            pending_tasks = []
            now = datetime.datetime.now()
            
            for database_name, task_config in config.get('tasks', {}).items():
                if not task_config.get('enabled', True):
                    continue
                    
                # 检查是否需要执行全量备份
                next_full = task_config.get('next_full_backup')
                if next_full and datetime.datetime.strptime(next_full, '%Y-%m-%d %H:%M:%S') <= now:
                    pending_tasks.append({
                        'database_name': database_name,
                        'backup_type': 'full',
                        'config': task_config
                    })
                
                # 检查是否需要执行增量备份
                next_incremental = task_config.get('next_incremental_backup')
                if next_incremental and datetime.datetime.strptime(next_incremental, '%Y-%m-%d %H:%M:%S') <= now:
                    # 确保有全量备份基础
                    if task_config.get('last_full_backup'):
                        pending_tasks.append({
                            'database_name': database_name,
                            'backup_type': 'incremental',
                            'config': task_config
                        })
            
            return pending_tasks
            
        except Exception as e:
            print(f"获取待执行任务失败: {e}")
            return []

    def _calculate_next_time(self, current_time: datetime.datetime, interval_minutes: int) -> str:
        """计算下次执行时间"""
        next_time = current_time + datetime.timedelta(minutes=interval_minutes)
        return next_time.strftime('%Y-%m-%d %H:%M:%S')

    def _get_task_status(self, task: Dict[str, Any]) -> str:
        """获取任务状态"""
        if not task.get('enabled', True):
            return 'disabled'
        
        now = datetime.datetime.now()
        
        # 检查是否有备份正在进行
        if task.get('backup_running', False):
            return 'running'
        
        # 检查下次执行时间
        next_full = task.get('next_full_backup')
        next_incremental = task.get('next_incremental_backup')
        
        if next_full and datetime.datetime.strptime(next_full, '%Y-%m-%d %H:%M:%S') <= now:
            return 'pending_full'
        elif next_incremental and datetime.datetime.strptime(next_incremental, '%Y-%m-%d %H:%M:%S') <= now:
            return 'pending_incremental'
        else:
            return 'waiting'

    def set_task_running_status(self, database_name: str, running: bool) -> bool:
        """设置任务运行状态"""
        try:
            config = self._read_config()
            
            if database_name in config.get('tasks', {}):
                config['tasks'][database_name]['backup_running'] = running
                return self._write_config(config)
            
            return False
        except Exception as e:
            print(f"设置任务运行状态失败: {e}")
            return False

if __name__ == '__main__':
    # 测试代码
    config_manager = ConfigManager()
    
    # 测试保存任务配置
    test_config = {
        'database_name': 'test_db',
        'full_backup_interval': 24,    # 24小时执行一次全量备份
        'incremental_backup_interval': 30,  # 30分钟执行一次增量备份
        'enabled': True,
        'create_time': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    }
    
    result = config_manager.save_backup_task_config(test_config)
    print("保存配置结果:", result)
    
    # 测试获取任务列表
    tasks = config_manager.get_backup_task_list()
    print("任务列表:", tasks)
    
    # 测试获取待执行任务
    pending = config_manager.get_pending_tasks()
    print("待执行任务:", pending)