From 2bc659d8f95a97d0514491e48ed9c66828a4e308 Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 13 Jan 2021 14:32:08 +0800 Subject: BackupMethod now becomes stateful --- .../operation/backup/method/BackupMethod.java | 23 ----- .../backup/method/ConfiguredBackupMethod.java | 23 +++++ .../method/ConfiguredIncrementalBackupMethod.java | 52 ++++++++++++ .../method/ConfiguredPrimitiveBackupMethod.java | 98 ++++++++++++++++++++++ .../backup/method/IncrementalBackupMethod.java | 17 ---- .../backup/method/PrimitiveBackupMethod.java | 95 --------------------- 6 files changed, 173 insertions(+), 135 deletions(-) delete mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredBackupMethod.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/method/PrimitiveBackupMethod.java (limited to 'src/main/java/com/keuin/kbackupfabric/operation/backup') diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java deleted file mode 100644 index 7afbabd..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.keuin.kbackupfabric.operation.backup.method; - -import com.keuin.kbackupfabric.operation.backup.feedback.BackupFeedback; - -import java.io.IOException; - -/** - * Provide specific backup method, which is implemented statelessly. - */ -public interface BackupMethod { - - /** - * Perform a backup with given method. The backup will be saved as the given name. - * Note: real file name depends on the backup type. - * - * @param customBackupName the custom backup name. - * @return if the backup operation succeed. - */ - BackupFeedback backup(String customBackupName, String levelPath, String backupSaveDirectory) throws IOException; - - boolean restore(String backupName, String levelPath, String backupSaveDirectory) throws IOException; - -} 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 new file mode 100644 index 0000000..b1b8d90 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredBackupMethod.java @@ -0,0 +1,23 @@ +package com.keuin.kbackupfabric.operation.backup.method; + +import com.keuin.kbackupfabric.operation.backup.feedback.BackupFeedback; + +import java.io.IOException; + +/** + * Provide specific backup method, which has been configured with proper settings, + * such as saving directory and level path. + */ +public interface ConfiguredBackupMethod { + + /** + * Perform a backup with given method. The backup will be saved as the given name. + * Note: real file name depends on the backup type. + * + * @return backup result. + */ + BackupFeedback backup() throws IOException; + + boolean restore() throws IOException; + +} 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; + } +} 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 new file mode 100644 index 0000000..c3013e9 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java @@ -0,0 +1,98 @@ +package com.keuin.kbackupfabric.operation.backup.method; + +import com.keuin.kbackupfabric.exception.ZipUtilException; +import com.keuin.kbackupfabric.metadata.BackupMetadata; +import com.keuin.kbackupfabric.operation.backup.feedback.PrimitiveBackupFeedback; +import com.keuin.kbackupfabric.util.FilesystemUtil; +import com.keuin.kbackupfabric.util.PrintUtil; +import com.keuin.kbackupfabric.util.ZipUtil; +import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; +import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; +import com.keuin.kbackupfabric.util.backup.name.PrimitiveBackupFileNameEncoder; + +import java.io.File; +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; + private final String levelPath; + private final String backupSavePath; + + public ConfiguredPrimitiveBackupMethod(String backupFileName, String levelPath, String backupSavePath) { + this.backupFileName = backupFileName; + this.levelPath = levelPath; + this.backupSavePath = backupSavePath; + } + + @Deprecated + private String getBackupFileName(LocalDateTime time, String backupName) { + String timeString = BackupNameTimeFormatter.localDateTimeToString(time); + return String.format("%s%s_%s%s", BackupFilesystemUtil.getBackupFileNamePrefix(), timeString, backupName, ".zip"); + } + + @Override + public PrimitiveBackupFeedback backup() throws IOException { + try { + String customBackupName = new PrimitiveBackupFileNameEncoder().decode(backupFileName).customName; + BackupMetadata backupMetadata = new BackupMetadata(System.currentTimeMillis(), customBackupName); + PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSavePath)); + PrintUtil.info("Compressing level ..."); + ZipUtil.makeBackupZip(levelPath, backupSavePath, backupFileName, backupMetadata); + } catch (ZipUtilException exception) { + PrintUtil.info("Infinite recursive of directory tree detected, backup was aborted."); + return new PrimitiveBackupFeedback(false, 0); + } + + // Get backup file size and return + return new PrimitiveBackupFeedback(true, FilesystemUtil.getFileSizeBytes(backupSavePath, backupFileName)); + } + + @Override + 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())); + 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); +// } catch (InterruptedException ignored) { +// } + + return true; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java deleted file mode 100644 index 25e5731..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.keuin.kbackupfabric.operation.backup.method; - -import com.keuin.kbackupfabric.operation.backup.feedback.IncrementalBackupFeedback; - -import java.io.IOException; - -public class IncrementalBackupMethod implements BackupMethod { - @Override - public IncrementalBackupFeedback backup(String customBackupName, String levelPath, String backupSaveDirectory) throws IOException { - return null; - } - - @Override - public boolean restore(String backupName, String levelPath, String backupSaveDirectory) throws IOException { - return false; - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/PrimitiveBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/PrimitiveBackupMethod.java deleted file mode 100644 index 1f15346..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/PrimitiveBackupMethod.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.keuin.kbackupfabric.operation.backup.method; - -import com.keuin.kbackupfabric.exception.ZipUtilException; -import com.keuin.kbackupfabric.metadata.BackupMetadata; -import com.keuin.kbackupfabric.operation.backup.feedback.PrimitiveBackupFeedback; -import com.keuin.kbackupfabric.util.FilesystemUtil; -import com.keuin.kbackupfabric.util.PrintUtil; -import com.keuin.kbackupfabric.util.ZipUtil; -import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; -import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; -import com.keuin.kbackupfabric.util.backup.name.PrimitiveBackupFileNameEncoder; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Paths; -import java.time.LocalDateTime; - -import static org.apache.commons.io.FileUtils.forceDelete; - -public class PrimitiveBackupMethod implements BackupMethod { - - private static final PrimitiveBackupMethod INSTANCE = new PrimitiveBackupMethod(); - - public static PrimitiveBackupMethod getInstance() { - return INSTANCE; - } - - @Deprecated - private String getBackupFileName(LocalDateTime time, String backupName) { - String timeString = BackupNameTimeFormatter.localDateTimeToString(time); - return String.format("%s%s_%s%s", BackupFilesystemUtil.getBackupFileNamePrefix(), timeString, backupName, ".zip"); - } - - @Override - public PrimitiveBackupFeedback backup(String customBackupName, String levelPath, String backupSavePath) throws IOException { -// String backupFileName = getBackupFileName(LocalDateTime.now(),backupName); - String backupFileName = new PrimitiveBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()); - try { - BackupMetadata backupMetadata = new BackupMetadata(System.currentTimeMillis(), customBackupName); - PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSavePath)); - PrintUtil.info("Compressing level ..."); - ZipUtil.makeBackupZip(levelPath, backupSavePath, backupFileName, backupMetadata); - } catch (ZipUtilException exception) { - PrintUtil.info("Infinite recursive of directory tree detected, backup was aborted."); - return new PrimitiveBackupFeedback(false, 0); - } - - // Get backup file size and return - return new PrimitiveBackupFeedback(true, FilesystemUtil.getFileSizeBytes(backupSavePath, backupFileName)); - } - - @Override - public boolean restore(String backupFileName, String levelDirectory, String backupSaveDirectory) throws IOException { - // Delete old level - PrintUtil.info("Server stopped. Deleting old level ..."); - File levelDirFile = new File(levelDirectory); - 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())); - return false; - } - - // TODO: Refactor this to the concrete BackupMethod. - // Decompress archive - PrintUtil.info("Decompressing archived level ..."); - ZipUtil.unzip(Paths.get(backupSaveDirectory, backupFileName).toString(), levelDirectory, 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); -// } catch (InterruptedException ignored) { -// } - - return true; - } -} -- cgit v1.2.3