diff options
author | Keuin <[email protected]> | 2021-01-13 14:32:08 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-13 14:32:08 +0800 |
commit | 2bc659d8f95a97d0514491e48ed9c66828a4e308 (patch) | |
tree | 202962a05caadf652ec117812cc6704ea64d001c /src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java | |
parent | 1c6d68ee303ad3d4b1df5c3d433bf972256f0c8e (diff) |
BackupMethod now becomes stateful
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java | 52 |
1 files changed, 52 insertions, 0 deletions
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 new file mode 100644 index 0000000..0201d18 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java @@ -0,0 +1,52 @@ +package com.keuin.kbackupfabric.operation.backup.method; + +import com.keuin.kbackupfabric.operation.backup.feedback.IncrementalBackupFeedback; +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; + +public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod { + + private final String backupFileName; + private final String levelPath; + private final String backupSavePath; + + public ConfiguredIncrementalBackupMethod(String backupFileName, String levelPath, String backupSavePath) { + this.backupFileName = backupFileName; + this.levelPath = levelPath; + this.backupSavePath = backupSavePath; + } + + @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 + ObjectCollection collection = new ObjectCollectionFactory<>(Sha256Identifier.getFactory()) + .fromDirectory(levelPathFile); + + // update storage + IncrementalBackupStorageManager storageManager = new IncrementalBackupStorageManager(Paths.get(backupSavePath)); + int filesAdded = storageManager.addObjectCollection(collection, levelPathFile); + + // save index file + ObjectCollectionSerializer.toFile(collection, new File(backupSavePath, backupIndexFileName)); + + return new IncrementalBackupFeedback(filesAdded >= 0, filesAdded); + } + + @Override + public boolean restore() throws IOException { + return false; + } +} |