From 0c99862793b1ec34444a9716c1fb2b6f0e9930f0 Mon Sep 17 00:00:00 2001 From: JeromSar Date: Wed, 27 Aug 2014 10:58:26 +0200 Subject: [PATCH] Perform backups in a seperate folder Perform weekly backups on config.yml Minor refractoring --- buildnumber.properties | 4 +- .../Config/TFM_MainConfig.java | 8 +-- .../HTTPD/Module_permbans.java | 4 +- .../TotalFreedomMod/TFM_AdminList.java | 8 +-- .../TotalFreedomMod/TFM_PermbanList.java | 2 +- .../TotalFreedomMod/TFM_ProtectedArea.java | 4 +- .../TotalFreedomMod/TFM_Util.java | 60 ++++++++++++------- .../TotalFreedomMod/TotalFreedomMod.java | 14 +++-- 8 files changed, 61 insertions(+), 43 deletions(-) diff --git a/buildnumber.properties b/buildnumber.properties index 8719d4f8..71f709dc 100644 --- a/buildnumber.properties +++ b/buildnumber.properties @@ -1,3 +1,3 @@ #Build Number for ANT. Do not edit! -#Wed Aug 27 00:27:30 CEST 2014 -build.number=939 +#Wed Aug 27 10:56:44 CEST 2014 +build.number=943 diff --git a/src/me/StevenLawson/TotalFreedomMod/Config/TFM_MainConfig.java b/src/me/StevenLawson/TotalFreedomMod/Config/TFM_MainConfig.java index 6f3c3399..480beda5 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Config/TFM_MainConfig.java +++ b/src/me/StevenLawson/TotalFreedomMod/Config/TFM_MainConfig.java @@ -1,6 +1,5 @@ package me.StevenLawson.TotalFreedomMod.Config; -import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -16,8 +15,7 @@ import org.bukkit.configuration.file.YamlConfiguration; public class TFM_MainConfig { - public static final String CONFIG_FILENAME = "config.yml"; - public static final File CONFIG_FILE = new File(TotalFreedomMod.plugin.getDataFolder(), CONFIG_FILENAME); + public static final File CONFIG_FILE = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.CONFIG_FILENAME); // private static final EnumMap ENTRY_MAP; @@ -56,7 +54,7 @@ public class TFM_MainConfig throw new AssertionError(); } - public static final void load() + public static void load() { try { @@ -261,7 +259,7 @@ public class TFM_MainConfig private static InputStream getDefaultConfig() { - return TotalFreedomMod.plugin.getResource(CONFIG_FILENAME); + return TotalFreedomMod.plugin.getResource(TotalFreedomMod.CONFIG_FILENAME); } private static class TFM_Config_DefaultsLoader diff --git a/src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_permbans.java b/src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_permbans.java index 658f08f8..54fd311d 100644 --- a/src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_permbans.java +++ b/src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_permbans.java @@ -13,10 +13,10 @@ public class Module_permbans extends TFM_HTTPD_Module @Override public NanoHTTPD.Response getResponse() { - File permbanFile = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.PERMBAN_FILE); + File permbanFile = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.PERMBAN_FILENAME); if (permbanFile.exists()) { - return TFM_HTTPD_Manager.serveFileBasic(new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.PERMBAN_FILE)); + return TFM_HTTPD_Manager.serveFileBasic(new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.PERMBAN_FILENAME)); } else { diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_AdminList.java b/src/me/StevenLawson/TotalFreedomMod/TFM_AdminList.java index fa06288e..162d95c3 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_AdminList.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_AdminList.java @@ -139,7 +139,7 @@ public class TFM_AdminList adminList.remove(oldUuid); adminList.put(newUuid, newAdmin); - final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, TotalFreedomMod.SUPERADMIN_FILE, true); + final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, TotalFreedomMod.SUPERADMIN_FILENAME, true); config.load(); config.set("admins." + oldUuid.toString(), null); @@ -152,7 +152,7 @@ public class TFM_AdminList { adminList.clear(); - final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, TotalFreedomMod.SUPERADMIN_FILE, true); + final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, TotalFreedomMod.SUPERADMIN_FILENAME, true); config.load(); cleanThreshold = config.getInt("clean_threshold_hours", cleanThreshold); @@ -274,7 +274,7 @@ public class TFM_AdminList public static void saveAll() { - final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, TotalFreedomMod.SUPERADMIN_FILE, true); + final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, TotalFreedomMod.SUPERADMIN_FILENAME, true); config.load(); config.set("clean_threshold_hours", cleanThreshold); @@ -308,7 +308,7 @@ public class TFM_AdminList return; } - final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, TotalFreedomMod.SUPERADMIN_FILE, true); + final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, TotalFreedomMod.SUPERADMIN_FILENAME, true); config.load(); final UUID uuid = admin.getUniqueId(); diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_PermbanList.java b/src/me/StevenLawson/TotalFreedomMod/TFM_PermbanList.java index b714015c..d3cf5bfd 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_PermbanList.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_PermbanList.java @@ -38,7 +38,7 @@ public class TFM_PermbanList PERMBANNED_PLAYERS.clear(); PERMBANNED_IPS.clear(); - final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, TotalFreedomMod.PERMBAN_FILE, true); + final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, TotalFreedomMod.PERMBAN_FILENAME, true); config.load(); for (String playername : config.getKeys(false)) diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_ProtectedArea.java b/src/me/StevenLawson/TotalFreedomMod/TFM_ProtectedArea.java index 42922c4e..0633c261 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_ProtectedArea.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_ProtectedArea.java @@ -216,7 +216,7 @@ public class TFM_ProtectedArea { try { - FileOutputStream fos = new FileOutputStream(new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.PROTECTED_AREA_FILE)); + FileOutputStream fos = new FileOutputStream(new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.PROTECTED_AREA_FILENAME)); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(TFM_ProtectedArea.PROTECTED_AREAS); oos.close(); @@ -231,7 +231,7 @@ public class TFM_ProtectedArea @SuppressWarnings("unchecked") public static void loadProtectedAreas() { - File input = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.PROTECTED_AREA_FILE); + File input = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.PROTECTED_AREA_FILENAME); try { if (input.exists()) diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java b/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java index f86a7d52..8facbf22 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java @@ -700,7 +700,7 @@ public class TFM_Util { Map flags = null; - File input = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.SAVED_FLAGS_FILE); + File input = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.SAVED_FLAGS_FILENAME); if (input.exists()) { try @@ -757,7 +757,7 @@ public class TFM_Util try { - final FileOutputStream fos = new FileOutputStream(new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.SAVED_FLAGS_FILE)); + final FileOutputStream fos = new FileOutputStream(new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.SAVED_FLAGS_FILENAME)); final ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(flags); oos.close(); @@ -770,28 +770,16 @@ public class TFM_Util } public static void createBackups(String file) + { + createBackups(file, false); + } + + public static void createBackups(String file, boolean onlyWeekly) { final String save = file.split("\\.")[0]; - final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, "backup.yml", false); + final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, "backup/backup.yml", false); config.load(); - // Daily - if (!config.isInt(save + ".daily")) - { - performBackup(file, "daily"); - config.set(save + ".daily", TFM_Util.getUnixTime()); - } - else - { - int lastBackupDaily = config.getInt(save + ".daily"); - - if (lastBackupDaily + 3600 * 24 < TFM_Util.getUnixTime()) - { - performBackup(file, "daily"); - config.set(save + ".daily", TFM_Util.getUnixTime()); - } - } - // Weekly if (!config.isInt(save + ".weekly")) { @@ -809,14 +797,44 @@ public class TFM_Util } } + if (onlyWeekly) + { + config.save(); + return; + } + + // Daily + if (!config.isInt(save + ".daily")) + { + performBackup(file, "daily"); + config.set(save + ".daily", TFM_Util.getUnixTime()); + } + else + { + int lastBackupDaily = config.getInt(save + ".daily"); + + if (lastBackupDaily + 3600 * 24 < TFM_Util.getUnixTime()) + { + performBackup(file, "daily"); + config.set(save + ".daily", TFM_Util.getUnixTime()); + } + } + config.save(); } private static void performBackup(String file, String type) { TFM_Log.info("Backing up " + file + " to " + file + "." + type + ".bak"); + final File backupFolder = new File(TotalFreedomMod.plugin.getDataFolder(), "backup"); + + if (!backupFolder.exists()) + { + backupFolder.mkdirs(); + } + final File oldYaml = new File(TotalFreedomMod.plugin.getDataFolder(), file); - final File newYaml = new File(TotalFreedomMod.plugin.getDataFolder(), file + "." + type + ".bak"); + final File newYaml = new File(backupFolder, file + "." + type + ".bak"); FileUtil.copy(oldYaml, newYaml); } diff --git a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java index e2135c1a..5b2b6ca2 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java +++ b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java @@ -30,10 +30,11 @@ public class TotalFreedomMod extends JavaPlugin public static final long HEARTBEAT_RATE = 5L; // Seconds public static final long SERVICE_CHECKER_RATE = 120L; // - public static final String SUPERADMIN_FILE = "superadmin.yml"; - public static final String PERMBAN_FILE = "permban.yml"; - public static final String PROTECTED_AREA_FILE = "protectedareas.dat"; - public static final String SAVED_FLAGS_FILE = "savedflags.dat"; + public static final String CONFIG_FILENAME = "config.yml"; + public static final String SUPERADMIN_FILENAME = "superadmin.yml"; + public static final String PERMBAN_FILENAME = "permban.yml"; + public static final String PROTECTED_AREA_FILENAME = "protectedareas.dat"; + public static final String SAVED_FLAGS_FILENAME = "savedflags.dat"; // public static String buildNumber = "1"; public static String buildDate = TotalFreedomMod.buildDate = TFM_Util.dateToString(new Date()); @@ -84,8 +85,9 @@ public class TotalFreedomMod extends JavaPlugin TFM_Util.deleteFolder(new File("./_deleteme")); // Create backups - TFM_Util.createBackups(SUPERADMIN_FILE); - TFM_Util.createBackups(PERMBAN_FILE); + TFM_Util.createBackups(CONFIG_FILENAME, true); + TFM_Util.createBackups(SUPERADMIN_FILENAME); + TFM_Util.createBackups(PERMBAN_FILENAME); // Load services TFM_AdminList.load();