mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-08-20 11:42:50 +02:00
Implemented backup operations
This commit is contained in:
parent
751e0e6cb4
commit
4a2f18d9ee
|
@ -3,10 +3,12 @@ package com.djrapitops.plan.system.database.databases.operation;
|
|||
import com.djrapitops.plan.api.exceptions.database.DBException;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public interface BackupOperations {
|
||||
|
||||
void backup(Database toDatabase) throws DBException;
|
||||
void backup(Database toDatabase) throws DBException, SQLException;
|
||||
|
||||
void restore(Database fromDatabase) throws DBException;
|
||||
void restore(Database fromDatabase) throws DBException, SQLException;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
package com.djrapitops.plan.system.database.databases.sql.operation;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.database.DBException;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.database.databases.operation.BackupOperations;
|
||||
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.tables.move.BatchOperationTable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class SQLBackupOps extends SQLOps implements BackupOperations {
|
||||
|
||||
|
@ -11,12 +15,16 @@ public class SQLBackupOps extends SQLOps implements BackupOperations {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void backup(Database toDatabase) {
|
||||
// TODO
|
||||
public void backup(Database toDatabase) throws SQLException {
|
||||
BatchOperationTable toDB = new BatchOperationTable((SQLDB) toDatabase);
|
||||
BatchOperationTable fromDB = new BatchOperationTable(db);
|
||||
|
||||
toDB.removeAllData();
|
||||
fromDB.copyEverything(toDB);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restore(Database fromDatabase) {
|
||||
// TODO
|
||||
public void restore(Database fromDatabase) throws DBException, SQLException {
|
||||
fromDatabase.backup().backup(db);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
package com.djrapitops.plan.utilities;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.database.DBException;
|
||||
import com.djrapitops.plan.api.exceptions.database.DBInitException;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.SQLiteDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.tables.move.BatchOperationTable;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
|
@ -29,18 +28,20 @@ public class ManageUtils {
|
|||
* @param dbName Name of database (mysql/sqlite)
|
||||
* @param copyFromDB Database you want to backup.
|
||||
*/
|
||||
public static void backup(String dbName, Database copyFromDB) throws DBInitException, SQLException {
|
||||
public static void backup(String dbName, Database copyFromDB) throws SQLException {
|
||||
SQLiteDB backupDB = null;
|
||||
try {
|
||||
String timeStamp = new Date().toString().substring(4, 10).replace(" ", "-");
|
||||
String fileName = dbName + "-backup-" + timeStamp;
|
||||
backupDB = new SQLiteDB(fileName);
|
||||
Collection<UUID> uuids = ManageUtils.getUUIDS(copyFromDB);
|
||||
Collection<UUID> uuids = copyFromDB.fetch().getSavedUUIDs();
|
||||
if (uuids.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
backupDB.init();
|
||||
clearAndCopy(backupDB, copyFromDB);
|
||||
} catch (DBException e) {
|
||||
Log.toLog(ManageUtils.class, e);
|
||||
} finally {
|
||||
if (backupDB != null) {
|
||||
backupDB.close();
|
||||
|
@ -48,22 +49,6 @@ public class ManageUtils {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the saved UUIDs in a hashset
|
||||
*
|
||||
* @param db Database to get UUIDs from
|
||||
* @return uuids hashset as a Collection.
|
||||
*/
|
||||
public static Collection<UUID> getUUIDS(Database db) {
|
||||
final Set<UUID> uuids = new HashSet<>();
|
||||
try {
|
||||
uuids.addAll(db.fetch().getSavedUUIDs());
|
||||
} catch (DBException e) {
|
||||
Log.toLog(ManageUtils.class, e);
|
||||
}
|
||||
return uuids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears a database and copies data from other database to that database.
|
||||
*
|
||||
|
@ -71,11 +56,7 @@ public class ManageUtils {
|
|||
* to.
|
||||
* @param copyFromDB Database where data will be copied from
|
||||
*/
|
||||
public static void clearAndCopy(Database clearAndCopyToDB, Database copyFromDB) throws SQLException {
|
||||
BatchOperationTable toDB = new BatchOperationTable((SQLDB) clearAndCopyToDB);
|
||||
BatchOperationTable fromDB = new BatchOperationTable((SQLDB) copyFromDB);
|
||||
|
||||
toDB.removeAllData();
|
||||
fromDB.copyEverything(toDB);
|
||||
public static void clearAndCopy(Database clearAndCopyToDB, Database copyFromDB) throws SQLException, DBException {
|
||||
copyFromDB.backup().backup(clearAndCopyToDB);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user