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:
@@ -2,8 +2,12 @@ ARG BUN_VERSION="1.3.1"
|
|||||||
|
|
||||||
FROM oven/bun:${BUN_VERSION}-alpine AS base
|
FROM oven/bun:${BUN_VERSION}-alpine AS base
|
||||||
|
|
||||||
RUN apk add --no-cache davfs2=1.6.1-r2
|
RUN apk add --no-cache \
|
||||||
|
davfs2=1.6.1-r2 \
|
||||||
|
mariadb-client \
|
||||||
|
mysql-client \
|
||||||
|
postgresql-client \
|
||||||
|
sqlite
|
||||||
|
|
||||||
# ------------------------------
|
# ------------------------------
|
||||||
# DEPENDENCIES
|
# DEPENDENCIES
|
||||||
|
|||||||
@@ -35,6 +35,10 @@ const defaultValuesForType = {
|
|||||||
nfs: { backend: "nfs" as const, port: 2049, version: "4.1" as const },
|
nfs: { backend: "nfs" as const, port: 2049, version: "4.1" as const },
|
||||||
smb: { backend: "smb" as const, port: 445, vers: "3.0" as const },
|
smb: { backend: "smb" as const, port: 445, vers: "3.0" as const },
|
||||||
webdav: { backend: "webdav" as const, port: 80, ssl: false },
|
webdav: { backend: "webdav" as const, port: 80, ssl: false },
|
||||||
|
mariadb: { backend: "mariadb" as const, port: 3306 },
|
||||||
|
mysql: { backend: "mysql" as const, port: 3306 },
|
||||||
|
postgres: { backend: "postgres" as const, port: 5432, dumpFormat: "custom" as const },
|
||||||
|
sqlite: { backend: "sqlite" as const, path: "/" },
|
||||||
};
|
};
|
||||||
|
|
||||||
export const CreateVolumeForm = ({ onSubmit, mode = "create", initialValues, formId, loading, className }: Props) => {
|
export const CreateVolumeForm = ({ onSubmit, mode = "create", initialValues, formId, loading, className }: Props) => {
|
||||||
@@ -81,7 +85,14 @@ export const CreateVolumeForm = ({ onSubmit, mode = "create", initialValues, for
|
|||||||
const handleTestConnection = async () => {
|
const handleTestConnection = async () => {
|
||||||
const formValues = getValues();
|
const formValues = getValues();
|
||||||
|
|
||||||
if (formValues.backend === "nfs" || formValues.backend === "smb" || formValues.backend === "webdav") {
|
if (
|
||||||
|
formValues.backend === "nfs" ||
|
||||||
|
formValues.backend === "smb" ||
|
||||||
|
formValues.backend === "webdav" ||
|
||||||
|
formValues.backend === "mariadb" ||
|
||||||
|
formValues.backend === "mysql" ||
|
||||||
|
formValues.backend === "postgres"
|
||||||
|
) {
|
||||||
testBackendConnection.mutate({
|
testBackendConnection.mutate({
|
||||||
body: { config: formValues },
|
body: { config: formValues },
|
||||||
});
|
});
|
||||||
@@ -130,6 +141,10 @@ export const CreateVolumeForm = ({ onSubmit, mode = "create", initialValues, for
|
|||||||
<SelectItem value="nfs">NFS</SelectItem>
|
<SelectItem value="nfs">NFS</SelectItem>
|
||||||
<SelectItem value="smb">SMB</SelectItem>
|
<SelectItem value="smb">SMB</SelectItem>
|
||||||
<SelectItem value="webdav">WebDAV</SelectItem>
|
<SelectItem value="webdav">WebDAV</SelectItem>
|
||||||
|
<SelectItem value="mariadb">MariaDB</SelectItem>
|
||||||
|
<SelectItem value="mysql">MySQL</SelectItem>
|
||||||
|
<SelectItem value="postgres">PostgreSQL</SelectItem>
|
||||||
|
<SelectItem value="sqlite">SQLite</SelectItem>
|
||||||
</SelectContent>
|
</SelectContent>
|
||||||
</Select>
|
</Select>
|
||||||
<FormDescription>Choose the storage backend for this volume.</FormDescription>
|
<FormDescription>Choose the storage backend for this volume.</FormDescription>
|
||||||
@@ -536,7 +551,290 @@ export const CreateVolumeForm = ({ onSubmit, mode = "create", initialValues, for
|
|||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{watchedBackend !== "directory" && (
|
{watchedBackend === "mariadb" && (
|
||||||
|
<>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="host"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Host</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="localhost" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>MariaDB server hostname or IP address.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="port"
|
||||||
|
defaultValue={3306}
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Port</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input type="number" placeholder="3306" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>MariaDB server port (default: 3306).</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="username"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Username</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="root" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>Database user with backup privileges.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="password"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Password</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input type="password" placeholder="••••••••" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>Password for database authentication.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="database"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Database</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="myapp_production" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>Name of the database to backup.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{watchedBackend === "mysql" && (
|
||||||
|
<>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="host"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Host</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="localhost" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>MySQL server hostname or IP address.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="port"
|
||||||
|
defaultValue={3306}
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Port</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input type="number" placeholder="3306" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>MySQL server port (default: 3306).</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="username"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Username</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="root" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>Database user with backup privileges.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="password"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Password</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input type="password" placeholder="••••••••" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>Password for database authentication.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="database"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Database</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="myapp_production" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>Name of the database to backup.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{watchedBackend === "postgres" && (
|
||||||
|
<>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="host"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Host</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="localhost" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>PostgreSQL server hostname or IP address.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="port"
|
||||||
|
defaultValue={5432}
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Port</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input type="number" placeholder="5432" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>PostgreSQL server port (default: 5432).</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="username"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Username</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="postgres" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>Database user with backup privileges.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="password"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Password</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input type="password" placeholder="••••••••" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>Password for database authentication.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="database"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Database</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input placeholder="myapp_production" {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>Name of the database to backup.</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="dumpFormat"
|
||||||
|
defaultValue="custom"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Dump Format</FormLabel>
|
||||||
|
<Select onValueChange={field.onChange} defaultValue={field.value || "custom"}>
|
||||||
|
<FormControl>
|
||||||
|
<SelectTrigger>
|
||||||
|
<SelectValue placeholder="Select dump format" />
|
||||||
|
</SelectTrigger>
|
||||||
|
</FormControl>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectItem value="custom">Custom (Compressed)</SelectItem>
|
||||||
|
<SelectItem value="plain">Plain SQL</SelectItem>
|
||||||
|
<SelectItem value="directory">Directory</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
<FormDescription>Format for database dumps (custom recommended).</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{watchedBackend === "sqlite" && (
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="path"
|
||||||
|
render={({ field }) => {
|
||||||
|
return (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Database File Path</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
{field.value ? (
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<div className="flex-1 border rounded-md p-3 bg-muted/50">
|
||||||
|
<div className="text-xs font-medium text-muted-foreground mb-1">Selected database:</div>
|
||||||
|
<div className="text-sm font-mono break-all">{field.value}</div>
|
||||||
|
</div>
|
||||||
|
<Button type="button" variant="outline" size="sm" onClick={() => field.onChange("")}>
|
||||||
|
Change
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<DirectoryBrowser onSelectPath={(path) => field.onChange(path)} selectedPath={field.value} />
|
||||||
|
)}
|
||||||
|
</FormControl>
|
||||||
|
<FormDescription>Path to the SQLite database file (.db, .sqlite, .sqlite3).</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{watchedBackend !== "directory" && watchedBackend !== "sqlite" && (
|
||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Cloud, Folder, Server, Share2 } from "lucide-react";
|
import { Cloud, Database, Folder, Server, Share2 } from "lucide-react";
|
||||||
import type { BackendType } from "~/schemas/volumes";
|
import type { BackendType } from "~/schemas/volumes";
|
||||||
|
|
||||||
type VolumeIconProps = {
|
type VolumeIconProps = {
|
||||||
@@ -32,6 +32,30 @@ const getIconAndColor = (backend: BackendType) => {
|
|||||||
color: "text-green-600 dark:text-green-400",
|
color: "text-green-600 dark:text-green-400",
|
||||||
label: "WebDAV",
|
label: "WebDAV",
|
||||||
};
|
};
|
||||||
|
case "mariadb":
|
||||||
|
return {
|
||||||
|
icon: Database,
|
||||||
|
color: "text-teal-600 dark:text-teal-400",
|
||||||
|
label: "MariaDB",
|
||||||
|
};
|
||||||
|
case "mysql":
|
||||||
|
return {
|
||||||
|
icon: Database,
|
||||||
|
color: "text-cyan-600 dark:text-cyan-400",
|
||||||
|
label: "MySQL",
|
||||||
|
};
|
||||||
|
case "postgres":
|
||||||
|
return {
|
||||||
|
icon: Database,
|
||||||
|
color: "text-indigo-600 dark:text-indigo-400",
|
||||||
|
label: "PostgreSQL",
|
||||||
|
};
|
||||||
|
case "sqlite":
|
||||||
|
return {
|
||||||
|
icon: Database,
|
||||||
|
color: "text-slate-600 dark:text-slate-400",
|
||||||
|
label: "SQLite",
|
||||||
|
};
|
||||||
default:
|
default:
|
||||||
return {
|
return {
|
||||||
icon: Folder,
|
icon: Folder,
|
||||||
|
|||||||
@@ -118,6 +118,7 @@ export default function VolumeDetails({ loaderData }: Route.ComponentProps) {
|
|||||||
|
|
||||||
const { volume, statfs } = data;
|
const { volume, statfs } = data;
|
||||||
const dockerAvailable = capabilities.docker;
|
const dockerAvailable = capabilities.docker;
|
||||||
|
const isDatabaseVolume = ["mariadb", "mysql", "postgres", "sqlite"].includes(volume.config.backend);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -152,7 +153,7 @@ export default function VolumeDetails({ loaderData }: Route.ComponentProps) {
|
|||||||
<Tabs value={activeTab} onValueChange={(value) => setSearchParams({ tab: value })} className="mt-4">
|
<Tabs value={activeTab} onValueChange={(value) => setSearchParams({ tab: value })} className="mt-4">
|
||||||
<TabsList className="mb-2">
|
<TabsList className="mb-2">
|
||||||
<TabsTrigger value="info">Configuration</TabsTrigger>
|
<TabsTrigger value="info">Configuration</TabsTrigger>
|
||||||
<TabsTrigger value="files">Files</TabsTrigger>
|
{!isDatabaseVolume && <TabsTrigger value="files">Files</TabsTrigger>}
|
||||||
<Tooltip>
|
<Tooltip>
|
||||||
<TooltipTrigger>
|
<TooltipTrigger>
|
||||||
<TabsTrigger disabled={!dockerAvailable} value="docker">
|
<TabsTrigger disabled={!dockerAvailable} value="docker">
|
||||||
@@ -167,9 +168,11 @@ export default function VolumeDetails({ loaderData }: Route.ComponentProps) {
|
|||||||
<TabsContent value="info">
|
<TabsContent value="info">
|
||||||
<VolumeInfoTabContent volume={volume} statfs={statfs} />
|
<VolumeInfoTabContent volume={volume} statfs={statfs} />
|
||||||
</TabsContent>
|
</TabsContent>
|
||||||
<TabsContent value="files">
|
{!isDatabaseVolume && (
|
||||||
<FilesTabContent volume={volume} />
|
<TabsContent value="files">
|
||||||
</TabsContent>
|
<FilesTabContent volume={volume} />
|
||||||
|
</TabsContent>
|
||||||
|
)}
|
||||||
{dockerAvailable && (
|
{dockerAvailable && (
|
||||||
<TabsContent value="docker">
|
<TabsContent value="docker">
|
||||||
<DockerTabContent volume={volume} />
|
<DockerTabContent volume={volume} />
|
||||||
|
|||||||
@@ -109,6 +109,11 @@ export default function Volumes({ loaderData }: Route.ComponentProps) {
|
|||||||
<SelectItem value="directory">Directory</SelectItem>
|
<SelectItem value="directory">Directory</SelectItem>
|
||||||
<SelectItem value="nfs">NFS</SelectItem>
|
<SelectItem value="nfs">NFS</SelectItem>
|
||||||
<SelectItem value="smb">SMB</SelectItem>
|
<SelectItem value="smb">SMB</SelectItem>
|
||||||
|
<SelectItem value="webdav">WebDAV</SelectItem>
|
||||||
|
<SelectItem value="mariadb">MariaDB</SelectItem>
|
||||||
|
<SelectItem value="mysql">MySQL</SelectItem>
|
||||||
|
<SelectItem value="postgres">PostgreSQL</SelectItem>
|
||||||
|
<SelectItem value="sqlite">SQLite</SelectItem>
|
||||||
</SelectContent>
|
</SelectContent>
|
||||||
</Select>
|
</Select>
|
||||||
{(searchQuery || statusFilter || backendFilter) && (
|
{(searchQuery || statusFilter || backendFilter) && (
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ export const BACKEND_TYPES = {
|
|||||||
smb: "smb",
|
smb: "smb",
|
||||||
directory: "directory",
|
directory: "directory",
|
||||||
webdav: "webdav",
|
webdav: "webdav",
|
||||||
|
mariadb: "mariadb",
|
||||||
|
mysql: "mysql",
|
||||||
|
postgres: "postgres",
|
||||||
|
sqlite: "sqlite",
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export type BackendType = keyof typeof BACKEND_TYPES;
|
export type BackendType = keyof typeof BACKEND_TYPES;
|
||||||
@@ -47,7 +51,50 @@ export const webdavConfigSchema = type({
|
|||||||
ssl: "boolean?",
|
ssl: "boolean?",
|
||||||
});
|
});
|
||||||
|
|
||||||
export const volumeConfigSchema = nfsConfigSchema.or(smbConfigSchema).or(webdavConfigSchema).or(directoryConfigSchema);
|
export const mariadbConfigSchema = type({
|
||||||
|
backend: "'mariadb'",
|
||||||
|
host: "string",
|
||||||
|
port: type("string.integer").or(type("number")).to("1 <= number <= 65535").default(3306),
|
||||||
|
username: "string",
|
||||||
|
password: "string",
|
||||||
|
database: "string",
|
||||||
|
dumpOptions: "string[]?",
|
||||||
|
});
|
||||||
|
|
||||||
|
export const mysqlConfigSchema = type({
|
||||||
|
backend: "'mysql'",
|
||||||
|
host: "string",
|
||||||
|
port: type("string.integer").or(type("number")).to("1 <= number <= 65535").default(3306),
|
||||||
|
username: "string",
|
||||||
|
password: "string",
|
||||||
|
database: "string",
|
||||||
|
dumpOptions: "string[]?",
|
||||||
|
});
|
||||||
|
|
||||||
|
export const postgresConfigSchema = type({
|
||||||
|
backend: "'postgres'",
|
||||||
|
host: "string",
|
||||||
|
port: type("string.integer").or(type("number")).to("1 <= number <= 65535").default(5432),
|
||||||
|
username: "string",
|
||||||
|
password: "string",
|
||||||
|
database: "string",
|
||||||
|
dumpFormat: type("'plain' | 'custom' | 'directory'").default("custom"),
|
||||||
|
dumpOptions: "string[]?",
|
||||||
|
});
|
||||||
|
|
||||||
|
export const sqliteConfigSchema = type({
|
||||||
|
backend: "'sqlite'",
|
||||||
|
path: "string",
|
||||||
|
});
|
||||||
|
|
||||||
|
export const volumeConfigSchema = nfsConfigSchema
|
||||||
|
.or(smbConfigSchema)
|
||||||
|
.or(webdavConfigSchema)
|
||||||
|
.or(directoryConfigSchema)
|
||||||
|
.or(mariadbConfigSchema)
|
||||||
|
.or(mysqlConfigSchema)
|
||||||
|
.or(postgresConfigSchema)
|
||||||
|
.or(sqliteConfigSchema);
|
||||||
|
|
||||||
export type BackendConfig = typeof volumeConfigSchema.infer;
|
export type BackendConfig = typeof volumeConfigSchema.infer;
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ import { makeDirectoryBackend } from "./directory/directory-backend";
|
|||||||
import { makeNfsBackend } from "./nfs/nfs-backend";
|
import { makeNfsBackend } from "./nfs/nfs-backend";
|
||||||
import { makeSmbBackend } from "./smb/smb-backend";
|
import { makeSmbBackend } from "./smb/smb-backend";
|
||||||
import { makeWebdavBackend } from "./webdav/webdav-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 = {
|
type OperationResult = {
|
||||||
error?: string;
|
error?: string;
|
||||||
@@ -33,5 +37,20 @@ export const createVolumeBackend = (volume: Volume): VolumeBackend => {
|
|||||||
case "webdav": {
|
case "webdav": {
|
||||||
return makeWebdavBackend(volume.config, path);
|
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 cron from "node-cron";
|
||||||
import { CronExpressionParser } from "cron-parser";
|
import { CronExpressionParser } from "cron-parser";
|
||||||
import { NotFoundError, BadRequestError, ConflictError } from "http-errors-enhanced";
|
import { NotFoundError, BadRequestError, ConflictError } from "http-errors-enhanced";
|
||||||
|
import * as fs from "node:fs/promises";
|
||||||
import { db } from "../../db/db";
|
import { db } from "../../db/db";
|
||||||
import { backupSchedulesTable, repositoriesTable, volumesTable } from "../../db/schema";
|
import { backupSchedulesTable, repositoriesTable, volumesTable } from "../../db/schema";
|
||||||
import { restic } from "../../utils/restic";
|
import { restic } from "../../utils/restic";
|
||||||
import { logger } from "../../utils/logger";
|
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 type { CreateBackupScheduleBody, UpdateBackupScheduleBody } from "./backups.dto";
|
||||||
import { toMessage } from "../../utils/errors";
|
import { toMessage } from "../../utils/errors";
|
||||||
import { serverEvents } from "../../core/events";
|
import { serverEvents } from "../../core/events";
|
||||||
|
import { executeDatabaseDump, type DatabaseConfig } from "../../utils/database-dump";
|
||||||
|
|
||||||
const runningBackups = new Map<number, AbortController>();
|
const runningBackups = new Map<number, AbortController>();
|
||||||
|
|
||||||
@@ -206,7 +208,28 @@ const executeBackup = async (scheduleId: number, manual = false) => {
|
|||||||
runningBackups.set(scheduleId, abortController);
|
runningBackups.set(scheduleId, abortController);
|
||||||
|
|
||||||
try {
|
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: {
|
const backupOptions: {
|
||||||
exclude?: string[];
|
exclude?: string[];
|
||||||
@@ -226,7 +249,7 @@ const executeBackup = async (scheduleId: number, manual = false) => {
|
|||||||
backupOptions.include = schedule.includePatterns;
|
backupOptions.include = schedule.includePatterns;
|
||||||
}
|
}
|
||||||
|
|
||||||
await restic.backup(repository.config, volumePath, {
|
await restic.backup(repository.config, backupPath, {
|
||||||
...backupOptions,
|
...backupOptions,
|
||||||
onProgress: (progress) => {
|
onProgress: (progress) => {
|
||||||
serverEvents.emit("backup: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() });
|
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);
|
const nextBackupAt = calculateNextRun(schedule.cronExpression);
|
||||||
await db
|
await db
|
||||||
.update(backupSchedulesTable)
|
.update(backupSchedulesTable)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { VOLUME_MOUNT_BASE } from "../../core/constants";
|
import { VOLUME_MOUNT_BASE } from "../../core/constants";
|
||||||
import type { Volume } from "../../db/schema";
|
import type { Volume } from "../../db/schema";
|
||||||
|
import type { BackendConfig } from "~/schemas/volumes";
|
||||||
|
|
||||||
export const getVolumePath = (volume: Volume) => {
|
export const getVolumePath = (volume: Volume) => {
|
||||||
if (volume.config.backend === "directory") {
|
if (volume.config.backend === "directory") {
|
||||||
@@ -8,3 +9,37 @@ export const getVolumePath = (volume: Volume) => {
|
|||||||
|
|
||||||
return `${VOLUME_MOUNT_BASE}/${volume.name}/_data`;
|
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}`;
|
||||||
|
};
|
||||||
|
|||||||
282
app/server/utils/database-dump.ts
Normal file
282
app/server/utils/database-dump.ts
Normal file
@@ -0,0 +1,282 @@
|
|||||||
|
import * as fs from "node:fs/promises";
|
||||||
|
import * as path from "node:path";
|
||||||
|
import { safeSpawn } from "./spawn";
|
||||||
|
import { logger } from "./logger";
|
||||||
|
import { toMessage } from "./errors";
|
||||||
|
import type { BackendConfig } from "~/schemas/volumes";
|
||||||
|
|
||||||
|
export type DatabaseConfig = Extract<
|
||||||
|
BackendConfig,
|
||||||
|
{ backend: "mariadb" | "mysql" | "postgres" | "sqlite" }
|
||||||
|
>;
|
||||||
|
|
||||||
|
// MariaDB
|
||||||
|
export const dumpMariaDB = async (config: DatabaseConfig, outputPath: string): Promise<void> => {
|
||||||
|
if (config.backend !== "mariadb") {
|
||||||
|
throw new Error("Invalid backend type for MariaDB dump");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(`Starting MariaDB dump for database: ${config.database}`);
|
||||||
|
|
||||||
|
const args = [
|
||||||
|
`--host=${config.host}`,
|
||||||
|
`--port=${config.port}`,
|
||||||
|
`--user=${config.username}`,
|
||||||
|
`--skip-ssl`,
|
||||||
|
`--single-transaction`,
|
||||||
|
`--quick`,
|
||||||
|
`--lock-tables=false`,
|
||||||
|
...(config.dumpOptions || []),
|
||||||
|
config.database,
|
||||||
|
];
|
||||||
|
|
||||||
|
const env = {
|
||||||
|
...process.env,
|
||||||
|
MYSQL_PWD: config.password,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await safeSpawn({ command: "mariadb-dump", args, env });
|
||||||
|
await fs.writeFile(outputPath, result.stdout);
|
||||||
|
logger.info(`MariaDB dump completed: ${outputPath}`);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`MariaDB dump failed: ${toMessage(error)}`);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const testMariaDBConnection = async (config: DatabaseConfig): Promise<void> => {
|
||||||
|
if (config.backend !== "mariadb") {
|
||||||
|
throw new Error("Invalid backend type for MariaDB connection test");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug(`Testing MariaDB connection to: ${config.host}:${config.port}`);
|
||||||
|
|
||||||
|
const args = [
|
||||||
|
`--host=${config.host}`,
|
||||||
|
`--port=${config.port}`,
|
||||||
|
`--user=${config.username}`,
|
||||||
|
`--database=${config.database}`,
|
||||||
|
"--skip-ssl",
|
||||||
|
"--execute=SELECT 1",
|
||||||
|
];
|
||||||
|
|
||||||
|
const env = {
|
||||||
|
...process.env,
|
||||||
|
MYSQL_PWD: config.password,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
await safeSpawn({ command: "mariadb", args, env, timeout: 10000 });
|
||||||
|
logger.debug("MariaDB connection test successful");
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`MariaDB connection test failed: ${toMessage(error)}`);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// MySQL
|
||||||
|
export const dumpMySQL = async (config: DatabaseConfig, outputPath: string): Promise<void> => {
|
||||||
|
if (config.backend !== "mysql") {
|
||||||
|
throw new Error("Invalid backend type for MySQL dump");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(`Starting MySQL dump for database: ${config.database}`);
|
||||||
|
|
||||||
|
const args = [
|
||||||
|
`--host=${config.host}`,
|
||||||
|
`--port=${config.port}`,
|
||||||
|
`--user=${config.username}`,
|
||||||
|
`--skip-ssl`,
|
||||||
|
`--single-transaction`,
|
||||||
|
`--quick`,
|
||||||
|
`--lock-tables=false`,
|
||||||
|
...(config.dumpOptions || []),
|
||||||
|
config.database,
|
||||||
|
];
|
||||||
|
|
||||||
|
const env = {
|
||||||
|
...process.env,
|
||||||
|
MYSQL_PWD: config.password,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await safeSpawn({ command: "mysqldump", args, env });
|
||||||
|
await fs.writeFile(outputPath, result.stdout);
|
||||||
|
logger.info(`MySQL dump completed: ${outputPath}`);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`MySQL dump failed: ${toMessage(error)}`);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const testMySQLConnection = async (config: DatabaseConfig): Promise<void> => {
|
||||||
|
if (config.backend !== "mysql") {
|
||||||
|
throw new Error("Invalid backend type for MySQL connection test");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug(`Testing MySQL connection to: ${config.host}:${config.port}`);
|
||||||
|
|
||||||
|
const args = [
|
||||||
|
`--host=${config.host}`,
|
||||||
|
`--port=${config.port}`,
|
||||||
|
`--user=${config.username}`,
|
||||||
|
`--database=${config.database}`,
|
||||||
|
"--skip-ssl",
|
||||||
|
"--execute=SELECT 1",
|
||||||
|
];
|
||||||
|
|
||||||
|
const env = {
|
||||||
|
...process.env,
|
||||||
|
MYSQL_PWD: config.password,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
await safeSpawn({ command: "mysql", args, env, timeout: 10000 });
|
||||||
|
logger.debug("MySQL connection test successful");
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`MySQL connection test failed: ${toMessage(error)}`);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// PostgreSQL
|
||||||
|
export const dumpPostgres = async (config: DatabaseConfig, outputPath: string): Promise<void> => {
|
||||||
|
if (config.backend !== "postgres") {
|
||||||
|
throw new Error("Invalid backend type for PostgreSQL dump");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(`Starting PostgreSQL dump for database: ${config.database}`);
|
||||||
|
|
||||||
|
const args = [
|
||||||
|
`--host=${config.host}`,
|
||||||
|
`--port=${config.port}`,
|
||||||
|
`--username=${config.username}`,
|
||||||
|
`--dbname=${config.database}`,
|
||||||
|
`--format=${config.dumpFormat}`,
|
||||||
|
`--file=${outputPath}`,
|
||||||
|
"--no-password",
|
||||||
|
...(config.dumpOptions || []),
|
||||||
|
];
|
||||||
|
|
||||||
|
const env = {
|
||||||
|
...process.env,
|
||||||
|
PGPASSWORD: config.password,
|
||||||
|
PGSSLMODE: "disable",
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
await safeSpawn({ command: "pg_dump", args, env });
|
||||||
|
logger.info(`PostgreSQL dump completed: ${outputPath}`);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`PostgreSQL dump failed: ${toMessage(error)}`);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const testPostgresConnection = async (config: DatabaseConfig): Promise<void> => {
|
||||||
|
if (config.backend !== "postgres") {
|
||||||
|
throw new Error("Invalid backend type for PostgreSQL connection test");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug(`Testing PostgreSQL connection to: ${config.host}:${config.port}`);
|
||||||
|
|
||||||
|
const args = [
|
||||||
|
`--host=${config.host}`,
|
||||||
|
`--port=${config.port}`,
|
||||||
|
`--username=${config.username}`,
|
||||||
|
`--dbname=${config.database}`,
|
||||||
|
"--command=SELECT 1",
|
||||||
|
"--no-password",
|
||||||
|
];
|
||||||
|
|
||||||
|
const env = {
|
||||||
|
...process.env,
|
||||||
|
PGPASSWORD: config.password,
|
||||||
|
PGSSLMODE: "disable",
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
await safeSpawn({ command: "psql", args, env, timeout: 10000 });
|
||||||
|
logger.debug("PostgreSQL connection test successful");
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`PostgreSQL connection test failed: ${toMessage(error)}`);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// SQLite
|
||||||
|
export const dumpSQLite = async (config: DatabaseConfig, outputPath: string): Promise<void> => {
|
||||||
|
if (config.backend !== "sqlite") {
|
||||||
|
throw new Error("Invalid backend type for SQLite dump");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(`Starting SQLite dump for database: ${config.path}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fs.access(config.path);
|
||||||
|
|
||||||
|
const result = await safeSpawn({ command: "sqlite3", args: [config.path, ".dump"] });
|
||||||
|
await fs.writeFile(outputPath, result.stdout);
|
||||||
|
logger.info(`SQLite dump completed: ${outputPath}`);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`SQLite dump failed: ${toMessage(error)}`);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const testSQLiteConnection = async (config: DatabaseConfig): Promise<void> => {
|
||||||
|
if (config.backend !== "sqlite") {
|
||||||
|
throw new Error("Invalid backend type for SQLite connection test");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug(`Testing SQLite connection to: ${config.path}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fs.access(config.path, fs.constants.R_OK);
|
||||||
|
const result = await safeSpawn({ command: "sqlite3", args: [config.path, "SELECT 1"] });
|
||||||
|
|
||||||
|
if (!result.stdout.includes("1")) {
|
||||||
|
throw new Error("SQLite database query failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug("SQLite connection test successful");
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`SQLite connection test failed: ${toMessage(error)}`);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Utils
|
||||||
|
export const executeDatabaseDump = async (config: DatabaseConfig, outputPath: string): Promise<void> => {
|
||||||
|
const outputDir = path.dirname(outputPath);
|
||||||
|
await fs.mkdir(outputDir, { recursive: true });
|
||||||
|
|
||||||
|
switch (config.backend) {
|
||||||
|
case "mariadb":
|
||||||
|
return dumpMariaDB(config, outputPath);
|
||||||
|
case "mysql":
|
||||||
|
return dumpMySQL(config, outputPath);
|
||||||
|
case "postgres":
|
||||||
|
return dumpPostgres(config, outputPath);
|
||||||
|
case "sqlite":
|
||||||
|
return dumpSQLite(config, outputPath);
|
||||||
|
default:
|
||||||
|
throw new Error(`Unsupported database backend: ${(config as any).backend}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const testDatabaseConnection = async (config: DatabaseConfig): Promise<void> => {
|
||||||
|
switch (config.backend) {
|
||||||
|
case "mariadb":
|
||||||
|
return testMariaDBConnection(config);
|
||||||
|
case "mysql":
|
||||||
|
return testMySQLConnection(config);
|
||||||
|
case "postgres":
|
||||||
|
return testPostgresConnection(config);
|
||||||
|
case "sqlite":
|
||||||
|
return testSQLiteConnection(config);
|
||||||
|
default:
|
||||||
|
throw new Error(`Unsupported database backend: ${(config as any).backend}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -5,6 +5,8 @@ interface Params {
|
|||||||
args: string[];
|
args: string[];
|
||||||
env?: NodeJS.ProcessEnv;
|
env?: NodeJS.ProcessEnv;
|
||||||
signal?: AbortSignal;
|
signal?: AbortSignal;
|
||||||
|
stdin?: string;
|
||||||
|
timeout?: number;
|
||||||
onStdout?: (data: string) => void;
|
onStdout?: (data: string) => void;
|
||||||
onStderr?: (error: string) => void;
|
onStderr?: (error: string) => void;
|
||||||
onError?: (error: Error) => Promise<void> | void;
|
onError?: (error: Error) => Promise<void> | void;
|
||||||
@@ -19,17 +21,32 @@ type SpawnResult = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const safeSpawn = (params: Params) => {
|
export const safeSpawn = (params: Params) => {
|
||||||
const { command, args, env = {}, signal, ...callbacks } = params;
|
const { command, args, env = {}, signal, stdin, timeout, ...callbacks } = params;
|
||||||
|
|
||||||
return new Promise<SpawnResult>((resolve) => {
|
return new Promise<SpawnResult>((resolve, reject) => {
|
||||||
let stdoutData = "";
|
let stdoutData = "";
|
||||||
let stderrData = "";
|
let stderrData = "";
|
||||||
|
let timeoutId: NodeJS.Timeout | undefined;
|
||||||
|
|
||||||
const child = spawn(command, args, {
|
const child = spawn(command, args, {
|
||||||
env: { ...process.env, ...env },
|
env: { ...process.env, ...env },
|
||||||
signal: signal,
|
signal: signal,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Handle timeout if specified
|
||||||
|
if (timeout) {
|
||||||
|
timeoutId = setTimeout(() => {
|
||||||
|
child.kill("SIGTERM");
|
||||||
|
reject(new Error(`Command timed out after ${timeout}ms`));
|
||||||
|
}, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write stdin if provided
|
||||||
|
if (stdin && child.stdin) {
|
||||||
|
child.stdin.write(stdin);
|
||||||
|
child.stdin.end();
|
||||||
|
}
|
||||||
|
|
||||||
child.stdout.on("data", (data) => {
|
child.stdout.on("data", (data) => {
|
||||||
if (callbacks.onStdout) {
|
if (callbacks.onStdout) {
|
||||||
callbacks.onStdout(data.toString());
|
callbacks.onStdout(data.toString());
|
||||||
@@ -47,6 +64,7 @@ export const safeSpawn = (params: Params) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
child.on("error", async (error) => {
|
child.on("error", async (error) => {
|
||||||
|
if (timeoutId) clearTimeout(timeoutId);
|
||||||
if (callbacks.onError) {
|
if (callbacks.onError) {
|
||||||
await callbacks.onError(error);
|
await callbacks.onError(error);
|
||||||
}
|
}
|
||||||
@@ -54,14 +72,11 @@ export const safeSpawn = (params: Params) => {
|
|||||||
await callbacks.finally();
|
await callbacks.finally();
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve({
|
reject(error);
|
||||||
exitCode: -1,
|
|
||||||
stdout: stdoutData,
|
|
||||||
stderr: stderrData,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
child.on("close", async (code) => {
|
child.on("close", async (code) => {
|
||||||
|
if (timeoutId) clearTimeout(timeoutId);
|
||||||
if (callbacks.onClose) {
|
if (callbacks.onClose) {
|
||||||
await callbacks.onClose(code);
|
await callbacks.onClose(code);
|
||||||
}
|
}
|
||||||
@@ -69,11 +84,15 @@ export const safeSpawn = (params: Params) => {
|
|||||||
await callbacks.finally();
|
await callbacks.finally();
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve({
|
if (code !== 0 && code !== null) {
|
||||||
exitCode: code === null ? -1 : code,
|
reject(new Error(`Command failed with exit code ${code}: ${stderrData || stdoutData}`));
|
||||||
stdout: stdoutData,
|
} else {
|
||||||
stderr: stderrData,
|
resolve({
|
||||||
});
|
exitCode: code === null ? -1 : code,
|
||||||
|
stdout: stdoutData,
|
||||||
|
stderr: stderrData,
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user