summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/backup/Backup.java
blob: e638782f49c919d943f76bb75e1c690eba95f68f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package net.ess3.backup;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import static net.ess3.I18n._;
import net.ess3.api.IBackup;
import net.ess3.api.IEssentials;
import net.ess3.api.ISettings;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;


public class Backup implements Runnable, IBackup
{
	private final Server server;
	private final IEssentials ess;
	private final AtomicBoolean running = new AtomicBoolean(false);
	private int taskId = -1;
	private final AtomicBoolean active = new AtomicBoolean(false);

	public Backup(final IEssentials ess)
	{
		this.ess = ess;
		server = ess.getServer();
		if (server.getOnlinePlayers().length > 0)
		{
			startTask();
		}
	}

	@Override
	public final void startTask()
	{
		if (running.compareAndSet(false, true))
		{
			final ISettings settings = ess.getSettings();
			final long interval = settings.getData().getGeneral().getBackup().getInterval() * 1200; // minutes -> ticks
			if (interval < 1200)
			{
				running.set(false);
				return;
			}
			taskId = ess.getPlugin().scheduleSyncRepeatingTask(this, interval, interval);
		}
	}

	@Override
	public void run()
	{
		if (!active.compareAndSet(false, true))
		{
			return;
		}

		final ISettings settings = ess.getSettings();

		final net.ess3.settings.Backup backupSettings = settings.getData().getGeneral().getBackup();

		String backupCommand = backupSettings.getCommand() == null || backupSettings.getCommand().isEmpty() ? ("NORUN") : backupSettings.getCommand();
		
		/*if (backupCommand.equals("NORUN")) { TODO: Un-comment if you do not want commands to be run if there is no backup command
			return;
		}*/

		ess.getLogger().log(Level.INFO, _("Backup started."));

		if (!backupSettings.getCommandsBeforeBackup().isEmpty())
		{
			final CommandSender consoleSender = server.getConsoleSender();
			for (String command : backupSettings.getCommandsBeforeBackup())
			{
				server.dispatchCommand(consoleSender, command);
			}
		}

		ess.getPlugin().runTaskAsynchronously(new BackupRunner(backupCommand));
	}


	private class BackupRunner implements Runnable
	{
		private final String command;

		public BackupRunner(final String command)
		{
			this.command = command;
		}

		@Override
		public void run()
		{
			try
			{
				if (!command.equalsIgnoreCase("NORUN"))
				{
					final ProcessBuilder childBuilder = new ProcessBuilder(command);
					childBuilder.redirectErrorStream(true);
					childBuilder.directory(ess.getPlugin().getRootFolder());
					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)
							{
								ess.getLogger().log(Level.INFO, line);
							}
						}
						while (line != null);
					}
					finally
					{
						reader.close();
					}
				}
			}
			catch (InterruptedException ex)
			{
				ess.getLogger().log(Level.SEVERE, null, ex);
			}
			catch (IOException ex)
			{
				ess.getLogger().log(Level.SEVERE, null, ex);
			}
			finally
			{
				ess.getPlugin().scheduleSyncDelayedTask(new EnableSavingRunner());
			}
		}
	}


	private class EnableSavingRunner implements Runnable
	{
		@Override
		public void run()
		{
			server.dispatchCommand(server.getConsoleSender(), "save-on");
			if (server.getOnlinePlayers().length > 0)
			{
				running.set(false);
				if (taskId != -1)
				{
					ess.getPlugin().cancelTask(taskId);
				}
			}

			active.set(false);
			ess.getLogger().log(Level.INFO, _("Backup finished."));
		}
	}
}