22
22
import org .zeroturnaround .zip .ZipUtil ;
23
23
24
24
import java .io .File ;
25
+ import java .io .FileWriter ;
26
+ import java .io .IOException ;
25
27
import java .time .LocalDateTime ;
26
28
import java .util .HashSet ;
27
29
import java .util .LinkedHashMap ;
@@ -33,7 +35,7 @@ public class AutoBackup extends JavaPlugin {
33
35
34
36
private static AutoBackup plugin ;
35
37
private Set <BackupMode > backupModes , defaultModes ;
36
- private File root , backupsDir ;
38
+ private File root , backupsDir , logFile ;
37
39
38
40
@ Override
39
41
public void onEnable () {
@@ -70,6 +72,9 @@ public void loadSettings() {
70
72
71
73
root = getConfig ().getBoolean ("relative-paths" ) ? new File (new File (getDataFolder ().getAbsoluteFile ().getParent ()).getParent ()) : null ;
72
74
backupsDir = new File (root , getConfig ().getString ("backup-dir" ));
75
+ if (getConfig ().getBoolean ("backup-log.enable" )) {
76
+ logFile = new File (backupsDir , "backup-log.txt" );
77
+ }
73
78
74
79
loadBackups ();
75
80
scheduleAll ();
@@ -87,7 +92,7 @@ public void scheduleAll() {
87
92
public void run () {
88
93
if (log ) getServer ().getConsoleSender ().sendMessage (Message .SCHEDULED_BACKUP_LOG .toConsoleString ()
89
94
.replaceAll ("%NAME%" , getServer ().getConsoleSender ().getName ()).replaceAll ("%MODE%" , mode .getName ()));
90
- performBackup (mode , true );
95
+ performBackup (mode , true , getConfig (). getBoolean ( "backup-log.enable" ) ? "CONSOLE" : null );
91
96
}
92
97
}.runTaskTimer (this , getConfig ().getBoolean ("immediate-backup" ) ? 0 : period , period );
93
98
}
@@ -102,7 +107,7 @@ public Set<BackupMode> getDefaultBackups() {
102
107
return defaultModes ;
103
108
}
104
109
105
- public boolean performBackup (BackupMode mode , boolean async ) {
110
+ public boolean performBackup (BackupMode mode , boolean async , String logEntity ) {
106
111
if (!backupsDir .isDirectory ()) {
107
112
if (!backupsDir .mkdirs ()) {
108
113
getLogger ().log (Level .SEVERE , Message .DIR_NOT_CREATED .toConsoleString ());
@@ -128,7 +133,6 @@ public void run() {
128
133
ZipUtil .pack (mode .getDir (), zipFile , s -> {
129
134
String path = mode .getDir ().getAbsoluteFile ().toPath ().relativize (backupsDir .getAbsoluteFile ().toPath ()).toString ();
130
135
if (s .startsWith (path )) {
131
- // System.out.println(s);
132
136
return null ;
133
137
}
134
138
return s ;
@@ -149,6 +153,26 @@ public void run() {
149
153
150
154
if (async ) runnable .runTaskAsynchronously (this );
151
155
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
+
152
176
return success [0 ];
153
177
}
154
178
@@ -177,4 +201,8 @@ public boolean loadBackups() {
177
201
public static AutoBackup getInstance () {
178
202
return plugin ;
179
203
}
204
+
205
+ public File getLogFile () {
206
+ return logFile ;
207
+ }
180
208
}
0 commit comments