com.github.ludoviccarretti:postgresql-backup4j

This is a simple library for backing up postgresql databases and sending to emails, cloud storage and so on. It also provide a method for programmatically, importing SQL queries generated during the export process,

License

License

Categories

Categories

PostgreSQL Data Databases
GroupId

GroupId

com.github.ludoviccarretti
ArtifactId

ArtifactId

postgresql-backup4j
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

com.github.ludoviccarretti:postgresql-backup4j
This is a simple library for backing up postgresql databases and sending to emails, cloud storage and so on. It also provide a method for programmatically, importing SQL queries generated during the export process,
Project URL

Project URL

https://github.com/ludoviccarretti/postresql-backup4j
Source Code Management

Source Code Management

https://github.com/ludoviccarretti/postresql-backup4j

Download postgresql-backup4j

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.ludoviccarretti/postgresql-backup4j/ -->
<dependency>
    <groupId>com.github.ludoviccarretti</groupId>
    <artifactId>postgresql-backup4j</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.ludoviccarretti/postgresql-backup4j/
implementation 'com.github.ludoviccarretti:postgresql-backup4j:1.0.1'
// https://jarcasting.com/artifacts/com.github.ludoviccarretti/postgresql-backup4j/
implementation ("com.github.ludoviccarretti:postgresql-backup4j:1.0.1")
'com.github.ludoviccarretti:postgresql-backup4j:jar:1.0.1'
<dependency org="com.github.ludoviccarretti" name="postgresql-backup4j" rev="1.0.1">
  <artifact name="postgresql-backup4j" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.ludoviccarretti', module='postgresql-backup4j', version='1.0.1')
)
libraryDependencies += "com.github.ludoviccarretti" % "postgresql-backup4j" % "1.0.1"
[com.github.ludoviccarretti/postgresql-backup4j "1.0.1"]

Dependencies

compile (4)

Group / Artifact Type Version
org.postgresql : postgresql jar 42.2.8
org.zeroturnaround : zt-zip jar 1.12
javax.mail : mail jar 1.5.0-b01
org.slf4j : slf4j-api jar 1.7.25

test (2)

Group / Artifact Type Version
org.slf4j : slf4j-simple jar 1.7.25
org.junit.jupiter : junit-jupiter-api jar 5.0.0-M4

Project Modules

There are no modules declared in this project.

postgresql-backup4j

postgresql-backup4j is a library for programmatically exporting postgresql databases and sending the zipped dump to email, Amazon S3, Google Drive or any other cloud storage of choice

It gives the developer access to the generated zip file and the generated SQL query string for use in other part of the application.

It also provides a method for importing the SQL exported by the tool - programmatically.

Installation

The artifact is available on Maven Central and can be added to the project's pom.xml:

<dependency>
    <groupId>com.github.ludoviccarretti</groupId>
    <artifactId>postgresql-backup4j</artifactId>
    <version>1.0.1</version>
</dependency>

The latest version can be found here

Usage

The minimum configuration required for the library is the database name, username and password.

However, if you want the backup file to be sent to your email automatically after backup, you must provide email configurations as well.

//required properties for exporting of db
Properties properties = new Properties();
properties.setProperty(PropertiesOptions.DB_NAME, "database-name");
properties.setProperty(PropertiesOptions.DB_USERNAME, "root");
properties.setProperty(PropertiesOptions.DB_PASSWORD, "root");
        
//properties relating to email config
properties.setProperty(PropertiesOptions.EMAIL_HOST, "smtp.mailtrap.io");
properties.setProperty(PropertiesOptions.EMAIL_PORT, "25");
properties.setProperty(PropertiesOptions.EMAIL_USERNAME, "mailtrap-username");
properties.setProperty(PropertiesOptions.EMAIL_PASSWORD, "mailtrap-password");
properties.setProperty(PropertiesOptions.EMAIL_FROM, "[email protected]");
properties.setProperty(PropertiesOptions.EMAIL_TO, "[email protected]");

//set the outputs temp dir
properties.setProperty(PropertiesOptions.TEMP_DIR, new File("external").getPath());

PostgresqlExportService postgresqlExportService = new PostgresqlExportService(properties);
postgresqlExportService.export();

Calling postgresqlExportService.export(); will export the database and save the dump temporarily in the configured TEMP_DIR

If an email config is supplied, the dump will be sent as an attachment. Finally, when all operations are completed the temporary dir is cleared and deleted.

If you want to get the generated backup file as a Java File object, you need to specify this property as part of the configuration:

//...
properties.setProperty(PostgresqlExportService.PRESERVE_GENERATED_ZIP, "true");

and then you can call this method:

File file = postgresqlExportService.getGeneratedZipFile();

Because you set preserve generated file to be true, the library will not clear the temp dir as expected and you have to do that manually by calling this method:

postgresqlExportService.clearTempFiles(false);

Finally, let's say for some reason you want the generated SQL string you can do this:

String generatedSql = postgresqlExportService.getGeneratedSql();

Other parameters are:

properties.setProperty(PropertiesOptions.ADD_IF_NOT_EXISTS, "true");
properties.setProperty(PropertiesOptions.JDBC_DRIVER_NAME, "root.ss");
properties.setProperty(PropertiesOptions.JDBC_CONNECTION_STRING, "jdbc:postgresql://localhost:5432/database-name");

They are explained in a detailed manner in this tutorial

Importing a Database

To import a database, you need to use the ImportService like so:

String sql = new String(Files.readAllBytes(Paths.get("path/to/sql/dump/file.sql")));

boolean res = PostgresqlImportService.builder()
        .setDatabase("database-name")
        .setSqlString(sql)
        .setUsername("root")
        .setPassword("root")
        .setDeleteExisting(true)
        .setDropExisting(true)
        .importDatabase();
        
assertTrue(res);

First get SQL as a String and then pass it to the import service with the right configurations.

Alternatively, you can also use the .setJdbcConnString(jdbcURL) method on the import service.

e.g.

boolean res = PostgresqlImportService.builder()
                .setSqlString(generatedSql)
                .setJdbcConnString("jdbc:postgresql://localhost:5432/backup4j_test")
                .setUsername("db-username")
                .setPassword("db-password")
                .setDeleteExisting(true)
                .setDropExisting(true)
                .importDatabase();

setDeleteExisting(true) will delete all data from existing tables in the target database.

While setDropExisting(true) will drop the table.

Supplying false to these functions will disable their respective actions.

NOTE: The import service is only guaranteed to work with SQL files generated by the export service of this library

Contributions and Support

Love this project or found it useful? You can buy me a cup of coffee

If you want to create a new feature, though not compulsory, but it will be helpful to reach out to me first before proceeding.

To avoid a scenario where you submit a PR for an issue that someone else is working on already.

Tutorials / Articles

Versions

Version
1.0.1