Skip to content

Commit

Permalink
Rework backup purge logic - fix #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
HeatherComputer committed Jun 15, 2023
1 parent f53a3ba commit bff70a0
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,54 +129,72 @@ public static void makeSingleBackup(long delay) {
public static void finishBackup() {
File directory = new File(AVConfig.config.getPath());
ThreadedBackup.running = false;
PlatformMethodWrapper.enableSaving();

switch(AVConfig.config.getBackupType()) {
case "zip" : {
directory = new File(directory, "/zips/");
break;
long date = Long.MIN_VALUE;
while (true) {
if (calculateDirectorySize(directory) < AVConfig.config.getMaxSize() * 1000000000L) return;
File file = getFirstBackupAfterDate(directory, date);
date = file.lastModified();
file.delete();
}
}
case "differential" : {
directory = new File(directory, "/differential/");
break;
long date = Long.MIN_VALUE;
while (true) {
if (calculateDirectorySize(directory) < AVConfig.config.getMaxSize() * 1000000000L) return;
File file = getFirstBackupAfterDate(directory, date);
date = file.lastModified();
if (file.getName().contains("full")) {
File nextFile = getFirstBackupAfterDate(directory, date);
if (nextFile.getName().contains("partial")) {
nextFile.delete();
}
else {
file.delete();
}
}
else {
file.delete();
}

}
}
case "incremental" : {
directory = new File(directory, "/incremental/");
break;
}
if (!AVConfig.config.getPurgeIncrementals()) return;
long date = Long.MIN_VALUE;
while (true) {
if (calculateDirectorySize(directory) < AVConfig.config.getMaxSize() * 1000000000L) return;
if (calculateChainCount(directory) < 2) return;
PlatformMethodWrapper.errorLogger.accept("Purging incremental backup chain - too much space taken up!");
File file = getFirstBackupAfterDate(directory, date);
date = file.lastModified();
if (file.getName().contains("full")) {
file.delete();
while (true) {
file = getFirstBackupAfterDate(directory, date);
date = file.lastModified();
if (file.getName().contains("full")) {
file.delete();
}
else {
break;
}
}
}
else {
file.delete();
}

}

while (true)
{
File[] files = directory.listFiles();
File oldestFile = null;
long totalLength = 0;
long lastModifiedTime = Long.MAX_VALUE;
if (files == null || files.length == 0) break;
for (File file : files) {
if (file.isFile()) {
totalLength += file.length();
}
else {
totalLength += calculateDirectorySize(file);
}
if (file.lastModified() < lastModifiedTime) {
lastModifiedTime = file.lastModified();
oldestFile = file;
}
}
if (oldestFile != null && totalLength >= AVConfig.config.getMaxSize() * 1000000000L) {
oldestFile.delete();
}
else {
break;
}
}


PlatformMethodWrapper.enableSaving();


}
}


Expand All @@ -195,5 +213,30 @@ public static long calculateDirectorySize(File directory) {
return size;
}

public static File getFirstBackupAfterDate(File directory, long date) {
File[] files = directory.listFiles();
File oldestFile = null;
long currentDate = Long.MAX_VALUE;
if (files == null || files.length == 0) return null;
for (File file : files) {
if (file.lastModified() < currentDate && file.lastModified() > date) {
currentDate = file.lastModified();
oldestFile = file;
}
}

return oldestFile;
}

public static int calculateChainCount(File directory) {
int count = 0;
File[] files = directory.listFiles();
if (files == null || files.length == 0) return 0;
for (File file : files) {
if (file.getName().contains("full")) {
count++;
}
}
return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public class AVConfig {

"config.advancedbackups.chains.length",
"config.advancedbackups.chains.compress",
"config.advancedbackups.chains.smart"
"config.advancedbackups.chains.smart",

"config.advancedbackups.purge.incrementals"

};

Expand Down Expand Up @@ -106,6 +108,8 @@ public static void loadConfig() {
config.setCompressChains(props.getProperty("config.advancedbackups.chains.compress", "true"));
config.setSmartChains(props.getProperty("config.advancedbackups.chains.smart", "true"));

config.setPurgeIncrementals(props.getProperty("config.advancedbackups.purge.incrementals", "false"));



String timingsString = config.getSchedule();
Expand Down Expand Up @@ -143,7 +147,7 @@ public static void loadConfig() {
config.getMaxTimer(), config.getUptimeSchedule(), config.getSchedule(),
config.getForceOnShutdown(), config.getStartupDelay(), config.getStartupDelay(),
config.getSilent(), config.getCompressionLevel(), config.getMaxDepth(),
config.getCompressChains(), config.getSmartChains()
config.getCompressChains(), config.getSmartChains(), config.getPurgeIncrementals()
));
writer.close();
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public class ConfigData {
// Smart chain resetting.
// TRUE OR FALSE

private boolean purgeIncrementals;
// Whether to purge incremental backups if over the limit.
// TRUE OR FALSE

public Boolean getEnabled() {
return enabled;
}
Expand Down Expand Up @@ -221,6 +225,15 @@ public boolean getSmartChains() {
public void setSmartChains(String smartChains) {
this.smartChains = Boolean.parseBoolean(smartChains);
}


public boolean getPurgeIncrementals() {
return purgeIncrementals;
}

public void setPurgeIncrementals(String purgeIncrementals) {
this.purgeIncrementals = Boolean.parseBoolean(purgeIncrementals);
}



Expand Down Expand Up @@ -320,7 +333,13 @@ public void setSmartChains(String smartChains) {
"",
"#Whether to enable \"smart\" reset for chains - if every file is being backed up, mark the backup as complete and reset chain length regardless of intended backup type.",
"#Options : true, false #Default : true",
"config.advancedbackups.chains.smart=%s"
"config.advancedbackups.chains.smart=%s",
"",
"#Whether to delete incremental backup chains if max size is exceeded. If not, incremental backups do not respect the max size config and never delete.",
"#Options : true, false #Default : false",
"config.advancedbackups.purge.incrementals=%s"


);

public static final String defaults = String.format(plainConfig,
Expand All @@ -329,6 +348,7 @@ public void setSmartChains(String smartChains) {
"0.5", "24", "true",
"12:00", "false", "false",
"5", "false", "4",
"50", "true", "true");
"50", "true", "true",
"false");

}

0 comments on commit bff70a0

Please sign in to comment.