diff options
author | Keuin <[email protected]> | 2021-01-21 12:13:48 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-21 12:15:18 +0800 |
commit | 821b497a256e0c2825a78c0b070ee3398f79a509 (patch) | |
tree | 1dbb4abf3a818ebb773f736511ba5ff3d9916694 /src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java | |
parent | 64345efb5f4397993fbc9f2a2102ebcd3fe9f125 (diff) |
Fix minor bug in ZipUtil. Do some clean if backup is failed. Small code refactor.
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 | 56 |
1 files changed, 39 insertions, 17 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 index 60b825e..baa6580 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 @@ -14,6 +14,7 @@ import java.io.IOException; import java.nio.file.Paths; import java.util.Arrays; import java.util.HashSet; +import java.util.logging.Logger; public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod { @@ -22,6 +23,8 @@ public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod private final String backupIndexFileSaveDirectory; private final String backupBaseDirectory; + private static final Logger LOGGER = Logger.getLogger(ConfiguredIncrementalBackupMethod.class.getName()); + public ConfiguredIncrementalBackupMethod(String backupIndexFileName, String levelPath, String backupIndexFileSaveDirectory, String backupBaseDirectory) { this.backupIndexFileName = backupIndexFileName; this.levelPath = levelPath; @@ -30,26 +33,45 @@ public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod } @Override - public IncrementalBackupFeedback backup() throws IOException { - File levelPathFile = new File(levelPath); + public IncrementalBackupFeedback backup() { + IncrementalBackupFeedback feedback; + try { + File levelPathFile = new File(levelPath); + + // construct incremental backup index + PrintUtil.info("Hashing files..."); + ObjectCollection collection = new ObjectCollectionFactory<>(Sha256Identifier.getFactory()) + .fromDirectory(levelPathFile, new HashSet<>(Arrays.asList("session.lock", "kbackup_metadata"))); + + // update storage + PrintUtil.info("Copying files..."); + IncrementalBackupStorageManager storageManager = new IncrementalBackupStorageManager(Paths.get(backupBaseDirectory)); + int filesAdded = storageManager.addObjectCollection(collection, levelPathFile); + + // save index file + PrintUtil.info("Saving index file..."); + ObjectCollectionSerializer.toFile(collection, new File(backupIndexFileSaveDirectory, backupIndexFileName)); + + // return result + PrintUtil.info("Incremental backup finished."); + feedback = new IncrementalBackupFeedback(filesAdded >= 0, filesAdded); + } catch (IOException e) { + feedback = new IncrementalBackupFeedback(false, 0); + } - // construct incremental backup index - PrintUtil.info("Hashing files..."); - ObjectCollection collection = new ObjectCollectionFactory<>(Sha256Identifier.getFactory()) - .fromDirectory(levelPathFile, new HashSet<>(Arrays.asList("session.lock", "kbackup_metadata"))); + if (!feedback.isSuccess()) { + // do clean-up if failed + File backupIndexFile = new File(backupIndexFileSaveDirectory, backupIndexFileName); + if (backupIndexFile.exists()) { + if (!backupIndexFile.delete()) { + LOGGER.warning("Failed to clean up: cannot delete file " + backupIndexFile.getName()); + } + } - // update storage - PrintUtil.info("Copying files..."); - IncrementalBackupStorageManager storageManager = new IncrementalBackupStorageManager(Paths.get(backupBaseDirectory)); - int filesAdded = storageManager.addObjectCollection(collection, levelPathFile); - - // save index file - PrintUtil.info("Saving index file..."); - ObjectCollectionSerializer.toFile(collection, new File(backupIndexFileSaveDirectory, backupIndexFileName)); + //TODO: do more deep clean for object files + } - // return result - PrintUtil.info("Incremental backup finished."); - return new IncrementalBackupFeedback(filesAdded >= 0, filesAdded); + return feedback; } @Override |