Auto backups for MapParser. (Only tested on mac)

This commit is contained in:
Shaun Bennett 2014-10-15 15:04:44 -05:00
parent 58f69b49d7
commit 5cdd502d11
3 changed files with 120 additions and 5 deletions

View File

@ -0,0 +1,90 @@
package mineplex.mapparser;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.ZipUtil;
/**
* Created by shaun on 14-09-23.
*/
public class BackupTask implements Runnable
{
private final String _worldName;
private final Callback<Boolean> _callback;
private final JavaPlugin _plugin;
public BackupTask(JavaPlugin plugin, String worldName, Callback<Boolean> callback)
{
_plugin = plugin;
_worldName = worldName;
_callback = callback;
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, this);
}
@Override
public void run()
{
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
List<String> fileList = new ArrayList<String>();
List<String> folderList = new ArrayList<String>();
String dest = "backup" + _worldName.substring(3) + "/" + format.format(date) + ".zip";
File srcFile = new File(_worldName);
// Create backup folder if it doesnt already exist
File folder = new File(dest.substring(0, dest.lastIndexOf('/')));
if (!folder.exists())
folder.mkdirs();
// Get all files to backup
for (File file : srcFile.listFiles())
{
if (file.isFile())
fileList.add(_worldName + File.separator + file.getName());
else if (file.isDirectory())
folderList.add(_worldName + File.separator + file.getName());
}
// Delete old folders if more than 20 backups exist
if (folder.listFiles().length > 20)
{
File[] files = folder.listFiles();
File oldestFile = files[0];
for (int i = 1; i < files.length; i++)
{
File file = files[i];
if (file.getName().endsWith(".zip") && file.lastModified() < oldestFile.lastModified())
{
oldestFile = file;
}
}
System.out.println("Deleting oldest file: " + oldestFile.getName());
oldestFile.delete();
}
ZipUtil.ZipFolders(srcFile.getAbsolutePath(), dest, folderList, fileList);
if (_callback != null)
{
_plugin.getServer().getScheduler().runTask(_plugin, new Runnable()
{
@Override
public void run()
{
_callback.run(true);
}
});
}
}
}

View File

@ -3,6 +3,7 @@ package mineplex.mapparser;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
@ -30,19 +31,19 @@ import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.common.util.C;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.F;
import mineplex.core.common.util.MapUtil;
import mineplex.core.common.util.UtilInv;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.mapparser.command.AdminCommand;
import mineplex.mapparser.command.AuthorCommand;
import mineplex.mapparser.command.BaseCommand;
import mineplex.mapparser.command.AdminCommand;
import mineplex.mapparser.command.CopyCommand;
import mineplex.mapparser.command.CopySchematicsCommand;
import mineplex.mapparser.command.CreateCommand;
@ -63,6 +64,7 @@ public class MapParser extends JavaPlugin implements Listener
private Parse _curParse = null;
private HashMap<String, MapData> _mapData = new HashMap<String, MapData>();
private HashSet<String> _mapsBeingZipped = new HashSet<String>();
private List<BaseCommand> _commands = new ArrayList<BaseCommand>();
private Location _spawnLocation;
@ -269,7 +271,7 @@ public class MapParser extends JavaPlugin implements Listener
@EventHandler
public void SaveUnloadWorlds(TickEvent event)
{
for (World world : getServer().getWorlds())
for (final World world : getServer().getWorlds())
{
if (world.getName().equalsIgnoreCase("world"))
continue;
@ -284,6 +286,18 @@ public class MapParser extends JavaPlugin implements Listener
{
Announce("Saving & Closing World: " + F.elem(world.getName()));
MapUtil.UnloadWorld(this, world, true);
_mapsBeingZipped.add(world.getName());
System.out.println("Starting backup of " + world);
BackupTask backupTask = new BackupTask(this, world.getName(), new Callback<Boolean>()
{
@Override
public void run(Boolean data)
{
System.out.println("Finished backup of " + world);
_mapsBeingZipped.remove(world.getName());
}
});
}
}
}
@ -485,6 +499,11 @@ public class MapParser extends JavaPlugin implements Listener
}
}
public HashSet<String> getMapsBeingZipped()
{
return _mapsBeingZipped;
}
@EventHandler
public void Join(PlayerJoinEvent event)
{

View File

@ -72,6 +72,12 @@ public class MapCommand extends BaseCommand
worldName = getPlugin().getWorldString(args[0], gameType);
}
if (getPlugin().getMapsBeingZipped().contains(worldName))
{
message(player, "That map is being backed up now. Try again soon");
return true;
}
World world = getPlugin().GetMapWorld(worldName);
if (world == null)
{