summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java
diff options
context:
space:
mode:
authorKeuin <[email protected]>2021-01-21 01:50:08 +0800
committerKeuin <[email protected]>2021-01-21 01:50:08 +0800
commitac3b5e1476dedcefb723f19bd0fdd9a22fcb16e9 (patch)
tree75d0994f3f7aa37c3d240933f0f2d179d318e597 /src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java
parent82e3986045ac7eaca6aaa290fb2283fd6c6c901a (diff)
parent7a5297de3467b1069fdf5e4a1b2aaf510ca35663 (diff)
Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java58
1 files changed, 32 insertions, 26 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java
index 22397a1..b870746 100644
--- a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java
+++ b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java
@@ -1,47 +1,36 @@
package com.keuin.kbackupfabric.operation;
-import com.keuin.kbackupfabric.exception.ZipUtilException;
import com.keuin.kbackupfabric.operation.abstracts.InvokableBlockingOperation;
+import com.keuin.kbackupfabric.operation.backup.method.ConfiguredBackupMethod;
import com.keuin.kbackupfabric.util.PrintUtil;
-import com.keuin.kbackupfabric.util.ZipUtil;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
-import java.io.File;
import java.io.IOException;
-
-import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupFileName;
-import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupSaveDirectory;
-import static org.apache.commons.io.FileUtils.forceDelete;
+import java.util.Objects;
public class RestoreOperation extends InvokableBlockingOperation {
//private static final Logger LOGGER = LogManager.getLogger();
- private final String backupName;
private final Thread serverThread;
- private final String backupFilePath;
- private final String levelDirectory;
private final CommandContext<ServerCommandSource> context;
private final MinecraftServer server;
+ private final ConfiguredBackupMethod configuredBackupMethod;
- public RestoreOperation(CommandContext<ServerCommandSource> context, String backupFilePath, String levelDirectory, String backupName) {
- server = context.getSource().getMinecraftServer();
- this.backupName = backupName;
- this.serverThread = server.getThread();
- this.backupFilePath = backupFilePath;
- this.levelDirectory = levelDirectory;
- this.context = context;
+ public RestoreOperation(CommandContext<ServerCommandSource> context, ConfiguredBackupMethod configuredBackupMethod) {
+ server = Objects.requireNonNull(context.getSource().getMinecraftServer());
+ this.serverThread = Objects.requireNonNull(server.getThread());
+ this.context = Objects.requireNonNull(context);
+ this.configuredBackupMethod = Objects.requireNonNull(configuredBackupMethod);
}
@Override
protected boolean blockingContext() {
// do restore to backupName
- PrintUtil.broadcast(String.format("Restoring to previous world %s ...", backupName));
+ PrintUtil.broadcast(String.format("Restoring to backup %s ...", configuredBackupMethod.getBackupFileName()));
- String backupFileName = getBackupFileName(backupName);
- PrintUtil.debug("Backup file name: " + backupFileName);
- File backupFile = new File(getBackupSaveDirectory(server), backupFileName);
+ PrintUtil.debug("Backup file name: " + configuredBackupMethod.getBackupFileName());
PrintUtil.msgInfo(context, "Server will shutdown in a few seconds, depending on world size and disk speed, the progress may take from seconds to minutes.", true);
PrintUtil.msgInfo(context, "Please do not force the server stop, or the level would be broken.", true);
@@ -67,7 +56,7 @@ public class RestoreOperation extends InvokableBlockingOperation {
@Override
public String toString() {
- return String.format("restoration from %s", backupName);
+ return String.format("restoration from %s", configuredBackupMethod.getBackupFileName());
}
private class WorkerThread implements Runnable {
@@ -80,7 +69,7 @@ public class RestoreOperation extends InvokableBlockingOperation {
while (serverThread.isAlive()) {
try {
serverThread.join();
- } catch (InterruptedException ignored) {
+ } catch (InterruptedException | RuntimeException ignored) {
}
}
@@ -94,13 +83,30 @@ public class RestoreOperation extends InvokableBlockingOperation {
}while(--cnt > 0);
////////////////////
-
- //ServerRestartUtil.forkAndRestart();
- System.exit(111);
+ long startTime = System.currentTimeMillis();
+ if (configuredBackupMethod.restore()) {
+ 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");
+ //ServerRestartUtil.forkAndRestart();
+ System.exit(111);
+ } else {
+ PrintUtil.error("Failed to restore! server will not restart automatically.");
+ }
} catch (SecurityException e) {
PrintUtil.error("An exception occurred while restoring: " + e.getMessage());
+ e.printStackTrace();
+ } catch (IOException e) {
+ PrintUtil.error(e.toString());
+ PrintUtil.error("Failed to restore due to an unhandled I/O exception.");
+ e.printStackTrace();
}
+ System.exit(0); // all failed restoration will eventually go here
}
}
}