-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathbackupController.js
More file actions
196 lines (167 loc) · 6.63 KB
/
backupController.js
File metadata and controls
196 lines (167 loc) · 6.63 KB
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
import path from 'path';
import fs from '../../utils/fsWrapper.js';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const projectRootDir = path.resolve(__dirname, '../../');
const BACKUP_PATHS = [
'.env',
'.plugins.js',
'config/env.json',
'config/map.txt',
'config/parses.conf',
'config/player.json',
'scripts/cron',
'plugins'
];
const BACKINFO_FILENAME = '.backinfo';
const getBackupRootDir = () => {
return path.join(path.dirname(projectRootDir), path.basename(projectRootDir) + '-backup');
};
const getBackinfoPath = (backupDir) => {
return path.join(backupDir, BACKINFO_FILENAME);
};
const loadBackinfo = (backupDir) => {
const infoPath = getBackinfoPath(backupDir);
if (!fs.existsSync(infoPath)) {
return null;
}
try {
const content = fs.readFileSync(infoPath, 'utf-8');
return JSON.parse(content);
} catch (e) {
return null;
}
};
const saveBackinfo = (backupDir, data) => {
const infoPath = getBackinfoPath(backupDir);
fs.writeFileSync(infoPath, JSON.stringify(data, null, 2), 'utf-8');
};
const getEffectiveBackupPaths = (backupDir) => {
const info = loadBackinfo(backupDir);
if (info && Array.isArray(info.paths) && info.paths.length > 0) {
return { paths: info.paths, info };
}
return { paths: BACKUP_PATHS, info };
};
export const getBackupConfig = async (request, reply) => {
const backupDir = getBackupRootDir();
let paths;
let lastBackupAt = null;
let lastRestoreAt = null;
if (!fs.existsSync(backupDir)) {
paths = BACKUP_PATHS;
} else {
const result = getEffectiveBackupPaths(backupDir);
paths = result.paths;
if (result.info) {
lastBackupAt = result.info.lastBackupAt || null;
lastRestoreAt = result.info.lastRestoreAt || null;
}
}
return reply.send({ success: true, paths, lastBackupAt, lastRestoreAt });
};
export const updateBackupConfig = async (request, reply) => {
try {
const { paths } = request.body;
if (!Array.isArray(paths)) {
return reply.code(400).send({ success: false, message: 'paths must be an array' });
}
const backupDir = getBackupRootDir();
await fs.ensureDir(backupDir);
const info = loadBackinfo(backupDir) || {};
const backinfoData = {
...info,
paths
};
saveBackinfo(backupDir, backinfoData);
return reply.send({ success: true, message: 'Backup configuration updated successfully', paths });
} catch (error) {
request.log.error(`Update backup config failed: ${error.message}`);
return reply.code(500).send({ success: false, message: 'Update backup config failed: ' + error.message });
}
};
export const resetBackupConfig = async (request, reply) => {
try {
const backupDir = getBackupRootDir();
await fs.ensureDir(backupDir);
const info = loadBackinfo(backupDir) || {};
const backinfoData = {
...info,
paths: BACKUP_PATHS
};
saveBackinfo(backupDir, backinfoData);
return reply.send({ success: true, message: 'Backup configuration reset to defaults', paths: BACKUP_PATHS });
} catch (error) {
request.log.error(`Reset backup config failed: ${error.message}`);
return reply.code(500).send({ success: false, message: 'Reset backup config failed: ' + error.message });
}
};
export const createBackup = async (request, reply) => {
if (process.env.VERCEL) {
return reply.code(403).send({ success: false, message: 'Vercel environment does not support backup' });
}
try {
const backupDir = getBackupRootDir();
await fs.ensureDir(backupDir);
const { paths, info } = getEffectiveBackupPaths(backupDir);
const details = [];
for (const item of paths) {
const srcPath = path.join(projectRootDir, item);
const destPath = path.join(backupDir, item);
if (fs.existsSync(srcPath)) {
await fs.copy(srcPath, destPath, { overwrite: true });
details.push(`Backed up: ${item}`);
} else {
details.push(`Skipped (not found): ${item}`);
}
}
const now = new Date().toISOString();
const customPaths = info && Array.isArray(info.paths) && info.paths.length > 0 ? info.paths : [];
const backinfoData = {
paths: customPaths,
lastBackupAt: now,
lastRestoreAt: info && info.lastRestoreAt ? info.lastRestoreAt : null
};
saveBackinfo(backupDir, backinfoData);
return reply.send({ success: true, message: 'Backup completed successfully', backupDir, details });
} catch (error) {
request.log.error(`Backup failed: ${error.message}`);
return reply.code(500).send({ success: false, message: 'Backup failed: ' + error.message });
}
};
export const restoreBackup = async (request, reply) => {
if (process.env.VERCEL) {
return reply.code(403).send({ success: false, message: 'Vercel environment does not support restore' });
}
try {
const backupDir = getBackupRootDir();
if (!fs.existsSync(backupDir)) {
return reply.code(404).send({ success: false, message: 'Backup directory not found' });
}
const { paths, info } = getEffectiveBackupPaths(backupDir);
const details = [];
for (const item of paths) {
const srcPath = path.join(backupDir, item);
const destPath = path.join(projectRootDir, item);
if (fs.existsSync(srcPath)) {
await fs.copy(srcPath, destPath, { overwrite: true });
details.push(`Restored: ${item}`);
} else {
details.push(`Skipped (not found in backup): ${item}`);
}
}
const now = new Date().toISOString();
const customPaths = info && Array.isArray(info.paths) && info.paths.length > 0 ? info.paths : [];
const backinfoData = {
paths: customPaths,
lastBackupAt: info && info.lastBackupAt ? info.lastBackupAt : null,
lastRestoreAt: now
};
saveBackinfo(backupDir, backinfoData);
return reply.send({ success: true, message: 'Restore completed successfully', backupDir, details });
} catch (error) {
request.log.error(`Restore failed: ${error.message}`);
return reply.code(500).send({ success: false, message: 'Restore failed: ' + error.message });
}
};