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

Kai Reinhard
08.46.2018 5db1733cdfdac831cc10ff734c61d68079c47d1f
Caching started...
1 files deleted
3 files added
3 files modified
248 ■■■■ changed files
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java 40 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/RepoInfoCache.java 86 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/config/Configuration.java 12 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/json/borg/RepoInfo.java 10 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/json/borg/Repository.java 28 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/test/java/de/micromata/borgbutler/BorgCommandsTest.java 31 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/test/java/de/micromata/borgbutler/cache/CacheTest.java 41 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java
New file
@@ -0,0 +1,40 @@
package de.micromata.borgbutler.cache;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
public class ButlerCache {
    private static Logger log = LoggerFactory.getLogger(ButlerCache.class);
    public static final String CACHE_DIR_NAME = ".borgbutler";
    private static ButlerCache instance = new ButlerCache();
    private RepoInfoCache reposCache;
    @JsonIgnore
    private File cacheDir;
    public static ButlerCache getInstance() {
        return instance;
    }
    public static RepoInfoCache getReposCache() {
        return instance.reposCache;
    }
    public void save() {
        reposCache.save();
    }
    private ButlerCache() {
        String homeDir = System.getProperty("user.home");
        cacheDir = new File(homeDir, CACHE_DIR_NAME);
        if (!cacheDir.exists()) {
            log.info("Creating cache dir: " + cacheDir.getAbsolutePath());
            cacheDir.mkdir();
        }
        reposCache = new RepoInfoCache(cacheDir);
    }
}
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/RepoInfoCache.java
New file
@@ -0,0 +1,86 @@
package de.micromata.borgbutler.cache;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import de.micromata.borgbutler.json.JsonUtils;
import de.micromata.borgbutler.json.borg.RepoInfo;
import de.micromata.borgbutler.json.borg.Repository;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class RepoInfoCache {
    private static Logger log = LoggerFactory.getLogger(RepoInfoCache.class);
    public static final String CACHE_REPOS_FILENAME = "cache-repos.json";
    @JsonIgnore
    private File cacheReposFile;
    @JsonProperty
    private List<RepoInfo> repositories = new ArrayList<>();
    public RepoInfo getRepoInfo(String idOrName) {
        if (idOrName == null) {
            return null;
        }
        for (RepoInfo repoInfo : repositories) {
            Repository repository = repoInfo.getRepository();
            if (repository == null) {
                continue;
            }
            if (idOrName.equals(repository.getId()) || idOrName.equals(repository.getName()) || idOrName.equals(repository.getLocation())) {
                return repoInfo;
            }
        }
        return null;
    }
    public void upsert(RepoInfo repoInfo) {
        Repository repository = repoInfo.getRepository();
        if (repository == null) {
            log.error("Oups, no repository given in RepoInfo (ignoring it): " + repoInfo);
            return;
        }
        RepoInfo existingRepo = getRepoInfo(repository.getId());
        if (existingRepo == null) {
            repositories.add(repoInfo);
        } else {
            existingRepo.updateFrom(repoInfo);
        }
    }
    public void read() {
        try {
            String json = FileUtils.readFileToString(cacheReposFile, Charset.forName("UTF-8"));
            RepoInfoCache readCache = JsonUtils.fromJson(this.getClass(), json);
            if (readCache != null) {
                this.repositories = readCache.repositories;
            } else {
                log.error("Error while parsing repos cache: " + cacheReposFile.getAbsolutePath());
            }
        } catch (IOException ex) {
            log.error("Error while trying to read repos cache file '" + cacheReposFile.getAbsolutePath() + "': "
                    + ex.getMessage(), ex);
        }
    }
    public void save() {
        log.info("Saving repo infos to cache file: " + cacheReposFile);
        String json = JsonUtils.toJson(this);
        try {
            FileUtils.write(cacheReposFile, json, Charset.forName("UTF-8"));
        } catch (IOException ex) {
            log.error("Error while trying to write repos cache file '" + cacheReposFile.getAbsolutePath() + "': "
                    + ex.getMessage(), ex);
        }
    }
    RepoInfoCache(File cacheDir) {
        cacheReposFile = new File(cacheDir, CACHE_REPOS_FILENAME);
    }
}
borgbutler-core/src/main/java/de/micromata/borgbutler/config/Configuration.java
@@ -14,4 +14,16 @@
    public void add(BorgRepoConfig repo) {
        repos.add(repo);
    }
    public BorgRepoConfig getRepo(String idOrName) {
        if (idOrName == null) {
            return null;
        }
        for (BorgRepoConfig repoConfig : repos) {
            if (idOrName.equals(repoConfig.getRepo()) ||idOrName.equals(repoConfig.getName())) {
                return repoConfig;
            }
        }
        return null;
    }
}
borgbutler-core/src/main/java/de/micromata/borgbutler/json/borg/RepoInfo.java
@@ -24,4 +24,14 @@
    public String toString() {
        return JsonUtils.toJson(this, true);
    }
    public void updateFrom(RepoInfo repoInfo) {
        this.securityDir = repoInfo.securityDir;
        this.cache = repoInfo.cache;
        this.encryption = repoInfo.encryption;
        this.repository = repoInfo.getRepository();
        this.originalJson = repoInfo.originalJson;
    }
}
borgbutler-core/src/main/java/de/micromata/borgbutler/json/borg/Repository.java
@@ -1,9 +1,21 @@
package de.micromata.borgbutler.json.borg;
import com.fasterxml.jackson.annotation.JsonProperty;
import de.micromata.borgbutler.config.BorgRepoConfig;
import de.micromata.borgbutler.config.ConfigurationHandler;
import lombok.Getter;
import lombok.Setter;
public class Repository {
    /**
     * A name describing this config. Only used for displaying purposes. This is automatically set with the name
     * of the repository configuration.
     *
     * @see BorgRepoConfig#getName()
     */
    @Getter
    @Setter
    String name;
    @Getter
    private String id;
    @Getter
@@ -11,4 +23,20 @@
    private String lastModified;
    @Getter
    private String location;
    /**
     * Sets also the name for this repository if available in the configuration.
     *
     * @param location
     * @return
     */
    public Repository setLocation(String location) {
        this.location = location;
        // It's ugly but efficiently ;-)
        BorgRepoConfig repoConfig = ConfigurationHandler.getConfiguration().getRepo(location);
        if (repoConfig != null) {
            this.name = repoConfig.getName();
        }
        return this;
    }
}
borgbutler-core/src/test/java/de/micromata/borgbutler/BorgCommandsTest.java
File was deleted
borgbutler-core/src/test/java/de/micromata/borgbutler/cache/CacheTest.java
New file
@@ -0,0 +1,41 @@
package de.micromata.borgbutler.cache;
import de.micromata.borgbutler.BorgCommands;
import de.micromata.borgbutler.config.BorgRepoConfig;
import de.micromata.borgbutler.config.Configuration;
import de.micromata.borgbutler.config.ConfigurationHandler;
import de.micromata.borgbutler.json.borg.RepoInfo;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class CacheTest {
    private static Logger log = LoggerFactory.getLogger(CacheTest.class);
    @Test
    void reposCachetest() {
        ConfigurationHandler configHandler = ConfigurationHandler.getInstance();
        configHandler.read();
        Configuration config = ConfigurationHandler.getConfiguration();
        if (config.getRepos().size() == 0) {
            log.info("No repos configured. Please configure repos first in: " + configHandler.getConfigFile().getAbsolutePath());
            return;
        }
        for (BorgRepoConfig repo : config.getRepos()) {
            log.info("Processing repo '" + repo + "'");
            RepoInfo repoInfo = BorgCommands.info(repo);
            log.info("Repo info: " + repoInfo);
            RepoInfoCache cache = ButlerCache.getReposCache();
            cache.upsert(repoInfo);
            repoInfo = cache.getRepoInfo(repoInfo.getRepository().getId());
            assertNotNull(repoInfo);
            cache.save();
            log.info("Repo info: " + repoInfo);
            //RepoList repoList = BorgCommands.list(repo);
            //log.info("Repo list: " + repoList);
        }
    }
}