|
|
import path from "node:path"; |
|
|
import { getAllServers } from "@dokploy/server/services/server"; |
|
|
import { scheduleJob } from "node-schedule"; |
|
|
import { db } from "../../db/index"; |
|
|
import { |
|
|
cleanUpDockerBuilder, |
|
|
cleanUpSystemPrune, |
|
|
cleanUpUnusedImages, |
|
|
} from "../docker/utils"; |
|
|
import { sendDockerCleanupNotifications } from "../notifications/docker-cleanup"; |
|
|
import { execAsync, execAsyncRemote } from "../process/execAsync"; |
|
|
import { getS3Credentials, scheduleBackup } from "./utils"; |
|
|
|
|
|
import { member } from "@dokploy/server/db/schema"; |
|
|
import type { BackupSchedule } from "@dokploy/server/services/backup"; |
|
|
import { eq } from "drizzle-orm"; |
|
|
import { startLogCleanup } from "../access-log/handler"; |
|
|
|
|
|
export const initCronJobs = async () => { |
|
|
console.log("Setting up cron jobs...."); |
|
|
|
|
|
const admin = await db.query.member.findFirst({ |
|
|
where: eq(member.role, "owner"), |
|
|
with: { |
|
|
user: true, |
|
|
}, |
|
|
}); |
|
|
|
|
|
if (!admin) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if (admin.user.enableDockerCleanup) { |
|
|
scheduleJob("docker-cleanup", "0 0 * * *", async () => { |
|
|
console.log( |
|
|
`Docker Cleanup ${new Date().toLocaleString()}] Running docker cleanup`, |
|
|
); |
|
|
await cleanUpUnusedImages(); |
|
|
await cleanUpDockerBuilder(); |
|
|
await cleanUpSystemPrune(); |
|
|
await sendDockerCleanupNotifications(admin.user.id); |
|
|
}); |
|
|
} |
|
|
|
|
|
const servers = await getAllServers(); |
|
|
|
|
|
for (const server of servers) { |
|
|
const { serverId, enableDockerCleanup, name } = server; |
|
|
if (enableDockerCleanup) { |
|
|
scheduleJob(serverId, "0 0 * * *", async () => { |
|
|
console.log( |
|
|
`SERVER-BACKUP[${new Date().toLocaleString()}] Running Cleanup ${name}`, |
|
|
); |
|
|
await cleanUpUnusedImages(serverId); |
|
|
await cleanUpDockerBuilder(serverId); |
|
|
await cleanUpSystemPrune(serverId); |
|
|
await sendDockerCleanupNotifications( |
|
|
admin.user.id, |
|
|
`Docker cleanup for Server ${name} (${serverId})`, |
|
|
); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
const backups = await db.query.backups.findMany({ |
|
|
with: { |
|
|
destination: true, |
|
|
postgres: true, |
|
|
mariadb: true, |
|
|
mysql: true, |
|
|
mongo: true, |
|
|
user: true, |
|
|
compose: true, |
|
|
}, |
|
|
}); |
|
|
|
|
|
for (const backup of backups) { |
|
|
try { |
|
|
if (backup.enabled) { |
|
|
scheduleBackup(backup); |
|
|
console.log( |
|
|
`[Backup] ${backup.databaseType} Enabled with cron: [${backup.schedule}]`, |
|
|
); |
|
|
} |
|
|
} catch (error) { |
|
|
console.error(`[Backup] ${backup.databaseType} Error`, error); |
|
|
} |
|
|
} |
|
|
|
|
|
if (admin?.user.logCleanupCron) { |
|
|
await startLogCleanup(admin.user.logCleanupCron); |
|
|
} |
|
|
}; |
|
|
|
|
|
export const keepLatestNBackups = async ( |
|
|
backup: BackupSchedule, |
|
|
serverId?: string | null, |
|
|
) => { |
|
|
|
|
|
|
|
|
if (!backup.keepLatestCount) return; |
|
|
|
|
|
try { |
|
|
const rcloneFlags = getS3Credentials(backup.destination); |
|
|
const backupFilesPath = path.join( |
|
|
`:s3:${backup.destination.bucket}`, |
|
|
backup.prefix, |
|
|
); |
|
|
|
|
|
|
|
|
const rcloneList = `rclone lsf ${rcloneFlags.join(" ")} --include "*${backup.databaseType === "web-server" ? ".zip" : ".sql.gz"}" ${backupFilesPath}`; |
|
|
|
|
|
const sortAndPickUnwantedBackups = `sort -r | tail -n +$((${backup.keepLatestCount}+1)) | xargs -I{}`; |
|
|
|
|
|
|
|
|
const rcloneDelete = `rclone delete ${rcloneFlags.join(" ")} ${backupFilesPath}/{}`; |
|
|
|
|
|
const rcloneCommand = `${rcloneList} | ${sortAndPickUnwantedBackups} ${rcloneDelete}`; |
|
|
|
|
|
if (serverId) { |
|
|
await execAsyncRemote(serverId, rcloneCommand); |
|
|
} else { |
|
|
await execAsync(rcloneCommand); |
|
|
} |
|
|
} catch (error) { |
|
|
console.error(error); |
|
|
} |
|
|
}; |
|
|
|