diff options
author | Keuin <[email protected]> | 2021-01-23 15:44:15 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-23 15:44:15 +0800 |
commit | 483d91a54912f2c87d6d686184f66a8d091ef9b7 (patch) | |
tree | ea1cda511c69bb5efbf938d69060561d7a3d738b /src | |
parent | 4a1d885afa7217b47d6183488c3dc6537cef05b6 (diff) |
Now it will display information after restoring from an incremental backup
Diffstat (limited to 'src')
5 files changed, 34 insertions, 8 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java index 9f00790..73b61df 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java @@ -84,6 +84,8 @@ public final class KBCommands { * @return stat code. */ public static int list(CommandContext<ServerCommandSource> context) { + // TODO: Show real name and size and etc info for incremental backup + // TODO: Show concrete info from metadata for `.zip` backup MinecraftServer server = context.getSource().getMinecraftServer(); File[] files = getBackupSaveDirectory(server).listFiles( (dir, name) -> dir.isDirectory() && @@ -356,6 +358,7 @@ public final class KBCommands { * @return stat code. */ public static int prev(CommandContext<ServerCommandSource> context) { + // FIXME: This breaks after adding incremental backup try { // List all backups MinecraftServer server = context.getSource().getMinecraftServer(); diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncCopyResult.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncCopyResult.java index 6011ea5..dbaf9bf 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncCopyResult.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncCopyResult.java @@ -95,7 +95,7 @@ public class IncCopyResult { @Override public String toString() { return String.format( - "Files copied: %d (%s in size, totally %d files). Total file tree size: %s.", + "Files copied: %d (%s in size, totally %d files). Total file tree size: %s", filesCopied, BackupFilesystemUtil.getFriendlyFileSizeString(bytesCopied), totalFiles, diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java index 78c6943..9060d88 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java @@ -58,7 +58,7 @@ public class IncrementalBackupStorageManager { } { // element exists (file reused). Just update the stat info - copyCount = copyCount.addWith(new IncCopyResult(1, 1, 0, fileBytes)); + copyCount = copyCount.addWith(new IncCopyResult(1, 0, 0, fileBytes)); } } 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 2320ef3..c4be40d 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 @@ -9,13 +9,17 @@ import com.keuin.kbackupfabric.backup.incremental.serializer.IncBackupInfoSerial import com.keuin.kbackupfabric.backup.incremental.serializer.SavedIncrementalBackup; import com.keuin.kbackupfabric.backup.name.BackupFileNameEncoder; import com.keuin.kbackupfabric.backup.name.IncrementalBackupFileNameEncoder; +import com.keuin.kbackupfabric.metadata.BackupMetadata; 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.ThreadingUtil; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.ObjectOutputStream; +import java.nio.file.Files; import java.nio.file.Paths; import java.time.ZoneId; import java.util.Arrays; @@ -127,6 +131,20 @@ public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod IncrementalBackupStorageManager storageManager = new IncrementalBackupStorageManager(Paths.get(backupBaseDirectory)); int restoreObjectCount = storageManager.restoreObjectCollection(info.getObjectCollection(), levelPathFile); + // write metadata file + File metadataFile = new File(levelPathFile, BackupMetadata.metadataFileName); + try (FileOutputStream fos = new FileOutputStream(metadataFile)) { + try (ObjectOutputStream oos = new ObjectOutputStream(fos)) { + oos.writeObject(new BackupMetadata(info.getBackupTime().toEpochSecond() * 1000, info.getBackupName())); + } + } catch (IOException e) { + PrintUtil.warn("Failed to write restore metadata: " + e + ". KBackup won't print restoration information during the next startup."); + try { + Files.deleteIfExists(metadataFile.toPath()); + } catch (IOException ignored) { + } + } + PrintUtil.info(String.format("%d file(s) restored.", restoreObjectCount)); return true; } diff --git a/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java b/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java index 80c643d..5fe0411 100644 --- a/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java +++ b/src/test/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethodTest.java @@ -1,6 +1,7 @@ package com.keuin.kbackupfabric.operation.backup.method; import com.keuin.kbackupfabric.backup.name.IncrementalBackupFileNameEncoder; +import com.keuin.kbackupfabric.metadata.BackupMetadata; import com.keuin.kbackupfabric.operation.backup.feedback.IncrementalBackupFeedback; import org.apache.commons.codec.digest.DigestUtils; import org.junit.Test; @@ -88,10 +89,12 @@ public class ConfiguredIncrementalBackupMethodTest { int[] success = new int[1]; if (fake) { - Files.walk(sourcePath).filter(path -> path.toFile().isFile()).limit(3).forEach(path -> { + Files.walk(sourcePath).filter(path -> path.toFile().isFile()).limit(4).forEach(path -> { + System.out.println("Deleted file `" + path.toFile().getName() + "`."); if (!path.toFile().delete()) fail(); - success[0]++; + if (!Objects.equals(path.toFile().getName(), BackupMetadata.metadataFileName)) + success[0]++; }); if (success[0] == 0) fake = false; @@ -173,7 +176,7 @@ public class ConfiguredIncrementalBackupMethodTest { Vector<FileInputStream> fileStreams = new Vector<>(); System.out.println("Found files for hashing:"); - collectInputStreams(dirToHash, fileStreams, includeHiddenFiles); + collectInputStreams(dirToHash, fileStreams, includeHiddenFiles, Collections.singleton(BackupMetadata.metadataFileName)); SequenceInputStream seqStream = new SequenceInputStream(fileStreams.elements()); @@ -191,7 +194,8 @@ public class ConfiguredIncrementalBackupMethodTest { private void collectInputStreams(File dir, List<FileInputStream> foundStreams, - boolean includeHiddenFiles) { + boolean includeHiddenFiles, + Set<String> ignoredFiles) { File[] fileList = dir.listFiles(); Arrays.sort(fileList, // Need in reproducible order @@ -202,10 +206,11 @@ public class ConfiguredIncrementalBackupMethodTest { }); for (File f : fileList) { - if (!includeHiddenFiles && f.getName().startsWith(".")) { + if (!includeHiddenFiles && f.getName().startsWith(".") || + Optional.ofNullable(ignoredFiles).orElse(Collections.emptySet()).contains(f.getName())) { // Skip it } else if (f.isDirectory()) { - collectInputStreams(f, foundStreams, includeHiddenFiles); + collectInputStreams(f, foundStreams, includeHiddenFiles, ignoredFiles); } else { try { System.out.println("\t" + f.getAbsolutePath()); |