Skip to content

Commit 46eebd1

Browse files
Add compression level option
1 parent 497c0c4 commit 46eebd1

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
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.2</version>
9+
<version>1.1.0</version>
1010
<description>Schedule and perform automatic backups</description>
1111

1212
<properties>

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ public void onDisable() {
6767
public void loadSettings() {
6868
saveDefaultConfig();
6969
getConfig().options().copyDefaults(true);
70-
//reloadConfig();
71-
//saveConfig();
7270

7371
root = getConfig().getBoolean("relative-paths") ? new File(new File(getDataFolder().getAbsoluteFile().getParent()).getParent()) : null;
7472
backupsDir = new File(root, getConfig().getString("backup-dir"));
@@ -131,21 +129,24 @@ public void run() {
131129
File zipFile = new File(backupsDir, zipName);
132130
if (getConfig().getBoolean("exclude-backup")) {
133131
ZipUtil.pack(mode.getDir(), zipFile, s -> {
134-
String path = mode.getDir().getAbsoluteFile().toPath().relativize(backupsDir.getAbsoluteFile().toPath()).toString();
132+
String path = mode.getDir().getAbsoluteFile().toPath().relativize(backupsDir
133+
.getAbsoluteFile().toPath()).toString();
135134
if (s.startsWith(path)) {
136135
return null;
137136
}
138137
return s;
139-
});
138+
}, mode.getCompressionLevel());
140139
}
141140
else {
142-
ZipUtil.pack(mode.getDir(), zipFile);
141+
ZipUtil.pack(mode.getDir(), zipFile, mode.getCompressionLevel());
143142
}
144143

145-
if (log) getServer().getConsoleSender().sendMessage(Message.BACKUP_SUCCESSFUL.toConsoleString() + mode.getName());
144+
if (log) getServer().getConsoleSender().sendMessage(Message.BACKUP_SUCCESSFUL.toConsoleString()
145+
+ mode.getName());
146146
} catch(Exception e){
147147
e.printStackTrace();
148-
if (log) getServer().getConsoleSender().sendMessage(Message.BACKUP_FAILED.toConsoleString() + mode.getName());
148+
if (log) getServer().getConsoleSender().sendMessage(Message.BACKUP_FAILED.toConsoleString()
149+
+ mode.getName());
149150
success[0] = false;
150151
}
151152
}
@@ -187,7 +188,9 @@ public boolean loadBackups() {
187188
(String) map.get("name"),
188189
new File(root, (String) map.get("dir")),
189190
(Boolean) map.get("allow-manual"),
190-
(Integer) map.get("schedule")));
191+
(Integer) map.get("schedule"),
192+
(Integer) map.get("compression")
193+
));
191194
}
192195

193196
for (BackupMode mode : backupModes) {

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,41 @@
1717
package org.shanerx.backup;
1818

1919
import java.io.File;
20+
import java.util.logging.Level;
21+
import java.util.zip.Deflater;
2022

2123
public class BackupMode {
2224
private String name;
2325
private File dir;
2426
private boolean allowManual;
2527
private int schedule;
28+
private int compressionLevel;
2629

27-
public BackupMode(String name, File dir, boolean allowManual, int schedule) {
30+
public BackupMode(String name, File dir, boolean allowManual, int schedule, int compressionLevel) {
2831
this.name = name;
2932
this.dir = dir;
3033
this.allowManual = allowManual;
3134
this.schedule = Math.max(schedule, 0); // if <= 0 set to 0, otherwise to predefined value
35+
36+
switch (compressionLevel) {
37+
case -1:
38+
this.compressionLevel = Deflater.NO_COMPRESSION;
39+
break;
40+
case 0:
41+
this.compressionLevel = Deflater.DEFAULT_COMPRESSION;
42+
break;
43+
case 1:
44+
this.compressionLevel = Deflater.BEST_SPEED;
45+
break;
46+
case 2:
47+
this.compressionLevel = Deflater.BEST_COMPRESSION;
48+
break;
49+
default:
50+
AutoBackup.getInstance().getLogger().log(Level.WARNING,
51+
String.format("(Backup mode: %s) Invalid compression value: %d", name, compressionLevel));
52+
this.compressionLevel = Deflater.DEFAULT_COMPRESSION;
53+
break;
54+
}
3255
}
3356

3457
public String getName() {
@@ -46,4 +69,8 @@ public int getSchedule() {
4669
public boolean isAllowedManually() {
4770
return allowManual;
4871
}
72+
73+
public int getCompressionLevel() {
74+
return compressionLevel;
75+
}
4976
}

src/main/resources/config.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ backup-log:
7171
# - The schedule option determines whether or not to perform the backup automatically every x minutes.
7272
# If set to 0 or a negative integer, the scheduled backup is disabled.
7373
# If set to a positive integer, the value indicates the time in minutes between every automatic backup.
74+
# - The compression option specifies the degree of the compression. Better compression means less disk space
75+
# is occupied, but the backup itself takes longer.
76+
# Possible settings: -1 (No compression); 0 (Default Compression); 1 (Best speed); 2 (Best Compression).
77+
# Note: experimental results show that options 0 and 1 yield very similar results, although 0 has very slightly better
78+
# compression and 1 has very slightly better speed. It is still possible that larger backup sizes lead to a divergence
79+
# between these two.
7480
# NOTE: These are just (working) examples! You should add and tweak the ones you need and get rid of those you
7581
# do not need. In particular, the backup 'everything' is extremely inefficient and can take very long times. It
7682
# is therefore highly discouraged. You should rather setup separate smaller backups, ie. one for plugin data, one for
@@ -80,22 +86,27 @@ backup-modes:
8086
dir: '/'
8187
allow-manual: true
8288
schedule: 0
89+
compression: 0
8390
- name: 'plugins'
8491
dir: '/plugins'
8592
allow-manual: false
8693
schedule: 0
94+
compression: 0
8795
- name: 'overworld'
8896
dir: '/world'
8997
allow-manual: true
9098
schedule: 720
99+
compression: 0
91100
- name: 'nether'
92101
dir: '/world_nether'
93102
allow-manual: true
94103
schedule: 1440
104+
compression: 0
95105
- name: 'end'
96106
dir: '/world_the_end'
97107
allow-manual: true
98108
schedule: 2880
109+
compression: 0
99110

100111
# +-------------- End of Config --------------+
101112
# You are done configuring AutoBackup. Enjoy :)

0 commit comments

Comments
 (0)