mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +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
@@ -19,6 +19,8 @@ public abstract class Database {
|
||||
|
||||
public abstract void load();
|
||||
|
||||
public abstract void backup();
|
||||
|
||||
public void initialize() {
|
||||
Connection connection = getConnection();
|
||||
|
||||
|
||||
@@ -114,4 +114,9 @@ public class MySQL extends Database {
|
||||
|
||||
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 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 class SQLite extends Database {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user