2016-05-12 19:40:39 +00:00
|
|
|
package me.totalfreedom.totalfreedommod;
|
|
|
|
|
2020-07-02 09:08:57 +00:00
|
|
|
import com.sk89q.worldedit.bukkit.BukkitConfiguration;
|
2016-05-12 19:40:39 +00:00
|
|
|
import java.io.File;
|
2020-08-01 04:10:44 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.banning.PermbanList;
|
2020-07-01 01:51:06 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.config.YamlConfig;
|
2020-08-01 04:10:44 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.permissions.PermissionConfig;
|
|
|
|
import me.totalfreedom.totalfreedommod.punishments.PunishmentList;
|
2016-05-12 19:40:39 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
|
|
|
import org.bukkit.util.FileUtil;
|
2020-07-02 09:08:57 +00:00
|
|
|
import org.yaml.snakeyaml.Yaml;
|
2016-05-12 19:40:39 +00:00
|
|
|
|
2020-07-01 01:51:06 +00:00
|
|
|
public class BackupManager extends FreedomService
|
2016-05-12 19:40:39 +00:00
|
|
|
{
|
2020-07-01 01:51:06 +00:00
|
|
|
@Override
|
|
|
|
public void onStart()
|
|
|
|
{
|
|
|
|
}
|
2016-05-12 19:40:39 +00:00
|
|
|
|
2020-07-01 01:51:06 +00:00
|
|
|
@Override
|
|
|
|
public void onStop()
|
2016-05-12 19:40:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void createBackups(String file)
|
|
|
|
{
|
|
|
|
createBackups(file, false);
|
|
|
|
}
|
|
|
|
|
2020-08-01 04:10:44 +00:00
|
|
|
public void createAllBackups()
|
|
|
|
{
|
|
|
|
createBackups(TotalFreedomMod.CONFIG_FILENAME, true);
|
|
|
|
createBackups(PermbanList.CONFIG_FILENAME);
|
|
|
|
createBackups(PermissionConfig.PERMISSIONS_FILENAME, true);
|
|
|
|
createBackups(PunishmentList.CONFIG_FILENAME);
|
|
|
|
createBackups("database.db");
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public void createBackups(String file, boolean onlyWeekly)
|
|
|
|
{
|
|
|
|
final String save = file.split("\\.")[0];
|
|
|
|
final YamlConfig config = new YamlConfig(plugin, "backup/backup.yml", false);
|
|
|
|
config.load();
|
|
|
|
|
|
|
|
// Weekly
|
|
|
|
if (!config.isInt(save + ".weekly"))
|
|
|
|
{
|
|
|
|
performBackup(file, "weekly");
|
|
|
|
config.set(save + ".weekly", FUtil.getUnixTime());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int lastBackupWeekly = config.getInt(save + ".weekly");
|
|
|
|
|
|
|
|
if (lastBackupWeekly + 3600 * 24 * 7 < FUtil.getUnixTime())
|
|
|
|
{
|
|
|
|
performBackup(file, "weekly");
|
|
|
|
config.set(save + ".weekly", FUtil.getUnixTime());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onlyWeekly)
|
|
|
|
{
|
|
|
|
config.save();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Daily
|
|
|
|
if (!config.isInt(save + ".daily"))
|
|
|
|
{
|
|
|
|
performBackup(file, "daily");
|
|
|
|
config.set(save + ".daily", FUtil.getUnixTime());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int lastBackupDaily = config.getInt(save + ".daily");
|
|
|
|
|
|
|
|
if (lastBackupDaily + 3600 * 24 < FUtil.getUnixTime())
|
|
|
|
{
|
|
|
|
performBackup(file, "daily");
|
|
|
|
config.set(save + ".daily", FUtil.getUnixTime());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void performBackup(String file, String type)
|
|
|
|
{
|
|
|
|
FLog.info("Backing up " + file + " to " + file + "." + type + ".bak");
|
|
|
|
final File backupFolder = new File(plugin.getDataFolder(), "backup");
|
|
|
|
|
|
|
|
if (!backupFolder.exists())
|
|
|
|
{
|
|
|
|
backupFolder.mkdirs();
|
|
|
|
}
|
|
|
|
|
|
|
|
final File oldYaml = new File(plugin.getDataFolder(), file);
|
|
|
|
final File newYaml = new File(backupFolder, file + "." + type + ".bak");
|
|
|
|
FileUtil.copy(oldYaml, newYaml);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|