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

Postgres replication #7

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/db/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ typedef struct init_db_func_ptr_s {
struct db_t {
postgres_t *pg_conf;

void *host_conn;
void *origin_conn;
void *target_conn;
};

Expand Down
16 changes: 16 additions & 0 deletions include/db/postgres.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@

#include "db/db.h"

/*
* For the `pg_dump` and `pg_restore` to work we need to set the passwords as env variables,
* prefixed with `PGPASSWORD=`
*/
#define PG_PASS_PREFIX 11
#define PG_DUMP_COMMAND "pg_dump"
#define PG_RESTORE_COMMAND "pg_restore"

void read_buffer_pipe(int *);

void construct_dump_path(char *);

int construct_pg(void);

int connect_pg(struct db_t *);
void close_pg(struct db_t *pg_db_t);
int replicate(struct db_t *pg_db_t, struct options *pg_options);

int execve_binary(char* ,char *const[], char *const[]);

#endif
17 changes: 13 additions & 4 deletions src/db.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "db/db.h"
#include "db/postgres.h"

#include <stddef.h>
#include <stdlib.h>

Expand All @@ -11,6 +12,8 @@ struct db_operations **available_dbs;

int execute_db_operations(void)
{
int ret = 0;
struct db_t *available_db;
available_dbs = (struct db_operations **)calloc(
MAX_AVAILABLE_DBS, sizeof(struct db_operations **));

Expand All @@ -22,13 +25,19 @@ int execute_db_operations(void)
for (int i = 0; i < MAX_AVAILABLE_DBS; i++) {
/* do db_operations for all available db impl. */
if (available_dbs[i] != NULL) {
struct db_t *available_db = available_dbs[i]->db;
available_dbs[i]->connect(available_db);
available_dbs[i]->close(available_db);
available_db = available_dbs[i]->db;
ret = available_dbs[i]->connect(available_db);
if (ret != 0) {
available_dbs[i]->close(available_db);
return ret;
}
available_dbs[i]->replicate(available_db, NULL);
free(available_db);
available_dbs[i]->close(available_db);
}
}

free(available_db);
free(available_dbs);

return 0;
}
4 changes: 2 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ extern config_t *yaml_config;
extern bool verbose;

static struct option options[] = {
{ "config-file", required_argument, 0, 'f' },
{ "verbose", no_argument, 0, 'v' },
{ "config-file", required_argument, NULL, 'f' },
{ "verbose", no_argument, NULL, 'v' },
};

int main(int argc, char **argv)
Expand Down
Loading