-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
100 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ hs_err_pid*.log | |
|
||
#Run | ||
/run | ||
/backups | ||
|
||
# secrets | ||
**.secret |
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
75 changes: 75 additions & 0 deletions
75
src/main/java/com/gamebuster19901/excite/backup/Backup.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,75 @@ | ||
package com.gamebuster19901.excite.backup; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOError; | ||
import java.io.IOException; | ||
import java.nio.file.FileVisitResult; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.SimpleFileVisitor; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.time.Instant; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
import com.gamebuster19901.excite.bot.command.MessageContext; | ||
|
||
public class Backup { | ||
|
||
public static final String DIR_TO_BACKUP = "./run/"; | ||
public static final String BACKUP_STORAGE = "./backups/"; | ||
static { | ||
if(new File(DIR_TO_BACKUP).exists()) { | ||
File BACKUP_DIR = new File(BACKUP_STORAGE); | ||
if(!BACKUP_DIR.exists()) { | ||
BACKUP_DIR.mkdirs(); | ||
} | ||
} | ||
else { | ||
throw new AssertionError(new FileNotFoundException()); | ||
} | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
public static int backup(MessageContext context) { | ||
if(context.isOperator()) { | ||
context.sendMessage("Backing up data..."); | ||
backup(DIR_TO_BACKUP); | ||
context.sendMessage("Backup complete!"); | ||
} | ||
else { | ||
context.sendMessage("You do not have permission to execute this command"); | ||
} | ||
return 0; | ||
} | ||
|
||
private static void backup(String dirPath) { | ||
final Path sourceDir = Paths.get(dirPath); | ||
String zipFileName = BACKUP_STORAGE + Instant.now().toString() + ".zip"; | ||
try { | ||
final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(zipFileName)); | ||
Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() { | ||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) { | ||
try { | ||
Path targetFile = sourceDir.relativize(file); | ||
outputStream.putNextEntry(new ZipEntry(targetFile.toString())); | ||
byte[] bytes = Files.readAllBytes(file); | ||
outputStream.write(bytes, 0, bytes.length); | ||
outputStream.closeEntry(); | ||
} catch (IOException e) { | ||
throw new IOError(e); | ||
} | ||
return FileVisitResult.CONTINUE; | ||
} | ||
}); | ||
outputStream.close(); | ||
} catch (IOException e) { | ||
throw new IOError(e); | ||
} | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/gamebuster19901/excite/bot/command/BackupCommand.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,15 @@ | ||
package com.gamebuster19901.excite.bot.command; | ||
|
||
import com.gamebuster19901.excite.backup.Backup; | ||
import com.mojang.brigadier.CommandDispatcher; | ||
|
||
public class BackupCommand { | ||
|
||
@SuppressWarnings("rawtypes") | ||
public static void register(CommandDispatcher<MessageContext> dispatcher) { | ||
dispatcher.register(Commands.literal("!backup").executes((context) -> { | ||
return Backup.backup(context.getSource()); | ||
})); | ||
} | ||
|
||
} |
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