diff options
author | Keuin <[email protected]> | 2021-01-23 14:10:32 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-23 14:10:32 +0800 |
commit | 4a1d885afa7217b47d6183488c3dc6537cef05b6 (patch) | |
tree | 1b499db6b834cb0709029e30c0d52c0ddf200ffa /src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java | |
parent | 4ac575330130ac4e1b4b35386ffc0aacd431a5a4 (diff) |
Version 1.4.6 (preview): added metadata for incremental backup (need integrated test and display implementation)
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 | 47 |
1 files changed, 36 insertions, 11 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 569c4c6..2320ef3 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,10 +1,14 @@ package com.keuin.kbackupfabric.operation.backup.method; -import com.keuin.kbackupfabric.backup.incremental.ObjectCollection; +import com.keuin.kbackupfabric.backup.incremental.ObjectCollection2; import com.keuin.kbackupfabric.backup.incremental.ObjectCollectionFactory; -import com.keuin.kbackupfabric.backup.incremental.ObjectCollectionSerializer; import com.keuin.kbackupfabric.backup.incremental.identifier.Sha256Identifier; +import com.keuin.kbackupfabric.backup.incremental.manager.IncCopyResult; import com.keuin.kbackupfabric.backup.incremental.manager.IncrementalBackupStorageManager; +import com.keuin.kbackupfabric.backup.incremental.serializer.IncBackupInfoSerializer; +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.operation.backup.feedback.IncrementalBackupFeedback; import com.keuin.kbackupfabric.util.FilesystemUtil; import com.keuin.kbackupfabric.util.PrintUtil; @@ -13,6 +17,7 @@ import com.keuin.kbackupfabric.util.ThreadingUtil; import java.io.File; import java.io.IOException; import java.nio.file.Paths; +import java.time.ZoneId; import java.util.Arrays; import java.util.HashSet; import java.util.logging.Logger; @@ -35,9 +40,7 @@ public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod @Override public IncrementalBackupFeedback backup() { - final int hashFactoryThreads = ThreadingUtil.getRecommendedThreadCount(); // how many threads do we use to generate the hash tree - LOGGER.info("Threads: " + hashFactoryThreads); IncrementalBackupFeedback feedback; @@ -46,24 +49,44 @@ public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod // construct incremental backup index PrintUtil.info("Hashing files..."); - ObjectCollection collection = new ObjectCollectionFactory<>(Sha256Identifier.getFactory(), hashFactoryThreads, 16) + // TODO + ObjectCollection2 collection = new ObjectCollectionFactory<>(Sha256Identifier.getFactory(), hashFactoryThreads, 16) .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); + IncCopyResult copyResult = storageManager.addObjectCollection(collection, levelPathFile); + if (copyResult == null) { + PrintUtil.info("Failed to backup. No further information."); + return new IncrementalBackupFeedback(false, null); + } // save index file PrintUtil.info("Saving index file..."); - ObjectCollectionSerializer.toFile(collection, new File(backupIndexFileSaveDirectory, backupIndexFileName)); + + // legacy index file +// ObjectCollectionSerializer.toFile(collection, new File(backupIndexFileSaveDirectory, backupIndexFileName)); + + // newer saved info (with metadata) + File indexFile = new File(backupIndexFileSaveDirectory, backupIndexFileName); + BackupFileNameEncoder.BackupBasicInformation info = new IncrementalBackupFileNameEncoder().decode(backupIndexFileName); + IncBackupInfoSerializer.toFile(indexFile, SavedIncrementalBackup.newLatest( + collection, + info.customName, + info.time.atZone(ZoneId.systemDefault()), + copyResult.getBytesTotal(), + copyResult.getBytesCopied(), + copyResult.getFilesCopied(), + copyResult.getTotalFiles() + )); // return result PrintUtil.info("Incremental backup finished."); - feedback = new IncrementalBackupFeedback(filesAdded >= 0, filesAdded); + feedback = new IncrementalBackupFeedback(true, copyResult); } catch (IOException e) { e.printStackTrace(); // at least we should print it out if we discard the exception... Better than doing nothing. - feedback = new IncrementalBackupFeedback(false, 0); + feedback = new IncrementalBackupFeedback(false, null); } if (!feedback.isSuccess()) { @@ -85,10 +108,12 @@ public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod public boolean restore() throws IOException { // load collection PrintUtil.info("Loading file list..."); - ObjectCollection collection = ObjectCollectionSerializer.fromFile( + SavedIncrementalBackup info = IncBackupInfoSerializer.fromFile( new File(backupIndexFileSaveDirectory, backupIndexFileName) ); + PrintUtil.info("Backup Info: " + info); + // delete old level File levelPathFile = new File(levelPath); PrintUtil.info("Deleting old level..."); @@ -100,7 +125,7 @@ public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod // restore file PrintUtil.info("Copying files..."); IncrementalBackupStorageManager storageManager = new IncrementalBackupStorageManager(Paths.get(backupBaseDirectory)); - int restoreObjectCount = storageManager.restoreObjectCollection(collection, levelPathFile); + int restoreObjectCount = storageManager.restoreObjectCollection(info.getObjectCollection(), levelPathFile); PrintUtil.info(String.format("%d file(s) restored.", restoreObjectCount)); return true; |