initial push
This commit is contained in:
commit
75e95e8430
65
.gitignore
vendored
Normal file
65
.gitignore
vendored
Normal file
|
@ -0,0 +1,65 @@
|
|||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
# AWS User-specific
|
||||
.idea/**/aws.xml
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
# File-based project format
|
||||
*.iws
|
||||
# IntelliJ
|
||||
out/
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
# SonarLint plugin
|
||||
.idea/sonarlint/
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
target/
|
||||
run/
|
||||
/target/
|
||||
/run/
|
||||
/dependency-reduced-pom.xml
|
3
.idea/.gitignore
vendored
Normal file
3
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
14
.idea/compiler.xml
Normal file
14
.idea/compiler.xml
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile default="true" name="Default" enabled="true" />
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="SysBackup" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
7
.idea/encodings.xml
Normal file
7
.idea/encodings.xml
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
6
.idea/google-java-format.xml
Normal file
6
.idea/google-java-format.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GoogleJavaFormatSettings">
|
||||
<option name="enabled" value="true" />
|
||||
</component>
|
||||
</project>
|
20
.idea/jarRepositories.xml
Normal file
20
.idea/jarRepositories.xml
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
14
.idea/misc.xml
Normal file
14
.idea/misc.xml
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
115
pom.xml
Normal file
115
pom.xml
Normal file
|
@ -0,0 +1,115 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>net.horizoncode</groupId>
|
||||
<artifactId>SysBackup</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<name>SysBackup</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jline</groupId>
|
||||
<artifactId>jline</artifactId>
|
||||
<version>3.21.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.tomlj</groupId>
|
||||
<artifactId>tomlj</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.24</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.11.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>31.1-jre</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.lingala.zip4j</groupId>
|
||||
<artifactId>zip4j</artifactId>
|
||||
<version>2.11.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>me.tongfei</groupId>
|
||||
<artifactId>progressbar</artifactId>
|
||||
<version>0.9.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
<!-- testing dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<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>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
8
src/main/java/Bootstrapper.java
Normal file
8
src/main/java/Bootstrapper.java
Normal file
|
@ -0,0 +1,8 @@
|
|||
import net.horizoncode.sysbackup.App;
|
||||
|
||||
public class Bootstrapper {
|
||||
|
||||
public static void main(String[] args) {
|
||||
App.getInstance().start(args);
|
||||
}
|
||||
}
|
17
src/main/java/net/horizoncode/sysbackup/App.java
Normal file
17
src/main/java/net/horizoncode/sysbackup/App.java
Normal file
|
@ -0,0 +1,17 @@
|
|||
package net.horizoncode.sysbackup;
|
||||
|
||||
import net.horizoncode.sysbackup.cli.CLIProcessor;
|
||||
|
||||
public class App {
|
||||
private static App instance;
|
||||
|
||||
public static App getInstance() {
|
||||
if (instance != null) return instance;
|
||||
return instance = new App();
|
||||
}
|
||||
|
||||
public void start(String[] args){
|
||||
CLIProcessor cliProcessor = new CLIProcessor();
|
||||
cliProcessor.startCLI(args);
|
||||
}
|
||||
}
|
123
src/main/java/net/horizoncode/sysbackup/cli/CLIProcessor.java
Normal file
123
src/main/java/net/horizoncode/sysbackup/cli/CLIProcessor.java
Normal file
|
@ -0,0 +1,123 @@
|
|||
package net.horizoncode.sysbackup.cli;
|
||||
|
||||
import net.horizoncode.sysbackup.App;
|
||||
import net.horizoncode.sysbackup.config.Config;
|
||||
import net.horizoncode.sysbackup.tasks.TaskBuilder;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.tomlj.Toml;
|
||||
import org.tomlj.TomlParseResult;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
|
||||
public class CLIProcessor {
|
||||
|
||||
public static void usage() {
|
||||
String[] usage = {
|
||||
"Usage: java -jar sysbackup.jar [action] <args>",
|
||||
" Backup:",
|
||||
" backup <backup task> create a backup based on a backup task configuration file",
|
||||
" Miscellaneous:",
|
||||
" checkTaskConf <file name> checks a backup task configuration file",
|
||||
" generateTaskConf <file name> generate a example backup task configuration file",
|
||||
" Examples:",
|
||||
" java -jar sysbackup.jar generateTaskConf magento",
|
||||
" java -jar sysbackup.jar backup magento"
|
||||
};
|
||||
for (String u : usage) {
|
||||
System.out.println(u);
|
||||
}
|
||||
}
|
||||
|
||||
public void startCLI(String[] args) {
|
||||
try {
|
||||
if ((args == null) || (args.length == 0)) {
|
||||
usage();
|
||||
return;
|
||||
}
|
||||
|
||||
for (int index = 0; index < args.length; index++) {
|
||||
switch (args[index].toLowerCase(Locale.ROOT)) {
|
||||
case "backup":
|
||||
{
|
||||
if (args.length <= 1) {
|
||||
System.err.println("Please specify a output task config name!");
|
||||
return;
|
||||
}
|
||||
String fileName = args[1];
|
||||
File tasksFolder = new File("tasks");
|
||||
if (!tasksFolder.exists())
|
||||
if (!tasksFolder.mkdir()) System.err.println("Failed to create tasks folder!");
|
||||
File taskFile = new File(tasksFolder, fileName + ".toml");
|
||||
if (!taskFile.exists()) {
|
||||
System.err.println("TaskFile " + fileName + ".toml does not exist!");
|
||||
return;
|
||||
}
|
||||
|
||||
Config taskConfig = new Config(taskFile);
|
||||
TaskBuilder taskBuilder = TaskBuilder.builder().taskConfig(taskConfig).build();
|
||||
taskBuilder.start();
|
||||
break;
|
||||
}
|
||||
case "generatetaskconf":
|
||||
{
|
||||
if (args.length <= 1) {
|
||||
System.err.println("Please specify a output task config name!");
|
||||
return;
|
||||
}
|
||||
String fileName = args[1];
|
||||
File tasksFolder = new File("tasks");
|
||||
if (!tasksFolder.exists())
|
||||
if (!tasksFolder.mkdir()) System.err.println("Failed to create tasks folder!");
|
||||
System.out.println("Saving task config " + fileName + ".toml...");
|
||||
FileUtils.copyInputStreamToFile(
|
||||
Objects.requireNonNull(App.class.getResourceAsStream("/" + "exampletask.toml")),
|
||||
new File(tasksFolder, fileName + ".toml"));
|
||||
System.out.println(fileName + ".toml saved!");
|
||||
break;
|
||||
}
|
||||
case "checktaskconf":
|
||||
{
|
||||
if (args.length <= 1) {
|
||||
System.err.println("Please specify a output task config name!");
|
||||
return;
|
||||
}
|
||||
String fileName = args[1];
|
||||
File tasksFolder = new File("tasks");
|
||||
if (!tasksFolder.exists())
|
||||
if (!tasksFolder.mkdir()) System.err.println("Failed to create tasks folder!");
|
||||
File taskFile = new File(tasksFolder, fileName + ".toml");
|
||||
if (!taskFile.exists()) {
|
||||
System.err.println("TaskFile " + fileName + ".toml does not exist!");
|
||||
return;
|
||||
}
|
||||
TomlParseResult toml;
|
||||
try {
|
||||
toml = Toml.parse(taskFile.toPath());
|
||||
} catch (IOException e) {
|
||||
System.err.println("failed to read TaskFile.");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (toml.hasErrors()) {
|
||||
System.err.printf(
|
||||
"TaskFile checked: found %d issues!:\n", (long) toml.errors().size());
|
||||
toml.errors().forEach(error -> System.err.println(error.toString()));
|
||||
} else {
|
||||
System.out.println("TaskFile checked successfully: no issues found!");
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
if (index == 0) {
|
||||
usage();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
79
src/main/java/net/horizoncode/sysbackup/config/Config.java
Normal file
79
src/main/java/net/horizoncode/sysbackup/config/Config.java
Normal file
|
@ -0,0 +1,79 @@
|
|||
package net.horizoncode.sysbackup.config;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import net.horizoncode.sysbackup.App;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.tomlj.Toml;
|
||||
import org.tomlj.TomlArray;
|
||||
import org.tomlj.TomlParseResult;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
@Getter
|
||||
public class Config {
|
||||
|
||||
private final File configFile;
|
||||
|
||||
private TomlParseResult toml;
|
||||
|
||||
private boolean justCreated;
|
||||
|
||||
public Config(File configFile) {
|
||||
this.configFile = configFile;
|
||||
if (!configFile.exists()) {
|
||||
try {
|
||||
FileUtils.copyInputStreamToFile(
|
||||
Objects.requireNonNull(App.class.getResourceAsStream("/" + configFile.getName())),
|
||||
configFile);
|
||||
justCreated = true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
if (configFile.isDirectory()) {
|
||||
try {
|
||||
FileUtils.copyInputStreamToFile(
|
||||
Objects.requireNonNull(App.class.getResourceAsStream("/" + configFile.getName())),
|
||||
configFile);
|
||||
justCreated = true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (justCreated) return;
|
||||
|
||||
try {
|
||||
toml = Toml.parse(configFile.toPath());
|
||||
if (toml.hasErrors()) {
|
||||
toml.errors().forEach(error -> System.err.println(error.toString()));
|
||||
System.exit(-1);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public int getIntOrDefault(String key, int defaultValue) {
|
||||
return getToml().contains(key) && getToml().get(key) instanceof Number
|
||||
? Math.toIntExact((long) getToml().get(key))
|
||||
: defaultValue;
|
||||
}
|
||||
|
||||
public boolean getBooleanOrDefault(String key, boolean defaultValue) {
|
||||
return getToml().contains(key) && getToml().get(key) instanceof Boolean
|
||||
? getToml().getBoolean(key)
|
||||
: defaultValue;
|
||||
}
|
||||
|
||||
public String getStringOrDefault(String key, String defaultValue) {
|
||||
return getToml().contains(key) ? getToml().getString(key) : defaultValue;
|
||||
}
|
||||
|
||||
public TomlArray getArray(String key) {
|
||||
return getToml().getArrayOrEmpty(key);
|
||||
}
|
||||
}
|
7
src/main/java/net/horizoncode/sysbackup/tasks/Task.java
Normal file
7
src/main/java/net/horizoncode/sysbackup/tasks/Task.java
Normal file
|
@ -0,0 +1,7 @@
|
|||
package net.horizoncode.sysbackup.tasks;
|
||||
|
||||
public class Task {
|
||||
public void start() {}
|
||||
|
||||
public void onDone() {}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package net.horizoncode.sysbackup.tasks;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import net.horizoncode.sysbackup.config.Config;
|
||||
import net.horizoncode.sysbackup.tasks.impl.FileSystemTask;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.tomlj.TomlArray;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class TaskBuilder {
|
||||
|
||||
private final Config taskConfig;
|
||||
|
||||
@Builder.Default private final LinkedBlockingQueue<Task> taskList = new LinkedBlockingQueue<>();
|
||||
|
||||
public void start() {
|
||||
|
||||
File backupDir = new File("backups");
|
||||
if (!backupDir.exists())
|
||||
if (!backupDir.mkdir()) {
|
||||
System.err.println("Failed to create backups directory!");
|
||||
System.exit(2);
|
||||
}
|
||||
|
||||
SimpleDateFormat sdf =
|
||||
new SimpleDateFormat(
|
||||
getTaskConfig().getStringOrDefault("general.dateFormat", "yyyy-MM-dd HH-mm-ss"));
|
||||
String fileName =
|
||||
getTaskConfig().getStringOrDefault("filesystem.fileName", "{date} {taskName}") + ".zip";
|
||||
|
||||
fileName =
|
||||
fileName
|
||||
.replace(
|
||||
"{taskName}",
|
||||
FilenameUtils.removeExtension(getTaskConfig().getConfigFile().getName()))
|
||||
.replace("{date}", sdf.format(new Date()));
|
||||
|
||||
File outputFile = new File(backupDir, fileName);
|
||||
|
||||
if (getTaskConfig().getToml().contains("filesystem.targets")) {
|
||||
TomlArray filesArray = getTaskConfig().getArray("filesystem.targets");
|
||||
|
||||
IntStream.range(0, filesArray.size())
|
||||
.forEach(
|
||||
value -> {
|
||||
String target = filesArray.getString(value);
|
||||
taskList.add(
|
||||
new FileSystemTask(target, outputFile) {
|
||||
@Override
|
||||
public void onDone() {
|
||||
executeNextTask();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
executeNextTask();
|
||||
}
|
||||
|
||||
private void executeNextTask() {
|
||||
Task nextTask = taskList.poll();
|
||||
if (nextTask != null) nextTask.start();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package net.horizoncode.sysbackup.tasks.impl;
|
||||
|
||||
import me.tongfei.progressbar.ProgressBar;
|
||||
import me.tongfei.progressbar.ProgressBarBuilder;
|
||||
import me.tongfei.progressbar.ProgressBarStyle;
|
||||
import net.horizoncode.sysbackup.tasks.Task;
|
||||
import net.horizoncode.sysbackup.threading.ThreadPool;
|
||||
import net.lingala.zip4j.ZipFile;
|
||||
import net.lingala.zip4j.progress.ProgressMonitor;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class FileSystemTask extends Task {
|
||||
|
||||
private final File target;
|
||||
private File outputZipFile;
|
||||
|
||||
private final ThreadPool threadPool;
|
||||
|
||||
public FileSystemTask(String folderOrFilePath, File outputZipFile) {
|
||||
this.threadPool = new ThreadPool(3, 10);
|
||||
this.target = Paths.get(folderOrFilePath).toFile();
|
||||
if (!target.exists()) {
|
||||
onDone();
|
||||
System.err.println("File or folder named \"" + folderOrFilePath + "\" does not exist.");
|
||||
System.exit(2);
|
||||
return;
|
||||
}
|
||||
|
||||
this.outputZipFile = outputZipFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
threadPool
|
||||
.getPool()
|
||||
.submit(
|
||||
() -> {
|
||||
try (ZipFile zipFile = new ZipFile(outputZipFile)) {
|
||||
System.out.println("Indexing files...");
|
||||
ProgressMonitor progressMonitor = zipFile.getProgressMonitor();
|
||||
zipFile.setRunInThread(true);
|
||||
zipFile.addFolder(target);
|
||||
ProgressBarBuilder pbb =
|
||||
new ProgressBarBuilder()
|
||||
.setStyle(ProgressBarStyle.ASCII)
|
||||
.setInitialMax(progressMonitor.getTotalWork())
|
||||
.setTaskName("Adding Files...");
|
||||
try (ProgressBar pb = pbb.build()) {
|
||||
while (!progressMonitor.getState().equals(ProgressMonitor.State.READY)) {
|
||||
pb.stepTo(progressMonitor.getWorkCompleted());
|
||||
Thread.sleep(1);
|
||||
}
|
||||
pb.stepTo(progressMonitor.getTotalWork());
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
onDone();
|
||||
}
|
||||
progressMonitor.endProgressMonitor();
|
||||
onDone();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
onDone();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package net.horizoncode.sysbackup.threading;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
|
||||
public class ThreadPool {
|
||||
|
||||
@Getter private final ExecutorService pool;
|
||||
|
||||
public ThreadPool(int corePoolSize, int maxPoolSize) {
|
||||
this.pool = scheduledExecutorService(corePoolSize, maxPoolSize);
|
||||
}
|
||||
|
||||
private ScheduledExecutorService scheduledExecutorService(int corePoolSize, int maxPoolSize) {
|
||||
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor =
|
||||
new ScheduledThreadPoolExecutor(corePoolSize);
|
||||
scheduledThreadPoolExecutor.setMaximumPoolSize(maxPoolSize);
|
||||
return scheduledThreadPoolExecutor;
|
||||
}
|
||||
}
|
16
src/main/resources/exampletask.toml
Normal file
16
src/main/resources/exampletask.toml
Normal file
|
@ -0,0 +1,16 @@
|
|||
[general]
|
||||
dateFormat = "yyyy-MM-dd"
|
||||
|
||||
[mysql]
|
||||
enabled = true
|
||||
host = ""
|
||||
port = 3306
|
||||
database = "magento"
|
||||
user = ""
|
||||
password = ""
|
||||
fileName = "{date} - {taskName}"
|
||||
|
||||
[filesystem]
|
||||
targets = ["/home/magento/", "/home/test/test.ini"]
|
||||
fileName = "{date} - {taskName}"
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package net.horizoncode.sysbackup;
|
||||
|
||||
import net.horizoncode.sysbackup.config.Config;
|
||||
import org.junit.Test;
|
||||
import org.tomlj.TomlArray;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class TOMLArrayIterationTest {
|
||||
|
||||
@Test
|
||||
public void testTOMLIteration() {
|
||||
File tasksFolder = new File("tasks");
|
||||
if (!tasksFolder.exists())
|
||||
if (!tasksFolder.mkdir()) System.err.println("Failed to create tasks folder!");
|
||||
Config config = new Config(new File(tasksFolder, "magento.toml"));
|
||||
if (config.getToml().contains("filesystem.targets")) {
|
||||
TomlArray filesArray = config.getArray("filesystem.targets");
|
||||
|
||||
IntStream.range(0, filesArray.size())
|
||||
.forEach(
|
||||
value -> {
|
||||
String target = filesArray.getString(value);
|
||||
System.out.println(target);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user