From 37f64392dacf5340b031234188c43a3165a06882 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Wed, 29 Jun 2022 16:34:16 +0200 Subject: [PATCH] fixes lmao --- .idea/vcs.xml | 6 ++ .../sysbackup/tasks/TaskBuilder.java | 6 +- .../sysbackup/tasks/impl/DatabaseTask.java | 62 +++++++++++++++++++ .../sysbackup/tasks/impl/FileSystemTask.java | 19 +++++- 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 src/main/java/net/horizoncode/sysbackup/tasks/impl/DatabaseTask.java diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java b/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java index f5804da..380f9e8 100644 --- a/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java +++ b/src/main/java/net/horizoncode/sysbackup/tasks/TaskBuilder.java @@ -34,7 +34,7 @@ public class TaskBuilder { new SimpleDateFormat( getTaskConfig().getStringOrDefault("general.dateFormat", "yyyy-MM-dd HH-mm-ss")); String fileName = - getTaskConfig().getStringOrDefault("filesystem.fileName", "{date} {taskName}") + ".zip"; + getTaskConfig().getStringOrDefault("filesystem.fileName", "{date} - {taskName}") + ".zip"; fileName = fileName @@ -68,5 +68,9 @@ public class TaskBuilder { private void executeNextTask() { Task nextTask = taskList.poll(); if (nextTask != null) nextTask.start(); + else { + System.out.println("Backup completed!"); + System.out.println(0); + } } } diff --git a/src/main/java/net/horizoncode/sysbackup/tasks/impl/DatabaseTask.java b/src/main/java/net/horizoncode/sysbackup/tasks/impl/DatabaseTask.java new file mode 100644 index 0000000..3ad59bf --- /dev/null +++ b/src/main/java/net/horizoncode/sysbackup/tasks/impl/DatabaseTask.java @@ -0,0 +1,62 @@ +package net.horizoncode.sysbackup.tasks.impl; + +import lombok.Builder; +import lombok.Getter; +import net.horizoncode.sysbackup.tasks.Task; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.RandomStringUtils; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +@Getter +public class DatabaseTask extends Task { + + private final DatabaseCredentials databaseCredentials; + + private final File outputFile; + + public DatabaseTask(DatabaseCredentials credentials, File outputFile) { + this.databaseCredentials = credentials; + this.outputFile = outputFile; + } + + @Override + public void start() { + try { + String[] commandArgs = + new String[] { + "mysqldump", + "-u " + getDatabaseCredentials().username, + "-p" + new String(getDatabaseCredentials().password), + getDatabaseCredentials().database + }; + Runtime runtime = Runtime.getRuntime(); + Process process = runtime.exec(commandArgs); + + InputStream inputStream = process.getInputStream(); + FileUtils.copyInputStreamToFile( + inputStream, + new File( + outputFile.getParent(), + String.format( + "%s-%s.sql", + getDatabaseCredentials().database, RandomStringUtils.random(16, true, true)))); + + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Builder + private static class DatabaseCredentials { + private final String username; + private final char[] password; + private final String database; + } +} diff --git a/src/main/java/net/horizoncode/sysbackup/tasks/impl/FileSystemTask.java b/src/main/java/net/horizoncode/sysbackup/tasks/impl/FileSystemTask.java index aa26bb8..f337a53 100644 --- a/src/main/java/net/horizoncode/sysbackup/tasks/impl/FileSystemTask.java +++ b/src/main/java/net/horizoncode/sysbackup/tasks/impl/FileSystemTask.java @@ -3,12 +3,16 @@ package net.horizoncode.sysbackup.tasks.impl; import me.tongfei.progressbar.ProgressBar; import me.tongfei.progressbar.ProgressBarBuilder; import me.tongfei.progressbar.ProgressBarStyle; +import me.tongfei.progressbar.TerminalUtils; import net.horizoncode.sysbackup.tasks.Task; import net.horizoncode.sysbackup.threading.ThreadPool; import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.progress.ProgressMonitor; +import org.jline.terminal.Terminal; +import org.jline.terminal.TerminalBuilder; import java.io.File; +import java.io.IOException; import java.nio.file.Paths; public class FileSystemTask extends Task { @@ -37,6 +41,16 @@ public class FileSystemTask extends Task { .getPool() .submit( () -> { + int terminalWidth = -1; + try { + Terminal terminal = TerminalBuilder.terminal(); + try (terminal) { + terminalWidth = terminal.getWidth(); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + try (ZipFile zipFile = new ZipFile(outputZipFile)) { System.out.println("Indexing files..."); ProgressMonitor progressMonitor = zipFile.getProgressMonitor(); @@ -47,10 +61,13 @@ public class FileSystemTask extends Task { .setStyle(ProgressBarStyle.ASCII) .setInitialMax(progressMonitor.getTotalWork()) .setTaskName("Adding Files..."); + + if (terminalWidth != -1) pbb.setMaxRenderedLength(terminalWidth); + try (ProgressBar pb = pbb.build()) { while (!progressMonitor.getState().equals(ProgressMonitor.State.READY)) { pb.stepTo(progressMonitor.getWorkCompleted()); - Thread.sleep(1); + Thread.sleep(100); } pb.stepTo(progressMonitor.getTotalWork()); } catch (Exception exception) {