summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMehtod.java
blob: 3c2a6084b4c077e87aaf90ace8de3e7e1fd12a6a (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
package com.keuin.kbackupfabric.operation.backup;

import com.keuin.kbackupfabric.exception.ZipUtilException;
import com.keuin.kbackupfabric.metadata.BackupMetadata;
import com.keuin.kbackupfabric.util.PrintUtil;
import com.keuin.kbackupfabric.util.ZipUtil;
import com.keuin.kbackupfabric.util.backup.builder.BackupFileNameBuilder;
import com.keuin.kbackupfabric.util.backup.formatter.BackupFileNameFormatter;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;

public class PrimitiveBackupMehtod implements BackupMethod {
    @Override
    public boolean backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException {
        try {
            String backupFileName = BackupFileNameBuilder.primitiveZipBackup().build(LocalDateTime.now(),backupName);
            BackupMetadata backupMetadata = new BackupMetadata(System.currentTimeMillis(), backupName);

            PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectory));
            PrintUtil.info("Compressing level ...");
            ZipUtil.makeBackupZip(levelPath, backupSaveDirectory, backupFileName, backupMetadata);

        } catch (ZipUtilException exception) {
            PrintUtil.info("Infinite recursive of directory tree detected, backup was aborted.");
            return false;
        }
        return true;
    }

    @Override
    public BackupFileNameBuilder getBackupFileNameBuilder() {
        return BackupFileNameBuilder.primitiveZipBackup();
    }

    @Override
    public BackupFileNameFormatter getBackupFileNameFormatter() {
        return BFNF;
    }
}