Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Dry Run Command (#3)
Browse files Browse the repository at this point in the history
* Add rough dry run command

* Change CI command to shadowJar
  • Loading branch information
freddyheppell authored Jul 20, 2021
1 parent 503363a commit a7888f4
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
restore-keys: |
${{ runner.os }}-gradle-
- name: Build with Gradle
run: gradle jar
run: gradle shadowJar
- name: Stop Daemon
run: gradle --stop
- name: Upload a Build Artifact
Expand Down
14 changes: 6 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
plugins {
id 'java'
id 'maven-publish'
id 'com.github.johnrengelman.shadow' version '7.0.0'
}

repositories {
mavenCentral()
maven {
url = uri('https://repo.codemc.org/repository/maven-public')
}
mavenCentral()

maven {
url = uri('https://hub.spigotmc.org/nexus/content/repositories/snapshots/')
}
maven { url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
maven { url = 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url = 'https://nexus.hc.to/content/repositories/pub_releases' }
}

dependencies {
implementation 'com.amazonaws:aws-java-sdk-s3:1.11.690'
implementation 'org.bstats:bstats-bukkit:1.5'
compileOnly 'org.spigotmc:spigot-api:1.15-R0.1-SNAPSHOT'
compileOnly 'org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT'
}

group = 'cloud.stonehouse'
Expand Down
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ case "`uname`" in
Darwin* )
darwin=true
;;
MINGW* )
MSYS* | MINGW* )
msys=true
;;
NONSTOP* )
Expand Down
58 changes: 43 additions & 15 deletions src/main/java/cloud/stonehouse/s3backup/Archive.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,41 @@

import org.bukkit.entity.Player;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

class Archive {

private final S3Backup s3Backup;
private List<String> fileList;

Archive(S3Backup s3Backup) {
this.s3Backup = s3Backup;
this.fileList = new ArrayList<>();
}

void zipFile(Player player, String sourceFile, String destinationFile) throws IOException {
FileOutputStream fos = new FileOutputStream(destinationFile);
ZipOutputStream zipOut = new ZipOutputStream(fos);
void zipFile(Player player, String sourceFile, String destinationFile, boolean dryRun) throws IOException {
FileOutputStream fos = null;
ZipOutputStream zipOut = null;
if (!dryRun) {
fos = new FileOutputStream(destinationFile);
zipOut = new ZipOutputStream(fos);
}

File fileToZip = new File(sourceFile);

zipFile(player, fileToZip, fileToZip.getName(), zipOut);
zipFile(player, fileToZip, fileToZip.getName(), zipOut, dryRun);

if (dryRun) {
writeDryRunLog(player, destinationFile + ".dryrun.txt");
return;
}

zipOut.close();
fos.close();
}
Expand All @@ -35,29 +48,37 @@ void deleteFile(String sourceFile) {
}
}

private void zipFile(Player player, File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
private void zipFile(Player player, File fileToZip, String fileName, ZipOutputStream zipOut, boolean dryRun) throws IOException {
if (fileToZip.isDirectory()) {
if (fileName.endsWith(File.separator)) {
zipOut.putNextEntry(new ZipEntry(fileName));
zipOut.closeEntry();
} else {
zipOut.putNextEntry(new ZipEntry(fileName + File.separator));
if (!dryRun) {
if (fileName.endsWith(File.separator)) {
zipOut.putNextEntry(new ZipEntry(fileName));
} else {
zipOut.putNextEntry(new ZipEntry(fileName + File.separator));
}
zipOut.closeEntry();
}

File[] children = fileToZip.listFiles();
for (File childFile : children) {
String childPath = childFile.getCanonicalPath();
if (!childFile.getName().startsWith(s3Backup.getFileConfig().getBackupDir()) &&
Arrays.stream(s3Backup.getFileConfig().getIgnoreFiles()).noneMatch(childPath::contains)) {
try {
zipFile(player, childFile, fileName + File.separator + childFile.getName(), zipOut);
zipFile(player, childFile, fileName + File.separator + childFile.getName(), zipOut, dryRun);
} catch (IOException e) {
s3Backup.exception(player, "Error backing up file " + childFile.getName(), e);
}
}
}
return;
}
fileList.add(fileToZip.getAbsolutePath());

if (dryRun) {
return;
}

FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
Expand All @@ -69,4 +90,11 @@ private void zipFile(Player player, File fileToZip, String fileName, ZipOutputSt
}
fis.close();
}

private void writeDryRunLog(Player player, String destinationFile) throws IOException {
FileOutputStream outputStream = new FileOutputStream(destinationFile);
String implodedFileList = String.join(System.lineSeparator(), fileList);
outputStream.write(implodedFileList.getBytes(StandardCharsets.UTF_8));
s3Backup.sendMessage(player, "Wrote dry-run file");
}
}
12 changes: 10 additions & 2 deletions src/main/java/cloud/stonehouse/s3backup/Backup.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ class Backup extends BukkitRunnable {
private final String customPrefix;
private final S3Backup s3Backup;
private final Player player;
private final boolean dryRun;

Backup(S3Backup s3Backup, Player player, String customPrefix) {
Backup(S3Backup s3Backup, Player player, String customPrefix, boolean dryRun) {
this.customPrefix = customPrefix;
this.s3Backup = s3Backup;
this.player = player;
this.dryRun = dryRun;

this.backupInProgress = s3Backup.inProgress();

Expand Down Expand Up @@ -59,7 +61,13 @@ public void run() {
s3Backup.sendMessage(player, "Backup initiated");
String archivePath = backupDir + File.separator + archiveName;

s3Backup.archive().zipFile(player, Paths.get("").toAbsolutePath().normalize().toString(), archivePath);
s3Backup.archive().zipFile(player, Paths.get("").toAbsolutePath().normalize().toString(), archivePath, dryRun);

if (dryRun) {
s3Backup.sendMessage(player, "Backup completed! This was a dry run.");
return;
}

s3Backup.s3Put().put(archiveName);
s3Backup.archive().deleteFile(archivePath);

Expand Down
11 changes: 9 additions & 2 deletions src/main/java/cloud/stonehouse/s3backup/CommandS3Backup.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,23 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,
throw new StringFormatException("Invalid backup name. Only alphanumeric characters, " +
"underscores and hyphens are permitted");
} else {
new Backup(s3Backup, player, name + "-").runTaskAsynchronously(s3Backup);
new Backup(s3Backup, player, name + "-", false).runTaskAsynchronously(s3Backup);
}
} catch (StringFormatException e) {
s3Backup.exception(player, "Error", e);
}
} else {
new Backup(s3Backup, player, "manual-").runTaskAsynchronously(s3Backup);
new Backup(s3Backup, player, "manual-", false).runTaskAsynchronously(s3Backup);
}
} else {
s3Backup.sendMessage(player, "You do not have permission for this command");
}
} else if (args[0].equalsIgnoreCase("dry-run")) {
if (s3Backup.hasPermission(player, "s3backup.backup")) {
new Backup(s3Backup, player, "", true).runTaskAsynchronously(s3Backup);
} else {
s3Backup.sendMessage(player, "You do not have permission for this command");
}
} else if (args[0].equalsIgnoreCase("list")) {
if (s3Backup.hasPermission(player, "s3backup.list")) {
int limit = 0;
Expand Down Expand Up @@ -116,6 +122,7 @@ public List<String> onTabComplete(CommandSender commandSender, Command command,
types.add("get");
types.add("list");
types.add("sign");
types.add("dry-run");

if (commandSender instanceof Player) {
player = (Player) commandSender;
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/cloud/stonehouse/s3backup/S3Backup.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import cloud.stonehouse.s3backup.s3.*;
import com.amazonaws.services.s3.AmazonS3;
import org.bstats.bukkit.Metrics;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
Expand Down Expand Up @@ -45,8 +44,6 @@ public void onEnable() {
20 * 60 * backupInterval);

setProgress(false);

new Metrics(this);
}

@Override
Expand Down Expand Up @@ -74,6 +71,7 @@ Archive archive() {
return archive;
}


public String convertBytes(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cloud/stonehouse/s3backup/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class Scheduler extends BukkitRunnable {

@Override
public void run() {
new Backup(s3Backup, null, "").runTaskAsynchronously(s3Backup);
new Backup(s3Backup, null, "", false).runTaskAsynchronously(s3Backup);
}
}

0 comments on commit a7888f4

Please sign in to comment.