diff options
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java | 50 |
1 files changed, 34 insertions, 16 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java b/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java index 30fd8b5..0ca9015 100644 --- a/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java +++ b/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java @@ -1,5 +1,7 @@ package com.keuin.kbackupfabric.worker; +import com.keuin.kbackupfabric.data.BackupMetadata; +import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.ZipUtil; import com.keuin.kbackupfabric.util.ZipUtilException; import com.mojang.brigadier.context.CommandContext; @@ -14,7 +16,7 @@ import java.util.Map; import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.*; import static com.keuin.kbackupfabric.util.PrintUtil.debug; -import static com.keuin.kbackupfabric.util.PrintUtil.message; +import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo; /** * The backup worker @@ -24,18 +26,21 @@ public final class BackupWorker implements Runnable { private final CommandContext<ServerCommandSource> context; private final String backupName; private final Map<World, Boolean> oldWorldsSavingDisabled; + private final BackupMetadata backupMetadata; + private final long startTime; - - private BackupWorker(CommandContext<ServerCommandSource> context, String backupName, Map<World, Boolean> oldWorldsSavingDisabled) { + private BackupWorker(CommandContext<ServerCommandSource> context, String backupName, Map<World, Boolean> oldWorldsSavingDisabled, BackupMetadata backupMetadata, long startTime) { this.context = context; this.backupName = backupName; this.oldWorldsSavingDisabled = oldWorldsSavingDisabled; + this.backupMetadata = backupMetadata; + this.startTime = startTime; } - public static void invoke(CommandContext<ServerCommandSource> context, String backupName) { + public static void invoke(CommandContext<ServerCommandSource> context, String backupName, BackupMetadata backupMetadata) { //// Save world, save old autosave configs - message(context, String.format("Making backup %s, please wait ...", backupName), true); + PrintUtil.msgInfo(context, String.format("Making backup %s, please wait ...", backupName), true); Map<World, Boolean> oldWorldsSavingDisabled = new HashMap<>(); // old switch stat // Get server @@ -54,39 +59,52 @@ public final class BackupWorker implements Runnable { server.save(true, true, true); // Start threaded worker - BackupWorker worker = new BackupWorker(context, backupName, oldWorldsSavingDisabled); + BackupWorker worker = new BackupWorker(context, backupName, oldWorldsSavingDisabled, backupMetadata, System.currentTimeMillis()); Thread workerThread = new Thread(worker, "BackupWorker"); workerThread.start(); } @Override public void run() { - String destPathFolderName = ""; + String backupSaveDirectory = ""; MinecraftServer server = context.getSource().getMinecraftServer(); try { //// Do our main backup logic // Create backup saving directory - File destPathFile = getBackupSaveDirectory(server); - destPathFolderName = destPathFile.getName(); - if (!destPathFile.isDirectory() && !destPathFile.mkdir()) { - message(context, String.format("Failed to create backup saving directory: %s. Failed to backup.", destPathFolderName)); + File backupSaveDirectoryFile = getBackupSaveDirectory(server); + backupSaveDirectory = backupSaveDirectoryFile.getName(); + if (!backupSaveDirectoryFile.isDirectory() && !backupSaveDirectoryFile.mkdir()) { + msgInfo(context, String.format("Failed to create backup saving directory: %s. Failed to backup.", backupSaveDirectory)); return; } // Make zip String levelPath = getLevelPath(server); - debug(String.format("zip(srcPath=%s, destPath=%s)", levelPath, destPathFile.toString())); - ZipUtil.zip(levelPath, destPathFile.toString(), getBackupFileName(backupName)); + String backupFileName = getBackupFileName(backupName); + debug(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectoryFile.toString())); + ZipUtil.makeBackupZip(levelPath, backupSaveDirectoryFile.toString(), backupFileName, backupMetadata); + File backupZipFile = new File(backupSaveDirectoryFile, backupFileName); // Restore old autosave switch stat server.getWorlds().forEach(world -> world.savingDisabled = oldWorldsSavingDisabled.getOrDefault(world, true)); - message(context, "Done.", true); + // Print finish message: time elapsed and file size + long timeEscapedMillis = System.currentTimeMillis() - startTime; + msgInfo(context, String.format("Backup finished. (%.2fs)", timeEscapedMillis / 1000.0), true); + try { + double fileSize = backupZipFile.length() * 1.0 / 1024 / 1024; + if (fileSize > 1000) + msgInfo(context, String.format("File size: %.2fGB", fileSize / 1024)); + else + msgInfo(context, String.format("File size: %.2fMB", fileSize)); + } catch (SecurityException ignored) { + } + } catch (SecurityException e) { - message(context, String.format("Failed to create backup saving directory: %s. Failed to backup.", destPathFolderName)); + msgInfo(context, String.format("Failed to create backup saving directory: %s. Failed to backup.", backupSaveDirectory)); } catch (IOException | ZipUtilException e) { - message(context, "Failed to make zip: " + e.getMessage()); + msgInfo(context, "Failed to make zip: " + e.getMessage()); } } } |