mirror of
https://github.com/postgrespro/pg_probackup.git
synced 2026-06-21 01:34:15 +02:00
Refactoring. Introduce catalogState structure. Update code in init.c
This commit is contained in:
+16
-22
@@ -17,42 +17,37 @@
|
||||
* Initialize backup catalog.
|
||||
*/
|
||||
int
|
||||
do_init(char *backup_catalog_path)
|
||||
do_init(CatalogState *catalogState)
|
||||
{
|
||||
char path[MAXPGPATH];
|
||||
char arclog_path_dir[MAXPGPATH];
|
||||
int results;
|
||||
|
||||
results = pg_check_dir(backup_catalog_path);
|
||||
results = pg_check_dir(catalogState->catalog_path);
|
||||
|
||||
if (results == 4) /* exists and not empty*/
|
||||
elog(ERROR, "backup catalog already exist and it's not empty");
|
||||
else if (results == -1) /*trouble accessing directory*/
|
||||
{
|
||||
int errno_tmp = errno;
|
||||
elog(ERROR, "cannot open backup catalog directory \"%s\": %s",
|
||||
backup_catalog_path, strerror(errno_tmp));
|
||||
catalogState->catalog_path, strerror(errno_tmp));
|
||||
}
|
||||
|
||||
/* create backup catalog root directory */
|
||||
dir_create_dir(backup_catalog_path, DIR_PERMISSION, false);
|
||||
dir_create_dir(catalogState->catalog_path, DIR_PERMISSION, false);
|
||||
|
||||
/* create backup catalog data directory */
|
||||
join_path_components(path, backup_catalog_path, BACKUPS_DIR);
|
||||
dir_create_dir(path, DIR_PERMISSION, false);
|
||||
dir_create_dir(catalogState->backup_subdir_path, DIR_PERMISSION, false);
|
||||
|
||||
/* create backup catalog wal directory */
|
||||
join_path_components(arclog_path_dir, backup_catalog_path, "wal");
|
||||
dir_create_dir(arclog_path_dir, DIR_PERMISSION, false);
|
||||
dir_create_dir(catalogState->wal_subdir_path, DIR_PERMISSION, false);
|
||||
|
||||
elog(INFO, "Backup catalog '%s' successfully inited", backup_catalog_path);
|
||||
elog(INFO, "Backup catalog '%s' successfully inited", catalogState->catalog_path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
do_add_instance(char *backup_catalog_path, InstanceConfig *instance)
|
||||
do_add_instance(CatalogState *catalogState, InstanceConfig *instance)
|
||||
{
|
||||
char path[MAXPGPATH];
|
||||
char arclog_path_dir[MAXPGPATH];
|
||||
struct stat st;
|
||||
|
||||
/* PGDATA is always required */
|
||||
@@ -66,16 +61,15 @@ do_add_instance(char *backup_catalog_path, InstanceConfig *instance)
|
||||
instance->xlog_seg_size = get_xlog_seg_size(instance->pgdata);
|
||||
|
||||
/* Ensure that all root directories already exist */
|
||||
if (access(backup_catalog_path, F_OK) != 0)
|
||||
elog(ERROR, "Directory does not exist: '%s'", backup_catalog_path);
|
||||
/* TODO maybe call do_init() here instead of error?*/
|
||||
if (access(catalogState->catalog_path, F_OK) != 0)
|
||||
elog(ERROR, "Directory does not exist: '%s'", catalogState->catalog_path);
|
||||
|
||||
join_path_components(path, backup_catalog_path, BACKUPS_DIR);
|
||||
if (access(path, F_OK) != 0)
|
||||
elog(ERROR, "Directory does not exist: '%s'", path);
|
||||
if (access(catalogState->backup_subdir_path, F_OK) != 0)
|
||||
elog(ERROR, "Directory does not exist: '%s'", catalogState->backup_subdir_path);
|
||||
|
||||
join_path_components(arclog_path_dir, backup_catalog_path, "wal");
|
||||
if (access(arclog_path_dir, F_OK) != 0)
|
||||
elog(ERROR, "Directory does not exist: '%s'", arclog_path_dir);
|
||||
if (access(catalogState->wal_subdir_path, F_OK) != 0)
|
||||
elog(ERROR, "Directory does not exist: '%s'", catalogState->wal_subdir_path);
|
||||
|
||||
if (stat(instance->backup_instance_path, &st) == 0 && S_ISDIR(st.st_mode))
|
||||
elog(ERROR, "Instance '%s' backup directory already exists: '%s'",
|
||||
|
||||
+19
-5
@@ -71,10 +71,10 @@ char backup_instance_path[MAXPGPATH];
|
||||
*/
|
||||
char arclog_path[MAXPGPATH] = "";
|
||||
|
||||
|
||||
static CatalogState *catalogState = NULL;
|
||||
/* ================ catalogState (END) =========== */
|
||||
|
||||
|
||||
|
||||
/* colon separated external directories list ("/path1:/path2") */
|
||||
char *externaldir = NULL;
|
||||
/* common options */
|
||||
@@ -424,6 +424,7 @@ main(int argc, char *argv[])
|
||||
/* set location based on cmdline options only */
|
||||
setMyLocation(backup_subcmd);
|
||||
|
||||
/* ===== catalogState ======*/
|
||||
if (backup_path == NULL)
|
||||
{
|
||||
/*
|
||||
@@ -440,11 +441,24 @@ main(int argc, char *argv[])
|
||||
/* Ensure that backup_path is an absolute path */
|
||||
if (!is_absolute_path(backup_path))
|
||||
elog(ERROR, "-B, --backup-path must be an absolute path");
|
||||
|
||||
catalogState = pgut_new(CatalogState);
|
||||
strncpy(catalogState->catalog_path, backup_path, MAXPGPATH);
|
||||
join_path_components(catalogState->backup_subdir_path,
|
||||
catalogState->catalog_path, BACKUPS_DIR);
|
||||
join_path_components(catalogState->wal_subdir_path,
|
||||
catalogState->catalog_path, WAL_SUBDIR);
|
||||
}
|
||||
|
||||
/* backup_path is required for all pg_probackup commands except help, version and checkdb */
|
||||
if (backup_path == NULL && backup_subcmd != CHECKDB_CMD && backup_subcmd != HELP_CMD && backup_subcmd != VERSION_CMD)
|
||||
if (backup_path == NULL &&
|
||||
backup_subcmd != CHECKDB_CMD &&
|
||||
backup_subcmd != HELP_CMD &&
|
||||
backup_subcmd != VERSION_CMD)
|
||||
elog(ERROR, "required parameter not specified: BACKUP_PATH (-B, --backup-path)");
|
||||
|
||||
/* ===== catalogState (END) ======*/
|
||||
|
||||
/*
|
||||
* Option --instance is required for all commands except
|
||||
* init, show, checkdb and validate
|
||||
@@ -772,11 +786,11 @@ main(int argc, char *argv[])
|
||||
wal_file_path, wal_file_name, batch_size, !no_validate_wal);
|
||||
break;
|
||||
case ADD_INSTANCE_CMD:
|
||||
return do_add_instance(backup_path, &instance_config);
|
||||
return do_add_instance(catalogState, &instance_config);
|
||||
case DELETE_INSTANCE_CMD:
|
||||
return do_delete_instance();
|
||||
case INIT_CMD:
|
||||
return do_init(backup_path);
|
||||
return do_init(catalogState);
|
||||
case BACKUP_CMD:
|
||||
{
|
||||
current.stream = stream_wal;
|
||||
|
||||
+18
-2
@@ -55,6 +55,7 @@ extern const char *PROGRAM_EMAIL;
|
||||
/* Directory/File names */
|
||||
#define DATABASE_DIR "database"
|
||||
#define BACKUPS_DIR "backups"
|
||||
#define WAL_SUBDIR "wal"
|
||||
#if PG_VERSION_NUM >= 100000
|
||||
#define PG_XLOG_DIR "pg_wal"
|
||||
#define PG_LOG_DIR "log"
|
||||
@@ -129,6 +130,7 @@ extern const char *PROGRAM_EMAIL;
|
||||
#define TC_CYAN_BOLD "\033[1;36m"
|
||||
#define TC_RESET "\033[0m"
|
||||
|
||||
|
||||
typedef struct RedoParams
|
||||
{
|
||||
TimeLineID tli;
|
||||
@@ -746,11 +748,25 @@ typedef struct BackupPageHeader2
|
||||
|
||||
#define IsSshProtocol() (instance_config.remote.host && strcmp(instance_config.remote.proto, "ssh") == 0)
|
||||
|
||||
/* ====== CatalogState ======= */
|
||||
|
||||
/* directory options */
|
||||
extern char *backup_path;
|
||||
extern char backup_instance_path[MAXPGPATH];
|
||||
extern char arclog_path[MAXPGPATH];
|
||||
|
||||
typedef struct CatalogState
|
||||
{
|
||||
/* $BACKUP_PATH */
|
||||
char catalog_path[MAXPGPATH]; //previously global var backup_path
|
||||
/* $BACKUP_PATH/backups */
|
||||
char backup_subdir_path[MAXPGPATH];
|
||||
/* $BACKUP_PATH/wal */
|
||||
char wal_subdir_path[MAXPGPATH]; // previously global var arclog_path
|
||||
} CatalogState;
|
||||
|
||||
/* ====== CatalogState (END) ======= */
|
||||
|
||||
/* common options */
|
||||
extern pid_t my_pid;
|
||||
extern __thread int my_thread_num;
|
||||
@@ -839,8 +855,8 @@ extern void merge_chain(parray *parent_chain,
|
||||
extern parray *read_database_map(pgBackup *backup);
|
||||
|
||||
/* in init.c */
|
||||
extern int do_init(char *backup_catalog_path);
|
||||
extern int do_add_instance(char *backup_catalog_path, InstanceConfig *instance);
|
||||
extern int do_init(CatalogState *catalogState);
|
||||
extern int do_add_instance(CatalogState *catalogState, InstanceConfig *instance);
|
||||
|
||||
/* in archive.c */
|
||||
extern void do_archive_push(InstanceConfig *instance, char *wal_file_path,
|
||||
|
||||
Reference in New Issue
Block a user