summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/operation/backup/method
diff options
context:
space:
mode:
authorKeuin <[email protected]>2021-01-13 17:47:20 +0800
committerkeuin <[email protected]>2021-01-13 17:47:20 +0800
commite0c6a21fe9bfb01237fd145064f0af309879a9fb (patch)
tree321955f96030213c2bc8c3ec350961a81b60edee /src/main/java/com/keuin/kbackupfabric/operation/backup/method
parent2bc659d8f95a97d0514491e48ed9c66828a4e308 (diff)
Incremental backup now works (tested, but not thoroughly)
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/operation/backup/method')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredBackupMethod.java14
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java66
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java42
3 files changed, 83 insertions, 39 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredBackupMethod.java
index b1b8d90..bb80c80 100644
--- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredBackupMethod.java
+++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredBackupMethod.java
@@ -20,4 +20,18 @@ public interface ConfiguredBackupMethod {
boolean restore() throws IOException;
+ /**
+ * Create backup save directory and do some essential initialization before the backup process.
+ *
+ * @return false if failed, then the backup process won't proceed.
+ */
+ boolean touch();
+
+ /**
+ * Get the used backup file name.
+ *
+ * @return the file name.
+ */
+ String getBackupFileName();
+
}
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java
index 0201d18..b5d2463 100644
--- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java
+++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java
@@ -1,52 +1,92 @@
package com.keuin.kbackupfabric.operation.backup.method;
import com.keuin.kbackupfabric.operation.backup.feedback.IncrementalBackupFeedback;
+import com.keuin.kbackupfabric.util.FilesystemUtil;
+import com.keuin.kbackupfabric.util.PrintUtil;
import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollection;
import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollectionFactory;
import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollectionSerializer;
import com.keuin.kbackupfabric.util.backup.incremental.identifier.Sha256Identifier;
import com.keuin.kbackupfabric.util.backup.incremental.manager.IncrementalBackupStorageManager;
-import com.keuin.kbackupfabric.util.backup.name.IncrementalBackupFileNameEncoder;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
-import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.HashSet;
public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod {
- private final String backupFileName;
+ private final String backupIndexFileName;
private final String levelPath;
- private final String backupSavePath;
+ private final String backupIndexFileSaveDirectory;
+ private final String backupBaseDirectory;
- public ConfiguredIncrementalBackupMethod(String backupFileName, String levelPath, String backupSavePath) {
- this.backupFileName = backupFileName;
+ public ConfiguredIncrementalBackupMethod(String backupIndexFileName, String levelPath, String backupIndexFileSaveDirectory, String backupBaseDirectory) {
+ this.backupIndexFileName = backupIndexFileName;
this.levelPath = levelPath;
- this.backupSavePath = backupSavePath;
+ this.backupIndexFileSaveDirectory = backupIndexFileSaveDirectory;
+ this.backupBaseDirectory = backupBaseDirectory;
}
@Override
public IncrementalBackupFeedback backup() throws IOException {
- String customBackupName = new IncrementalBackupFileNameEncoder().decode(backupFileName).customName;
- String backupIndexFileName = new IncrementalBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now());
File levelPathFile = new File(levelPath);
// construct incremental backup index
+ PrintUtil.info("Hashing files...");
ObjectCollection collection = new ObjectCollectionFactory<>(Sha256Identifier.getFactory())
- .fromDirectory(levelPathFile);
+ .fromDirectory(levelPathFile, new HashSet<>(Arrays.asList("session.lock", "kbackup_metadata")));
// update storage
- IncrementalBackupStorageManager storageManager = new IncrementalBackupStorageManager(Paths.get(backupSavePath));
+ PrintUtil.info("Copying files...");
+ IncrementalBackupStorageManager storageManager = new IncrementalBackupStorageManager(Paths.get(backupBaseDirectory));
int filesAdded = storageManager.addObjectCollection(collection, levelPathFile);
// save index file
- ObjectCollectionSerializer.toFile(collection, new File(backupSavePath, backupIndexFileName));
+ PrintUtil.info("Saving index file...");
+ ObjectCollectionSerializer.toFile(collection, new File(backupIndexFileSaveDirectory, backupIndexFileName));
+ // return result
+ PrintUtil.info("Incremental backup finished.");
return new IncrementalBackupFeedback(filesAdded >= 0, filesAdded);
}
@Override
public boolean restore() throws IOException {
- return false;
+ // load collection
+ PrintUtil.info("Loading file list...");
+ ObjectCollection collection = ObjectCollectionSerializer.fromFile(
+ new File(backupIndexFileSaveDirectory, backupIndexFileName)
+ );
+
+ // delete old level
+ File levelPathFile = new File(levelPath);
+ PrintUtil.info("Deleting old level...");
+ if (!FilesystemUtil.forceDeleteDirectory(levelPathFile)) {
+ PrintUtil.info("Failed to delete old level!");
+ return false;
+ }
+
+ // restore file
+ PrintUtil.info("Copying files...");
+ IncrementalBackupStorageManager storageManager = new IncrementalBackupStorageManager(Paths.get(backupBaseDirectory));
+ int restoreObjectCount = storageManager.restoreObjectCollection(collection, levelPathFile);
+
+ PrintUtil.info(String.format("%d file(s) restored.", restoreObjectCount));
+ return true;
+ }
+
+ @Override
+ public boolean touch() {
+ File baseDirectoryFile = new File(backupBaseDirectory);
+ return baseDirectoryFile.isDirectory() || baseDirectoryFile.mkdir();
+ }
+
+ @Override
+ public String getBackupFileName() {
+ return backupIndexFileName;
}
+
+
}
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java
index c3013e9..1c3c9f6 100644
--- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java
+++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java
@@ -15,8 +15,6 @@ import java.io.IOException;
import java.nio.file.Paths;
import java.time.LocalDateTime;
-import static org.apache.commons.io.FileUtils.forceDelete;
-
public class ConfiguredPrimitiveBackupMethod implements ConfiguredBackupMethod {
private final String backupFileName;
@@ -56,37 +54,17 @@ public class ConfiguredPrimitiveBackupMethod implements ConfiguredBackupMethod {
public boolean restore() throws IOException {
// Delete old level
PrintUtil.info("Server stopped. Deleting old level ...");
- File levelDirFile = new File(levelPath);
- long startTime = System.currentTimeMillis();
-
- int failedCounter = 0;
- final int MAX_RETRY_TIMES = 20;
- while (failedCounter < MAX_RETRY_TIMES) {
- System.gc();
- if (!levelDirFile.delete() && levelDirFile.exists()) {
- System.gc();
- forceDelete(levelDirFile); // Try to force delete.
- }
- if (!levelDirFile.exists())
- break;
- ++failedCounter;
- try {
- Thread.sleep(500);
- } catch (InterruptedException ignored) {
- }
- }
- if (levelDirFile.exists()) {
- PrintUtil.error(String.format("Cannot restore: failed to delete old level %s .", levelDirFile.getName()));
+ if (!FilesystemUtil.forceDeleteDirectory(new File(levelPath))) {
+ PrintUtil.info("Failed to delete old level!");
return false;
}
+
// TODO: Refactor this to the concrete BackupMethod.
// Decompress archive
PrintUtil.info("Decompressing archived level ...");
ZipUtil.unzip(Paths.get(backupSavePath, backupFileName).toString(), levelPath, false);
- long endTime = System.currentTimeMillis();
- PrintUtil.info(String.format("Restore complete! (%.2fs) Please restart the server manually.", (endTime - startTime) / 1000.0));
- PrintUtil.info("If you want to restart automatically after restoring, please check the manual at: https://github.com/keuin/KBackup-Fabric/blob/master/README.md");
+
// try {
// Thread.sleep(1000);
@@ -95,4 +73,16 @@ public class ConfiguredPrimitiveBackupMethod implements ConfiguredBackupMethod {
return true;
}
+
+ @Override
+ public boolean touch() {
+ File backupSaveDirectoryFile = new File(backupSavePath);
+ return backupSaveDirectoryFile.isDirectory() || backupSaveDirectoryFile.mkdir();
+ }
+
+ @Override
+ public String getBackupFileName() {
+ return backupFileName;
+ }
+
}