au.net.causal.maven.plugins.boxdb.BackupMojo Maven / Gradle / Ivy
package au.net.causal.maven.plugins.boxdb;
import au.net.causal.maven.plugins.boxdb.db.BackupFileTypeHint;
import au.net.causal.maven.plugins.boxdb.db.BoxDatabaseException;
import au.net.causal.maven.plugins.boxdb.db.DockerService;
import io.fabric8.maven.docker.access.DockerAccessException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
/**
* Generates a backup archive from the configured database. This backup archive will be in a database-specific format,
* but can be restored later to another database of the same type and version.
*
*
* Version compatibility for backup archives is database-dependent. It may be possible to restore backups between
* different versions of a particular database type, but then again it might not be.
*/
@Mojo(name="backup", requiresProject = false)
public class BackupMojo extends StartMojo
{
/**
* The backup file that will be generated by this goal.
*/
@Parameter(property = "db.backupFile", defaultValue = "${project.build.directory}/dbbackup", required = true)
private File backupFile;
/**
* Hint to the database implementation on the format of the dump.
*
*
* COMPACT
implies a binary representation that takes up less space.
* TEXT
implies a human-readable representation, more suitable for checking into source control,
* doing diffs, etc.
*
*
* This parameter is only a hint. Some database implementations will ignore this value.
*/
@Parameter(property = "db.backupType", defaultValue = "COMPACT", required = true)
private BackupFileTypeHint backupType;
@Override
protected void executeInternal(ExceptionalSupplier dockerService)
throws DockerAccessException, MojoExecutionException
{
//Start
super.executeInternal(dockerService);
try
{
getLog().info("Backing up database to " + backupFile.getAbsolutePath() + "...");
database(dockerService).backup(backupFile.toPath(), backupType);
getLog().info("Backup complete.");
}
catch (BoxDatabaseException | IOException | SQLException e)
{
throw new MojoExecutionException("Error backing up database: " + e, e);
}
}
}