Search in sources :

Example 1 with BackupCopyTransaction

use of com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction in project Plan by plan-player-analytics.

the class DatabaseCommands method performBackup.

public void performBackup(CMDSender sender, Arguments arguments, String dbName, Database fromDB) {
    Database toDB = null;
    try {
        String timeStamp = timestamp.apply(System.currentTimeMillis());
        String fileName = dbName + "-backup-" + timeStamp;
        sender.send(locale.getString(CommandLang.DB_BACKUP_CREATE, fileName, dbName));
        toDB = sqliteFactory.usingFileCalled(fileName);
        toDB.init();
        toDB.executeTransaction(new BackupCopyTransaction(fromDB, toDB)).get();
    } catch (DBOpException | ExecutionException e) {
        errorLogger.error(e, ErrorContext.builder().related(sender, arguments).build());
    } catch (InterruptedException e) {
        toDB.close();
        Thread.currentThread().interrupt();
    } finally {
        if (toDB != null) {
            toDB.close();
        }
    }
}
Also used : DBOpException(com.djrapitops.plan.exceptions.database.DBOpException) Database(com.djrapitops.plan.storage.database.Database) BackupCopyTransaction(com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with BackupCopyTransaction

use of com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction in project Plan by plan-player-analytics.

the class DatabaseBackupTest method testBackupAndRestoreSQLite.

@Test
default void testBackupAndRestoreSQLite() throws Exception {
    File tempFile = Files.createTempFile(system().getPlanFiles().getDataFolder().toPath(), "backup-", ".db").toFile();
    tempFile.deleteOnExit();
    SQLiteDB backup = dbSystem().getSqLiteFactory().usingFile(tempFile);
    backup.setTransactionExecutorServiceProvider(MoreExecutors::newDirectExecutorService);
    try {
        backup.init();
        saveDataForBackup();
        backup.executeTransaction(new BackupCopyTransaction(db(), backup));
        assertQueryResultIsEqual(db(), backup, BaseUserQueries.fetchAllBaseUsers());
        assertQueryResultIsEqual(db(), backup, UserInfoQueries.fetchAllUserInformation());
        assertQueryResultIsEqual(db(), backup, NicknameQueries.fetchAllNicknameData());
        assertQueryResultIsEqual(db(), backup, GeoInfoQueries.fetchAllGeoInformation());
        assertQueryResultIsEqual(db(), backup, SessionQueries.fetchAllSessions());
        assertQueryResultIsEqual(db(), backup, LargeFetchQueries.fetchAllWorldNames());
        assertQueryResultIsEqual(db(), backup, LargeFetchQueries.fetchAllTPSData());
        assertQueryResultIsEqual(db(), backup, ServerQueries.fetchPlanServerInformation());
        assertQueryResultIsEqual(db(), backup, WebUserQueries.fetchAllUsers());
    } finally {
        backup.close();
    }
}
Also used : SQLiteDB(com.djrapitops.plan.storage.database.SQLiteDB) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) BackupCopyTransaction(com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 with BackupCopyTransaction

use of com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction in project Plan by plan-player-analytics.

the class DatabaseCommands method performMove.

private void performMove(CMDSender sender, DBType fromDB, DBType toDB) {
    try {
        Database fromDatabase = dbSystem.getActiveDatabaseByType(fromDB);
        Database toDatabase = dbSystem.getActiveDatabaseByType(toDB);
        fromDatabase.init();
        toDatabase.init();
        sender.send(locale.getString(CommandLang.DB_WRITE, toDB.getName()));
        toDatabase.executeTransaction(new BackupCopyTransaction(fromDatabase, toDatabase)).get();
        sender.send(locale.getString(CommandLang.PROGRESS_SUCCESS));
        boolean movingToCurrentDB = toDatabase.getType() == dbSystem.getDatabase().getType();
        if (movingToCurrentDB) {
            sender.send(locale.getString(CommandLang.HOTSWAP_REMINDER, toDatabase.getType().getConfigName()));
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (Exception e) {
        errorLogger.error(e, ErrorContext.builder().related(sender, fromDB.getName() + "->" + toDB.getName()).build());
        sender.send(locale.getString(CommandLang.PROGRESS_FAIL, e.getMessage()));
    }
}
Also used : Database(com.djrapitops.plan.storage.database.Database) BackupCopyTransaction(com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DBOpException(com.djrapitops.plan.exceptions.database.DBOpException)

Example 4 with BackupCopyTransaction

use of com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction in project Plan by plan-player-analytics.

the class DatabaseCommands method performRestore.

public void performRestore(CMDSender sender, File backupDBFile, Database toDB) {
    try {
        SQLiteDB fromDB = sqliteFactory.usingFile(backupDBFile);
        fromDB.init();
        sender.send(locale.getString(CommandLang.DB_WRITE, toDB.getType().getName()));
        toDB.executeTransaction(new BackupCopyTransaction(fromDB, toDB)).get();
        sender.send(locale.getString(CommandLang.PROGRESS_SUCCESS));
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (DBOpException | ExecutionException e) {
        errorLogger.error(e, ErrorContext.builder().related(backupDBFile, toDB.getType(), toDB.getState()).build());
        sender.send(locale.getString(CommandLang.PROGRESS_FAIL, e.getMessage()));
    }
}
Also used : DBOpException(com.djrapitops.plan.exceptions.database.DBOpException) SQLiteDB(com.djrapitops.plan.storage.database.SQLiteDB) BackupCopyTransaction(com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

BackupCopyTransaction (com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction)4 DBOpException (com.djrapitops.plan.exceptions.database.DBOpException)3 ExecutionException (java.util.concurrent.ExecutionException)3 Database (com.djrapitops.plan.storage.database.Database)2 SQLiteDB (com.djrapitops.plan.storage.database.SQLiteDB)2 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 File (java.io.File)1 IOException (java.io.IOException)1 Test (org.junit.jupiter.api.Test)1