-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
383 lines (325 loc) · 14.3 KB
/
server.js
File metadata and controls
383 lines (325 loc) · 14.3 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
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
// server.js
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const zlib = require('zlib');
const axios = require('axios');
const FormData = require('form-data');
// ---------------------------------------------------------
// WATERMARK & CREDITS: Basil Saji Mathew (BSM)
// Description: Advanced Automated FiveM Backup System
// ---------------------------------------------------------
const BSM_WATERMARK = "Created & Optimized by Basil Saji Mathew (BSM)";
// Try loading configuration
let config;
try {
const configPath = path.join(GetResourcePath(GetCurrentResourceName()), 'config.json');
config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
} catch (e) {
if (typeof GetResourcePath === 'undefined') {
// Fallback for standalone NodeJS usage
config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json'), 'utf8'));
} else {
console.error('^1[BSM AutoBackup] Error loading config.json! Please ensure it exists.^0');
process.exit(1);
}
}
// Prefix for console logs
const logPrefix = config.system && config.system.log_prefix ? config.system.log_prefix : "^6[BSM AutoBackup]^0";
const bsmLog = (msg) => console.log(`${logPrefix} ${msg}`);
const bsmWarn = (msg) => console.warn(`${logPrefix} ^3WARNING: ${msg}^0`);
const bsmError = (msg) => console.error(`${logPrefix} ^1ERROR: ${msg}^0`);
// Ensure the local backup folder exists
const resourceRoot = typeof GetResourcePath !== 'undefined' ? GetResourcePath(GetCurrentResourceName()) : __dirname;
const backupFolder = path.resolve(resourceRoot, config.backup.local_backup_folder || 'backups');
if (!fs.existsSync(backupFolder)) {
fs.mkdirSync(backupFolder, { recursive: true });
}
function getTimestamp() {
const d = new Date();
const pad = (n) => String(n).padStart(2, '0');
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}_${pad(d.getHours())}-${pad(d.getMinutes())}-${pad(d.getSeconds())}`;
}
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
async function sendToDiscord(dbName, filePath, fileName, success, extraInfo = "") {
bsmLog(`Sending webhook for ${success ? 'Successful' : 'Failed'} backup of ${dbName}...`);
// Check if Discord webhook is configured
if (!config.discord.webhook_url || !config.discord.webhook_url.startsWith('https://discord.com/api/webhooks/')) {
bsmWarn('Valid Discord Webhook URL not configured. Skipping upload.');
return false;
}
const form = new FormData();
form.append('username', config.discord.bot_name || 'BSM AutoBackup');
if (config.discord.avatar_url) {
form.append('avatar_url', config.discord.avatar_url);
}
let content = "";
if (!success && config.discord.ping_role_on_error) {
content = config.discord.ping_role_on_error === "@everyone" || config.discord.ping_role_on_error === "@here"
? config.discord.ping_role_on_error
: `<@&${config.discord.ping_role_on_error}>`;
}
let embedColorSuccess = 5814783;
let embedColorError = 16711680;
if (config.discord.embed_color_success) {
embedColorSuccess = parseInt(config.discord.embed_color_success.toString().replace("#", ""), 16) || 5814783;
}
if (config.discord.embed_color_error) {
embedColorError = parseInt(config.discord.embed_color_error.toString().replace("#", ""), 16) || 16711680;
}
let payloadJson;
if (success) {
let fileSizeStr = "Unknown";
if (filePath && fs.existsSync(filePath)) {
const stats = fs.statSync(filePath);
fileSizeStr = formatBytes(stats.size);
}
payloadJson = {
content: content,
embeds: [{
title: "✅ Automated Database Backup Successful",
description: `A database backup has been automatically successfully generated.\n\n**Database:** \`${dbName}\`\n**File Size:** \`${fileSizeStr}\`\n**Compression:** \`${config.backup.compress ? 'Enabled (GZ)' : 'Disabled'}\`\n**Time:** <t:${Math.floor(Date.now() / 1000)}:F>\n${extraInfo}`,
color: embedColorSuccess,
footer: {
text: BSM_WATERMARK
}
}]
};
form.append('payload_json', JSON.stringify(payloadJson));
if (filePath && fileName) {
form.append('file', fs.createReadStream(filePath), { filename: fileName });
}
} else {
payloadJson = {
content: content,
embeds: [{
title: "❌ Automated Database Backup Failed",
description: `An error occurred while attempting to backup the database.\n\n**Database:** \`${dbName}\`\n**Error Details:**\n\`\`\`${extraInfo}\`\`\`\n**Time:** <t:${Math.floor(Date.now() / 1000)}:F>`,
color: embedColorError,
footer: {
text: BSM_WATERMARK
}
}]
};
form.append('payload_json', JSON.stringify(payloadJson));
}
try {
await axios.post(config.discord.webhook_url, form, {
headers: form.getHeaders(),
maxBodyLength: Infinity,
maxContentLength: Infinity
});
bsmLog(`^2Webhook sent successfully!^0`);
return true;
} catch (err) {
bsmError(`Failed to send webhook: ${err.message}`);
if (err.response && err.response.data) {
console.error(err.response.data);
}
return false;
}
}
function compressFile(inputPath, outputPath) {
return new Promise((resolve, reject) => {
const source = fs.createReadStream(inputPath);
const destination = fs.createWriteStream(outputPath);
const gzip = zlib.createGzip({ level: config.backup.compression_level || 9 });
source.pipe(gzip).pipe(destination)
.on('finish', resolve)
.on('error', reject);
});
}
function cleanOldBackups() {
bsmLog('Checking for old backups to clean up...');
let retentionDays = config.backup.retention_days;
if (typeof retentionDays !== 'number' || retentionDays <= 0) return;
let now = Date.now();
let msInDay = 24 * 60 * 60 * 1000;
fs.readdir(backupFolder, (err, files) => {
if (err) {
bsmError(`Error reading backup directory for cleanup: ${err.message}`);
return;
}
let deletedCount = 0;
files.forEach(file => {
let filePath = path.join(backupFolder, file);
fs.stat(filePath, (err, stats) => {
if (err) return;
let ageMs = now - stats.mtimeMs;
let ageDays = ageMs / msInDay;
if (ageDays > retentionDays) {
try {
fs.unlinkSync(filePath);
deletedCount++;
if(config.system && config.system.debug_mode) {
bsmLog(`Deleted old backup: ${file} (Age: ${ageDays.toFixed(1)} days)`);
}
} catch (e) {
bsmWarn(`Could not delete old backup ${file}: ${e.message}`);
}
}
});
});
if (deletedCount > 0) {
bsmLog(`Cleaned up ${deletedCount} old backup files (exceeded ${retentionDays} days retention).`);
}
});
}
async function doBackupForDatabase(dbName) {
bsmLog(`Starting backup sequence for database: ^3${dbName}^0`);
const timestamp = getTimestamp();
const dbConfig = config.database;
const sqlFileName = `${dbName}_${timestamp}.sql`;
const sqlFilePath = path.join(backupFolder, sqlFileName);
let mysqldumpCmd = `"${config.backup.mysqldump_path}" -h ${dbConfig.host} `;
if(dbConfig.port) {
mysqldumpCmd += `-P ${dbConfig.port} `;
}
mysqldumpCmd += `-u ${dbConfig.user} `;
if (dbConfig.password && dbConfig.password.length > 0) {
if (process.platform === 'win32') {
mysqldumpCmd += `-p"${dbConfig.password}" `;
} else {
mysqldumpCmd += `-p'${dbConfig.password}' `;
}
}
// Additional mysqldump arguments
if (config.backup.mysqldump_args && config.backup.mysqldump_args.length > 0) {
mysqldumpCmd += `${config.backup.mysqldump_args.join(' ')} `;
}
mysqldumpCmd += `${dbName} > "${sqlFilePath}"`;
return new Promise((resolve) => {
const startTime = Date.now();
exec(mysqldumpCmd, { maxBuffer: 1024 * 1024 * 50 }, async (error, stdout, stderr) => {
if (error) {
bsmError(`mysqldump failed for ${dbName}: ${error.message}`);
await sendToDiscord(dbName, null, null, false, error.message);
resolve(false);
return;
}
if (stderr && !stderr.toLowerCase().includes('warning')) {
bsmWarn(`mysqldump stderr for ${dbName}: ${stderr}`);
}
bsmLog(`^2Database '${dbName}' dumped successfully to: ${sqlFileName}^0`);
let fileToSend = sqlFilePath;
let finalFileName = sqlFileName;
if (config.backup.compress) {
bsmLog(`Compressing SQL file for ${dbName}...`);
const gzFileName = `${sqlFileName}.gz`;
const gzFilePath = path.join(backupFolder, gzFileName);
try {
await compressFile(sqlFilePath, gzFilePath);
bsmLog(`^2Compression successful: ${gzFileName}^0`);
// Remove raw sql
try { fs.unlinkSync(sqlFilePath); } catch(e) {}
fileToSend = gzFilePath;
finalFileName = gzFileName;
} catch (err) {
bsmError(`Compression error for ${dbName}: ${err.message}`);
bsmLog('^3Proceeding with uncompressed SQL file.^0');
}
}
const executionTime = ((Date.now() - startTime) / 1000).toFixed(2);
const extraInfo = `**Execution Time:** \`${executionTime}s\``;
const success = await sendToDiscord(dbName, fileToSend, finalFileName, true, extraInfo);
if (success && !config.backup.keep_local_backups) {
try {
fs.unlinkSync(fileToSend);
bsmLog(`^2Cleaned up local backup file for ${dbName}.^0`);
} catch(e) {
bsmError(`Failed to delete local file for ${dbName}: ${e.message}`);
}
}
resolve(true);
});
});
}
async function runAllBackups() {
bsmLog('======================================');
bsmLog(' BSM AutoBackup Sequence Initiated ');
bsmLog('======================================');
let databases = [];
if (Array.isArray(config.database.databases)) {
databases = config.database.databases;
} else if (config.database.database_name) {
databases = [config.database.database_name]; // Fallback for old configs
}
if (databases.length === 0) {
bsmError('No databases configured for backup.');
return;
}
// Process sequentially to be safe with CPU/Mem and discord rate limits
for (let i = 0; i < databases.length; i++) {
await doBackupForDatabase(databases[i]);
}
// Run cleanup for old files if we keep them locally
if (config.backup.keep_local_backups && config.backup.retention_days > 0) {
cleanOldBackups();
}
bsmLog('======================================');
bsmLog(' BSM AutoBackup Sequence Finished ');
bsmLog('======================================');
}
// Start sequence
bsmLog(`======================================`);
bsmLog(` BSM AutoBackup V2 `);
bsmLog(` Created by: Basil Saji Mathew `);
bsmLog(`======================================`);
bsmLog(`Interval set to ${config.backup.interval_hours} hours.`);
if (config.discord.webhook_url && config.discord.webhook_url.startsWith('https://discord.com/api/webhooks/')) {
bsmLog('^2Discord Webhook properly configured.^0');
} else {
bsmWarn('Discord Webhook IS NOT configured properly in config.json.');
}
// Universal Framework Permission System (ESX, QBCore, Qbox, Standalone)
async function hasAdminPermission(source) {
if (source === 0) return true; // Console always allowed
// 1. Native ACE Permissions (Standalone / Best Practice / Qbox)
if (IsPlayerAceAllowed(source, 'command.runbackup') || IsPlayerAceAllowed(source, 'admin')) {
return true;
}
// 2. QBCore & Qbox native core Permission Check
try {
if (GetResourceState('qb-core') === 'started') {
const QBCore = exports['qb-core'].GetCoreObject();
if (QBCore && QBCore.Functions && QBCore.Functions.HasPermission(source, 'admin')) {
return true;
}
}
} catch (e) {}
// 3. ESX Permission Check
try {
if (GetResourceState('es_extended') === 'started') {
const ESX = exports['es_extended'].getSharedObject();
if (ESX) {
const xPlayer = ESX.GetPlayerFromId(source);
if (xPlayer && (xPlayer.getGroup() === 'admin' || xPlayer.getGroup() === 'superadmin')) {
return true;
}
}
}
} catch (e) {}
return false;
}
// Only register server side command if running under FiveM
if (typeof RegisterCommand !== 'undefined') {
RegisterCommand('runbackup', async (source, args, raw) => {
const isAdmin = await hasAdminPermission(source);
if (isAdmin) {
if (source !== 0) bsmLog(`Backup manually triggered by user ID ${source}.`);
await runAllBackups();
} else {
bsmError(`Access Denied [ID: ${source}]. You don't have Admin permissions.`);
}
}, true);
}
// Set up the interval
const intervalMs = config.backup.interval_hours * 60 * 60 * 1000;
setInterval(runAllBackups, intervalMs);