1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2024-11-24 08:52:38 +02:00
pg_probackup/init.c

77 lines
1.8 KiB
C

/*-------------------------------------------------------------------------
*
* init.c: manage backup catalog.
*
* Portions Copyright (c) 2009-2011, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
* Portions Copyright (c) 2015-2017, Postgres Professional
*
*-------------------------------------------------------------------------
*/
#include "pg_probackup.h"
#include <unistd.h>
#include <dirent.h>
/*
* selects function for scandir.
*/
static int selects(const struct dirent *dir)
{
return dir->d_name[0] != '.';
}
/*
* Initialize backup catalog.
*/
int
do_init(void)
{
char path[MAXPGPATH];
char arclog_path_dir[MAXPGPATH];
FILE *fp;
uint64 id;
struct dirent **dp;
int results;
/* PGDATA is always required */
if (pgdata == NULL)
elog(ERROR, "Required parameter not specified: PGDATA "
"(-D, --pgdata)");
if (access(backup_path, F_OK) == 0)
{
results = scandir(backup_path, &dp, selects, NULL);
if (results != 0)
elog(ERROR, "backup catalog already exist and it's not empty");
}
/* Read system_identifier from PGDATA */
id = get_system_identifier(false);
/* create backup catalog root directory */
dir_create_dir(backup_path, DIR_PERMISSION);
/* create directories for backup of online files */
join_path_components(path, backup_path, BACKUPS_DIR);
dir_create_dir(path, DIR_PERMISSION);
/* Create "wal" directory */
join_path_components(arclog_path_dir, backup_path, "wal");
dir_create_dir(arclog_path_dir, DIR_PERMISSION);
/* create pg_probackup.conf */
join_path_components(path, backup_path, BACKUP_CATALOG_CONF_FILE);
fp = fopen(path, "wt");
if (fp == NULL)
elog(ERROR, "cannot create %s: %s",
BACKUP_CATALOG_CONF_FILE, strerror(errno));
fprintf(fp, "system-identifier = %li\n", id);
fprintf(fp, "\n");
fclose(fp);
return 0;
}