Files
yunrui_asset/backend/utils/backupLog.js
T

139 lines
3.2 KiB
JavaScript
Raw Normal View History

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-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,
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-16 17:07:20 +08:00
if (status === 'running') {
updateData.startTime = new Date();
}
2026-03-16 17:07:20 +08:00
if (status === 'success' || status === 'failed') {
updateData.endTime = new Date();
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);
}
}
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, {
where: { id: logId },
2026-03-16 17:07:20 +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 = {};
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-16 17:07:20 +08:00
const { count, rows } = await BackupLog.findAndCountAll({
where,
order: [['createdAt', 'DESC']],
limit: pageSize,
offset,
2026-03-16 17:07:20 +08:00
});
2026-03-16 17:07:20 +08:00
return {
logs: rows,
total: count,
page,
pageSize,
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-16 17:07:20 +08:00
const deletedCount = await BackupLog.destroy({
where: {
createdAt: {
[require('sequelize').Op.lt]: cutoffDate,
},
},
2026-03-16 17:07:20 +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,
deleteOldLogs,
2026-03-16 17:07:20 +08:00
};