add more cli output, moving backups in their own folders, added vacuum to automaticly delete old backups to prevent high storage usage

This commit is contained in:
HorizonCode 2022-07-04 08:53:26 +02:00
parent ad208f9580
commit 2eddf3cc2a
4 changed files with 85 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package net.horizoncode.sysbackup.cli;
import net.horizoncode.sysbackup.config.Config; import net.horizoncode.sysbackup.config.Config;
import net.horizoncode.sysbackup.tasks.TaskBuilder; import net.horizoncode.sysbackup.tasks.TaskBuilder;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.tomlj.Toml; import org.tomlj.Toml;
import org.tomlj.TomlParseResult; import org.tomlj.TomlParseResult;
@ -54,9 +55,14 @@ public class CLIProcessor {
return; return;
} }
System.out.println("setuping TaskBuilder...");
Config taskConfig = new Config(taskFile); Config taskConfig = new Config(taskFile);
TaskBuilder taskBuilder = TaskBuilder taskBuilder =
TaskBuilder.builder().executionPath(executionPath).taskConfig(taskConfig).build(); TaskBuilder.builder()
.executionPath(executionPath)
.taskName(FilenameUtils.removeExtension(taskFile.getName()))
.taskConfig(taskConfig)
.build();
taskBuilder.start(); taskBuilder.start();
break; break;
} }
@ -84,6 +90,7 @@ public class CLIProcessor {
return; return;
} }
String fileName = args[1]; String fileName = args[1];
System.out.println(executionPath.getAbsolutePath());
File tasksFolder = new File(executionPath, "tasks"); File tasksFolder = new File(executionPath, "tasks");
if (!tasksFolder.exists()) if (!tasksFolder.exists())
if (!tasksFolder.mkdir()) System.err.println("Failed to create tasks folder!"); if (!tasksFolder.mkdir()) System.err.println("Failed to create tasks folder!");

View File

@ -5,13 +5,18 @@ import lombok.Getter;
import net.horizoncode.sysbackup.config.Config; import net.horizoncode.sysbackup.config.Config;
import net.horizoncode.sysbackup.tasks.impl.DatabaseTask; import net.horizoncode.sysbackup.tasks.impl.DatabaseTask;
import net.horizoncode.sysbackup.tasks.impl.FileSystemTask; import net.horizoncode.sysbackup.tasks.impl.FileSystemTask;
import net.horizoncode.sysbackup.tasks.impl.VacuumTask;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.tomlj.TomlArray; import org.tomlj.TomlArray;
import java.io.File; import java.io.File;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.Objects;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@Builder @Builder
@ -20,16 +25,25 @@ public class TaskBuilder {
private final Config taskConfig; private final Config taskConfig;
private final String taskName;
@Builder.Default private final LinkedBlockingQueue<Task> taskList = new LinkedBlockingQueue<>(); @Builder.Default private final LinkedBlockingQueue<Task> taskList = new LinkedBlockingQueue<>();
private final File executionPath; private final File executionPath;
public void start() { public void start() {
File backupDir = new File(executionPath, "backups"); File rootBackupDir = new File(executionPath, "backups");
if (!rootBackupDir.exists())
if (!rootBackupDir.mkdir()) {
System.err.println("Failed to create root backup directory!");
System.exit(2);
}
File backupDir = new File(rootBackupDir, getTaskName());
if (!backupDir.exists()) if (!backupDir.exists())
if (!backupDir.mkdir()) { if (!backupDir.mkdir()) {
System.err.println("Failed to create backups directory!"); System.err.println("Failed to create backup directory!");
System.exit(2); System.exit(2);
} }
@ -38,7 +52,7 @@ public class TaskBuilder {
getTaskConfig().getStringOrDefault("general.dateFormat", "yyyy-MM-dd HH-mm-ss")); getTaskConfig().getStringOrDefault("general.dateFormat", "yyyy-MM-dd HH-mm-ss"));
String fileName = String fileName =
getTaskConfig().getStringOrDefault("general.outputFile", "{date} - {taskName}") + ".zip"; getTaskConfig().getStringOrDefault("general.outputFile", "{date} - {taskName}") + ".zip";
boolean doVAC = getTaskConfig().getBooleanOrDefault("vacuum.enabled", false);
boolean doFS = getTaskConfig().getBooleanOrDefault("filesystem.enabled", false); boolean doFS = getTaskConfig().getBooleanOrDefault("filesystem.enabled", false);
boolean doDB = getTaskConfig().getBooleanOrDefault("mysql.enabled", false); boolean doDB = getTaskConfig().getBooleanOrDefault("mysql.enabled", false);
@ -50,13 +64,31 @@ public class TaskBuilder {
.replace("{date}", sdf.format(new Date())); .replace("{date}", sdf.format(new Date()));
File outputFile = new File(backupDir, fileName); File outputFile = new File(backupDir, fileName);
if (doVAC) {
TimeUnit unit =
TimeUnit.valueOf(getTaskConfig().getStringOrDefault("vacuum.unit", TimeUnit.DAYS.name()));
int value = getTaskConfig().getIntOrDefault("vacuum.time", 5);
System.out.printf("Adding VacuumTask with lifetime of %d %s%n", value, unit.name());
taskList.add(
new VacuumTask(backupDir, unit, value) {
@Override
public void onDone() {
executeNextTask();
}
});
}
if (doFS && getTaskConfig().getToml().contains("filesystem.targets")) { if (doFS && getTaskConfig().getToml().contains("filesystem.targets")) {
System.out.println("Adding FileSystemTask...");
TomlArray filesArray = getTaskConfig().getArray("filesystem.targets"); TomlArray filesArray = getTaskConfig().getArray("filesystem.targets");
IntStream.range(0, filesArray.size()) IntStream.range(0, filesArray.size())
.forEach( .forEach(
value -> { value -> {
String target = filesArray.getString(value); String target = filesArray.getString(value);
System.out.println("Adding \"" + target + "\"");
taskList.add( taskList.add(
new FileSystemTask(target, outputFile) { new FileSystemTask(target, outputFile) {
@Override @Override
@ -80,6 +112,8 @@ public class TaskBuilder {
.password(password.toCharArray()) .password(password.toCharArray())
.build(); .build();
System.out.println("Adding DatabaseTask for database \"" + database + "\"");
taskList.add( taskList.add(
new DatabaseTask(databaseCredentials, outputFile) { new DatabaseTask(databaseCredentials, outputFile) {
@Override @Override

View File

@ -0,0 +1,35 @@
package net.horizoncode.sysbackup.tasks.impl;
import lombok.Getter;
import net.horizoncode.sysbackup.tasks.Task;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Getter
public class VacuumTask extends Task {
private final File backupDir;
private final TimeUnit unit;
private final int value;
public VacuumTask(File backupDir, TimeUnit unit, int value) {
this.backupDir = backupDir;
this.unit = unit;
this.value = value;
}
@Override
public void start() {
if (backupDir.listFiles() != null) {
Arrays.stream(Objects.requireNonNull(backupDir.listFiles()))
.filter(file -> file.lastModified() + unit.toMillis(value) <= System.currentTimeMillis())
.forEachOrdered(File::deleteOnExit);
}
onDone();
}
}

View File

@ -2,6 +2,11 @@
dateFormat = "yyyy-MM-dd HH-mm-ss" dateFormat = "yyyy-MM-dd HH-mm-ss"
outputFile = "{date} - {taskName}" outputFile = "{date} - {taskName}"
[vacuum]
enabled = true
time = 5
unit = "DAYS" # See java.util.concurrent.TimeUnit
[mysql] [mysql]
enabled = true enabled = true
database = "magento" database = "magento"