Compare commits

...

6 Commits

7 changed files with 166 additions and 74 deletions

22
.idea/artifacts/SysBackup_jar.xml generated Normal file
View File

@ -0,0 +1,22 @@
<component name="ArtifactManager">
<artifact type="jar" name="SysBackup:jar">
<output-path>$PROJECT_DIR$/out/artifacts/SysBackup_jar</output-path>
<root id="archive" name="SysBackup.jar">
<element id="module-output" name="SysBackup" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/com/google/guava/guava/31.1-jre/guava-31.1-jre.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/net/lingala/zip4j/zip4j/2.11.1/zip4j-2.11.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/commons-io/commons-io/2.11.0/commons-io-2.11.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/me/tongfei/progressbar/0.9.3/progressbar-0.9.3.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/jline/jline/3.21.0/jline-3.21.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/com/google/errorprone/error_prone_annotations/2.11.0/error_prone_annotations-2.11.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/checkerframework/checker-qual/3.12.0/checker-qual-3.12.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.12.0/commons-lang3-3.12.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar" path-in-jar="/" />
</root>
</artifact>
</component>

56
pom.xml
View File

@ -7,6 +7,7 @@
<groupId>net.horizoncode</groupId>
<artifactId>SysBackup</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SysBackup</name>
@ -70,44 +71,25 @@
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Bootstrapper</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>

View File

@ -3,11 +3,13 @@ package net.horizoncode.sysbackup.tasks;
import lombok.Builder;
import lombok.Getter;
import net.horizoncode.sysbackup.config.Config;
import net.horizoncode.sysbackup.tasks.impl.DatabaseTask;
import net.horizoncode.sysbackup.tasks.impl.FileSystemTask;
import org.apache.commons.io.FilenameUtils;
import org.tomlj.TomlArray;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
@ -34,7 +36,10 @@ 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("general.outputFile", "{date} - {taskName}") + ".zip";
boolean doFS = getTaskConfig().getBooleanOrDefault("filesystem.enabled", false);
boolean doDB = getTaskConfig().getBooleanOrDefault("mysql.enabled", false);
fileName =
fileName
@ -44,8 +49,7 @@ public class TaskBuilder {
.replace("{date}", sdf.format(new Date()));
File outputFile = new File(backupDir, fileName);
if (getTaskConfig().getToml().contains("filesystem.targets")) {
if (doFS && getTaskConfig().getToml().contains("filesystem.targets")) {
TomlArray filesArray = getTaskConfig().getArray("filesystem.targets");
IntStream.range(0, filesArray.size())
@ -62,6 +66,31 @@ public class TaskBuilder {
});
}
if (doDB) {
String database = getTaskConfig().getStringOrDefault("mysql.database", "");
String user = getTaskConfig().getStringOrDefault("mysql.user", "");
String password = getTaskConfig().getStringOrDefault("mysql.password", "");
if (!database.isEmpty() && !user.isEmpty() && !password.isEmpty()) {
DatabaseTask.DatabaseCredentials databaseCredentials =
DatabaseTask.DatabaseCredentials.builder()
.database(database)
.username(user)
.password(password.toCharArray())
.build();
taskList.add(
new DatabaseTask(databaseCredentials, outputFile) {
@Override
public void onDone() {
executeNextTask();
}
});
} else {
System.err.println("username, password or database is empty.");
}
}
executeNextTask();
}
@ -70,7 +99,7 @@ public class TaskBuilder {
if (nextTask != null) nextTask.start();
else {
System.out.println("Backup completed!");
System.out.println(0);
System.exit(0);
}
}
}

View File

@ -2,17 +2,20 @@ package net.horizoncode.sysbackup.tasks.impl;
import lombok.Builder;
import lombok.Getter;
import me.tongfei.progressbar.ProgressBar;
import me.tongfei.progressbar.ProgressBarBuilder;
import me.tongfei.progressbar.ProgressBarStyle;
import net.horizoncode.sysbackup.tasks.Task;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.progress.ProgressMonitor;
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.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
@Getter
public class DatabaseTask extends Task {
@ -29,24 +32,90 @@ public class DatabaseTask extends Task {
@Override
public void start() {
try {
String[] commandArgs =
new String[] {
"mysqldump",
"-u " + getDatabaseCredentials().username,
"-p" + new String(getDatabaseCredentials().password),
getDatabaseCredentials().database
};
String commandArgs =
"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,
String databaseContent = "";
while (process.isAlive()) {
try {
Thread.sleep(1000);
databaseContent =
new BufferedReader(
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))
.lines()
.collect(Collectors.joining("\n"));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
int exitValue = process.exitValue();
if (exitValue != 0) {
String text =
new BufferedReader(
new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))
.lines()
.collect(Collectors.joining("\n"));
System.out.println(text);
onDone();
return;
}
if (databaseContent.isEmpty()) {
System.err.println("database content is empty");
onDone();
return;
}
File outputSQLFile =
new File(
outputFile.getParent(),
String.format(
"%s-%s.sql",
getDatabaseCredentials().database, RandomStringUtils.random(16, true, true))));
getDatabaseCredentials().database, RandomStringUtils.random(16, true, true)));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputSQLFile));
writer.write(databaseContent);
writer.close();
System.out.println("Adding database to backup zip...");
try (ZipFile zipFile = new ZipFile(getOutputFile())) {
ProgressMonitor progressMonitor = zipFile.getProgressMonitor();
zipFile.setRunInThread(true);
zipFile.addFile(outputSQLFile);
ProgressBarBuilder pbb =
new ProgressBarBuilder()
.setStyle(ProgressBarStyle.ASCII)
.setInitialMax(progressMonitor.getTotalWork())
.setTaskName("Adding DB File...");
try (ProgressBar pb = pbb.build()) {
while (!progressMonitor.getState().equals(ProgressMonitor.State.READY)) {
pb.stepTo(progressMonitor.getWorkCompleted());
Thread.sleep(100);
}
pb.stepTo(progressMonitor.getTotalWork());
} catch (Exception exception) {
exception.printStackTrace();
FileUtils.deleteQuietly(outputSQLFile);
onDone();
}
progressMonitor.endProgressMonitor();
FileUtils.deleteQuietly(outputSQLFile);
onDone();
} catch (Exception ex) {
ex.printStackTrace();
onDone();
}
} catch (IOException e) {
throw new RuntimeException(e);
@ -54,7 +123,7 @@ public class DatabaseTask extends Task {
}
@Builder
private static class DatabaseCredentials {
public static class DatabaseCredentials {
private final String username;
private final char[] password;
private final String database;

View File

@ -41,16 +41,6 @@ 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();
@ -62,8 +52,6 @@ public class FileSystemTask extends Task {
.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());

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Bootstrapper

View File

@ -1,16 +1,15 @@
[general]
dateFormat = "yyyy-MM-dd"
dateFormat = "yyyy-MM-dd HH-mm-ss"
outputFile = "{date} - {taskName}"
[mysql]
enabled = true
host = ""
port = 3306
database = "magento"
user = ""
password = ""
fileName = "{date} - {taskName}"
[filesystem]
enabled = true
targets = ["/home/magento/", "/home/test/test.ini"]
fileName = "{date} - {taskName}"