summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java
blob: 465b293d5323e66c8bbdf041756a3c35c87b9e8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.keuin.kbackupfabric.operation;

import com.keuin.kbackupfabric.operation.abstracts.InvokableAsyncBlockingOperation;
import com.keuin.kbackupfabric.operation.backup.BackupMethod;
import com.keuin.kbackupfabric.util.PrintUtil;
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.PrintUtil.msgInfo;
import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.*;

public class BackupOperation extends InvokableAsyncBlockingOperation {

    private final CommandContext<ServerCommandSource> context;
    private final String backupName;
    private final Map<World, Boolean> oldWorldsSavingDisabled = new HashMap<>();
    private final BackupMethod backupMethod;
    private long startTime;


    public BackupOperation(CommandContext<ServerCommandSource> context, String backupName, BackupMethod backupMethod) {
        super("BackupWorker");
        this.context = context;
        this.backupName = backupName;
        this.backupMethod = backupMethod;
    }

    @Override
    protected void async() {
        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);

            BackupMethod.BackupResult result = backupMethod.backup(backupName,levelPath,backupSaveDirectory);
            if(result.isSuccess()) {
                // 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);
                File backupZipFile = new File(backupSaveDirectory, backupFileName);
                msgText += String.format(" File size: %s.", getFriendlyFileSizeString(result.getBackupSizeBytes()));
                PrintUtil.msgInfo(context, msgText, true);
            } else {
                // failed
                PrintUtil.msgErr(context, "Backup operation failed. No further information.");
            }
        } catch (SecurityException e) {
            msgInfo(context, String.format("Failed to create backup saving directory: %s. Failed to backup.", backupSaveDirectory));
        } catch (IOException e) {
            msgInfo(context, "Failed to make zip: " + e.getMessage());
        }
    }

    @Override
    protected boolean sync() {
        //// Save world, save old autosave configs

        PrintUtil.broadcast(String.format("Making backup %s, please wait ...", backupName));

        // Get server
        MinecraftServer server = context.getSource().getMinecraftServer();

        // Save old autosave switch stat temporally
        oldWorldsSavingDisabled.clear();
        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);

        // Log start time
        startTime = System.currentTimeMillis();
        return true;
    }
}