Skip to content

Commit 44bc9cc

Browse files
Add backups-log.txt logging system and version bump to 1.0.2 (release)
1 parent 18d1255 commit 44bc9cc

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.shanerx.backup</groupId>
88
<artifactId>AutoBackup</artifactId>
9-
<version>1.0.1</version>
9+
<version>1.0.2</version>
1010
<description>Schedule and perform automatic backups</description>
1111

1212
<properties>

src/main/java/org/shanerx/backup/AutoBackup.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.zeroturnaround.zip.ZipUtil;
2323

2424
import java.io.File;
25+
import java.io.FileWriter;
26+
import java.io.IOException;
2527
import java.time.LocalDateTime;
2628
import java.util.HashSet;
2729
import java.util.LinkedHashMap;
@@ -33,7 +35,7 @@ public class AutoBackup extends JavaPlugin {
3335

3436
private static AutoBackup plugin;
3537
private Set<BackupMode> backupModes, defaultModes;
36-
private File root, backupsDir;
38+
private File root, backupsDir, logFile;
3739

3840
@Override
3941
public void onEnable() {
@@ -70,6 +72,9 @@ public void loadSettings() {
7072

7173
root = getConfig().getBoolean("relative-paths") ? new File(new File(getDataFolder().getAbsoluteFile().getParent()).getParent()) : null;
7274
backupsDir = new File(root, getConfig().getString("backup-dir"));
75+
if (getConfig().getBoolean("backup-log.enable")) {
76+
logFile = new File(backupsDir, "backup-log.txt");
77+
}
7378

7479
loadBackups();
7580
scheduleAll();
@@ -87,7 +92,7 @@ public void scheduleAll() {
8792
public void run() {
8893
if (log) getServer().getConsoleSender().sendMessage(Message.SCHEDULED_BACKUP_LOG.toConsoleString()
8994
.replaceAll("%NAME%", getServer().getConsoleSender().getName()).replaceAll("%MODE%", mode.getName()));
90-
performBackup(mode, true);
95+
performBackup(mode, true, getConfig().getBoolean("backup-log.enable") ? "CONSOLE" : null);
9196
}
9297
}.runTaskTimer(this, getConfig().getBoolean("immediate-backup") ? 0 : period, period);
9398
}
@@ -102,7 +107,7 @@ public Set<BackupMode> getDefaultBackups() {
102107
return defaultModes;
103108
}
104109

105-
public boolean performBackup(BackupMode mode, boolean async) {
110+
public boolean performBackup(BackupMode mode, boolean async, String logEntity) {
106111
if (!backupsDir.isDirectory()) {
107112
if (!backupsDir.mkdirs()) {
108113
getLogger().log(Level.SEVERE, Message.DIR_NOT_CREATED.toConsoleString());
@@ -128,7 +133,6 @@ public void run() {
128133
ZipUtil.pack(mode.getDir(), zipFile, s -> {
129134
String path = mode.getDir().getAbsoluteFile().toPath().relativize(backupsDir.getAbsoluteFile().toPath()).toString();
130135
if (s.startsWith(path)) {
131-
// System.out.println(s);
132136
return null;
133137
}
134138
return s;
@@ -149,6 +153,26 @@ public void run() {
149153

150154
if (async) runnable.runTaskAsynchronously(this);
151155
else runnable.runTask(this);
156+
157+
if (success[0] && getConfig().getBoolean("backup-log.enable")) {
158+
try {
159+
if (!logFile.exists()) {
160+
logFile.createNewFile();
161+
}
162+
163+
FileWriter writer = new FileWriter(plugin.getLogFile(), true);
164+
if (logEntity != null)
165+
writer.append(String.format("%s (by %s)\n", zipName, logEntity));
166+
else
167+
writer.append(zipName + "\n");
168+
169+
writer.flush();
170+
writer.close();
171+
} catch (IOException e) {
172+
e.printStackTrace();
173+
}
174+
}
175+
152176
return success[0];
153177
}
154178

@@ -177,4 +201,8 @@ public boolean loadBackups() {
177201
public static AutoBackup getInstance() {
178202
return plugin;
179203
}
204+
205+
public File getLogFile() {
206+
return logFile;
207+
}
180208
}

src/main/java/org/shanerx/backup/BackupCommand.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
import org.bukkit.command.CommandSender;
2222
import org.bukkit.entity.Player;
2323

24+
import java.io.FileWriter;
25+
import java.io.IOException;
26+
import java.io.OutputStreamWriter;
27+
2428
public class BackupCommand implements CommandExecutor {
2529

2630
private AutoBackup plugin;
@@ -103,8 +107,11 @@ else if (plugin.getConfig().getBoolean("log-to-console")) {
103107
}
104108

105109
StringBuilder sb = new StringBuilder();
110+
String name = sender.getName() +
111+
((sender instanceof Player) ? " : " + ((Player) sender).getUniqueId().toString() : "");
112+
106113
for (BackupMode mode : plugin.getDefaultBackups()) {
107-
if (plugin.performBackup(mode, true)) {
114+
if (plugin.performBackup(mode, true, plugin.getConfig().getBoolean("backup-log.log-entity") ? name : null)) {
108115
sb.append(mode.getName()).append(" ");
109116
continue;
110117
}
@@ -142,7 +149,9 @@ else if (args.length == 2) {
142149
return true;
143150
}
144151

145-
if (plugin.performBackup(mode, true)) {
152+
String name = sender.getName() +
153+
((sender instanceof Player) ? " : " + ((Player) sender).getUniqueId().toString() : "");
154+
if (plugin.performBackup(mode, true, plugin.getConfig().getBoolean("backup-log.log-entity") ? name : null)) {
146155
if (sender instanceof Player) sender.sendMessage(Message.BACKUP_PERFORMING.toString() + mode.getName());
147156
if (plugin.getConfig().getBoolean("log-to-console")) {
148157
plugin.getServer().getConsoleSender().sendMessage(Message.BACKUP_PERFORMING.toConsoleString() + mode.getName());

src/main/resources/config.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,16 @@ default-modes: ['overworld', 'nether', 'end']
2525
# Set to false at your own risk!
2626
exclude-backup: true
2727

28-
# Determines where to save the backups. DO NOT CHANGE unless you are exporting the backups outside
29-
# the Minecraft server root.
28+
# Determines where to save the backups.
29+
# Warnings:
30+
# - DO NOT CHANGE unless you are exporting the backups outside!
31+
# - If you are storing backups inside the Minecraft server directory, DO NOT CHANGE this value once you have started
32+
# taking backups, unless you erased those backups from where they used to be (as in exported them outside the
33+
# Minecraft server root).
34+
# - Either way, DO NOT set this to the same path as the Minecraft server root! (Subdirectories, sibling and parent
35+
# directories are fine).
36+
# If you do not wish to worry about these things, it is recommended you leave this setting to its default value
37+
# ('/backups').
3038
backup-dir: '/backups'
3139

3240
# Determines whether the paths in this config are to be taken relative to the Minecraft server root
@@ -48,6 +56,12 @@ immediate-backup: false
4856
# (allow-manual option) is disabled.
4957
allow-force-console: true
5058

59+
# Should AutoBackup log all backups to the backup-log.txt file? Should AutoBackup log which entity (player or console)
60+
# issued the backup?
61+
backup-log:
62+
enable: true
63+
log-entity: false
64+
5165
# Use this configuration section to determine what is backed up.
5266
# Every list entry is a different backup mode. Feel free to add your own modes and remove the examples
5367
# you don't need.

0 commit comments

Comments
 (0)