From a7aa012fa4c392754d8e88d9e1057d18c1d40769 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 20 Dec 2022 01:06:40 +0100 Subject: [PATCH] Backup manager update : improved code handling file filtering + refactored PaperBackupManager + new Bungee backup manager --- pandalib-bungee/pom.xml | 6 ++ .../lib/bungee/backup/BungeeBackupConfig.java | 16 +++++ .../bungee/backup/BungeeBackupManager.java | 46 +++++++++++++ .../bungee/backup/BungeeWorkdirProcess.java | 69 +++++++++++++++++++ .../lib/core/backup/BackupProcess.java | 24 ++++++- .../core/backup/RotatedLogsBackupProcess.java | 5 +- ...ckupConfig.java => PaperBackupConfig.java} | 2 +- .../lib/paper/backup/PaperBackupManager.java | 61 ++++++++-------- .../lib/paper/backup/PaperWorkdirProcess.java | 30 ++------ .../lib/paper/backup/PaperWorldProcess.java | 5 -- 10 files changed, 200 insertions(+), 64 deletions(-) create mode 100644 pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeBackupConfig.java create mode 100644 pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeBackupManager.java create mode 100644 pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeWorkdirProcess.java rename pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/{BackupConfig.java => PaperBackupConfig.java} (95%) diff --git a/pandalib-bungee/pom.xml b/pandalib-bungee/pom.xml index f6f8ae1..a090412 100644 --- a/pandalib-bungee/pom.xml +++ b/pandalib-bungee/pom.xml @@ -27,6 +27,12 @@ ${bungeecord.version} provided + + + fr.pandacube.lib + pandalib-core + ${project.version} + \ No newline at end of file diff --git a/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeBackupConfig.java b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeBackupConfig.java new file mode 100644 index 0000000..ef108e0 --- /dev/null +++ b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeBackupConfig.java @@ -0,0 +1,16 @@ +package fr.pandacube.lib.bungee.backup; + +import fr.pandacube.lib.core.backup.BackupCleaner; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +public class BungeeBackupConfig { + public boolean workdirBackupEnabled = true; + public boolean logsBackupEnabled = true; + public String scheduling = "0 2 * * *"; // cron format, here is everyday at 2am + public File backupDirectory = null; + public BackupCleaner workdirBackupCleaner = BackupCleaner.KEEPING_1_EVERY_N_MONTH(3).merge(BackupCleaner.KEEPING_N_LAST(5)); + public List workdirIgnoreList = new ArrayList<>(); +} diff --git a/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeBackupManager.java b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeBackupManager.java new file mode 100644 index 0000000..2108764 --- /dev/null +++ b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeBackupManager.java @@ -0,0 +1,46 @@ +package fr.pandacube.lib.bungee.backup; + +import fr.pandacube.lib.core.backup.BackupManager; +import fr.pandacube.lib.core.backup.BackupProcess; +import fr.pandacube.lib.core.backup.RotatedLogsBackupProcess; + +import java.io.File; + +public class BungeeBackupManager extends BackupManager { + + BungeeBackupConfig config; + + public BungeeBackupManager(BungeeBackupConfig config) { + super(config.backupDirectory); + setConfig(config); + + addProcess(new BungeeWorkdirProcess(this)); + addProcess(new RotatedLogsBackupProcess(this, false, new File("logs"), "[0-9]{4}-[0-9]{2}-[0-9]{2}(-[0-9]+)?\\.log\\.gz")); + } + + @Override + protected void addProcess(BackupProcess process) { + updateProcessConfig(process); + super.addProcess(process); + } + + public void setConfig(BungeeBackupConfig config) { + this.config = config; + backupQueue.forEach(this::updateProcessConfig); + } + + + public void updateProcessConfig(BackupProcess process) { + if (process instanceof BungeeWorkdirProcess) { + process.setEnabled(config.workdirBackupEnabled); + process.setBackupCleaner(config.workdirBackupCleaner); + process.setScheduling(config.scheduling); + process.setIgnoreList(config.workdirIgnoreList); + } + else if (process instanceof RotatedLogsBackupProcess) { + process.setEnabled(config.logsBackupEnabled); + } + } + + +} diff --git a/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeWorkdirProcess.java b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeWorkdirProcess.java new file mode 100644 index 0000000..9ea648c --- /dev/null +++ b/pandalib-bungee/src/main/java/fr/pandacube/lib/bungee/backup/BungeeWorkdirProcess.java @@ -0,0 +1,69 @@ +package fr.pandacube.lib.bungee.backup; + +import fr.pandacube.lib.core.backup.BackupProcess; +import fr.pandacube.lib.util.Log; +import net.md_5.bungee.api.ChatColor; + +import java.io.File; +import java.text.DateFormat; +import java.util.Date; +import java.util.function.BiPredicate; + +public class BungeeWorkdirProcess extends BackupProcess { + + protected BungeeWorkdirProcess(BungeeBackupManager bm) { + super(bm, "workdir"); + } + + @Override + public BungeeBackupManager getBackupManager() { + return (BungeeBackupManager) super.getBackupManager(); + } + + + public BiPredicate getFilenameFilter() { + return new BiPredicate() { + @Override + public boolean test(File file, String path) { + if (new File(getSourceDir(), "logs").equals(file)) + return false; + if (file.isFile() && file.getName().endsWith(".lck")) + return false; + return BungeeWorkdirProcess.super.getFilenameFilter().test(file, path); + } + }; + } + + + + @Override + public File getSourceDir() { + return new File("."); + } + + @Override + protected void onBackupStart() { } + + @Override + protected void onBackupEnd(boolean success) { + if (success) + setDirtySinceNow(); + } + + @Override + protected File getTargetDir() { + return new File(getBackupManager().getBackupDirectory(), "workdir"); + } + + @Override + protected String getDisplayName() { + return "workdir"; + } + + + + public void displayNextSchedule() { + Log.info("[Backup] " + ChatColor.GRAY + getDisplayName() + ChatColor.RESET + " next backup on " + + DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(new Date(getNext()))); + } +} diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/backup/BackupProcess.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/backup/BackupProcess.java index b1bb865..c56ea5e 100644 --- a/pandalib-core/src/main/java/fr/pandacube/lib/core/backup/BackupProcess.java +++ b/pandalib-core/src/main/java/fr/pandacube/lib/core/backup/BackupProcess.java @@ -59,7 +59,22 @@ public abstract class BackupProcess implements Comparable, Runnab - public abstract BiPredicate getFilenameFilter(); + public BiPredicate getFilenameFilter() { + return (file, path) -> { + for (String exclude : ignoreList) { + if (exclude.startsWith("/")) { // relative to source of workdir + if (path.matches(exclude.substring(1))) + return false; + } + else { + String name = path.substring(path.lastIndexOf("/") + 1); + if (name.matches(exclude)) + return false; + } + } + return true; + }; + } public abstract File getSourceDir(); @@ -98,6 +113,13 @@ public abstract class BackupProcess implements Comparable, Runnab this.backupCleaner = backupCleaner; } + public List getIgnoreList() { + return ignoreList; + } + + public void setIgnoreList(List ignoreList) { + this.ignoreList = ignoreList; + } diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/backup/RotatedLogsBackupProcess.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/backup/RotatedLogsBackupProcess.java index c4d4ac0..a2989db 100644 --- a/pandalib-core/src/main/java/fr/pandacube/lib/core/backup/RotatedLogsBackupProcess.java +++ b/pandalib-core/src/main/java/fr/pandacube/lib/core/backup/RotatedLogsBackupProcess.java @@ -27,7 +27,7 @@ public class RotatedLogsBackupProcess extends BackupProcess { @Override public void run() { - // do not call super. We override the zip archive process, we just want to copy log files, here + // do not call super. We override the zip archive process, we just want to copy already-zipped log files. if (inNewThread) { new Thread(this::actuallyRun, "Backup Thread " + identifier).start(); } @@ -40,6 +40,9 @@ public class RotatedLogsBackupProcess extends BackupProcess { private void actuallyRun() { + if (!getSourceDir().isDirectory()) + return; + Log.info("[Backup] Starting for " + ChatColor.GRAY + getDisplayName() + ChatColor.RESET + " ..."); try { diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/BackupConfig.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperBackupConfig.java similarity index 95% rename from pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/BackupConfig.java rename to pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperBackupConfig.java index c24bc46..5912444 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/BackupConfig.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperBackupConfig.java @@ -6,7 +6,7 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -public class BackupConfig { +public class PaperBackupConfig { public boolean worldBackupEnabled = true; public boolean workdirBackupEnabled = true; public boolean logsBackupEnabled = true; diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperBackupManager.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperBackupManager.java index a2c6c97..354bc62 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperBackupManager.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperBackupManager.java @@ -26,41 +26,48 @@ public class PaperBackupManager extends BackupManager implements Listener { private final Map compressWorlds = new HashMap<>(); - BackupConfig config; + PaperBackupConfig config; - public PaperBackupManager(BackupConfig config) { + public PaperBackupManager(PaperBackupConfig config) { super(config.backupDirectory); setConfig(config); - + for (final World world : Bukkit.getWorlds()) { initWorldProcess(world.getName()); } - - initWorkdirProcess(); - + addProcess(new PaperWorkdirProcess(this)); addProcess(new RotatedLogsBackupProcess(this, true, new File("logs"), "[0-9]{4}-[0-9]{2}-[0-9]{2}(-[0-9]+)?\\.log\\.gz")); Bukkit.getServer().getPluginManager().registerEvents(this, PandaLibPaper.getPlugin()); - } - public void setConfig(BackupConfig config) { + @Override + protected void addProcess(BackupProcess process) { + updateProcessConfig(process); + super.addProcess(process); + } + + public void setConfig(PaperBackupConfig config) { this.config = config; - for (BackupProcess process : backupQueue) { - if (process instanceof PaperWorkdirProcess) { - process.setEnabled(config.workdirBackupEnabled); - process.setBackupCleaner(config.workdirBackupCleaner); - process.setScheduling(config.scheduling); - } - else if (process instanceof PaperWorldProcess) { - process.setEnabled(config.worldBackupEnabled); - process.setBackupCleaner(config.worldBackupCleaner); - process.setScheduling(config.scheduling); - } - else if (process instanceof RotatedLogsBackupProcess) { - process.setEnabled(config.logsBackupEnabled); - } + backupQueue.forEach(this::updateProcessConfig); + } + + + public void updateProcessConfig(BackupProcess process) { + if (process instanceof PaperWorkdirProcess) { + process.setEnabled(config.workdirBackupEnabled); + process.setBackupCleaner(config.workdirBackupCleaner); + process.setScheduling(config.scheduling); + process.setIgnoreList(config.workdirIgnoreList); + } + else if (process instanceof PaperWorldProcess) { + process.setEnabled(config.worldBackupEnabled); + process.setBackupCleaner(config.worldBackupCleaner); + process.setScheduling(config.scheduling); + } + else if (process instanceof RotatedLogsBackupProcess) { + process.setEnabled(config.logsBackupEnabled); } } @@ -90,20 +97,10 @@ public class PaperBackupManager extends BackupManager implements Listener { if (compressWorlds.containsKey(worldName)) return; PaperWorldProcess process = new PaperWorldProcess(this, worldName); - process.setEnabled(config.worldBackupEnabled); - process.setBackupCleaner(config.worldBackupCleaner); - process.setScheduling(config.scheduling); addProcess(process); compressWorlds.put(worldName, process); } - private void initWorkdirProcess() { - PaperWorkdirProcess process = new PaperWorkdirProcess(this); - process.setEnabled(config.workdirBackupEnabled); - process.setBackupCleaner(config.workdirBackupCleaner); - process.setScheduling(config.scheduling); - addProcess(process); - } diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperWorkdirProcess.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperWorkdirProcess.java index 4454cb8..5fe07c8 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperWorkdirProcess.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperWorkdirProcess.java @@ -12,37 +12,19 @@ public class PaperWorkdirProcess extends PaperBackupProcess { protected PaperWorkdirProcess(PaperBackupManager bm) { super(bm, "workdir"); } - - + + public BiPredicate getFilenameFilter() { return new BiPredicate() { - @Override public boolean test(File file, String path) { - if (globalExcluded(file, path)) - return false; - for (String exclude : getBackupManager().config.workdirIgnoreList) { - if (exclude.startsWith("/")) { // relative to source of workdir - if (path.matches(exclude.substring(1))) - return false; - } - else { - String name = path.substring(path.lastIndexOf("/") + 1); - if (name.matches(exclude)) - return false; - } - } - return true; - } - - public boolean globalExcluded(File file, String path) { if (file.isDirectory() && new File(file, "level.dat").exists()) - return true; + return false; if (new File(getSourceDir(), "logs").equals(file)) - return true; + return false; if (file.isFile() && file.getName().endsWith(".lck")) - return true; - return false; + return false; + return PaperWorkdirProcess.super.getFilenameFilter().test(file, path); } }; } diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperWorldProcess.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperWorldProcess.java index e4d3a2d..724f59c 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperWorldProcess.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/backup/PaperWorldProcess.java @@ -27,11 +27,6 @@ public class PaperWorldProcess extends PaperBackupProcess { } - public BiPredicate getFilenameFilter() { - return (f, s) -> true; - } - - @Override public File getSourceDir() { return WorldUtil.worldDir(worldName);