summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java
blob: ded514c32d9d6a813bde389f9868d877de50e399 (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
103
104
package com.keuin.kbackupfabric.operation.backup.method;

import com.keuin.kbackupfabric.backup.BackupFilesystemUtil;
import com.keuin.kbackupfabric.backup.BackupNameTimeFormatter;
import com.keuin.kbackupfabric.backup.name.PrimitiveBackupFileNameEncoder;
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 java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.logging.Logger;

public class ConfiguredPrimitiveBackupMethod implements ConfiguredBackupMethod {

    private final String backupFileName;
    private final String levelPath;
    private final String backupSavePath;

    private final Logger LOGGER = Logger.getLogger(ConfiguredPrimitiveBackupMethod.class.getName());

    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() {

        PrimitiveBackupFeedback feedback;

        try {
            String customBackupName = PrimitiveBackupFileNameEncoder.INSTANCE.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);
            feedback = PrimitiveBackupFeedback.createSuccessFeedback(
                    FilesystemUtil.getFileSizeBytes(backupSavePath, backupFileName));
        } catch (ZipUtilException exception) {
            String msg = "Infinite recursive of directory tree detected, backup was aborted.";
            PrintUtil.info(msg);
            feedback = PrimitiveBackupFeedback.createFailFeedback(msg);
        } catch (IOException e) {
            feedback = PrimitiveBackupFeedback.createFailFeedback(e.getMessage());
        }

        if (!feedback.isSuccess()) {
            // do clean-up if failed
            File backupFile = new File(backupSavePath, backupFileName);
            if (backupFile.exists()) {
                LOGGER.info(String.format("Deleting incomplete backup file \"%s\"...", backupFile.getPath()));
                try {
                    Files.delete(backupFile.toPath());
                    LOGGER.info("Failed to backup, all files are cleaned up.");
                } catch (IOException e) {
                    LOGGER.warning("Cannot delete: " + e.getMessage());
                }
            }
        }
        return feedback;
    }

    @Override
    public boolean restore() throws IOException {
        // Delete old level
        PrintUtil.info("Server stopped. Deleting old level ...");
        if (!FilesystemUtil.forceDeleteDirectory(new File(levelPath))) {
            PrintUtil.info("Failed to delete old level!");
            return false;
        }

        // Decompress archive
        PrintUtil.info("Decompressing archived level ...");
        ZipUtil.unzip(Paths.get(backupSavePath, backupFileName).toString(), levelPath, false);

        return true;
    }

    @Override
    public boolean touch() {
        File backupSaveDirectoryFile = new File(backupSavePath);
        return backupSaveDirectoryFile.isDirectory() || backupSaveDirectoryFile.mkdir();
    }

    @Override
    public String getBackupFileName() {
        return backupFileName;
    }

}