-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48fa9eb
commit 3db5571
Showing
6 changed files
with
207 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/io/github/sakurawald/module/common/service/AutomaticBackup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package io.github.sakurawald.module.common.service; | ||
|
||
import io.github.sakurawald.Fuji; | ||
import io.github.sakurawald.config.Configs; | ||
import io.github.sakurawald.util.FileUtil; | ||
import lombok.experimental.UtilityClass; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.FileVisitResult; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.SimpleFileVisitor; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@UtilityClass | ||
public class AutomaticBackup { | ||
|
||
public static final Path BACKUP_PATH = Fuji.CONFIG_PATH.resolve("backup"); | ||
|
||
private static boolean skipPath(Path dir) { | ||
for (String other : Configs.configHandler.model().common.backup.skip) { | ||
if (dir.equals(Fuji.CONFIG_PATH.resolve(other))) return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static List<File> getInputFiles() { | ||
List<File> files = new ArrayList<>(); | ||
try { | ||
Files.walkFileTree(Fuji.CONFIG_PATH, new SimpleFileVisitor<>() { | ||
|
||
@Override | ||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { | ||
if (BACKUP_PATH.equals(dir)) return FileVisitResult.SKIP_SUBTREE; | ||
if (skipPath(dir)) return FileVisitResult.SKIP_SUBTREE; | ||
|
||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { | ||
files.add(file.toFile()); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
}); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return files; | ||
} | ||
|
||
private static void trimBackup() { | ||
List<Path> latestFiles = FileUtil.getLatestFiles(BACKUP_PATH); | ||
Iterator<Path> iterator = latestFiles.iterator(); | ||
while (iterator.hasNext() && latestFiles.size() > Configs.configHandler.model().common.backup.max_slots - 1) { | ||
iterator.next().toFile().delete(); | ||
iterator.remove(); | ||
} | ||
} | ||
|
||
private static File getOutputFile() { | ||
String fileName = System.currentTimeMillis() + ".zip"; | ||
return BACKUP_PATH.resolve(fileName).toFile(); | ||
} | ||
|
||
private static void newBackup() { | ||
BACKUP_PATH.toFile().mkdirs(); | ||
FileUtil.compressFiles(getInputFiles(), getOutputFile()); | ||
} | ||
|
||
public static void backup() { | ||
trimBackup(); | ||
newBackup(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.