summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/worker
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/worker')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java109
-rw-r--r--src/main/java/com/keuin/kbackupfabric/worker/RestoreWorker.java95
2 files changed, 0 insertions, 204 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java b/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java
deleted file mode 100644
index d6db636..0000000
--- a/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java
+++ /dev/null
@@ -1,109 +0,0 @@
-package com.keuin.kbackupfabric.worker;
-
-import com.keuin.kbackupfabric.metadata.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;
-import net.minecraft.server.MinecraftServer;
-import net.minecraft.server.command.ServerCommandSource;
-import net.minecraft.world.World;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
-import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.*;
-import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo;
-
-/**
- * The backup worker
- * To invoke this worker, simply call invoke() method.
- */
-public final class BackupWorker implements Runnable {
-
- //private static final Logger LOGGER = LogManager.getLogger();
- 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, 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, BackupMetadata backupMetadata) {
- //// Save world, save old autosave configs
-
- PrintUtil.broadcast(String.format("Making backup %s, please wait ...", backupName));
- Map<World, Boolean> oldWorldsSavingDisabled = new HashMap<>(); // old switch stat
-
- // Get server
- MinecraftServer server = context.getSource().getMinecraftServer();
-
- // Save old autosave switch stat temporally
- server.getWorlds().forEach(world -> {
- oldWorldsSavingDisabled.put(world, world.savingDisabled);
- world.savingDisabled = true;
- });
-
- // Force to save all player data and worlds
- PrintUtil.msgInfo(context, "Saving players ...");
- server.getPlayerManager().saveAllPlayerData();
- PrintUtil.msgInfo(context, "Saving worlds ...");
- server.save(true, true, true);
-
- // Start threaded worker
- BackupWorker worker = new BackupWorker(context, backupName, oldWorldsSavingDisabled, backupMetadata, System.currentTimeMillis());
- Thread workerThread = new Thread(worker, "BackupWorker");
- workerThread.start();
- }
-
- @Override
- public void run() {
- String backupSaveDirectory = "";
- MinecraftServer server = context.getSource().getMinecraftServer();
- try {
- //// Do our main backup logic
-
- // Create backup saving directory
- 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);
- String backupFileName = getBackupFileName(backupName);
- PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectoryFile.toString()));
- PrintUtil.info("Compressing level ...");
- 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));
-
- // Print finish message: time elapsed and file size
- long timeElapsedMillis = System.currentTimeMillis() - startTime;
- String msgText = String.format("Backup finished. Time elapsed: %.2fs.", timeElapsedMillis / 1000.0);
- try {
- msgText += String.format(" File size: %s.", humanFileSize(backupZipFile.length()));
- } catch (SecurityException ignored) {
- }
- PrintUtil.msgInfo(context, msgText, true);
-
- } catch (SecurityException e) {
- msgInfo(context, String.format("Failed to create backup saving directory: %s. Failed to backup.", backupSaveDirectory));
- } catch (IOException | ZipUtilException e) {
- msgInfo(context, "Failed to make zip: " + e.getMessage());
- }
- }
-}
diff --git a/src/main/java/com/keuin/kbackupfabric/worker/RestoreWorker.java b/src/main/java/com/keuin/kbackupfabric/worker/RestoreWorker.java
deleted file mode 100644
index 58560a1..0000000
--- a/src/main/java/com/keuin/kbackupfabric/worker/RestoreWorker.java
+++ /dev/null
@@ -1,95 +0,0 @@
-package com.keuin.kbackupfabric.worker;
-
-import com.keuin.kbackupfabric.util.PrintUtil;
-import com.keuin.kbackupfabric.util.ZipUtil;
-import com.keuin.kbackupfabric.util.ZipUtilException;
-import net.minecraft.server.MinecraftServer;
-
-import java.io.File;
-import java.io.IOException;
-
-import static org.apache.commons.io.FileUtils.forceDelete;
-
-/**
- * The restore worker
- * To invoke this worker, simply call invoke() method.
- */
-public final class RestoreWorker implements Runnable {
-
- //private static final Logger LOGGER = LogManager.getLogger();
-
- private final Thread serverThread;
- private final String backupFilePath;
- private final String levelDirectory;
-
- private RestoreWorker(Thread serverThread, String backupFilePath, String levelDirectory) {
- this.serverThread = serverThread;
- this.backupFilePath = backupFilePath;
- this.levelDirectory = levelDirectory;
- }
-
- public static void invoke(MinecraftServer server, String backupFilePath, String levelDirectory) {
- RestoreWorker worker = new RestoreWorker(server.getThread(), backupFilePath, levelDirectory);
- Thread workerThread = new Thread(worker, "RestoreWorker");
- workerThread.start();
- try {
- Thread.sleep(500);
- } catch (InterruptedException ignored) {
- }
- server.stop(false);
- }
-
- @Override
- public void run() {
- try {
- // Wait server thread die
- PrintUtil.info("Waiting for the server thread to exit ...");
- while (serverThread.isAlive()) {
- try {
- serverThread.join();
- } catch (InterruptedException ignored) {
- }
- }
-
- PrintUtil.info("Wait for 5 seconds ...");
- try {
- Thread.sleep(5000);
- } catch (InterruptedException ignored) {
- }
-
- // 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;
- }
-
- // Decompress archive
- PrintUtil.info("Decompressing archived level");
- ZipUtil.unzip(backupFilePath, levelDirectory, false);
- long endTime = System.currentTimeMillis();
- PrintUtil.info(String.format("Restore complete! (%.2fs) Please restart the server manually.", (endTime - startTime) / 1000.0));
- } catch (SecurityException | IOException | ZipUtilException e) {
- PrintUtil.error("An exception occurred while restoring: " + e.getMessage());
- }
- }
-}