3
0
mirror of https://github.com/Qortal/qortal.git synced 2025-02-12 10:15:49 +00:00

Modified repository backup and recovery to allow a custom filename to be specified.

This commit is contained in:
CalDescent 2021-09-28 09:26:18 +01:00
parent 8926d2a73c
commit e2a62f88a6
5 changed files with 12 additions and 12 deletions

View File

@ -668,7 +668,7 @@ public class AdminResource {
blockchainLock.lockInterruptibly(); blockchainLock.lockInterruptibly();
try { try {
repository.backup(true); repository.backup(true, "backup");
repository.saveChanges(); repository.saveChanges();
return "true"; return "true";

View File

@ -216,7 +216,7 @@ public class AutoUpdate extends Thread {
// Give repository a chance to backup in case things go badly wrong (if enabled) // Give repository a chance to backup in case things go badly wrong (if enabled)
if (Settings.getInstance().getRepositoryBackupInterval() > 0) if (Settings.getInstance().getRepositoryBackupInterval() > 0)
RepositoryManager.backup(true); RepositoryManager.backup(true, "backup");
// Call ApplyUpdate to end this process (unlocking current JAR so it can be replaced) // Call ApplyUpdate to end this process (unlocking current JAR so it can be replaced)
String javaHome = System.getProperty("java.home"); String javaHome = System.getProperty("java.home");

View File

@ -573,7 +573,7 @@ public class Controller extends Thread {
Translator.INSTANCE.translate("SysTray", "CREATING_BACKUP_OF_DB_FILES"), Translator.INSTANCE.translate("SysTray", "CREATING_BACKUP_OF_DB_FILES"),
MessageType.INFO); MessageType.INFO);
RepositoryManager.backup(true); RepositoryManager.backup(true, "backup");
} }
// Prune stuck/slow/old peers // Prune stuck/slow/old peers

View File

@ -379,7 +379,7 @@ public class HSQLDBRepository implements Repository {
} }
@Override @Override
public void backup(boolean quick) throws DataException { public void backup(boolean quick, String name) throws DataException {
if (!quick) if (!quick)
// First perform a CHECKPOINT // First perform a CHECKPOINT
try (Statement stmt = this.connection.createStatement()) { try (Statement stmt = this.connection.createStatement()) {
@ -401,7 +401,7 @@ public class HSQLDBRepository implements Repository {
return; return;
} }
String backupUrl = buildBackupUrl(dbPathname); String backupUrl = buildBackupUrl(dbPathname, name);
String backupPathname = getDbPathname(backupUrl); String backupPathname = getDbPathname(backupUrl);
if (backupPathname == null) if (backupPathname == null)
throw new DataException("Unable to determine location for repository backup?"); throw new DataException("Unable to determine location for repository backup?");
@ -423,7 +423,7 @@ public class HSQLDBRepository implements Repository {
// Actually create backup // Actually create backup
try (Statement stmt = this.connection.createStatement()) { try (Statement stmt = this.connection.createStatement()) {
stmt.execute("BACKUP DATABASE TO 'backup/' BLOCKING AS FILES"); stmt.execute(String.format("BACKUP DATABASE TO '%s/' BLOCKING AS FILES", name));
} catch (SQLException e) { } catch (SQLException e) {
throw new DataException("Unable to backup repository"); throw new DataException("Unable to backup repository");
} }
@ -472,22 +472,22 @@ public class HSQLDBRepository implements Repository {
return matcher.group(2); return matcher.group(2);
} }
private static String buildBackupUrl(String dbPathname) { private static String buildBackupUrl(String dbPathname, String backupName) {
Path oldRepoPath = Paths.get(dbPathname); Path oldRepoPath = Paths.get(dbPathname);
Path oldRepoDirPath = oldRepoPath.getParent(); Path oldRepoDirPath = oldRepoPath.getParent();
Path oldRepoFilePath = oldRepoPath.getFileName(); Path oldRepoFilePath = oldRepoPath.getFileName();
// Try to open backup. We need to remove "create=true" and insert "backup" dir before final filename. // Try to open backup. We need to remove "create=true" and insert "backup" dir before final filename.
String backupUrlTemplate = "jdbc:hsqldb:file:%s%sbackup%s%s;create=false;hsqldb.full_log_replay=true"; String backupUrlTemplate = "jdbc:hsqldb:file:%s%s%s%s%s;create=false;hsqldb.full_log_replay=true";
return String.format(backupUrlTemplate, oldRepoDirPath.toString(), File.separator, File.separator, oldRepoFilePath.toString()); return String.format(backupUrlTemplate, oldRepoDirPath.toString(), File.separator, backupName, File.separator, oldRepoFilePath.toString());
} }
/* package */ static void attemptRecovery(String connectionUrl) throws DataException { /* package */ static void attemptRecovery(String connectionUrl, String name) throws DataException {
String dbPathname = getDbPathname(connectionUrl); String dbPathname = getDbPathname(connectionUrl);
if (dbPathname == null) if (dbPathname == null)
throw new DataException("Unable to locate repository for backup?"); throw new DataException("Unable to locate repository for backup?");
String backupUrl = buildBackupUrl(dbPathname); String backupUrl = buildBackupUrl(dbPathname, name);
Path oldRepoDirPath = Paths.get(dbPathname).getParent(); Path oldRepoDirPath = Paths.get(dbPathname).getParent();
// Attempt connection to backup to see if it is viable // Attempt connection to backup to see if it is viable

View File

@ -54,7 +54,7 @@ public class HSQLDBRepositoryFactory implements RepositoryFactory {
throw new DataException("Unable to read repository: " + e.getMessage(), e); throw new DataException("Unable to read repository: " + e.getMessage(), e);
// Attempt recovery? // Attempt recovery?
HSQLDBRepository.attemptRecovery(connectionUrl); HSQLDBRepository.attemptRecovery(connectionUrl, "backup");
} }
this.connectionPool = new HSQLDBPool(Settings.getInstance().getRepositoryConnectionPoolSize()); this.connectionPool = new HSQLDBPool(Settings.getInstance().getRepositoryConnectionPoolSize());