mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 22:16:34 +00:00
Add failsafe backup creator to database upgrader and fix timestamp default setting issues, Close #87, #86
This commit is contained in:
1 parent
7539094a2d
commit
d8e2ea7214
5 files changed
+36
-2
No files matched your search
@@ -44,6 +44,7 @@ public final class HuskHomes extends JavaPlugin {
|
|||||||
public static Connection getConnection() {
|
public static Connection getConnection() {
|
||||||
return database.getConnection();
|
return database.getConnection();
|
||||||
}
|
}
|
||||||
|
public static void backupDatabase() { database.backup(); }
|
||||||
|
|
||||||
// Map integration handling
|
// Map integration handling
|
||||||
private static Map map;
|
private static Map map;
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ public abstract class Database {
|
|||||||
|
|
||||||
public abstract void load();
|
public abstract void load();
|
||||||
|
|
||||||
|
public abstract void backup();
|
||||||
|
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
|
|
||||||
|
|||||||
@@ -114,4 +114,9 @@ public class MySQL extends Database {
|
|||||||
|
|
||||||
initialize();
|
initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void backup() {
|
||||||
|
plugin.getLogger().info("Remember to make backups of your HuskHomes Database before updating HuskHomes!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,12 +2,18 @@ package me.william278.huskhomes2.data.SQL;
|
|||||||
|
|
||||||
import me.william278.huskhomes2.HuskHomes;
|
import me.william278.huskhomes2.HuskHomes;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
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;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class SQLite extends Database {
|
public class SQLite extends Database {
|
||||||
@@ -119,4 +125,22 @@ public class SQLite extends Database {
|
|||||||
|
|
||||||
initialize();
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -48,6 +48,7 @@ public class UpgradeDatabase {
|
|||||||
plugin.reloadConfig();
|
plugin.reloadConfig();
|
||||||
if (plugin.getConfig().getInt("config_file_version", 1) <= 6) {
|
if (plugin.getConfig().getInt("config_file_version", 1) <= 6) {
|
||||||
plugin.getLogger().info("Database upgrade needed: Adding logout position tracking and ignoring request data...");
|
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;
|
String[] statements = SQLiteUpgradeStatements;
|
||||||
if (HuskHomes.getSettings().getDatabaseType().equalsIgnoreCase("mysql")) {
|
if (HuskHomes.getSettings().getDatabaseType().equalsIgnoreCase("mysql")) {
|
||||||
statements = mySQLUpgradeStatements;
|
statements = mySQLUpgradeStatements;
|
||||||
@@ -72,6 +73,7 @@ public class UpgradeDatabase {
|
|||||||
|
|
||||||
if (plugin.getConfig().getInt("config_file_version", 1) <= 7) {
|
if (plugin.getConfig().getInt("config_file_version", 1) <= 7) {
|
||||||
plugin.getLogger().info("Database upgrade needed: Adding creation timestamps to homes and warps...");
|
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(
|
try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement(
|
||||||
"ALTER TABLE " + HuskHomes.getSettings().getHomesDataTable()
|
"ALTER TABLE " + HuskHomes.getSettings().getHomesDataTable()
|
||||||
+ " ADD `creation_time` timestamp NULL DEFAULT NULL;")) {
|
+ " ADD `creation_time` timestamp NULL DEFAULT NULL;")) {
|
||||||
|
|||||||
Reference in new issue
Block a user