Perform backups in a seperate folder

Perform weekly backups on config.yml
Minor refractoring
This commit is contained in:
JeromSar 2014-08-27 10:58:26 +02:00
parent 0240b65fad
commit 0c99862793
8 changed files with 61 additions and 43 deletions

View File

@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit! #Build Number for ANT. Do not edit!
#Wed Aug 27 00:27:30 CEST 2014 #Wed Aug 27 10:56:44 CEST 2014
build.number=939 build.number=943

View File

@ -1,6 +1,5 @@
package me.StevenLawson.TotalFreedomMod.Config; package me.StevenLawson.TotalFreedomMod.Config;
import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
@ -16,8 +15,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
public class TFM_MainConfig public class TFM_MainConfig
{ {
public static final String CONFIG_FILENAME = "config.yml"; public static final File CONFIG_FILE = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.CONFIG_FILENAME);
public static final File CONFIG_FILE = new File(TotalFreedomMod.plugin.getDataFolder(), CONFIG_FILENAME);
// //
private static final EnumMap<TFM_ConfigEntry, Object> ENTRY_MAP; private static final EnumMap<TFM_ConfigEntry, Object> ENTRY_MAP;
@ -56,7 +54,7 @@ public class TFM_MainConfig
throw new AssertionError(); throw new AssertionError();
} }
public static final void load() public static void load()
{ {
try try
{ {
@ -261,7 +259,7 @@ public class TFM_MainConfig
private static InputStream getDefaultConfig() private static InputStream getDefaultConfig()
{ {
return TotalFreedomMod.plugin.getResource(CONFIG_FILENAME); return TotalFreedomMod.plugin.getResource(TotalFreedomMod.CONFIG_FILENAME);
} }
private static class TFM_Config_DefaultsLoader private static class TFM_Config_DefaultsLoader

View File

@ -13,10 +13,10 @@ public class Module_permbans extends TFM_HTTPD_Module
@Override @Override
public NanoHTTPD.Response getResponse() 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()) 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 else
{ {

View File

@ -139,7 +139,7 @@ public class TFM_AdminList
adminList.remove(oldUuid); adminList.remove(oldUuid);
adminList.put(newUuid, newAdmin); 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.load();
config.set("admins." + oldUuid.toString(), null); config.set("admins." + oldUuid.toString(), null);
@ -152,7 +152,7 @@ public class TFM_AdminList
{ {
adminList.clear(); 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(); config.load();
cleanThreshold = config.getInt("clean_threshold_hours", cleanThreshold); cleanThreshold = config.getInt("clean_threshold_hours", cleanThreshold);
@ -274,7 +274,7 @@ public class TFM_AdminList
public static void saveAll() 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.load();
config.set("clean_threshold_hours", cleanThreshold); config.set("clean_threshold_hours", cleanThreshold);
@ -308,7 +308,7 @@ public class TFM_AdminList
return; 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(); config.load();
final UUID uuid = admin.getUniqueId(); final UUID uuid = admin.getUniqueId();

View File

@ -38,7 +38,7 @@ public class TFM_PermbanList
PERMBANNED_PLAYERS.clear(); PERMBANNED_PLAYERS.clear();
PERMBANNED_IPS.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(); config.load();
for (String playername : config.getKeys(false)) for (String playername : config.getKeys(false))

View File

@ -216,7 +216,7 @@ public class TFM_ProtectedArea
{ {
try 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); ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(TFM_ProtectedArea.PROTECTED_AREAS); oos.writeObject(TFM_ProtectedArea.PROTECTED_AREAS);
oos.close(); oos.close();
@ -231,7 +231,7 @@ public class TFM_ProtectedArea
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void loadProtectedAreas() 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 try
{ {
if (input.exists()) if (input.exists())

View File

@ -700,7 +700,7 @@ public class TFM_Util
{ {
Map<String, Boolean> flags = null; Map<String, Boolean> 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()) if (input.exists())
{ {
try try
@ -757,7 +757,7 @@ public class TFM_Util
try 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); final ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(flags); oos.writeObject(flags);
oos.close(); oos.close();
@ -770,28 +770,16 @@ public class TFM_Util
} }
public static void createBackups(String file) public static void createBackups(String file)
{
createBackups(file, false);
}
public static void createBackups(String file, boolean onlyWeekly)
{ {
final String save = file.split("\\.")[0]; 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(); 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 // Weekly
if (!config.isInt(save + ".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(); config.save();
} }
private static void performBackup(String file, String type) private static void performBackup(String file, String type)
{ {
TFM_Log.info("Backing up " + file + " to " + file + "." + type + ".bak"); 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 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); FileUtil.copy(oldYaml, newYaml);
} }

View File

@ -30,10 +30,11 @@ public class TotalFreedomMod extends JavaPlugin
public static final long HEARTBEAT_RATE = 5L; // Seconds public static final long HEARTBEAT_RATE = 5L; // Seconds
public static final long SERVICE_CHECKER_RATE = 120L; public static final long SERVICE_CHECKER_RATE = 120L;
// //
public static final String SUPERADMIN_FILE = "superadmin.yml"; public static final String CONFIG_FILENAME = "config.yml";
public static final String PERMBAN_FILE = "permban.yml"; public static final String SUPERADMIN_FILENAME = "superadmin.yml";
public static final String PROTECTED_AREA_FILE = "protectedareas.dat"; public static final String PERMBAN_FILENAME = "permban.yml";
public static final String SAVED_FLAGS_FILE = "savedflags.dat"; 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 buildNumber = "1";
public static String buildDate = TotalFreedomMod.buildDate = TFM_Util.dateToString(new Date()); 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")); TFM_Util.deleteFolder(new File("./_deleteme"));
// Create backups // Create backups
TFM_Util.createBackups(SUPERADMIN_FILE); TFM_Util.createBackups(CONFIG_FILENAME, true);
TFM_Util.createBackups(PERMBAN_FILE); TFM_Util.createBackups(SUPERADMIN_FILENAME);
TFM_Util.createBackups(PERMBAN_FILENAME);
// Load services // Load services
TFM_AdminList.load(); TFM_AdminList.load();