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!
#Wed Aug 27 00:27:30 CEST 2014
build.number=939
#Wed Aug 27 10:56:44 CEST 2014
build.number=943

View File

@ -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<TFM_ConfigEntry, Object> 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

View File

@ -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
{

View File

@ -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();

View File

@ -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))

View File

@ -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())

View File

@ -700,7 +700,7 @@ public class TFM_Util
{
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())
{
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);
}

View File

@ -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();