Repaired BackupHelper
parent
267776789d
commit
a9befa2301
|
@ -92,7 +92,7 @@ public class ConfigPOJO implements ConfigData {
|
||||||
If set to 0 then backups will not be deleted
|
If set to 0 then backups will not be deleted
|
||||||
""")
|
""")
|
||||||
@ConfigEntry.Gui.Tooltip()
|
@ConfigEntry.Gui.Tooltip()
|
||||||
public int maxSize = 0;
|
public long maxSize = 0;
|
||||||
|
|
||||||
@Comment("\nCompression level \n0 - 9\n Only affects zip compression.\n")
|
@Comment("\nCompression level \n0 - 9\n Only affects zip compression.\n")
|
||||||
@ConfigEntry.Gui.Tooltip()
|
@ConfigEntry.Gui.Tooltip()
|
||||||
|
|
|
@ -86,12 +86,12 @@ public class BackupHelper {
|
||||||
|
|
||||||
if (Files.isDirectory(root) && Files.exists(root) && !isEmpty(root)) {
|
if (Files.isDirectory(root) && Files.exists(root) && !isEmpty(root)) {
|
||||||
if (config.get().maxAge > 0) { // delete files older that configured
|
if (config.get().maxAge > 0) { // delete files older that configured
|
||||||
final LocalDateTime now = LocalDateTime.now();
|
final long now = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
|
||||||
|
|
||||||
try(Stream<Path> stream = Files.list(root)) {
|
try(Stream<Path> stream = Files.list(root)) {
|
||||||
deletedFiles += stream
|
deletedFiles += stream
|
||||||
.filter(Utilities::isValidBackup)// We check if we can get file's creation date so that the next line won't throw an exception
|
.filter(Utilities::isValidBackup)// We check if we can get file's creation date so that the next line won't throw an exception
|
||||||
.filter(f -> now.toEpochSecond(ZoneOffset.UTC) - Utilities.getFileCreationTime(f).get().toEpochSecond(ZoneOffset.UTC) > config.get().maxAge)
|
.filter(f -> now - Utilities.getFileCreationTime(f).get().toEpochSecond(ZoneOffset.UTC) > config.get().maxAge)
|
||||||
.mapToInt(f -> deleteFile(f, ctx))
|
.mapToInt(f -> deleteFile(f, ctx))
|
||||||
.sum();
|
.sum();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -99,8 +99,8 @@ public class BackupHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int noToKeep = config.get().backupsToKeep > 0 ? config.get().backupsToKeep : Integer.MAX_VALUE;
|
final int noToKeep = config.get().backupsToKeep > 0 ? config.get().backupsToKeep : Integer.MAX_VALUE;
|
||||||
long maxSize = config.get().maxSize > 0 ? config.get().maxSize : Long.MAX_VALUE;
|
final long maxSize = config.get().maxSize > 0 ? config.get().maxSize * 1024: Long.MAX_VALUE;
|
||||||
|
|
||||||
AtomicInteger currentNo = new AtomicInteger(countBackups(root));
|
AtomicInteger currentNo = new AtomicInteger(countBackups(root));
|
||||||
AtomicLong currentSize = new AtomicLong(countSize(root));
|
AtomicLong currentSize = new AtomicLong(countSize(root));
|
||||||
|
@ -110,10 +110,10 @@ public class BackupHelper {
|
||||||
.filter(Utilities::isValidBackup)
|
.filter(Utilities::isValidBackup)
|
||||||
.sorted(Comparator.comparing(f -> Utilities.getFileCreationTime(f).get()))
|
.sorted(Comparator.comparing(f -> Utilities.getFileCreationTime(f).get()))
|
||||||
.takeWhile(f -> (currentNo.get() > noToKeep) || (currentSize.get() > maxSize))
|
.takeWhile(f -> (currentNo.get() > noToKeep) || (currentSize.get() > maxSize))
|
||||||
.peek(f -> currentNo.decrementAndGet())
|
|
||||||
.peek(f -> {
|
.peek(f -> {
|
||||||
|
currentNo.decrementAndGet();
|
||||||
try {
|
try {
|
||||||
currentSize.addAndGet(Files.size(f));
|
currentSize.addAndGet(-Files.size(f));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
currentSize.set(0);
|
currentSize.set(0);
|
||||||
}
|
}
|
||||||
|
@ -133,23 +133,28 @@ public class BackupHelper {
|
||||||
return (int) stream
|
return (int) stream
|
||||||
.filter(Utilities::isValidBackup)
|
.filter(Utilities::isValidBackup)
|
||||||
.count();
|
.count();
|
||||||
} catch (IOException ignored) {}
|
} catch (IOException e) {
|
||||||
|
log.error("Error while counting files!", e);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long countSize(Path path) {
|
private static long countSize(Path path) {
|
||||||
try(Stream<Path> stream = Files.list(path)) {
|
try(Stream<Path> stream = Files.list(path)) {
|
||||||
return (int) stream
|
return stream
|
||||||
.filter(Utilities::isValidBackup)
|
.filter(Utilities::isValidBackup)
|
||||||
.mapToLong(f -> {
|
.mapToLong(f -> {
|
||||||
try {
|
try {
|
||||||
return Files.size(f);
|
return Files.size(f);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
log.error("Couldn't delete a file!", e);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.sum();
|
.sum();
|
||||||
} catch (IOException ignored) {}
|
} catch (IOException e) {
|
||||||
|
log.error("Error while counting files!", e);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue