2026-03-16 17:07:20 +08:00
|
|
|
const BackupLog = require('../models/BackupLog');
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
|
|
async function createLogEntry(options) {
|
|
|
|
|
const { logType, description, backupType, includeFiles, compressed } = options;
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-03-16 17:07:20 +08:00
|
|
|
try {
|
|
|
|
|
const log = await BackupLog.create({
|
|
|
|
|
logType: logType || 'manual',
|
|
|
|
|
status: 'pending',
|
|
|
|
|
description: description || '',
|
|
|
|
|
backupType: backupType || 'full',
|
|
|
|
|
includeFiles: includeFiles || false,
|
|
|
|
|
compressed: compressed || false,
|
2026-03-27 19:12:16 +08:00
|
|
|
startTime: new Date(),
|
2026-03-16 17:07:20 +08:00
|
|
|
});
|
|
|
|
|
return log;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('创建备份日志失败:', error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function updateLogStatus(logId, status, options = {}) {
|
|
|
|
|
try {
|
|
|
|
|
const updateData = { status };
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-03-16 17:07:20 +08:00
|
|
|
if (status === 'running') {
|
|
|
|
|
updateData.startTime = new Date();
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-03-16 17:07:20 +08:00
|
|
|
if (status === 'success' || status === 'failed') {
|
|
|
|
|
updateData.endTime = new Date();
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-03-16 17:07:20 +08:00
|
|
|
const log = await BackupLog.findByPk(logId);
|
|
|
|
|
if (log && log.startTime) {
|
|
|
|
|
updateData.duration = new Date() - new Date(log.startTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-27 19:12:16 +08:00
|
|
|
|
|
|
|
|
if (options.filename) {
|
|
|
|
|
updateData.filename = options.filename;
|
|
|
|
|
}
|
|
|
|
|
if (options.filePath) {
|
|
|
|
|
updateData.filePath = options.filePath;
|
|
|
|
|
}
|
|
|
|
|
if (options.fileSize) {
|
|
|
|
|
updateData.fileSize = options.fileSize;
|
|
|
|
|
}
|
|
|
|
|
if (options.errorMessage) {
|
|
|
|
|
updateData.errorMessage = options.errorMessage;
|
|
|
|
|
}
|
|
|
|
|
if (options.remoteUploads) {
|
|
|
|
|
updateData.remoteUploads = options.remoteUploads;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 17:07:20 +08:00
|
|
|
await BackupLog.update(updateData, {
|
2026-03-27 19:12:16 +08:00
|
|
|
where: { id: logId },
|
2026-03-16 17:07:20 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-03-16 17:07:20 +08:00
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('更新备份日志失败:', error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getBackupLogs(options = {}) {
|
|
|
|
|
try {
|
|
|
|
|
const { page = 1, pageSize = 20, logType, status } = options;
|
|
|
|
|
const where = {};
|
2026-03-27 19:12:16 +08:00
|
|
|
|
|
|
|
|
if (logType) {
|
|
|
|
|
where.logType = logType;
|
|
|
|
|
}
|
|
|
|
|
if (status) {
|
|
|
|
|
where.status = status;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 17:07:20 +08:00
|
|
|
const offset = (page - 1) * pageSize;
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-03-16 17:07:20 +08:00
|
|
|
const { count, rows } = await BackupLog.findAndCountAll({
|
|
|
|
|
where,
|
|
|
|
|
order: [['createdAt', 'DESC']],
|
|
|
|
|
limit: pageSize,
|
2026-03-27 19:12:16 +08:00
|
|
|
offset,
|
2026-03-16 17:07:20 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-03-16 17:07:20 +08:00
|
|
|
return {
|
|
|
|
|
logs: rows,
|
|
|
|
|
total: count,
|
|
|
|
|
page,
|
|
|
|
|
pageSize,
|
2026-03-27 19:12:16 +08:00
|
|
|
totalPages: Math.ceil(count / pageSize),
|
2026-03-16 17:07:20 +08:00
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取备份日志失败:', error);
|
|
|
|
|
return { logs: [], total: 0, page: 1, pageSize: 20, totalPages: 0 };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getBackupLogById(id) {
|
|
|
|
|
try {
|
|
|
|
|
return await BackupLog.findByPk(id);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取备份日志详情失败:', error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function deleteOldLogs(days = 30) {
|
|
|
|
|
try {
|
|
|
|
|
const cutoffDate = new Date();
|
|
|
|
|
cutoffDate.setDate(cutoffDate.getDate() - days);
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-03-16 17:07:20 +08:00
|
|
|
const deletedCount = await BackupLog.destroy({
|
|
|
|
|
where: {
|
|
|
|
|
createdAt: {
|
2026-03-27 19:12:16 +08:00
|
|
|
[require('sequelize').Op.lt]: cutoffDate,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-03-16 17:07:20 +08:00
|
|
|
});
|
2026-03-27 19:12:16 +08:00
|
|
|
|
2026-03-16 17:07:20 +08:00
|
|
|
console.log(`删除了 ${deletedCount} 条旧备份日志`);
|
|
|
|
|
return deletedCount;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('删除旧备份日志失败:', error);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
createLogEntry,
|
|
|
|
|
updateLogStatus,
|
|
|
|
|
getBackupLogs,
|
|
|
|
|
getBackupLogById,
|
2026-03-27 19:12:16 +08:00
|
|
|
deleteOldLogs,
|
2026-03-16 17:07:20 +08:00
|
|
|
};
|