Search in sources :

Example 1 with BackupItem

use of org.platformlayer.ops.backups.BackupItem in project platformlayer by platformlayer.

the class PostgresqlServerBackup method doBackup.

@Handler(BackupAction.class)
public void doBackup() throws OpsException, IOException {
    OpsContext opsContext = OpsContext.get();
    // Machine machine = opsContext.getInstance(Machine.class);
    OpsTarget target = opsContext.getInstance(OpsTarget.class);
    // We use pg_dump, not pg_dumpall:
    // 1) pg_dumpall doesn't support binary dumping (?)
    // 2) pg_dumpall wouldn't let us split the dump into different files (?)
    List<String> databases = listDatabases(target);
    BackupContext backupContext = backups.getContext();
    String baseName = UUID.randomUUID().toString();
    PostgresqlServer server = OpsContext.get().getInstance(PostgresqlServer.class);
    backupContext.add(new BackupItem(server.getKey(), FORMAT, baseName));
    {
        Command dumpAll = Command.build("su postgres -c \"pg_dumpall --globals-only\"");
        Backup request = new Backup();
        request.target = target;
        request.objectName = baseName + "/pgdump_meta";
        backupContext.uploadStream(request, dumpAll);
    }
    for (String database : databases) {
        // template0 cannot be backed up
        if (database.equals("template0")) {
            continue;
        }
        // template1 can be backed up, even though it isn't typically very useful
        String fileName = "pgdump_db_" + database;
        Backup request = new Backup();
        request.target = target;
        request.objectName = baseName + "/" + fileName;
        Command dumpDatabase = Command.build("su postgres -c \"pg_dump --oids -Fc --verbose {0}\"", database);
        backupContext.uploadStream(request, dumpDatabase);
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) BackupItem(org.platformlayer.ops.backups.BackupItem) Command(org.platformlayer.ops.Command) Backup(org.platformlayer.ops.backups.Backup) BackupContext(org.platformlayer.ops.backups.BackupContext) PostgresqlServer(org.platformlayer.service.postgresql.model.PostgresqlServer) OpsContext(org.platformlayer.ops.OpsContext) Handler(org.platformlayer.ops.Handler)

Aggregations

Command (org.platformlayer.ops.Command)1 Handler (org.platformlayer.ops.Handler)1 OpsContext (org.platformlayer.ops.OpsContext)1 OpsTarget (org.platformlayer.ops.OpsTarget)1 Backup (org.platformlayer.ops.backups.Backup)1 BackupContext (org.platformlayer.ops.backups.BackupContext)1 BackupItem (org.platformlayer.ops.backups.BackupItem)1 PostgresqlServer (org.platformlayer.service.postgresql.model.PostgresqlServer)1