fixes lmao

This commit is contained in:
HorizonCode 2022-06-29 16:34:16 +02:00
parent 75e95e8430
commit 37f64392da
4 changed files with 91 additions and 2 deletions

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -34,7 +34,7 @@ public class TaskBuilder {
new SimpleDateFormat( new SimpleDateFormat(
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("filesystem.fileName", "{date} {taskName}") + ".zip"; getTaskConfig().getStringOrDefault("filesystem.fileName", "{date} - {taskName}") + ".zip";
fileName = fileName =
fileName fileName
@ -68,5 +68,9 @@ public class TaskBuilder {
private void executeNextTask() { private void executeNextTask() {
Task nextTask = taskList.poll(); Task nextTask = taskList.poll();
if (nextTask != null) nextTask.start(); if (nextTask != null) nextTask.start();
else {
System.out.println("Backup completed!");
System.out.println(0);
}
} }
} }

View File

@ -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;
}
}

View File

@ -3,12 +3,16 @@ package net.horizoncode.sysbackup.tasks.impl;
import me.tongfei.progressbar.ProgressBar; import me.tongfei.progressbar.ProgressBar;
import me.tongfei.progressbar.ProgressBarBuilder; import me.tongfei.progressbar.ProgressBarBuilder;
import me.tongfei.progressbar.ProgressBarStyle; import me.tongfei.progressbar.ProgressBarStyle;
import me.tongfei.progressbar.TerminalUtils;
import net.horizoncode.sysbackup.tasks.Task; import net.horizoncode.sysbackup.tasks.Task;
import net.horizoncode.sysbackup.threading.ThreadPool; import net.horizoncode.sysbackup.threading.ThreadPool;
import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.progress.ProgressMonitor; import net.lingala.zip4j.progress.ProgressMonitor;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Paths; import java.nio.file.Paths;
public class FileSystemTask extends Task { public class FileSystemTask extends Task {
@ -37,6 +41,16 @@ public class FileSystemTask extends Task {
.getPool() .getPool()
.submit( .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)) { try (ZipFile zipFile = new ZipFile(outputZipFile)) {
System.out.println("Indexing files..."); System.out.println("Indexing files...");
ProgressMonitor progressMonitor = zipFile.getProgressMonitor(); ProgressMonitor progressMonitor = zipFile.getProgressMonitor();
@ -47,10 +61,13 @@ public class FileSystemTask extends Task {
.setStyle(ProgressBarStyle.ASCII) .setStyle(ProgressBarStyle.ASCII)
.setInitialMax(progressMonitor.getTotalWork()) .setInitialMax(progressMonitor.getTotalWork())
.setTaskName("Adding Files..."); .setTaskName("Adding Files...");
if (terminalWidth != -1) pbb.setMaxRenderedLength(terminalWidth);
try (ProgressBar pb = pbb.build()) { try (ProgressBar pb = pbb.build()) {
while (!progressMonitor.getState().equals(ProgressMonitor.State.READY)) { while (!progressMonitor.getState().equals(ProgressMonitor.State.READY)) {
pb.stepTo(progressMonitor.getWorkCompleted()); pb.stepTo(progressMonitor.getWorkCompleted());
Thread.sleep(1); Thread.sleep(100);
} }
pb.stepTo(progressMonitor.getTotalWork()); pb.stepTo(progressMonitor.getTotalWork());
} catch (Exception exception) { } catch (Exception exception) {