summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredIncrementalBackupMethod.java
blob: 0201d18f70b70e49d4200646bb5b0d16f21bd038 (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
package com.keuin.kbackupfabric.operation.backup.method;

import com.keuin.kbackupfabric.operation.backup.feedback.IncrementalBackupFeedback;
import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollection;
import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollectionFactory;
import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollectionSerializer;
import com.keuin.kbackupfabric.util.backup.incremental.identifier.Sha256Identifier;
import com.keuin.kbackupfabric.util.backup.incremental.manager.IncrementalBackupStorageManager;
import com.keuin.kbackupfabric.util.backup.name.IncrementalBackupFileNameEncoder;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.time.LocalDateTime;

public class ConfiguredIncrementalBackupMethod implements ConfiguredBackupMethod {

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

    public ConfiguredIncrementalBackupMethod(String backupFileName, String levelPath, String backupSavePath) {
        this.backupFileName = backupFileName;
        this.levelPath = levelPath;
        this.backupSavePath = backupSavePath;
    }

    @Override
    public IncrementalBackupFeedback backup() throws IOException {
        String customBackupName = new IncrementalBackupFileNameEncoder().decode(backupFileName).customName;
        String backupIndexFileName = new IncrementalBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now());
        File levelPathFile = new File(levelPath);

        // construct incremental backup index
        ObjectCollection collection = new ObjectCollectionFactory<>(Sha256Identifier.getFactory())
                .fromDirectory(levelPathFile);

        // update storage
        IncrementalBackupStorageManager storageManager = new IncrementalBackupStorageManager(Paths.get(backupSavePath));
        int filesAdded = storageManager.addObjectCollection(collection, levelPathFile);

        // save index file
        ObjectCollectionSerializer.toFile(collection, new File(backupSavePath, backupIndexFileName));

        return new IncrementalBackupFeedback(filesAdded >= 0, filesAdded);
    }

    @Override
    public boolean restore() throws IOException {
        return false;
    }
}