use of org.platformlayer.service.postgresql.model.PostgresqlServer in project platformlayer by platformlayer.
the class PostgresqlServerBootstrap method handler.
@Handler
public void handler() throws OpsException {
PostgresqlServer model = OpsContext.get().getInstance(PostgresqlServer.class);
OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
// su postgres -c "psql --command=\"ALTER USER postgres WITH PASSWORD 'secret';\""
String password = model.rootPassword.plaintext();
String sql = String.format("ALTER USER postgres WITH PASSWORD '%s';", password);
String psql = String.format("psql --command=\"%s\"", sql);
Command command = Command.build("su postgres -c {0}", psql);
target.executeCommand(command);
}
use of org.platformlayer.service.postgresql.model.PostgresqlServer 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);
}
}
Aggregations