2013-01-24 09:35:48 +03:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* init.c: manage backup catalog.
|
|
|
|
*
|
2017-03-01 15:50:07 +02:00
|
|
|
* Portions Copyright (c) 2009-2011, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
|
|
|
|
* Portions Copyright (c) 2015-2017, Postgres Professional
|
2013-01-24 09:35:48 +03:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2016-11-16 19:34:21 +02:00
|
|
|
#include "pg_probackup.h"
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* selects function for scandir.
|
|
|
|
*/
|
|
|
|
static int selects(const struct dirent *dir)
|
|
|
|
{
|
2017-04-05 18:48:55 +02:00
|
|
|
return dir->d_name[0] != '.';
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize backup catalog.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
do_init(void)
|
|
|
|
{
|
2017-04-05 18:48:55 +02:00
|
|
|
char path[MAXPGPATH];
|
|
|
|
char arclog_path_dir[MAXPGPATH];
|
|
|
|
FILE *fp;
|
|
|
|
uint64 id;
|
2013-01-24 09:35:48 +03:00
|
|
|
|
|
|
|
struct dirent **dp;
|
|
|
|
int results;
|
2017-02-01 14:40:47 +02:00
|
|
|
|
|
|
|
/* PGDATA is always required */
|
|
|
|
if (pgdata == NULL)
|
|
|
|
elog(ERROR, "Required parameter not specified: PGDATA "
|
|
|
|
"(-D, --pgdata)");
|
|
|
|
|
2016-01-14 09:36:39 +02:00
|
|
|
if (access(backup_path, F_OK) == 0)
|
|
|
|
{
|
2013-01-24 09:35:48 +03:00
|
|
|
results = scandir(backup_path, &dp, selects, NULL);
|
2016-01-14 09:36:39 +02:00
|
|
|
if (results != 0)
|
2016-11-28 15:33:13 +02:00
|
|
|
elog(ERROR, "backup catalog already exist and it's not empty");
|
2013-01-24 09:35:48 +03:00
|
|
|
}
|
|
|
|
|
2017-04-05 18:48:55 +02:00
|
|
|
/* Read system_identifier from PGDATA */
|
|
|
|
id = get_system_identifier(false);
|
|
|
|
|
2013-01-24 09:35:48 +03:00
|
|
|
/* create backup catalog root directory */
|
|
|
|
dir_create_dir(backup_path, DIR_PERMISSION);
|
|
|
|
|
|
|
|
/* create directories for backup of online files */
|
2016-11-07 16:35:53 +02:00
|
|
|
join_path_components(path, backup_path, BACKUPS_DIR);
|
2013-01-24 09:35:48 +03:00
|
|
|
dir_create_dir(path, DIR_PERMISSION);
|
|
|
|
|
2017-02-12 22:42:10 +02:00
|
|
|
/* Create "wal" directory */
|
|
|
|
join_path_components(arclog_path_dir, backup_path, "wal");
|
|
|
|
dir_create_dir(arclog_path_dir, DIR_PERMISSION);
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2016-11-16 19:34:21 +02:00
|
|
|
/* create pg_probackup.conf */
|
2017-02-12 22:42:10 +02:00
|
|
|
join_path_components(path, backup_path, BACKUP_CATALOG_CONF_FILE);
|
2013-01-24 09:35:48 +03:00
|
|
|
fp = fopen(path, "wt");
|
|
|
|
if (fp == NULL)
|
2017-03-24 15:58:35 +02:00
|
|
|
elog(ERROR, "cannot create %s: %s",
|
|
|
|
BACKUP_CATALOG_CONF_FILE, strerror(errno));
|
2013-01-24 09:35:48 +03:00
|
|
|
|
2017-04-05 18:48:55 +02:00
|
|
|
fprintf(fp, "system-identifier = %li\n", id);
|
2013-01-24 09:35:48 +03:00
|
|
|
fprintf(fp, "\n");
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|