Skip to content

Commit

Permalink
Added backupConfigs
Browse files Browse the repository at this point in the history
added `backupConfigs` to copy all current config files (and database) to a backup folder on plugin load / reload (thank you faun for helping me fix this mess)
  • Loading branch information
sh0inx committed Sep 29, 2023
1 parent 0335000 commit 05f3ca0
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/main/java/com/iridium/iridiumskyblock/IridiumSkyblock.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPluginLoader;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
Expand Down Expand Up @@ -146,6 +144,7 @@ public void registerListeners() {

@Override
public void loadConfigs() {
backupConfigs("backup");
this.configuration = getPersist().load(Configuration.class);
this.messages = getPersist().load(Messages.class);
this.commands = getPersist().load(Commands.class);
Expand Down Expand Up @@ -275,6 +274,33 @@ private void saveFile(File parent, String name) {
}
}

private void backupConfigs(String backupFolderName) {

getLogger().info("Attempting to create backup of configuration files...");

File pluginFolder = new File(getDataFolder().getPath());
File backupFolder = new File(pluginFolder.getPath() + File.separator + backupFolderName);
if (!backupFolder.exists()) backupFolder.mkdir();

File[] configFiles = pluginFolder.listFiles((dir, name) -> name.endsWith(".yml") || name.endsWith(".db"));

if(configFiles == null) {
getLogger().info("No files found.");
return;
}

for (File configFile : configFiles) {
File backupFile = new File(backupFolder, configFile.getName());

try {
Files.copy(configFile.toPath(), backupFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException exception) {
getLogger().warning("Could not copy " + configFile.getName() + " to " + backupFile.getAbsolutePath());
}
}
getLogger().info("Backup successful, check " + backupFolder.getPath() + ".");
}

@Override
public ChunkGenerator getDefaultWorldGenerator(String worldName, String id) {
return this.chunkGenerator;
Expand Down

0 comments on commit 05f3ca0

Please sign in to comment.