mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
add mysql, mariadb, postgresql, sqlite volumes support
This commit is contained in:
@@ -5,6 +5,10 @@ import { makeDirectoryBackend } from "./directory/directory-backend";
|
||||
import { makeNfsBackend } from "./nfs/nfs-backend";
|
||||
import { makeSmbBackend } from "./smb/smb-backend";
|
||||
import { makeWebdavBackend } from "./webdav/webdav-backend";
|
||||
import { makeMariaDBBackend } from "./mariadb/mariadb-backend";
|
||||
import { makeMySQLBackend } from "./mysql/mysql-backend";
|
||||
import { makePostgresBackend } from "./postgres/postgres-backend";
|
||||
import { makeSQLiteBackend } from "./sqlite/sqlite-backend";
|
||||
|
||||
type OperationResult = {
|
||||
error?: string;
|
||||
@@ -33,5 +37,20 @@ export const createVolumeBackend = (volume: Volume): VolumeBackend => {
|
||||
case "webdav": {
|
||||
return makeWebdavBackend(volume.config, path);
|
||||
}
|
||||
case "mariadb": {
|
||||
return makeMariaDBBackend(volume.config, path);
|
||||
}
|
||||
case "mysql": {
|
||||
return makeMySQLBackend(volume.config, path);
|
||||
}
|
||||
case "postgres": {
|
||||
return makePostgresBackend(volume.config, path);
|
||||
}
|
||||
case "sqlite": {
|
||||
return makeSQLiteBackend(volume.config, path);
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Unsupported backend type: ${(volume.config as any).backend}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
57
app/server/modules/backends/mariadb/mariadb-backend.ts
Normal file
57
app/server/modules/backends/mariadb/mariadb-backend.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import * as fs from "node:fs/promises";
|
||||
import { toMessage } from "../../../utils/errors";
|
||||
import { logger } from "../../../utils/logger";
|
||||
import { testMariaDBConnection } from "../../../utils/database-dump";
|
||||
import type { VolumeBackend } from "../backend";
|
||||
import { BACKEND_STATUS, type BackendConfig } from "~/schemas/volumes";
|
||||
|
||||
const mount = async (config: BackendConfig, volumePath: string) => {
|
||||
if (config.backend !== "mariadb") {
|
||||
return { status: BACKEND_STATUS.error, error: "Invalid backend type" };
|
||||
}
|
||||
|
||||
logger.info(`Testing MariaDB connection to: ${config.host}:${config.port}`);
|
||||
|
||||
try {
|
||||
await testMariaDBConnection(config);
|
||||
await fs.mkdir(volumePath, { recursive: true });
|
||||
|
||||
logger.info("MariaDB connection successful");
|
||||
return { status: BACKEND_STATUS.mounted };
|
||||
} catch (error) {
|
||||
logger.error("Failed to connect to MariaDB:", error);
|
||||
return { status: BACKEND_STATUS.error, error: toMessage(error) };
|
||||
}
|
||||
};
|
||||
|
||||
const unmount = async (volumePath: string) => {
|
||||
logger.info("Cleaning up MariaDB dump directory");
|
||||
|
||||
try {
|
||||
await fs.rm(volumePath, { recursive: true, force: true });
|
||||
return { status: BACKEND_STATUS.unmounted };
|
||||
} catch (error) {
|
||||
logger.warn(`Failed to clean up MariaDB dump directory: ${toMessage(error)}`);
|
||||
return { status: BACKEND_STATUS.unmounted };
|
||||
}
|
||||
};
|
||||
|
||||
const checkHealth = async (config: BackendConfig) => {
|
||||
if (config.backend !== "mariadb") {
|
||||
return { status: BACKEND_STATUS.error, error: "Invalid backend type" };
|
||||
}
|
||||
|
||||
try {
|
||||
await testMariaDBConnection(config);
|
||||
return { status: BACKEND_STATUS.mounted };
|
||||
} catch (error) {
|
||||
logger.error("MariaDB health check failed:", error);
|
||||
return { status: BACKEND_STATUS.error, error: toMessage(error) };
|
||||
}
|
||||
};
|
||||
|
||||
export const makeMariaDBBackend = (config: BackendConfig, volumePath: string): VolumeBackend => ({
|
||||
mount: () => mount(config, volumePath),
|
||||
unmount: () => unmount(volumePath),
|
||||
checkHealth: () => checkHealth(config),
|
||||
});
|
||||
57
app/server/modules/backends/mysql/mysql-backend.ts
Normal file
57
app/server/modules/backends/mysql/mysql-backend.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import * as fs from "node:fs/promises";
|
||||
import { toMessage } from "../../../utils/errors";
|
||||
import { logger } from "../../../utils/logger";
|
||||
import { testMySQLConnection } from "../../../utils/database-dump";
|
||||
import type { VolumeBackend } from "../backend";
|
||||
import { BACKEND_STATUS, type BackendConfig } from "~/schemas/volumes";
|
||||
|
||||
const mount = async (config: BackendConfig, volumePath: string) => {
|
||||
if (config.backend !== "mysql") {
|
||||
return { status: BACKEND_STATUS.error, error: "Invalid backend type" };
|
||||
}
|
||||
|
||||
logger.info(`Testing MySQL connection to: ${config.host}:${config.port}`);
|
||||
|
||||
try {
|
||||
await testMySQLConnection(config);
|
||||
await fs.mkdir(volumePath, { recursive: true });
|
||||
|
||||
logger.info("MySQL connection successful");
|
||||
return { status: BACKEND_STATUS.mounted };
|
||||
} catch (error) {
|
||||
logger.error("Failed to connect to MySQL:", error);
|
||||
return { status: BACKEND_STATUS.error, error: toMessage(error) };
|
||||
}
|
||||
};
|
||||
|
||||
const unmount = async (volumePath: string) => {
|
||||
logger.info("Cleaning up MySQL dump directory");
|
||||
|
||||
try {
|
||||
await fs.rm(volumePath, { recursive: true, force: true });
|
||||
return { status: BACKEND_STATUS.unmounted };
|
||||
} catch (error) {
|
||||
logger.warn(`Failed to clean up MySQL dump directory: ${toMessage(error)}`);
|
||||
return { status: BACKEND_STATUS.unmounted };
|
||||
}
|
||||
};
|
||||
|
||||
const checkHealth = async (config: BackendConfig) => {
|
||||
if (config.backend !== "mysql") {
|
||||
return { status: BACKEND_STATUS.error, error: "Invalid backend type" };
|
||||
}
|
||||
|
||||
try {
|
||||
await testMySQLConnection(config);
|
||||
return { status: BACKEND_STATUS.mounted };
|
||||
} catch (error) {
|
||||
logger.error("MySQL health check failed:", error);
|
||||
return { status: BACKEND_STATUS.error, error: toMessage(error) };
|
||||
}
|
||||
};
|
||||
|
||||
export const makeMySQLBackend = (config: BackendConfig, volumePath: string): VolumeBackend => ({
|
||||
mount: () => mount(config, volumePath),
|
||||
unmount: () => unmount(volumePath),
|
||||
checkHealth: () => checkHealth(config),
|
||||
});
|
||||
57
app/server/modules/backends/postgres/postgres-backend.ts
Normal file
57
app/server/modules/backends/postgres/postgres-backend.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import * as fs from "node:fs/promises";
|
||||
import { toMessage } from "../../../utils/errors";
|
||||
import { logger } from "../../../utils/logger";
|
||||
import { testPostgresConnection } from "../../../utils/database-dump";
|
||||
import type { VolumeBackend } from "../backend";
|
||||
import { BACKEND_STATUS, type BackendConfig } from "~/schemas/volumes";
|
||||
|
||||
const mount = async (config: BackendConfig, volumePath: string) => {
|
||||
if (config.backend !== "postgres") {
|
||||
return { status: BACKEND_STATUS.error, error: "Invalid backend type" };
|
||||
}
|
||||
|
||||
logger.info(`Testing PostgreSQL connection to: ${config.host}:${config.port}`);
|
||||
|
||||
try {
|
||||
await testPostgresConnection(config);
|
||||
await fs.mkdir(volumePath, { recursive: true });
|
||||
|
||||
logger.info("PostgreSQL connection successful");
|
||||
return { status: BACKEND_STATUS.mounted };
|
||||
} catch (error) {
|
||||
logger.error("Failed to connect to PostgreSQL:", error);
|
||||
return { status: BACKEND_STATUS.error, error: toMessage(error) };
|
||||
}
|
||||
};
|
||||
|
||||
const unmount = async (volumePath: string) => {
|
||||
logger.info("Cleaning up PostgreSQL dump directory");
|
||||
|
||||
try {
|
||||
await fs.rm(volumePath, { recursive: true, force: true });
|
||||
return { status: BACKEND_STATUS.unmounted };
|
||||
} catch (error) {
|
||||
logger.warn(`Failed to clean up PostgreSQL dump directory: ${toMessage(error)}`);
|
||||
return { status: BACKEND_STATUS.unmounted };
|
||||
}
|
||||
};
|
||||
|
||||
const checkHealth = async (config: BackendConfig) => {
|
||||
if (config.backend !== "postgres") {
|
||||
return { status: BACKEND_STATUS.error, error: "Invalid backend type" };
|
||||
}
|
||||
|
||||
try {
|
||||
await testPostgresConnection(config);
|
||||
return { status: BACKEND_STATUS.mounted };
|
||||
} catch (error) {
|
||||
logger.error("PostgreSQL health check failed:", error);
|
||||
return { status: BACKEND_STATUS.error, error: toMessage(error) };
|
||||
}
|
||||
};
|
||||
|
||||
export const makePostgresBackend = (config: BackendConfig, volumePath: string): VolumeBackend => ({
|
||||
mount: () => mount(config, volumePath),
|
||||
unmount: () => unmount(volumePath),
|
||||
checkHealth: () => checkHealth(config),
|
||||
});
|
||||
57
app/server/modules/backends/sqlite/sqlite-backend.ts
Normal file
57
app/server/modules/backends/sqlite/sqlite-backend.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import * as fs from "node:fs/promises";
|
||||
import { toMessage } from "../../../utils/errors";
|
||||
import { logger } from "../../../utils/logger";
|
||||
import { testSQLiteConnection } from "../../../utils/database-dump";
|
||||
import type { VolumeBackend } from "../backend";
|
||||
import { BACKEND_STATUS, type BackendConfig } from "~/schemas/volumes";
|
||||
|
||||
const mount = async (config: BackendConfig, volumePath: string) => {
|
||||
if (config.backend !== "sqlite") {
|
||||
return { status: BACKEND_STATUS.error, error: "Invalid backend type" };
|
||||
}
|
||||
|
||||
logger.info(`Testing SQLite connection to: ${config.path}`);
|
||||
|
||||
try {
|
||||
await testSQLiteConnection(config);
|
||||
await fs.mkdir(volumePath, { recursive: true });
|
||||
|
||||
logger.info("SQLite connection successful");
|
||||
return { status: BACKEND_STATUS.mounted };
|
||||
} catch (error) {
|
||||
logger.error("Failed to access SQLite database:", error);
|
||||
return { status: BACKEND_STATUS.error, error: toMessage(error) };
|
||||
}
|
||||
};
|
||||
|
||||
const unmount = async (volumePath: string) => {
|
||||
logger.info("Cleaning up SQLite dump directory");
|
||||
|
||||
try {
|
||||
await fs.rm(volumePath, { recursive: true, force: true });
|
||||
return { status: BACKEND_STATUS.unmounted };
|
||||
} catch (error) {
|
||||
logger.warn(`Failed to clean up SQLite dump directory: ${toMessage(error)}`);
|
||||
return { status: BACKEND_STATUS.unmounted };
|
||||
}
|
||||
};
|
||||
|
||||
const checkHealth = async (config: BackendConfig) => {
|
||||
if (config.backend !== "sqlite") {
|
||||
return { status: BACKEND_STATUS.error, error: "Invalid backend type" };
|
||||
}
|
||||
|
||||
try {
|
||||
await testSQLiteConnection(config);
|
||||
return { status: BACKEND_STATUS.mounted };
|
||||
} catch (error) {
|
||||
logger.error("SQLite health check failed:", error);
|
||||
return { status: BACKEND_STATUS.error, error: toMessage(error) };
|
||||
}
|
||||
};
|
||||
|
||||
export const makeSQLiteBackend = (config: BackendConfig, volumePath: string): VolumeBackend => ({
|
||||
mount: () => mount(config, volumePath),
|
||||
unmount: () => unmount(volumePath),
|
||||
checkHealth: () => checkHealth(config),
|
||||
});
|
||||
@@ -2,14 +2,16 @@ import { eq } from "drizzle-orm";
|
||||
import cron from "node-cron";
|
||||
import { CronExpressionParser } from "cron-parser";
|
||||
import { NotFoundError, BadRequestError, ConflictError } from "http-errors-enhanced";
|
||||
import * as fs from "node:fs/promises";
|
||||
import { db } from "../../db/db";
|
||||
import { backupSchedulesTable, repositoriesTable, volumesTable } from "../../db/schema";
|
||||
import { restic } from "../../utils/restic";
|
||||
import { logger } from "../../utils/logger";
|
||||
import { getVolumePath } from "../volumes/helpers";
|
||||
import { getVolumePath, isDatabaseVolume, getDumpFilePath } from "../volumes/helpers";
|
||||
import type { CreateBackupScheduleBody, UpdateBackupScheduleBody } from "./backups.dto";
|
||||
import { toMessage } from "../../utils/errors";
|
||||
import { serverEvents } from "../../core/events";
|
||||
import { executeDatabaseDump, type DatabaseConfig } from "../../utils/database-dump";
|
||||
|
||||
const runningBackups = new Map<number, AbortController>();
|
||||
|
||||
@@ -206,7 +208,28 @@ const executeBackup = async (scheduleId: number, manual = false) => {
|
||||
runningBackups.set(scheduleId, abortController);
|
||||
|
||||
try {
|
||||
const volumePath = getVolumePath(volume);
|
||||
let backupPath: string;
|
||||
let dumpFilePath: string | null = null;
|
||||
const isDatabase = isDatabaseVolume(volume);
|
||||
|
||||
if (isDatabase) {
|
||||
logger.info(`Creating database dump for volume ${volume.name}`);
|
||||
|
||||
const timestamp = Date.now();
|
||||
dumpFilePath = getDumpFilePath(volume, timestamp);
|
||||
|
||||
try {
|
||||
await executeDatabaseDump(volume.config as DatabaseConfig, dumpFilePath);
|
||||
logger.info(`Database dump created at: ${dumpFilePath}`);
|
||||
} catch (error) {
|
||||
logger.error(`Failed to create database dump: ${toMessage(error)}`);
|
||||
throw error;
|
||||
}
|
||||
|
||||
backupPath = dumpFilePath;
|
||||
} else {
|
||||
backupPath = getVolumePath(volume);
|
||||
}
|
||||
|
||||
const backupOptions: {
|
||||
exclude?: string[];
|
||||
@@ -226,7 +249,7 @@ const executeBackup = async (scheduleId: number, manual = false) => {
|
||||
backupOptions.include = schedule.includePatterns;
|
||||
}
|
||||
|
||||
await restic.backup(repository.config, volumePath, {
|
||||
await restic.backup(repository.config, backupPath, {
|
||||
...backupOptions,
|
||||
onProgress: (progress) => {
|
||||
serverEvents.emit("backup:progress", {
|
||||
@@ -242,6 +265,16 @@ const executeBackup = async (scheduleId: number, manual = false) => {
|
||||
await restic.forget(repository.config, schedule.retentionPolicy, { tag: schedule.id.toString() });
|
||||
}
|
||||
|
||||
// Clean up dump file if it was created
|
||||
if (dumpFilePath) {
|
||||
try {
|
||||
await fs.unlink(dumpFilePath);
|
||||
logger.info(`Cleaned up dump file: ${dumpFilePath}`);
|
||||
} catch (error) {
|
||||
logger.warn(`Failed to clean up dump file: ${toMessage(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
const nextBackupAt = calculateNextRun(schedule.cronExpression);
|
||||
await db
|
||||
.update(backupSchedulesTable)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { VOLUME_MOUNT_BASE } from "../../core/constants";
|
||||
import type { Volume } from "../../db/schema";
|
||||
import type { BackendConfig } from "~/schemas/volumes";
|
||||
|
||||
export const getVolumePath = (volume: Volume) => {
|
||||
if (volume.config.backend === "directory") {
|
||||
@@ -8,3 +9,37 @@ export const getVolumePath = (volume: Volume) => {
|
||||
|
||||
return `${VOLUME_MOUNT_BASE}/${volume.name}/_data`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if a volume is a database volume
|
||||
*/
|
||||
export const isDatabaseVolume = (volume: Volume): boolean => {
|
||||
return ["mariadb", "mysql", "postgres", "sqlite"].includes(volume.config.backend);
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if a backend config is a database backend
|
||||
*/
|
||||
export const isDatabaseBackend = (config: BackendConfig): boolean => {
|
||||
return ["mariadb", "mysql", "postgres", "sqlite"].includes(config.backend);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the dump directory path for a database volume
|
||||
*/
|
||||
export const getDumpPath = (volume: Volume): string => {
|
||||
return `${VOLUME_MOUNT_BASE}/${volume.name}/dumps`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the dump file path for a database volume backup
|
||||
*/
|
||||
export const getDumpFilePath = (volume: Volume, timestamp: number): string => {
|
||||
const dumpDir = getDumpPath(volume);
|
||||
const extension = volume.config.backend === "postgres" &&
|
||||
volume.config.backend === "postgres" &&
|
||||
(volume.config as Extract<BackendConfig, { backend: "postgres" }>).dumpFormat !== "plain"
|
||||
? "dump"
|
||||
: "sql";
|
||||
return `${dumpDir}/${volume.name}-${timestamp}.${extension}`;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user