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

Kai Reinhard
19.43.2021 8667234fe392524078cbf26e7e57f45d70023458
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
package de.micromata.borgbutler.config
 
import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import de.micromata.borgbutler.config.BorgRepoConfig
import de.micromata.borgbutler.demo.DemoRepos
import mu.KotlinLogging
import org.apache.commons.lang3.StringUtils
import org.slf4j.LoggerFactory
import java.io.File
 
private val log = KotlinLogging.logger {}
 
/**
 * Representation of ~/.borgbutler/borgbutler-config.json.
 */
open class Configuration {
 
    @JsonIgnore
    private var workingDir: File? = null
    /**
     * Optional borg command to use.
     */
    /**
     * The path of the borg command to use (optional).
     */
    var borgCommand: String? = null
        private set
 
    /**
     * The borg version to install from github (optional).
     */
    // @JsonIgnore needed by client: ConfigurationserverTab.jsx fails otherwise (conflicting borgVersion fields).
    @JsonIgnore
    var borgVersion: String? = null
 
    /**
     * Default is 100 MB (approximately).
     */
    var maxArchiveContentCacheCapacityMb = 100
        private set
    var showDemoRepos = true
        private set
 
    /**
     * Default is restore inside BorgButler's home dir (~/.borgbutler/restore).
     */
    @JsonProperty("restoreDir")
    val restoreDirPath: String? = null
 
    @JsonIgnore
    private var restoreHomeDir: File? = null
 
    @JsonProperty
    private val repoConfigs = mutableListOf<BorgRepoConfig>()
 
    fun add(repoConfig: BorgRepoConfig) {
        synchronized(repoConfigs) { repoConfigs.add(repoConfig) }
    }
 
    fun remove(idOrName: String?): Boolean {
        if (idOrName == null) {
            return false
        }
        synchronized(repoConfigs) {
            for (repoConfig in allRepoConfigs) {
                if (StringUtils.equals(idOrName, repoConfig.repo) || StringUtils.equals(idOrName, repoConfig.id)) {
                    repoConfigs.remove(repoConfig)
                    return true
                }
            }
        }
        return false
    }
 
    fun getRepoConfig(idOrName: String?): BorgRepoConfig? {
        if (idOrName == null) {
            return null
        }
        for (repoConfig in allRepoConfigs) {
            if (StringUtils.equals(idOrName, repoConfig.repo) || StringUtils.equals(idOrName, repoConfig.id)) {
                return repoConfig
            }
        }
        return null
    }
 
    fun getRestoreHomeDir(): File {
        if (restoreHomeDir == null) {
            restoreDirPath?.let {
                restoreHomeDir = File(it)
            } ?: run {
                restoreHomeDir = File(workingDir, RESTORE_DIRNAME)
            }
            if (!restoreHomeDir!!.exists()) {
                log.info("Creating dir '" + restoreHomeDir?.absolutePath + "' for restoring backup files and directories.")
            }
        }
        return restoreHomeDir!!
    }
 
    fun copyFrom(other: Configuration) {
        borgCommand = other.borgCommand
        maxArchiveContentCacheCapacityMb = other.maxArchiveContentCacheCapacityMb
        showDemoRepos = other.showDemoRepos
    }
 
    @get:JsonIgnore
    val allRepoConfigs: List<BorgRepoConfig>
        get() = DemoRepos.getAllRepos(repoConfigs)
 
    fun getRepoConfigs(): List<BorgRepoConfig> {
        return repoConfigs
    }
 
    fun setWorkingDir(workingDir: File?): Configuration {
        this.workingDir = workingDir
        return this
    }
 
    fun setBorgCommand(borgCommand: String?): Configuration {
        this.borgCommand = borgCommand
        return this
    }
 
    companion object {
        /**
         * Default dir name for restoring archives.
         */
        private const val RESTORE_DIRNAME = "restore"
    }
}