diff --git a/src/main/java/me/william278/huskhomes2/HuskHomes.java b/src/main/java/me/william278/huskhomes2/HuskHomes.java index fe9e395bd..376f9aa59 100644 --- a/src/main/java/me/william278/huskhomes2/HuskHomes.java +++ b/src/main/java/me/william278/huskhomes2/HuskHomes.java @@ -44,6 +44,7 @@ public static HuskHomes getInstance() { public static Connection getConnection() { return database.getConnection(); } + public static void backupDatabase() { database.backup(); } // Map integration handling private static Map map; diff --git a/src/main/java/me/william278/huskhomes2/data/SQL/Database.java b/src/main/java/me/william278/huskhomes2/data/SQL/Database.java index 4805f76b4..2fcbb549d 100644 --- a/src/main/java/me/william278/huskhomes2/data/SQL/Database.java +++ b/src/main/java/me/william278/huskhomes2/data/SQL/Database.java @@ -19,6 +19,8 @@ public Database(HuskHomes instance) { public abstract void load(); + public abstract void backup(); + public void initialize() { Connection connection = getConnection(); diff --git a/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java b/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java index 695a4b864..93e6662c9 100644 --- a/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java +++ b/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java @@ -114,4 +114,9 @@ public void load() { initialize(); } + + @Override + public void backup() { + plugin.getLogger().info("Remember to make backups of your HuskHomes Database before updating HuskHomes!"); + } } diff --git a/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java b/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java index 46dac2cba..5e01f2507 100644 --- a/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java +++ b/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java @@ -2,12 +2,18 @@ import me.william278.huskhomes2.HuskHomes; -import java.io.File; -import java.io.IOException; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.util.Locale; import java.util.logging.Level; public class SQLite extends Database { @@ -119,4 +125,22 @@ public void load() { initialize(); } + + @Override + public void backup() { + final String BACKUPS_FOLDER_NAME = "database-backups"; + final String backupFileName = DATABASE_NAME + "Backup_" + DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss-SS") + .withLocale(Locale.getDefault()) + .withZone(ZoneId.systemDefault()) + .format(Instant.now()).replaceAll(" ", "-") + ".db"; + final File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db"); + new File(plugin.getDataFolder(), BACKUPS_FOLDER_NAME).mkdirs(); + final File backUpFile = new File(plugin.getDataFolder(), BACKUPS_FOLDER_NAME + File.separator + backupFileName); + try { + Files.copy(databaseFile.toPath(), backUpFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + plugin.getLogger().info("Created a backup of your database."); + } catch (IOException iox) { + plugin.getLogger().log(Level.WARNING, "An error occurred making a database backup", iox); + } + } } diff --git a/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java b/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java index d84511730..a4fa69c49 100644 --- a/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java +++ b/src/main/java/me/william278/huskhomes2/migrators/UpgradeDatabase.java @@ -48,6 +48,7 @@ public static void upgradeDatabase() { plugin.reloadConfig(); if (plugin.getConfig().getInt("config_file_version", 1) <= 6) { plugin.getLogger().info("Database upgrade needed: Adding logout position tracking and ignoring request data..."); + HuskHomes.backupDatabase(); // Backup database before upgrades are carried out! String[] statements = SQLiteUpgradeStatements; if (HuskHomes.getSettings().getDatabaseType().equalsIgnoreCase("mysql")) { statements = mySQLUpgradeStatements; @@ -72,6 +73,7 @@ public static void upgradeDatabase() { if (plugin.getConfig().getInt("config_file_version", 1) <= 7) { plugin.getLogger().info("Database upgrade needed: Adding creation timestamps to homes and warps..."); + HuskHomes.backupDatabase(); // Backup database before upgrades are carried out! try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement( "ALTER TABLE " + HuskHomes.getSettings().getHomesDataTable() + " ADD `creation_time` timestamp NULL DEFAULT NULL;")) {