Search in sources :

Example 1 with ServerBackup

use of com.thoughtworks.go.server.domain.ServerBackup in project gocd by gocd.

the class BackupServiceTest method shouldReturnTheLatestBackupTime.

@Test
public void shouldReturnTheLatestBackupTime() {
    ServerBackupRepository repo = mock(ServerBackupRepository.class);
    Date serverBackupTime = new Date();
    when(repo.lastBackup()).thenReturn(new ServerBackup("file_path", serverBackupTime, "user"));
    BackupService backupService = new BackupService(null, null, mock(GoConfigService.class), null, repo, systemEnvironment, serverVersion, configRepo, databaseStrategy);
    backupService.initialize();
    Date date = backupService.lastBackupTime();
    assertThat(date, is(serverBackupTime));
}
Also used : ServerBackup(com.thoughtworks.go.server.domain.ServerBackup) Date(java.util.Date) ServerBackupRepository(com.thoughtworks.go.server.persistence.ServerBackupRepository) Test(org.junit.Test)

Example 2 with ServerBackup

use of com.thoughtworks.go.server.domain.ServerBackup in project gocd by gocd.

the class BackupServiceTest method shouldReturnTheUserThatTriggeredTheLastBackup.

@Test
public void shouldReturnTheUserThatTriggeredTheLastBackup() {
    ServerBackupRepository repo = mock(ServerBackupRepository.class);
    when(repo.lastBackup()).thenReturn(new ServerBackup("file_path", new Date(), "loser"));
    BackupService backupService = new BackupService(null, null, mock(GoConfigService.class), null, repo, systemEnvironment, serverVersion, configRepo, databaseStrategy);
    backupService.initialize();
    String username = backupService.lastBackupUser();
    assertThat(username, is("loser"));
}
Also used : ServerBackup(com.thoughtworks.go.server.domain.ServerBackup) Date(java.util.Date) ServerBackupRepository(com.thoughtworks.go.server.persistence.ServerBackupRepository) Test(org.junit.Test)

Example 3 with ServerBackup

use of com.thoughtworks.go.server.domain.ServerBackup in project gocd by gocd.

the class BackupsControllerDelegate method create.

public String create(Request request, Response response) throws IOException {
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    ServerBackup backup = backupService.startBackup(currentUsername(), result);
    if (result.isSuccessful()) {
        return writerForTopLevelObject(request, response, outputWriter -> BackupRepresenter.toJSON(outputWriter, backup));
    }
    return renderHTTPOperationResult(result, request, response, localizer);
}
Also used : ServerBackup(com.thoughtworks.go.server.domain.ServerBackup) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)

Example 4 with ServerBackup

use of com.thoughtworks.go.server.domain.ServerBackup in project gocd by gocd.

the class DirectoryStructureWalker method startBackup.

public ServerBackup startBackup(Username username, HttpLocalizedOperationResult result) {
    if (!goConfigService.isUserAdmin(username)) {
        result.unauthorized(LocalizedMessage.string("UNAUTHORIZED_TO_BACKUP"), HealthStateType.unauthorised());
        return null;
    }
    synchronized (BACKUP_MUTEX) {
        DateTime now = timeProvider.currentDateTime();
        final File destDir = new File(backupLocation(), BACKUP + now.toString("YYYYMMdd-HHmmss"));
        if (!destDir.mkdirs()) {
            result.badRequest(LocalizedMessage.string("BACKUP_UNSUCCESSFUL", "Could not create the backup directory."));
            return null;
        }
        try {
            backupRunningSince = now;
            backupStartedBy = username.getUsername().toString();
            backupVersion(destDir);
            backupConfig(destDir);
            configRepository.doLocked(new VoidThrowingFn<IOException>() {

                @Override
                public void run() throws IOException {
                    backupConfigRepository(destDir);
                }
            });
            backupDb(destDir);
            ServerBackup serverBackup = new ServerBackup(destDir.getAbsolutePath(), now.toDate(), username.getUsername().toString());
            serverBackupRepository.save(serverBackup);
            mailSender.send(EmailMessageDrafter.backupSuccessfullyCompletedMessage(destDir.getAbsolutePath(), goConfigService.adminEmail(), username));
            result.setMessage(LocalizedMessage.string("BACKUP_COMPLETED_SUCCESSFULLY"));
            return serverBackup;
        } catch (Exception e) {
            FileUtils.deleteQuietly(destDir);
            result.badRequest(LocalizedMessage.string("BACKUP_UNSUCCESSFUL", e.getMessage()));
            LOGGER.error("[Backup] Failed to backup Go.", e);
            mailSender.send(EmailMessageDrafter.backupFailedMessage(e.getMessage(), goConfigService.adminEmail()));
        } finally {
            backupRunningSince = null;
            backupStartedBy = null;
        }
        return null;
    }
}
Also used : ServerBackup(com.thoughtworks.go.server.domain.ServerBackup) IOException(java.io.IOException) File(java.io.File) DateTime(org.joda.time.DateTime) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 5 with ServerBackup

use of com.thoughtworks.go.server.domain.ServerBackup in project gocd by gocd.

the class ServerBackupRepositoryTest method shouldReturnTheLastBackupTime.

@Test
public void shouldReturnTheLastBackupTime() {
    Date time = new Date();
    repository.save(new ServerBackup("file_path", time, "user"));
    Date latest = new Date(time.getTime() + 10000);
    repository.save(new ServerBackup("file_path", latest, "user"));
    ServerBackup serverBackup = repository.lastBackup();
    assertEquals(new ServerBackup("file_path", latest, "user"), serverBackup);
}
Also used : ServerBackup(com.thoughtworks.go.server.domain.ServerBackup) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ServerBackup (com.thoughtworks.go.server.domain.ServerBackup)7 Test (org.junit.Test)5 Date (java.util.Date)4 ServerBackupRepository (com.thoughtworks.go.server.persistence.ServerBackupRepository)2 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)2 File (java.io.File)2 DateTime (org.joda.time.DateTime)2 RunIf (com.googlecode.junit.ext.RunIf)1 TimeProvider (com.thoughtworks.go.util.TimeProvider)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1