From 5cdd502d11d3848395aae14128207a9ecd468029 Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Wed, 15 Oct 2014 15:04:44 -0500 Subject: [PATCH] Auto backups for MapParser. (Only tested on mac) --- .../src/mineplex/mapparser/BackupTask.java | 90 +++++++++++++++++++ .../src/mineplex/mapparser/MapParser.java | 29 ++++-- .../mapparser/command/MapCommand.java | 6 ++ 3 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 Plugins/Mineplex.MapParser/src/mineplex/mapparser/BackupTask.java diff --git a/Plugins/Mineplex.MapParser/src/mineplex/mapparser/BackupTask.java b/Plugins/Mineplex.MapParser/src/mineplex/mapparser/BackupTask.java new file mode 100644 index 000000000..43052a30e --- /dev/null +++ b/Plugins/Mineplex.MapParser/src/mineplex/mapparser/BackupTask.java @@ -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 _callback; + private final JavaPlugin _plugin; + + public BackupTask(JavaPlugin plugin, String worldName, Callback 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 fileList = new ArrayList(); + List folderList = new ArrayList(); + 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); + } + }); + } + } +} diff --git a/Plugins/Mineplex.MapParser/src/mineplex/mapparser/MapParser.java b/Plugins/Mineplex.MapParser/src/mineplex/mapparser/MapParser.java index 36010f749..c9a588eb3 100644 --- a/Plugins/Mineplex.MapParser/src/mineplex/mapparser/MapParser.java +++ b/Plugins/Mineplex.MapParser/src/mineplex/mapparser/MapParser.java @@ -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 _mapData = new HashMap(); + private HashSet _mapsBeingZipped = new HashSet(); private List _commands = new ArrayList(); private Location _spawnLocation; @@ -142,7 +144,7 @@ public class MapParser extends JavaPlugin implements Listener @EventHandler public void Command(PlayerCommandPreprocessEvent event) - { + { Player player = event.getPlayer(); String[] parts = event.getMessage().split(" "); @@ -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,10 +286,22 @@ 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() + { + @Override + public void run(Boolean data) + { + System.out.println("Finished backup of " + world); + _mapsBeingZipped.remove(world.getName()); + } + }); } } } - + public void Announce(String msg) { for (Player player : UtilServer.getPlayers()) @@ -484,6 +498,11 @@ public class MapParser extends JavaPlugin implements Listener } } } + + public HashSet getMapsBeingZipped() + { + return _mapsBeingZipped; + } @EventHandler public void Join(PlayerJoinEvent event) diff --git a/Plugins/Mineplex.MapParser/src/mineplex/mapparser/command/MapCommand.java b/Plugins/Mineplex.MapParser/src/mineplex/mapparser/command/MapCommand.java index 9bb845b1b..490f4b8cf 100644 --- a/Plugins/Mineplex.MapParser/src/mineplex/mapparser/command/MapCommand.java +++ b/Plugins/Mineplex.MapParser/src/mineplex/mapparser/command/MapCommand.java @@ -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) {