-
Notifications
You must be signed in to change notification settings - Fork 4
/
BackupThread.java
257 lines (227 loc) · 10.8 KB
/
BackupThread.java
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package de.melanx.simplebackups;
import de.melanx.simplebackups.compat.Mc2DiscordCompat;
import de.melanx.simplebackups.config.BackupType;
import de.melanx.simplebackups.config.CommonConfig;
import de.melanx.simplebackups.config.ServerConfig;
import net.minecraft.ChatFormatting;
import net.minecraft.DefaultUncaughtExceptionHandler;
import net.minecraft.FileUtil;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.storage.LevelStorageSource;
import net.minecraftforge.common.ForgeI18n;
import net.minecraftforge.network.ConnectionData;
import net.minecraftforge.network.NetworkHooks;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.SignStyle;
import java.time.temporal.ChronoField;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class BackupThread extends Thread {
private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
.appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD).appendLiteral('-')
.appendValue(ChronoField.MONTH_OF_YEAR, 2).appendLiteral('-')
.appendValue(ChronoField.DAY_OF_MONTH, 2).appendLiteral('_')
.appendValue(ChronoField.HOUR_OF_DAY, 2).appendLiteral('-')
.appendValue(ChronoField.MINUTE_OF_HOUR, 2).appendLiteral('-')
.appendValue(ChronoField.SECOND_OF_MINUTE, 2)
.toFormatter();
public static final Logger LOGGER = LoggerFactory.getLogger(BackupThread.class);
private final MinecraftServer server;
private final boolean quiet;
private final long lastSaved;
private final boolean fullBackup;
private final LevelStorageSource.LevelStorageAccess storageSource;
private BackupThread(@Nonnull MinecraftServer server, boolean quiet, BackupData backupData) {
this.server = server;
this.storageSource = server.storageSource;
this.quiet = quiet;
if (backupData == null) {
this.lastSaved = 0;
this.fullBackup = true;
} else {
this.lastSaved = CommonConfig.backupType() == BackupType.MODIFIED_SINCE_LAST ? backupData.getLastSaved() : backupData.getLastFullBackup();
this.fullBackup = CommonConfig.backupType() == BackupType.FULL_BACKUPS || System.currentTimeMillis() - CommonConfig.getFullBackupTimer() > backupData.getLastFullBackup();
}
this.setName("SimpleBackups");
this.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler(LOGGER));
}
public static boolean tryCreateBackup(MinecraftServer server) {
BackupData backupData = BackupData.get(server);
if (CommonConfig.isEnabled() && !backupData.isPaused() && System.currentTimeMillis() - CommonConfig.getTimer() > backupData.getLastSaved()) {
BackupThread thread = new BackupThread(server, false, backupData);
thread.start();
backupData.updateSaveTime(System.currentTimeMillis());
if (thread.fullBackup) {
backupData.updateFullBackupTime(System.currentTimeMillis());
}
return true;
}
return false;
}
public static void createBackup(MinecraftServer server, boolean quiet) {
BackupThread thread = new BackupThread(server, quiet, null);
thread.start();
}
public void deleteFiles() {
File backups = CommonConfig.getOutputPath().toFile();
if (backups.isDirectory()) {
List<File> files = new ArrayList<>(Arrays.stream(Objects.requireNonNull(backups.listFiles())).filter(File::isFile).toList());
if (files.size() >= CommonConfig.getBackupsToKeep()) {
files.sort(Comparator.comparingLong(File::lastModified));
while (files.size() >= CommonConfig.getBackupsToKeep()) {
boolean deleted = files.get(0).delete();
String name = files.get(0).getName();
if (deleted) {
files.remove(0);
LOGGER.info("Successfully deleted \"" + name + "\"");
}
}
}
}
}
public static void saveStorageSize() {
try {
while (BackupThread.getOutputFolderSize() > CommonConfig.getMaxDiskSize()) {
File[] files = CommonConfig.getOutputPath().toFile().listFiles();
if (Objects.requireNonNull(files).length == 1) {
LOGGER.error("Cannot delete old files to save disk space. Only one backup file left!");
return;
}
Arrays.sort(Objects.requireNonNull(files), Comparator.comparingLong(File::lastModified));
File file = files[0];
String name = file.getName();
if (file.delete()) {
LOGGER.info("Successfully deleted \"" + name + "\"");
}
}
} catch (NullPointerException | IOException e) {
LOGGER.error("Cannot delete old files to save disk space", e);
}
}
@Override
public void run() {
try {
this.deleteFiles();
Files.createDirectories(CommonConfig.getOutputPath());
long start = System.currentTimeMillis();
this.broadcast("simplebackups.backup_started", Style.EMPTY.withColor(ChatFormatting.GOLD));
long size = this.makeWorldBackup();
long end = System.currentTimeMillis();
String time = Timer.getTimer(end - start);
BackupThread.saveStorageSize();
this.broadcast("simplebackups.backup_finished", Style.EMPTY.withColor(ChatFormatting.GOLD), time, StorageSize.getFormattedSize(size), StorageSize.getFormattedSize(BackupThread.getOutputFolderSize()));
} catch (IOException e) {
e.printStackTrace();
}
}
private static long getOutputFolderSize() throws IOException {
File[] files = CommonConfig.getOutputPath().toFile().listFiles();
long size = 0;
try {
for (File file : Objects.requireNonNull(files)) {
size += Files.size(file.toPath());
}
} catch (NullPointerException e) {
return 0;
}
return size;
}
private void broadcast(String message, Style style, Object... parameters) {
if (CommonConfig.sendMessages() && !this.quiet) {
this.server.execute(() -> {
this.server.getPlayerList().getPlayers().forEach(player -> {
if (ServerConfig.messagesForEveryone() || player.hasPermissions(2)) {
player.sendSystemMessage(BackupThread.component(player, message, parameters).withStyle(style));
}
});
});
if (Mc2DiscordCompat.isLoaded() && CommonConfig.mc2discord()) {
Mc2DiscordCompat.announce(BackupThread.component(null, message, parameters));
}
}
}
public static MutableComponent component(@Nullable ServerPlayer player, String key, Object... parameters) {
if (player != null) {
ConnectionData data = NetworkHooks.getConnectionData(player.connection.connection);
if (data != null && data.getModList().contains(SimpleBackups.MODID)) {
return Component.translatable(key, parameters);
}
}
return Component.literal(String.format(ForgeI18n.getPattern(key), parameters));
}
// vanilla copy with modifications
private long makeWorldBackup() throws IOException {
this.storageSource.checkLock();
String fileName = this.storageSource.levelId + "_" + LocalDateTime.now().format(FORMATTER);
Path path = CommonConfig.getOutputPath();
try {
Files.createDirectories(Files.exists(path) ? path.toRealPath() : path);
} catch (IOException ioexception) {
throw new RuntimeException(ioexception);
}
Path outputFile = path.resolve(FileUtil.findAvailableName(path, fileName, ".zip"));
final ZipOutputStream zipStream = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(outputFile)));
zipStream.setLevel(CommonConfig.getCompressionLevel());
try {
Path levelName = Paths.get(this.storageSource.levelId);
Path levelPath = this.storageSource.getWorldDir().resolve(this.storageSource.levelId).toRealPath();
Files.walkFileTree(levelPath, new SimpleFileVisitor<>() {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (!file.endsWith("session.lock")) {
long lastModified = file.toFile().lastModified();
if (BackupThread.this.fullBackup || lastModified - BackupThread.this.lastSaved > 0) {
String completePath = levelName.resolve(levelPath.relativize(file)).toString().replace('\\', '/');
ZipEntry zipentry = new ZipEntry(completePath);
zipStream.putNextEntry(zipentry);
com.google.common.io.Files.asByteSource(file.toFile()).copyTo(zipStream);
zipStream.closeEntry();
}
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
try {
zipStream.close();
} catch (IOException e1) {
e.addSuppressed(e1);
}
throw e;
}
zipStream.close();
return Files.size(outputFile);
}
private static class Timer {
private static final SimpleDateFormat SECONDS = new SimpleDateFormat("s.SSS");
private static final SimpleDateFormat MINUTES = new SimpleDateFormat("mm:ss");
private static final SimpleDateFormat HOURS = new SimpleDateFormat("HH:mm");
public static String getTimer(long milliseconds) {
Date date = new Date(milliseconds);
double seconds = milliseconds / 1000d;
if (seconds < 60) {
return SECONDS.format(date) + "s";
} else if (seconds < 3600) {
return MINUTES.format(date) + "min";
} else {
return HOURS.format(date) + "h";
}
}
}
}