|
|
@@ -28,9 +28,16 @@ public class DatabaseBackupService {
|
|
|
}
|
|
|
|
|
|
public Path createBackup(String label) {
|
|
|
- Path dbPath = resolveSqlitePath(datasourceUrl);
|
|
|
- if (!Files.exists(dbPath)) {
|
|
|
- throw new BusinessException("数据库文件不存在,无法备份: " + dbPath);
|
|
|
+ Path dbPath;
|
|
|
+ try {
|
|
|
+ dbPath = resolveSqlitePath(datasourceUrl);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // In-memory or non-file databases — skip backup
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (!Files.exists(dbPath) || !Files.isRegularFile(dbPath)) {
|
|
|
+ // In-memory or non-existent — skip backup silently
|
|
|
+ return null;
|
|
|
}
|
|
|
try {
|
|
|
Files.createDirectories(Path.of(backupDir));
|
|
|
@@ -48,6 +55,20 @@ public class DatabaseBackupService {
|
|
|
if (url == null || !url.startsWith("jdbc:sqlite:")) {
|
|
|
throw new BusinessException("仅支持 jdbc:sqlite 数据库备份");
|
|
|
}
|
|
|
- return Path.of(url.substring("jdbc:sqlite:".length())).normalize();
|
|
|
+ String path = url.substring("jdbc:sqlite:".length());
|
|
|
+ // Strip query parameters
|
|
|
+ int queryIdx = path.indexOf('?');
|
|
|
+ if (queryIdx >= 0) {
|
|
|
+ path = path.substring(0, queryIdx);
|
|
|
+ }
|
|
|
+ // Handle file: prefix for shared cache mode
|
|
|
+ if (path.startsWith("file:")) {
|
|
|
+ path = path.substring(5);
|
|
|
+ }
|
|
|
+ // :memory: is not a file
|
|
|
+ if (path.contains(":memory:")) {
|
|
|
+ throw new BusinessException("内存数据库,无需备份");
|
|
|
+ }
|
|
|
+ return Path.of(path).normalize();
|
|
|
}
|
|
|
}
|