feat: change backup format to plain SQL, admin-only cron, backup now button, import restore UI
This commit is contained in:
@@ -14,7 +14,7 @@ import {
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from "@/components/ui/alert-dialog";
|
||||
import { FolderOpen, Trash2 } from "lucide-react";
|
||||
import { FolderOpen, HardDrive, Trash2 } from "lucide-react";
|
||||
import { apiRequest, queryClient } from "@/lib/queryClient";
|
||||
import { useToast } from "@/hooks/use-toast";
|
||||
import { FolderBrowserModal } from "./folder-browser-modal";
|
||||
@@ -102,6 +102,21 @@ export function BackupDestinationManager() {
|
||||
},
|
||||
});
|
||||
|
||||
const backupNowMutation = useMutation({
|
||||
mutationFn: async () => {
|
||||
const res = await apiRequest("POST", "/api/database-management/backup-path");
|
||||
if (!res.ok) throw new Error((await res.json()).error || "Backup failed");
|
||||
return res.json();
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
toast({ title: "Backup complete", description: `Saved: ${data.filename}` });
|
||||
queryClient.invalidateQueries({ queryKey: ["/db/status"] });
|
||||
},
|
||||
onError: (err: any) => {
|
||||
toast({ title: "Backup failed", description: err.message, variant: "destructive" });
|
||||
},
|
||||
});
|
||||
|
||||
// ==============================
|
||||
// Folder browser
|
||||
// ==============================
|
||||
@@ -172,13 +187,25 @@ export function BackupDestinationManager() {
|
||||
className="flex justify-between items-center border rounded p-2"
|
||||
>
|
||||
<span className="text-sm text-gray-700">{d.path}</span>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="destructive"
|
||||
onClick={() => setDeleteId(d.id)}
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => backupNowMutation.mutate()}
|
||||
disabled={backupNowMutation.isPending}
|
||||
title="Backup now to this destination"
|
||||
>
|
||||
<HardDrive className="h-4 w-4 mr-1" />
|
||||
{backupNowMutation.isPending ? "Backing up..." : "Backup Now"}
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="destructive"
|
||||
onClick={() => setDeleteId(d.id)}
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
import { useRef, useState } from "react";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Upload, UploadCloud } from "lucide-react";
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from "@/components/ui/alert-dialog";
|
||||
import { useToast } from "@/hooks/use-toast";
|
||||
|
||||
export function ImportDatabaseSection() {
|
||||
const { toast } = useToast();
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const [selectedFile, setSelectedFile] = useState<File | null>(null);
|
||||
const [confirmOpen, setConfirmOpen] = useState(false);
|
||||
|
||||
const restoreMutation = useMutation({
|
||||
mutationFn: async (file: File) => {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
|
||||
const token = localStorage.getItem("token");
|
||||
const res = await fetch("/api/database-management/restore", {
|
||||
method: "POST",
|
||||
headers: token ? { Authorization: `Bearer ${token}` } : {},
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const body = await res.json().catch(() => ({}));
|
||||
throw new Error(body.error || "Restore failed");
|
||||
}
|
||||
return res.json();
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast({
|
||||
title: "Database Restored",
|
||||
description: "The database has been successfully restored from the backup file.",
|
||||
});
|
||||
setSelectedFile(null);
|
||||
if (fileInputRef.current) fileInputRef.current.value = "";
|
||||
},
|
||||
onError: (err: any) => {
|
||||
toast({
|
||||
title: "Restore Failed",
|
||||
description: err.message,
|
||||
variant: "destructive",
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0] ?? null;
|
||||
setSelectedFile(file);
|
||||
};
|
||||
|
||||
const handleImportClick = () => {
|
||||
if (!selectedFile) return;
|
||||
setConfirmOpen(true);
|
||||
};
|
||||
|
||||
const handleConfirm = () => {
|
||||
setConfirmOpen(false);
|
||||
if (selectedFile) restoreMutation.mutate(selectedFile);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center space-x-2">
|
||||
<UploadCloud className="h-5 w-5" />
|
||||
<span>Import Database</span>
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<p className="text-sm text-gray-500">
|
||||
Restore the database from a <span className="font-medium text-gray-700">.sql</span> backup file.
|
||||
This will overwrite all existing data.
|
||||
</p>
|
||||
|
||||
<div className="flex items-center gap-3">
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept=".sql"
|
||||
onChange={handleFileChange}
|
||||
className="block text-sm text-gray-600 file:mr-3 file:py-1.5 file:px-3 file:rounded file:border file:border-gray-300 file:text-sm file:bg-white file:text-gray-700 hover:file:bg-gray-50 cursor-pointer"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{selectedFile && (
|
||||
<p className="text-sm text-gray-500">
|
||||
Selected: <span className="font-medium text-gray-800">{selectedFile.name}</span>{" "}
|
||||
({(selectedFile.size / 1024 / 1024).toFixed(1)} MB)
|
||||
</p>
|
||||
)}
|
||||
|
||||
<Button
|
||||
onClick={handleImportClick}
|
||||
disabled={!selectedFile || restoreMutation.isPending}
|
||||
variant="destructive"
|
||||
className="flex items-center space-x-2"
|
||||
>
|
||||
<Upload className="h-4 w-4" />
|
||||
<span>{restoreMutation.isPending ? "Restoring..." : "Import & Restore"}</span>
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<AlertDialog open={confirmOpen} onOpenChange={setConfirmOpen}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Restore database?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
This will overwrite <strong>all existing data</strong> with the contents of{" "}
|
||||
<strong>{selectedFile?.name}</strong>. This action cannot be undone.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction onClick={handleConfirm} className="bg-red-600 hover:bg-red-700">
|
||||
Yes, restore
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import { useMutation, useQuery } from "@tanstack/react-query";
|
||||
import { apiRequest, queryClient } from "@/lib/queryClient";
|
||||
import { formatDateToHumanReadable } from "@/utils/dateUtils";
|
||||
import { BackupDestinationManager } from "@/components/database-management/backup-destination-manager";
|
||||
import { ImportDatabaseSection } from "@/components/database-management/import-database-section";
|
||||
|
||||
export default function DatabaseManagementPage() {
|
||||
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
|
||||
@@ -260,6 +261,9 @@ export default function DatabaseManagementPage() {
|
||||
{/* Externa Drive automatic backup manager */}
|
||||
<BackupDestinationManager />
|
||||
|
||||
{/* Import / Restore Database */}
|
||||
<ImportDatabaseSection />
|
||||
|
||||
{/* Database Status Section */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
|
||||
Reference in New Issue
Block a user