feat: add configurable backup time for local and USB backups

Replace hardcoded 8 PM / 9 PM backup schedules with user-selectable
hour dropdowns. Adds autoBackupHour and usbBackupHour fields to User
model. Cron jobs now check every hour against the configured time.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
ff
2026-06-24 23:37:34 -04:00
parent 5e881c9ff7
commit 70b5e2ba47
5 changed files with 74 additions and 76 deletions

View File

@@ -55,26 +55,19 @@ async function getAdminUser() {
export const startBackupCron = () => {
// ============================================================
// 8 PM — Local automatic backup to apps/Backend/backups/
// Every hour — Local automatic backup (runs when hour matches setting)
// ============================================================
cron.schedule("0 20 * * *", async () => {
console.log("🔄 [8 PM] Running local auto-backup...");
ensureLocalBackupDir();
cron.schedule("0 * * * *", async () => {
const admin = await getAdminUser();
if (!admin) {
console.warn("No admin user found, skipping local backup.");
return;
}
if (!admin) return;
if (!admin.autoBackupEnabled) return;
if (!admin.autoBackupEnabled) {
console.log("✅ [8 PM] Auto-backup is disabled for admin, skipped.");
const startedAt = new Date();
const log = await cronJobLogStorage.createJobLog("local-backup", startedAt);
await cronJobLogStorage.completeJobLog(log.id, "skipped", new Date());
await storage.deleteNotificationsByType(admin.id, "BACKUP");
return;
}
const currentHour = new Date().getHours();
const backupHour = admin.autoBackupHour ?? 20;
if (currentHour !== backupHour) return;
console.log(`🔄 [${backupHour}:00] Running local auto-backup...`);
ensureLocalBackupDir();
const startedAt = new Date();
const log = await cronJobLogStorage.createJobLog("local-backup", startedAt);
@@ -97,30 +90,21 @@ export const startBackupCron = () => {
"❌ Automatic backup failed. Please check the server backup folder."
);
}
console.log("✅ [8 PM] Local backup complete.");
});
// ============================================================
// 9 PM — USB backup to the "USB Backup" folder on the drive
// Every hour — USB backup (runs when hour matches setting)
// ============================================================
cron.schedule("0 21 * * *", async () => {
console.log("🔄 [9 PM] Running USB backup...");
cron.schedule("0 * * * *", async () => {
const admin = await getAdminUser();
if (!admin) {
console.warn("No admin user found, skipping USB backup.");
return;
}
if (!admin) return;
if (!admin.usbBackupEnabled) return;
if (!admin.usbBackupEnabled) {
console.log("✅ [9 PM] USB backup is disabled for admin, skipped.");
const startedAt = new Date();
const log = await cronJobLogStorage.createJobLog("usb-backup", startedAt);
await cronJobLogStorage.completeJobLog(log.id, "skipped", new Date());
await storage.deleteNotificationsByType(admin.id, "BACKUP");
return;
}
const currentHour = new Date().getHours();
const backupHour = admin.usbBackupHour ?? 21;
if (currentHour !== backupHour) return;
console.log(`🔄 [${backupHour}:00] Running USB backup...`);
const startedAt = new Date();
const log = await cronJobLogStorage.createJobLog("usb-backup", startedAt);
@@ -172,8 +156,6 @@ export const startBackupCron = () => {
"❌ USB backup failed. Please check the USB drive and try again."
);
}
console.log("✅ [9 PM] USB backup complete.");
});
// ============================================================