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 
 
 
 
 
 
 
 
7f4dc1a
4daba04
7f4dc1a
 
4daba04
 
 
 
 
 
 
 
 
7f4dc1a
4daba04
7f4dc1a
 
4daba04
 
 
a8f2cc7
7f4dc1a
e10a1ce
 
 
 
 
7f4dc1a
e10a1ce
 
7f4dc1a
e10a1ce
 
7f4dc1a
 
e10a1ce
 
7f4dc1a
e10a1ce
 
 
 
 
 
7f4dc1a
 
e10a1ce
 
7f4dc1a
e10a1ce
7f4dc1a
 
 
e10a1ce
 
 
 
7f4dc1a
e10a1ce
7f4dc1a
 
 
 
 
 
 
 
 
 
 
 
e10a1ce
 
 
 
a8f2cc7
7f4dc1a
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import os
import tarfile
import datetime
import threading
import time
import json
import shutil
from flask import Flask, render_template_string, request, jsonify
from huggingface_hub import HfApi

app = Flask(__name__)

SOURCE_MOUNT = "/mnt/data"
BACKUP_DATASET = "sjfs/memos-backup-data"
CONFIG_FILE = "/tmp/auto_backup_config.json"

auto_interval_seconds = 86400
_last_backup_time = None
_auto_stop_event = threading.Event()
_auto_thread = None


def load_config():
    global auto_interval_seconds
    try:
        with open(CONFIG_FILE, 'r') as f:
            config = json.load(f)
            auto_interval_seconds = config.get('interval_seconds', 86400)
    except (FileNotFoundError, json.JSONDecodeError, OSError):
        pass


def save_config():
    try:
        with open(CONFIG_FILE, 'w') as f:
            json.dump({'interval_seconds': auto_interval_seconds}, f)
    except OSError:
        pass


HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Memos Backup</title>
    <style>
        :root {
            --bg: #F5F3EF;
            --surface: #FFFFFF;
            --border: #DDD9D2;
            --border-light: #EBE8E2;
            --text: #1F1F1F;
            --text-muted: #8A8A8A;
            --text-faint: #B5B5B5;
            --accent: #6B7280;
            --accent-hover: #555D67;
            --accent-surface: #F2F3F5;
            --green: #6B8F71;
            --green-surface: #EFF5F0;
            --red: #B86B6B;
            --red-surface: #F5F0F0;
            --blue: #6B8BA4;
            --blue-surface: #EFF4F7;
            --radius: 5px;
            --transition: 0.2s ease;
        }

        * { margin: 0; padding: 0; box-sizing: border-box; }

        body {
            font-family: -apple-system, 'Hiragino Sans', 'Noto Sans SC', 'Yu Gothic', sans-serif;
            background: var(--bg);
            color: var(--text);
            font-size: 14px;
            line-height: 1.6;
            -webkit-font-smoothing: antialiased;
        }

        .top-bar {
            height: 3px;
            background: var(--accent);
        }

        .container {
            max-width: 680px;
            margin: 0 auto;
            padding: 40px 20px 60px;
        }

        .header {
            margin-bottom: 36px;
        }

        .header h1 {
            font-size: 22px;
            font-weight: 600;
            letter-spacing: -0.02em;
            color: var(--text);
            margin-bottom: 8px;
        }

        .header h1::after {
            content: '';
            display: block;
            width: 24px;
            height: 2px;
            background: var(--accent);
            margin-top: 10px;
        }

        .header-meta {
            font-size: 12px;
            color: var(--text-muted);
            margin-top: 10px;
        }

        .header-meta code {
            background: var(--accent-surface);
            padding: 2px 6px;
            border-radius: 3px;
            font-size: 11px;
            color: var(--accent);
            font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
        }

        .header-meta .sep {
            margin: 0 6px;
            color: var(--text-faint);
        }

        .card {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: var(--radius);
            padding: 24px;
            margin-bottom: 16px;
            transition: border-color var(--transition);
        }

        .card:hover {
            border-color: #CCC8C0;
        }

        .card-title {
            font-size: 11px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.1em;
            color: var(--text-muted);
            margin-bottom: 16px;
        }

        .btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 9px 20px;
            border: none;
            border-radius: var(--radius);
            font-size: 13px;
            font-weight: 500;
            letter-spacing: 0.02em;
            cursor: pointer;
            transition: background var(--transition), opacity var(--transition);
            background: var(--accent);
            color: #fff;
            font-family: inherit;
        }

        .btn:hover { background: var(--accent-hover); }
        .btn:disabled { opacity: 0.5; cursor: not-allowed; }

        .btn-danger { background: var(--red); }
        .btn-danger:hover { background: #A55D5D; }

        input, select {
            padding: 8px 12px;
            border: 1px solid var(--border);
            border-radius: var(--radius);
            font-size: 13px;
            color: var(--text);
            background: var(--surface);
            transition: border-color var(--transition);
            font-family: inherit;
        }

        input:focus, select:focus {
            outline: none;
            border-color: var(--accent);
        }

        .status {
            margin-top: 12px;
            padding: 10px 14px;
            border-radius: var(--radius);
            font-size: 13px;
            line-height: 1.5;
            animation: fadeIn 0.3s ease;
        }

        .status-success { background: var(--green-surface); color: var(--green); }
        .status-error { background: var(--red-surface); color: var(--red); }
        .status-info { background: var(--blue-surface); color: var(--blue); }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-4px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .backup-list { list-style: none; }

        .backup-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 0;
            border-bottom: 1px solid var(--border-light);
            font-size: 13px;
        }

        .backup-item:last-child { border-bottom: none; }

        .backup-date {
            color: var(--text);
            font-size: 13px;
            font-weight: 500;
        }

        .backup-filename {
            color: var(--text-faint);
            font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
            font-size: 11px;
            margin-top: 2px;
        }

        .backup-size {
            color: var(--text-muted);
            font-size: 12px;
            white-space: nowrap;
        }

        .empty-state {
            text-align: center;
            padding: 24px;
            color: var(--text-faint);
            font-size: 13px;
        }

        .auto-config {
            display: flex;
            align-items: center;
            gap: 8px;
            flex-wrap: wrap;
        }

        .auto-config label {
            font-size: 13px;
            color: var(--text-muted);
        }

        .auto-status {
            margin-top: 14px;
            padding-top: 14px;
            border-top: 1px solid var(--border-light);
            font-size: 12px;
            color: var(--text-muted);
            line-height: 1.8;
        }

        .pulse {
            display: inline-block;
            width: 6px;
            height: 6px;
            border-radius: 50%;
            background: var(--green);
            margin-right: 6px;
            vertical-align: middle;
            animation: pulse 2s ease-in-out infinite;
        }

        @keyframes pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.3; }
        }

        .warning-block {
            font-size: 12px;
            color: var(--red);
            margin-bottom: 14px;
            padding: 10px 14px;
            background: var(--red-surface);
            border-radius: var(--radius);
            border-left: 3px solid var(--red);
            line-height: 1.5;
        }

        .restore-row {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        @media (max-width: 480px) {
            .container { padding: 24px 16px 40px; }
            .card { padding: 18px; }
            .auto-config { flex-direction: column; align-items: stretch; }
            .restore-row { flex-direction: column; align-items: stretch; }
            .restore-row .btn { width: 100%; }
        }
    </style>
</head>
<body>
    <div class="top-bar"></div>
    <div class="container">
        <header class="header">
            <h1>Memos Backup</h1>
            <div class="header-meta">
                源 <code>{{ source_path }}</code><span class="sep">→</span>目标 <code>{{ backup_dataset }}</code>
            </div>
        </header>

        <section class="card">
            <div class="card-title">手动备份</div>
            <button class="btn" id="backupBtn" onclick="manualBackup()">执行备份</button>
            <div id="backupResult"></div>
        </section>

        <section class="card">
            <div class="card-title">自动备份</div>
            <div class="auto-config">
                <label>间隔</label>
                <input type="number" id="intervalInput" value="{{ auto_interval_value }}" min="1" style="width:80px;">
                <select id="intervalUnit">
                    <option value="minute" {% if auto_interval_unit == 'minute' %}selected{% endif %}>分钟</option>
                    <option value="hour" {% if auto_interval_unit == 'hour' %}selected{% endif %}>小时</option>
                    <option value="day" {% if auto_interval_unit == 'day' %}selected{% endif %}>天</option>
                </select>
                <button class="btn" onclick="setAutoInterval()">保存</button>
            </div>
            <div id="intervalResult"></div>
            <div class="auto-status" id="autoStatus">加载中...</div>
        </section>

        <section class="card">
            <div class="card-title">备份记录</div>
            <div id="backupList">
                <div class="empty-state">加载中...</div>
            </div>
        </section>

        <section class="card">
            <div class="card-title">恢复数据</div>
            <div class="warning-block">恢复操作将覆盖当前数据,此操作不可逆。</div>
            <div class="restore-row">
                <select id="restoreSelect" style="flex:1;">
                    <option value="">选择备份文件</option>
                </select>
                <button class="btn btn-danger" onclick="manualRestore()">恢复</button>
            </div>
            <div id="restoreResult"></div>
        </section>
    </div>

    <script>
        function escapeHtml(str) {
            var div = document.createElement('div');
            div.textContent = str;
            return div.innerHTML;
        }

        function formatDate(dateStr) {
            var parts = dateStr.split('_');
            if (parts.length !== 2) return dateStr;
            var d = parts[0], t = parts[1];
            if (d.length !== 8 || t.length !== 6) return dateStr;
            return d.substr(0,4) + '-' + d.substr(4,2) + '-' + d.substr(6,2) + ' ' +
                   t.substr(0,2) + ':' + t.substr(2,2) + ':' + t.substr(4,2);
        }

        function formatInterval(seconds) {
            if (seconds >= 86400 && seconds % 86400 === 0) return (seconds / 86400) + ' 天';
            if (seconds >= 3600 && seconds % 3600 === 0) return (seconds / 3600) + ' 小时';
            return Math.round(seconds / 60) + ' 分钟';
        }

        function formatSize(bytes) {
            if (!bytes) return '--';
            if (bytes < 1024) return bytes + ' B';
            if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
            return (bytes / 1024 / 1024).toFixed(2) + ' MB';
        }

        async function loadAutoStatus() {
            try {
                var response = await fetch('/api/auto_status');
                var data = await response.json();
                var el = document.getElementById('autoStatus');
                if (data.enabled) {
                    el.innerHTML = '<span class="pulse"></span>已启用 · 间隔 ' + formatInterval(data.interval_seconds) +
                        '<br>下次备份 ' + escapeHtml(data.next_backup) +
                        (data.last_backup ? '<br>上次备份 ' + escapeHtml(data.last_backup) : '');
                } else {
                    el.innerHTML = '未启用';
                }
            } catch (e) {
                document.getElementById('autoStatus').innerHTML = '状态获取失败';
            }
        }

        async function setAutoInterval() {
            var value = document.getElementById('intervalInput').value;
            var unit = document.getElementById('intervalUnit').value;
            var seconds = parseInt(value);

            if (isNaN(seconds) || seconds < 1) {
                alert('请输入有效的数字');
                return;
            }

            if (unit === 'hour') seconds *= 3600;
            if (unit === 'day') seconds *= 86400;
            if (seconds < 60) {
                alert('间隔不能小于 1 分钟');
                return;
            }

            var response = await fetch('/api/set_interval', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ interval_seconds: seconds })
            });
            var data = await response.json();
            var el = document.getElementById('intervalResult');

            if (data.success) {
                el.innerHTML = '<div class="status status-success">设置已保存</div>';
                loadAutoStatus();
                setTimeout(function() { el.innerHTML = ''; }, 3000);
            } else {
                el.innerHTML = '<div class="status status-error">' + escapeHtml(data.error) + '</div>';
            }
        }

        async function refreshBackupList() {
            try {
                var response = await fetch('/api/backups');
                var backups = await response.json();
                var listDiv = document.getElementById('backupList');
                var select = document.getElementById('restoreSelect');

                if (backups.length === 0) {
                    listDiv.innerHTML = '<div class="empty-state">暂无备份记录</div>';
                    select.innerHTML = '<option value="">暂无备份</option>';
                    return;
                }

                var html = '<ul class="backup-list">';
                select.innerHTML = '<option value="">选择备份文件</option>';
                for (var i = 0; i < backups.length; i++) {
                    var b = backups[i];
                    var dateFormatted = formatDate(b.date);
                    var sizeFormatted = formatSize(b.size);
                    html += '<li class="backup-item">' +
                        '<div><div class="backup-date">' + escapeHtml(dateFormatted) + '</div>' +
                        '<div class="backup-filename">' + escapeHtml(b.name) + '</div></div>' +
                        '<div class="backup-size">' + sizeFormatted + '</div></li>';
                    select.innerHTML += '<option value="' + escapeHtml(b.name) + '">' +
                        escapeHtml(dateFormatted) + ' (' + sizeFormatted + ')</option>';
                }
                html += '</ul>';
                listDiv.innerHTML = html;
            } catch (e) {
                document.getElementById('backupList').innerHTML = '<div class="empty-state">加载失败</div>';
            }
        }

        async function manualBackup() {
            var btn = document.getElementById('backupBtn');
            var resultDiv = document.getElementById('backupResult');
            btn.disabled = true;
            btn.textContent = '备份中...';
            resultDiv.innerHTML = '<div class="status status-info">正在备份,请稍候...</div>';

            try {
                var response = await fetch('/api/backup', { method: 'POST' });
                var data = await response.json();
                if (data.success) {
                    resultDiv.innerHTML = '<div class="status status-success">备份完成 · ' + escapeHtml(data.backup_name || '') + '</div>';
                    refreshBackupList();
                    loadAutoStatus();
                } else {
                    resultDiv.innerHTML = '<div class="status status-error">' + escapeHtml(data.error) + '</div>';
                }
            } catch (e) {
                resultDiv.innerHTML = '<div class="status status-error">请求失败</div>';
            }

            btn.disabled = false;
            btn.textContent = '执行备份';
        }

        async function manualRestore() {
            var select = document.getElementById('restoreSelect');
            var backupName = select.value;
            if (!backupName) {
                alert('请先选择一个备份');
                return;
            }
            if (!confirm('确认恢复?当前数据将被覆盖。')) return;

            var resultDiv = document.getElementById('restoreResult');
            resultDiv.innerHTML = '<div class="status status-info">正在恢复,请稍候...</div>';

            try {
                var response = await fetch('/api/restore', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ backup_name: backupName })
                });
                var data = await response.json();
                if (data.success) {
                    resultDiv.innerHTML = '<div class="status status-success">恢复完成</div>';
                } else {
                    resultDiv.innerHTML = '<div class="status status-error">' + escapeHtml(data.error) + '</div>';
                }
            } catch (e) {
                resultDiv.innerHTML = '<div class="status status-error">请求失败</div>';
            }
        }

        refreshBackupList();
        loadAutoStatus();
        setInterval(refreshBackupList, 30000);
        setInterval(loadAutoStatus, 60000);
    </script>
</body>
</html>
"""


def get_backup_files():
    try:
        api = HfApi()
        repo_info = api.repo_info(repo_id=BACKUP_DATASET, repo_type="dataset")
        siblings = getattr(repo_info, 'siblings', [])
        backups = []
        for f in siblings:
            if f.rfilename.startswith("backup_") and f.rfilename.endswith(".tar.gz"):
                backups.append({
                    "name": f.rfilename,
                    "date": f.rfilename.replace("backup_", "").replace(".tar.gz", ""),
                    "size": getattr(f, 'size', 0) or 0
                })
        backups.sort(key=lambda x: x["name"], reverse=True)
        return backups
    except Exception as e:
        print(f"获取备份列表失败: {e}")
        return []


def create_backup():
    global _last_backup_time
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    backup_name = f"backup_{timestamp}.tar.gz"
    backup_path = f"/tmp/{backup_name}"

    try:
        with tarfile.open(backup_path, "w:gz") as tar:
            tar.add(SOURCE_MOUNT, arcname="memos_data")

        api = HfApi()
        api.upload_file(
            path_or_fileobj=backup_path,
            path_in_repo=backup_name,
            repo_id=BACKUP_DATASET,
            repo_type="dataset"
        )

        os.remove(backup_path)
        _last_backup_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        return {"success": True, "backup_name": backup_name}
    except Exception as e:
        return {"success": False, "error": str(e)}


def safe_extract_tar(tar, path):
    for member in tar.getmembers():
        if member.name.startswith('/') or '..' in member.name.split('/'):
            raise ValueError(f"不安全的路径: {member.name}")
        member_path = os.path.join(path, member.name)
        if not os.path.abspath(member_path).startswith(os.path.abspath(path) + os.sep):
            raise ValueError(f"不安全的路径: {member.name}")
    tar.extractall(path)


@app.route('/')
def index():
    interval = auto_interval_seconds
    if interval % 86400 == 0:
        interval_value = interval // 86400
        interval_unit = 'day'
    elif interval % 3600 == 0:
        interval_value = interval // 3600
        interval_unit = 'hour'
    else:
        interval_value = interval // 60
        interval_unit = 'minute'

    return render_template_string(
        HTML_TEMPLATE,
        source_path=SOURCE_MOUNT,
        backup_dataset=BACKUP_DATASET,
        auto_interval_value=interval_value,
        auto_interval_unit=interval_unit,
        last_backup_time=_last_backup_time or '尚未备份'
    )


@app.route('/api/backups')
def list_backups():
    return jsonify(get_backup_files())


@app.route('/api/backup', methods=['POST'])
def do_backup():
    return jsonify(create_backup())


@app.route('/api/restore', methods=['POST'])
def do_restore():
    data = request.json
    backup_name = data.get('backup_name')
    if not backup_name:
        return jsonify({"success": False, "error": "未指定备份文件"})

    try:
        api = HfApi()
        backup_path = f"/tmp/{backup_name}"
        api.hf_hub_download(
            repo_id=BACKUP_DATASET,
            filename=backup_name,
            local_dir="/tmp",
            repo_type="dataset"
        )

        with tarfile.open(backup_path, "r:gz") as tar:
            safe_extract_tar(tar, "/tmp/restore")

        for item in os.listdir("/tmp/restore/memos_data"):
            src = f"/tmp/restore/memos_data/{item}"
            dst = f"{SOURCE_MOUNT}/{item}"
            if os.path.isdir(src):
                if os.path.exists(dst):
                    shutil.rmtree(dst)
                shutil.copytree(src, dst)
            else:
                shutil.copy2(src, dst)

        os.remove(backup_path)
        shutil.rmtree("/tmp/restore", ignore_errors=True)

        return jsonify({"success": True, "message": f"已从 {backup_name} 恢复"})
    except Exception as e:
        return jsonify({"success": False, "error": str(e)})


@app.route('/api/set_interval', methods=['POST'])
def set_interval():
    global auto_interval_seconds
    data = request.json
    new_interval = data.get('interval_seconds')

    if not new_interval or new_interval < 60:
        return jsonify({"success": False, "error": "间隔不能小于 60 秒"})

    auto_interval_seconds = new_interval
    save_config()
    restart_auto_backup()

    return jsonify({"success": True, "message": f"自动备份间隔已设为 {new_interval // 60} 分钟"})


@app.route('/api/auto_status')
def auto_status():
    next_time = datetime.datetime.now() + datetime.timedelta(seconds=auto_interval_seconds)
    return jsonify({
        "enabled": True,
        "interval_seconds": auto_interval_seconds,
        "next_backup": next_time.strftime("%Y-%m-%d %H:%M:%S"),
        "last_backup": _last_backup_time
    })


def auto_backup_worker():
    while not _auto_stop_event.is_set():
        if _auto_stop_event.wait(timeout=auto_interval_seconds):
            break
        with app.app_context():
            result = create_backup()
            print(f"[{datetime.datetime.now()}] 自动备份: {result}")


def start_auto_backup_thread():
    global _auto_thread
    _auto_stop_event.clear()
    _auto_thread = threading.Thread(target=auto_backup_worker, daemon=True)
    _auto_thread.start()


def restart_auto_backup():
    _auto_stop_event.set()
    if _auto_thread and _auto_thread.is_alive():
        _auto_thread.join(timeout=5)
    start_auto_backup_thread()


load_config()
start_auto_backup_thread()

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)