2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2011-11-21 01:55:26 +00:00
|
|
|
import static com.earth2me.essentials.I18n._;
|
2011-08-27 15:50:44 +00:00
|
|
|
import java.io.BufferedReader;
|
2011-03-19 22:39:51 +00:00
|
|
|
import java.io.IOException;
|
2011-08-27 15:50:44 +00:00
|
|
|
import java.io.InputStreamReader;
|
2011-03-19 22:39:51 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2011-10-06 09:54:09 +00:00
|
|
|
import org.bukkit.Server;
|
2011-03-19 22:39:51 +00:00
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
|
|
|
|
|
2011-08-27 15:50:44 +00:00
|
|
|
public class Backup implements Runnable
|
|
|
|
{
|
|
|
|
private static final Logger LOGGER = Logger.getLogger("Minecraft");
|
2011-10-06 09:54:09 +00:00
|
|
|
private transient final Server server;
|
2011-08-27 15:50:44 +00:00
|
|
|
private transient final IEssentials ess;
|
|
|
|
private transient boolean running = false;
|
|
|
|
private transient int taskId = -1;
|
|
|
|
private transient boolean active = false;
|
|
|
|
|
|
|
|
public Backup(final IEssentials ess)
|
|
|
|
{
|
2011-06-01 10:40:12 +00:00
|
|
|
this.ess = ess;
|
2011-10-06 09:54:09 +00:00
|
|
|
server = ess.getServer();
|
2011-08-27 15:50:44 +00:00
|
|
|
if (server.getOnlinePlayers().length > 0)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
startTask();
|
|
|
|
}
|
2011-08-27 15:50:44 +00:00
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2011-08-27 15:50:44 +00:00
|
|
|
void onPlayerJoin()
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
startTask();
|
|
|
|
}
|
2011-08-27 15:50:44 +00:00
|
|
|
|
|
|
|
private void startTask()
|
|
|
|
{
|
|
|
|
if (!running)
|
|
|
|
{
|
|
|
|
final long interval = ess.getSettings().getBackupInterval() * 1200; // minutes -> ticks
|
|
|
|
if (interval < 1200)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-06-01 10:40:12 +00:00
|
|
|
taskId = ess.scheduleSyncRepeatingTask(this, interval, interval);
|
2011-03-19 22:39:51 +00:00
|
|
|
running = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 18:07:28 +00:00
|
|
|
@Override
|
2011-08-27 15:50:44 +00:00
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
if (active)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
active = true;
|
2011-06-01 10:40:12 +00:00
|
|
|
final String command = ess.getSettings().getBackupCommand();
|
2011-08-27 15:50:44 +00:00
|
|
|
if (command == null || "".equals(command))
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-03-25 23:39:39 +00:00
|
|
|
if ("save-all".equalsIgnoreCase(command)) {
|
|
|
|
final CommandSender cs = server.getConsoleSender();
|
|
|
|
server.dispatchCommand(cs, "save-all");
|
|
|
|
active = false;
|
|
|
|
return;
|
|
|
|
}
|
2011-11-21 01:55:26 +00:00
|
|
|
LOGGER.log(Level.INFO, _("backupStarted"));
|
2011-10-06 09:54:09 +00:00
|
|
|
final CommandSender cs = server.getConsoleSender();
|
2011-03-19 22:39:51 +00:00
|
|
|
server.dispatchCommand(cs, "save-all");
|
|
|
|
server.dispatchCommand(cs, "save-off");
|
|
|
|
|
2013-01-31 19:16:09 +00:00
|
|
|
ess.runTaskAsynchronously(
|
2011-08-27 15:50:44 +00:00
|
|
|
new Runnable()
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
@Override
|
2011-08-27 15:50:44 +00:00
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
final ProcessBuilder childBuilder = new ProcessBuilder(command);
|
|
|
|
childBuilder.redirectErrorStream(true);
|
|
|
|
childBuilder.directory(ess.getDataFolder().getParentFile().getParentFile());
|
|
|
|
final Process child = childBuilder.start();
|
|
|
|
final BufferedReader reader = new BufferedReader(new InputStreamReader(child.getInputStream()));
|
|
|
|
try
|
|
|
|
{
|
|
|
|
child.waitFor();
|
|
|
|
String line;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
line = reader.readLine();
|
|
|
|
if (line != null)
|
|
|
|
{
|
|
|
|
LOGGER.log(Level.INFO, line);
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
2011-08-27 15:50:44 +00:00
|
|
|
while (line != null);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
reader.close();
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-27 15:50:44 +00:00
|
|
|
catch (InterruptedException ex)
|
|
|
|
{
|
|
|
|
LOGGER.log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
|
|
|
LOGGER.log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
ess.scheduleSyncDelayedTask(
|
|
|
|
new Runnable()
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
@Override
|
2011-08-27 15:50:44 +00:00
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
server.dispatchCommand(cs, "save-on");
|
|
|
|
if (server.getOnlinePlayers().length == 0)
|
|
|
|
{
|
|
|
|
running = false;
|
|
|
|
if (taskId != -1)
|
|
|
|
{
|
|
|
|
server.getScheduler().cancelTask(taskId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
active = false;
|
2011-11-21 01:55:26 +00:00
|
|
|
LOGGER.log(Level.INFO, _("backupFinished"));
|
2011-08-27 15:50:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|
|
|
|
}
|