2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
import net.ess3.api.IEssentials;
|
|
|
|
import org.bukkit.Server;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
|
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;
|
2015-04-15 04:06:16 +00:00
|
|
|
|
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
2011-03-19 22:39:51 +00:00
|
|
|
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public class Backup implements Runnable {
|
|
|
|
private static final Logger LOGGER = Logger.getLogger("Essentials");
|
|
|
|
private transient final Server server;
|
|
|
|
private transient final IEssentials ess;
|
|
|
|
private transient boolean running = false;
|
|
|
|
private transient int taskId = -1;
|
|
|
|
private transient boolean active = false;
|
2011-08-27 15:50:44 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public Backup(final IEssentials ess) {
|
|
|
|
this.ess = ess;
|
|
|
|
server = ess.getServer();
|
|
|
|
if (!ess.getOnlinePlayers().isEmpty()) {
|
2019-12-31 13:58:09 +00:00
|
|
|
ess.runTaskAsynchronously(this::startTask);
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public void onPlayerJoin() {
|
|
|
|
startTask();
|
|
|
|
}
|
2011-08-27 15:50:44 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public synchronized void stopTask() {
|
|
|
|
running = false;
|
|
|
|
if (taskId != -1) {
|
|
|
|
server.getScheduler().cancelTask(taskId);
|
|
|
|
}
|
|
|
|
taskId = -1;
|
|
|
|
}
|
2013-04-13 22:10:01 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
private synchronized void startTask() {
|
|
|
|
if (!running) {
|
|
|
|
final long interval = ess.getSettings().getBackupInterval() * 1200; // minutes -> ticks
|
|
|
|
if (interval < 1200) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
taskId = ess.scheduleSyncRepeatingTask(this, interval, interval);
|
|
|
|
running = true;
|
|
|
|
}
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
active = true;
|
|
|
|
final String command = ess.getSettings().getBackupCommand();
|
|
|
|
if (command == null || "".equals(command)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ("save-all".equalsIgnoreCase(command)) {
|
|
|
|
final CommandSender cs = server.getConsoleSender();
|
|
|
|
server.dispatchCommand(cs, "save-all");
|
|
|
|
active = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOGGER.log(Level.INFO, tl("backupStarted"));
|
|
|
|
final CommandSender cs = server.getConsoleSender();
|
|
|
|
server.dispatchCommand(cs, "save-all");
|
|
|
|
server.dispatchCommand(cs, "save-off");
|
2011-03-19 22:39:51 +00:00
|
|
|
|
2020-04-25 12:08:57 +00:00
|
|
|
ess.runTaskAsynchronously(() -> {
|
|
|
|
try {
|
|
|
|
final ProcessBuilder childBuilder = new ProcessBuilder(command);
|
|
|
|
childBuilder.redirectErrorStream(true);
|
|
|
|
childBuilder.directory(ess.getDataFolder().getParentFile().getParentFile());
|
|
|
|
final Process child = childBuilder.start();
|
|
|
|
ess.runTaskAsynchronously(() -> {
|
|
|
|
try {
|
|
|
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(child.getInputStream()))) {
|
|
|
|
String line;
|
|
|
|
do {
|
|
|
|
line = reader.readLine();
|
|
|
|
if (line != null) {
|
|
|
|
LOGGER.log(Level.INFO, line);
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2020-04-25 12:08:57 +00:00
|
|
|
} while (line != null);
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2020-04-25 12:08:57 +00:00
|
|
|
} catch (IOException ex) {
|
|
|
|
LOGGER.log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
child.waitFor();
|
|
|
|
} catch (InterruptedException | IOException ex) {
|
|
|
|
LOGGER.log(Level.SEVERE, null, ex);
|
|
|
|
} finally {
|
|
|
|
class BackupEnableSaveTask implements Runnable {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
server.dispatchCommand(cs, "save-on");
|
|
|
|
if (ess.getOnlinePlayers().isEmpty()) {
|
|
|
|
stopTask();
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2020-04-25 12:08:57 +00:00
|
|
|
active = false;
|
|
|
|
LOGGER.log(Level.INFO, tl("backupFinished"));
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-25 12:08:57 +00:00
|
|
|
ess.scheduleSyncDelayedTask(new BackupEnableSaveTask());
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|