Skip to content

Commit

Permalink
Add backup system
Browse files Browse the repository at this point in the history
Resolves #4
  • Loading branch information
Gamebuster19901 committed May 5, 2020
1 parent ed5438b commit 46a05b7
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ hs_err_pid*.log

#Run
/run
/backups

# secrets
**.secret
8 changes: 8 additions & 0 deletions src/main/java/com/gamebuster19901/excite/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

import javax.security.auth.login.LoginException;

import com.gamebuster19901.excite.backup.Backup;
import com.gamebuster19901.excite.bot.DiscordBot;
import com.gamebuster19901.excite.bot.command.Commands;
import com.gamebuster19901.excite.bot.command.MessageContext;
import com.gamebuster19901.excite.bot.server.DiscordServer;
import com.gamebuster19901.excite.bot.user.DiscordUser;
import com.gamebuster19901.excite.bot.user.UserPreferences;
Expand All @@ -31,6 +33,7 @@ public class Main {

private static ConcurrentLinkedDeque<String> consoleCommandsAwaitingProcessing = new ConcurrentLinkedDeque<String>();

@SuppressWarnings("rawtypes")
public static void main(String[] args) throws InterruptedException {

if(args.length % 2 != 0) {
Expand All @@ -57,6 +60,7 @@ public static void main(String[] args) throws InterruptedException {
Instant nextWiimmfiPing = Instant.now();
Instant nextDiscordPing = Instant.now();
Instant updateCooldowns = Instant.now();
Instant nextBackupTime = Instant.now();
startConsole();

try {
Expand All @@ -65,6 +69,10 @@ public static void main(String[] args) throws InterruptedException {
while(!consoleCommandsAwaitingProcessing.isEmpty()) {
Commands.DISPATCHER.handleCommand(consoleCommandsAwaitingProcessing.pollFirst());
}
if(nextBackupTime.isBefore(Instant.now())) {
Backup.backup(new MessageContext());
nextBackupTime = nextBackupTime.plus(Duration.ofHours(1));
}
if(nextWiimmfiPing.isBefore(Instant.now())) {
wiimmfi.update();
if(error == null) {
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/com/gamebuster19901/excite/backup/Backup.java
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);
}
}

}
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());
}));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public Commands() {
StopCommand.register(dispatcher);
AdminRoleCommand.register(dispatcher);
HelpCommand.register(dispatcher);
BackupCommand.register(dispatcher);
}

public void handleCommand(String command) {
Expand Down

0 comments on commit 46a05b7

Please sign in to comment.