Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-5030. Add exception to log #29269

Merged
merged 35 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3657321
CB-5030. Add exception to log
DenisSinelnikov Apr 29, 2024
347129f
Merge branch 'devel' into CB-5030-on-auto-backup
kseniaguzeeva May 3, 2024
2356f1e
Merge branch 'devel' into CB-5030-on-auto-backup
kseniaguzeeva May 7, 2024
7edc558
Merge branch 'devel' into CB-5030-on-auto-backup
kseniaguzeeva May 7, 2024
5e2833b
Merge branch 'devel' into CB-5030-on-auto-backup
kseniaguzeeva May 8, 2024
b2bfacc
CB-5030. Add exception to log
DenisSinelnikov May 8, 2024
e6faa34
Merge branch 'devel' into CB-5030-on-auto-backup
DenisSinelnikov May 8, 2024
bb3847f
Merge branch 'devel' into CB-5030-on-auto-backup
kseniaguzeeva May 13, 2024
095005a
Merge branch 'devel' into CB-5030-on-auto-backup
kseniaguzeeva May 13, 2024
940f228
Merge branch 'devel' into CB-5030-on-auto-backup
kseniaguzeeva May 14, 2024
ee0db32
Merge branch 'devel' into CB-5030-on-auto-backup
kseniaguzeeva May 14, 2024
818ce42
Merge branch 'devel' into CB-5030-on-auto-backup
kseniaguzeeva May 23, 2024
6e04ab4
CB-5030. Fixed if no schema do dump
DenisSinelnikov May 23, 2024
569b682
Merge remote-tracking branch 'origin/CB-5030-on-auto-backup' into CB-…
DenisSinelnikov May 23, 2024
0a65246
Merge branch 'devel' into CB-5030-on-auto-backup
kseniaguzeeva May 24, 2024
674060a
CB-5030. Added database to pg_dump script
DenisSinelnikov May 24, 2024
8d13eb6
CB-5030. Refactor after review
DenisSinelnikov May 24, 2024
53bb754
CB-5030. Revert schema logic
DenisSinelnikov May 24, 2024
4f7a553
CB-5030. Revert schema logic
DenisSinelnikov May 24, 2024
f087a8d
CB-5030. Fixed after review
DenisSinelnikov May 24, 2024
8dbd228
Merge branch 'devel' into CB-5030-on-auto-backup
kseniaguzeeva May 25, 2024
f6bf45c
CB-5030. Add logs
DenisSinelnikov May 27, 2024
26f6d06
Merge remote-tracking branch 'origin/CB-5030-on-auto-backup' into CB-…
DenisSinelnikov May 27, 2024
7096f66
CB-5030. Add logs
DenisSinelnikov May 27, 2024
7a1ddee
CB-5030. Revert logs
DenisSinelnikov May 27, 2024
c773d5c
CB-5030. Remove user from logs, revert schema
DenisSinelnikov May 27, 2024
3cdbc2d
CB-5030. Remove user from logs, revert schema
DenisSinelnikov May 27, 2024
235cdf5
CB-5030. Remove user from logs, revert schema
DenisSinelnikov May 27, 2024
7e1270e
Merge branch 'devel' into CB-5030-on-auto-backup
DenisSinelnikov May 27, 2024
4ca65e4
CB-5030. Remove user from logs, revert schema
DenisSinelnikov May 27, 2024
1c8c99a
Merge remote-tracking branch 'origin/CB-5030-on-auto-backup' into CB-…
DenisSinelnikov May 27, 2024
35a77f8
CB-5030. Remove user from logs, revert schema
DenisSinelnikov May 27, 2024
52c1471
CB-5030 fix pg_dump schema parameter
alexander-skoblikov May 27, 2024
cf517af
Merge branch 'devel' into CB-5030-on-auto-backup
kseniaguzeeva May 28, 2024
6a5c387
Merge branch 'devel' into CB-5030-on-auto-backup
kseniaguzeeva May 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jkiss.dbeaver.model.sql.backup.SQLBackupConstants;
import org.jkiss.dbeaver.runtime.DBWorkbench;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
Expand All @@ -34,14 +35,14 @@ public class JDBCDatabaseH2BackupHandler implements JDBCDatabaseBackupHandler {

@Override
public void doBackup(
@NotNull Connection connection,
int currentSchemaVersion,
@NotNull InternalDatabaseConfig databaseConfig
) throws DBException {
@NotNull Connection connection,
int currentSchemaVersion,
@NotNull InternalDatabaseConfig databaseConfig
) throws DBException, IOException {
Path workspace = DBWorkbench.getPlatform().getWorkspace().getAbsolutePath().resolve(SQLBackupConstants.BACKUP_FOLDER);
Path backupFile = workspace.resolve(SQLBackupConstants.BACKUP_FILE_NAME + currentSchemaVersion
+ SQLBackupConstants.BACKUP_FILE_TYPE);
try (Statement statement = connection.createStatement()) {
Path workspace = DBWorkbench.getPlatform().getWorkspace().getAbsolutePath().resolve(SQLBackupConstants.BACKUP_FOLDER);
Path backupFile = workspace.resolve(SQLBackupConstants.BACKUP_FILE_NAME + currentSchemaVersion
+ SQLBackupConstants.BACKUP_FILE_TYPE);
if (Files.notExists(backupFile)) {
Files.createDirectories(workspace);

Expand All @@ -51,6 +52,7 @@ public void doBackup(
log.info("Reserve backup created to path: " + workspace + "backup");
}
} catch (Exception e) {
Files.deleteIfExists(backupFile);
log.error("Create backup is failed: " + e.getMessage());
throw new DBException("Backup is failed: " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.connection.InternalDatabaseConfig;
import org.jkiss.dbeaver.model.sql.backup.SQLBackupConstants;
import org.jkiss.dbeaver.model.sql.backup.JDBCDatabaseBackupHandler;
import org.jkiss.dbeaver.model.sql.backup.SQLBackupConstants;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.utils.CommonUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -36,37 +40,43 @@ public class JDBCDatabasePostgresBackupHandler implements JDBCDatabaseBackupHand

@Override
public void doBackup(
@NotNull Connection connection,
int currentSchemaVersion,
@NotNull InternalDatabaseConfig databaseConfig
@NotNull Connection connection,
int currentSchemaVersion,
@NotNull InternalDatabaseConfig databaseConfig
) throws DBException {
try {
URI uri = new URI(databaseConfig.getUrl().replace("jdbc:", ""));
Path workspace = DBWorkbench.getPlatform().getWorkspace().getAbsolutePath().resolve(SQLBackupConstants.BACKUP_FOLDER);
Path backupFile = workspace.resolve(uri.getPath().replace("/", "") + "_"
+ SQLBackupConstants.BACKUP_FILE_NAME + databaseConfig.getSchema()
+ currentSchemaVersion + SQLBackupConstants.BACKUP_FILE_TYPE);
+ SQLBackupConstants.BACKUP_FILE_NAME + databaseConfig.getSchema()
+ currentSchemaVersion + SQLBackupConstants.BACKUP_FILE_TYPE);
if (Files.notExists(backupFile)) {
Files.createDirectories(workspace);
ProcessBuilder processBuilder = getBuilder(databaseConfig, uri, backupFile);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();

while (process.isAlive()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
try (InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {

StringBuilder processOutput = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
processOutput.append(line).append("\n");
}
}

int exitCode = process.exitValue();
int exitCode = process.waitFor();

if (exitCode == 0) {
log.info("Postgres backup successful");
} else {
log.error("Postgres backup failed");
throw new DBException("Postgres backup failed");
if (exitCode == 0) {
log.info("Postgres backup successful");
} else {
Files.deleteIfExists(backupFile);
log.error("Postgres backup failed with output: " + processOutput.toString());
throw new DBException("Postgres backup failed");
}
} catch (IOException e) {
log.error("Error reading process output", e);
}
}
} catch (Exception e) {
Expand All @@ -76,18 +86,38 @@ public void doBackup(
}

private static ProcessBuilder getBuilder(@NotNull InternalDatabaseConfig databaseConfig, URI uri, Path backupFile) {
String databaseName = uri.getPath();
if (databaseName != null) {
if (databaseName.startsWith("/")) {
databaseName = databaseName.substring(1);
}
int questionMarkIndex = databaseName.indexOf("?");
if (questionMarkIndex != -1) {
databaseName = databaseName.substring(0, questionMarkIndex);
}
}

ProcessBuilder processBuilder = new ProcessBuilder(
"pg_dump",
"--host", uri.getHost(),
"--port", String.valueOf(uri.getPort()),
"--username", databaseConfig.getUser(),
"--schema", databaseConfig.getSchema(),
"--format", "c",
"--blobs",
"--verbose",
"--file", backupFile.toAbsolutePath().toString()
"pg_dump",
databaseName,
"--host", uri.getHost(),
"--port", String.valueOf(uri.getPort()),
"--blobs",
"--verbose",
"--file", backupFile.toAbsolutePath().toString()
);

if (CommonUtils.isNotEmpty(databaseConfig.getSchema())) {
processBuilder.command().add("--schema");
processBuilder.command().add(databaseConfig.getSchema());
}

String backupCommand = String.join(" ", processBuilder.command());
log.info("Command started: " + backupCommand);

processBuilder.command().add("--username");
processBuilder.command().add(databaseConfig.getUser());

if (CommonUtils.isNotEmpty(databaseConfig.getPassword())) {
processBuilder.environment().put("PGPASSWORD", databaseConfig.getPassword());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.connection.InternalDatabaseConfig;

import java.io.IOException;
import java.sql.Connection;

public interface JDBCDatabaseBackupHandler {
void doBackup(
@NotNull Connection connection,
int currentSchemaVersion,
@NotNull InternalDatabaseConfig databaseConfig
) throws DBException;
) throws DBException, IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void updateSchema(DBRProgressMonitor monitor) throws DBException {
descriptor.getInstance().doBackup(dbCon, currentSchemaVersion, databaseConfig);
log.info("Starting backup execution");
} catch (DBException e) {
throw new DBException("Internal database backup has failed");
throw new DBException("Internal database backup has failed", e);
}
}
}
Expand Down