From 2eddf3cc2a070aee0b98e4c96d330b55d076fe63 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Mon, 4 Jul 2022 08:53:26 +0200 Subject: [PATCH] add more cli output, moving backups in their own folders, added vacuum to automaticly delete old backups to prevent high storage usage --- .../sysbackup/cli/CLIProcessor.java | 9 ++++- .../sysbackup/tasks/TaskBuilder.java | 40 +++++++++++++++++-- .../sysbackup/tasks/impl/VacuumTask.java | 35 ++++++++++++++++ src/main/resources/exampletask.toml | 5 +++ 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/horizoncode/sysbackup/tasks/impl/VacuumTask.java diff --git a/src/main/java/net/horizoncode/sysbackup/cli/CLIProcessor.java b/src/main/java/net/horizoncode/sysbackup/cli/CLIProcessor.java index fd96853..4b9cef2 100644 --- a/src/main/java/net/horizoncode/sysbackup/cli/CLIProcessor.java +++ b/src/main/java/net/horizoncode/sysbackup/cli/CLIProcessor.java @@ -3,6 +3,7 @@ package net.horizoncode.sysbackup.cli; import net.horizoncode.sysbackup.config.Config; import net.horizoncode.sysbackup.tasks.TaskBuilder; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; import org.tomlj.Toml; import org.tomlj.TomlParseResult; @@ -54,9 +55,14 @@ public class CLIProcessor { return; } + System.out.println("setuping TaskBuilder..."); Config taskConfig = new Config(taskFile); TaskBuilder taskBuilder = - TaskBuilder.builder().executionPath(executionPath).taskConfig(taskConfig).build(); + TaskBuilder.builder() + .executionPath(executionPath) + .taskName(FilenameUtils.removeExtension(taskFile.getName())) + .taskConfig(taskConfig) + .build(); taskBuilder.start(); break; } @@ -84,6 +90,7 @@ public class CLIProcessor { return; } String fileName = args[1]; + System.out.println(executionPath.getAbsolutePath()); File tasksFolder = new File(executionPath, "tasks"); if (!tasksFolder.exists()) if (!tasksFolder.mkdir()) System.err.println("Failed to create tasks folder!"); diff --git a/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java b/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java index 7fc036c..2d2336f 100644 --- a/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java +++ b/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java @@ -5,13 +5,18 @@ import lombok.Getter; import net.horizoncode.sysbackup.config.Config; import net.horizoncode.sysbackup.tasks.impl.DatabaseTask; import net.horizoncode.sysbackup.tasks.impl.FileSystemTask; +import net.horizoncode.sysbackup.tasks.impl.VacuumTask; import org.apache.commons.io.FilenameUtils; import org.tomlj.TomlArray; import java.io.File; import java.text.SimpleDateFormat; +import java.time.temporal.ChronoUnit; +import java.util.Arrays; import java.util.Date; +import java.util.Objects; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; @Builder @@ -20,16 +25,25 @@ public class TaskBuilder { private final Config taskConfig; + private final String taskName; + @Builder.Default private final LinkedBlockingQueue taskList = new LinkedBlockingQueue<>(); private final File executionPath; 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.mkdir()) { - System.err.println("Failed to create backups directory!"); + System.err.println("Failed to create backup directory!"); System.exit(2); } @@ -38,7 +52,7 @@ public class TaskBuilder { getTaskConfig().getStringOrDefault("general.dateFormat", "yyyy-MM-dd HH-mm-ss")); String fileName = getTaskConfig().getStringOrDefault("general.outputFile", "{date} - {taskName}") + ".zip"; - + boolean doVAC = getTaskConfig().getBooleanOrDefault("vacuum.enabled", false); boolean doFS = getTaskConfig().getBooleanOrDefault("filesystem.enabled", false); boolean doDB = getTaskConfig().getBooleanOrDefault("mysql.enabled", false); @@ -50,13 +64,31 @@ public class TaskBuilder { .replace("{date}", sdf.format(new Date())); 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")) { + System.out.println("Adding FileSystemTask..."); TomlArray filesArray = getTaskConfig().getArray("filesystem.targets"); IntStream.range(0, filesArray.size()) .forEach( value -> { String target = filesArray.getString(value); + System.out.println("Adding \"" + target + "\""); taskList.add( new FileSystemTask(target, outputFile) { @Override @@ -80,6 +112,8 @@ public class TaskBuilder { .password(password.toCharArray()) .build(); + System.out.println("Adding DatabaseTask for database \"" + database + "\""); + taskList.add( new DatabaseTask(databaseCredentials, outputFile) { @Override diff --git a/src/main/java/net/horizoncode/sysbackup/tasks/impl/VacuumTask.java b/src/main/java/net/horizoncode/sysbackup/tasks/impl/VacuumTask.java new file mode 100644 index 0000000..ffbde94 --- /dev/null +++ b/src/main/java/net/horizoncode/sysbackup/tasks/impl/VacuumTask.java @@ -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(); + } +} diff --git a/src/main/resources/exampletask.toml b/src/main/resources/exampletask.toml index 5ec1e97..1a2365d 100644 --- a/src/main/resources/exampletask.toml +++ b/src/main/resources/exampletask.toml @@ -2,6 +2,11 @@ dateFormat = "yyyy-MM-dd HH-mm-ss" outputFile = "{date} - {taskName}" +[vacuum] +enabled = true +time = 5 +unit = "DAYS" # See java.util.concurrent.TimeUnit + [mysql] enabled = true database = "magento"