mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
14.20.2021 5e39c0040ddde260831a5b9f73c0bbfec3738f94
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package de.micromata.borgbutler.config;
 
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import de.micromata.borgbutler.demo.DemoRepos;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
/**
 * Representation of ~/.borgbutler/borgbutler-config.json.
 */
public class Configuration {
    private Logger log = LoggerFactory.getLogger(Configuration.class);
    /**
     * Default dir name for restoring archives.
     */
    private static final String RESTORE_DIRNAME = "restore";
 
    @JsonIgnore
    private File workingDir;
    /**
     * The path of the borg command to use (optional).
     */
    private String borgCommand;
 
    /**
     * The borg version to install from github (optional).
     */
    private String borgVersion;
 
    /**
     * Default is 100 MB (approximately).
     */
    private int maxArchiveContentCacheCapacityMb = 100;
 
    private boolean showDemoRepos = true;
 
    /**
     * Default is restore inside BorgButler's home dir (~/.borgbutler/restore).
     */
    @JsonProperty("restoreDir")
    private String restoreDirPath;
    @JsonIgnore
    private File restoreHomeDir;
 
    @JsonProperty
    private List<BorgRepoConfig> repoConfigs = new ArrayList<>();
 
    public void add(BorgRepoConfig repoConfig) {
        synchronized (repoConfigs) {
            repoConfigs.add(repoConfig);
        }
    }
 
    public boolean remove(String idOrName) {
        if (idOrName == null) {
            return false;
        }
        synchronized (repoConfigs) {
            for (BorgRepoConfig repoConfig : getAllRepoConfigs()) {
                if (StringUtils.equals(idOrName, repoConfig.getRepo()) || StringUtils.equals(idOrName, repoConfig.getId())) {
                    repoConfigs.remove(repoConfig);
                    return true;
                }
            }
        }
        return false;
    }
 
    public BorgRepoConfig getRepoConfig(String idOrName) {
        if (idOrName == null) {
            return null;
        }
        for (BorgRepoConfig repoConfig : getAllRepoConfigs()) {
            if (StringUtils.equals(idOrName, repoConfig.getRepo()) || StringUtils.equals(idOrName, repoConfig.getId())) {
                return repoConfig;
            }
        }
        return null;
    }
 
    public File getRestoreHomeDir() {
        if (restoreHomeDir == null) {
            if (StringUtils.isNotBlank(restoreDirPath)) {
                restoreHomeDir = new File(restoreDirPath);
            } else {
                restoreHomeDir = new File(workingDir, RESTORE_DIRNAME);
            }
            if (!restoreHomeDir.exists()) {
                log.info("Creating dir '" + restoreHomeDir.getAbsolutePath() + "' for restoring backup files and directories.");
            }
        }
        return restoreHomeDir;
    }
 
    public void copyFrom(Configuration other) {
        this.borgCommand = other.borgCommand;
        this.maxArchiveContentCacheCapacityMb = other.maxArchiveContentCacheCapacityMb;
        this.showDemoRepos = other.showDemoRepos;
    }
 
    @JsonIgnore
    public List<BorgRepoConfig> getAllRepoConfigs() {
        return DemoRepos.getAllRepos(repoConfigs);
    }
 
    List<BorgRepoConfig> getRepoConfigs() {
        return repoConfigs;
    }
 
    /**
     * Optional borg command to use.
     */
    public String getBorgCommand() {
        return this.borgCommand;
    }
 
    public String getBorgVersion() {
        return borgVersion;
    }
 
    public int getMaxArchiveContentCacheCapacityMb() {
        return this.maxArchiveContentCacheCapacityMb;
    }
 
    public boolean isShowDemoRepos() {
        return this.showDemoRepos;
    }
 
    public String getRestoreDirPath() {
        return this.restoreDirPath;
    }
 
    Configuration setWorkingDir(File workingDir) {
        this.workingDir = workingDir;
        return this;
    }
 
    public Configuration setBorgCommand(String borgCommand) {
        this.borgCommand = borgCommand;
        return this;
    }
 
    public void setBorgVersion(String borgVersion) {
        this.borgVersion = borgVersion;
    }
 
    public Configuration setShowDemoRepos(boolean showDemoRepos) {
        this.showDemoRepos = showDemoRepos;
        return this;
    }
}