Skip to content

Commit c9d11c5

Browse files
committed
PGPRO-1184: Add --overwrite option for archive-push
1 parent e330230 commit c9d11c5

File tree

7 files changed

+39
-26
lines changed

7 files changed

+39
-26
lines changed

src/archive.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* compute and validate checksums.
2323
*/
2424
int
25-
do_archive_push(char *wal_file_path, char *wal_file_name)
25+
do_archive_push(char *wal_file_path, char *wal_file_name, bool overwrite)
2626
{
2727
char backup_wal_file_path[MAXPGPATH];
2828
char absolute_wal_file_path[MAXPGPATH];
@@ -62,8 +62,6 @@ do_archive_push(char *wal_file_path, char *wal_file_name)
6262
join_path_components(backup_wal_file_path, arclog_path, wal_file_name);
6363

6464
elog(INFO, "pg_probackup archive-push from %s to %s", absolute_wal_file_path, backup_wal_file_path);
65-
if (access(backup_wal_file_path, F_OK) != -1)
66-
elog(ERROR, "file '%s', already exists.", backup_wal_file_path);
6765

6866
#ifdef HAVE_LIBZ
6967
if (compress_alg == PGLZ_COMPRESS)
@@ -72,7 +70,8 @@ do_archive_push(char *wal_file_path, char *wal_file_name)
7270
is_compress = IsXLogFileName(wal_file_name);
7371
#endif
7472

75-
push_wal_file(absolute_wal_file_path, backup_wal_file_path, is_compress);
73+
push_wal_file(absolute_wal_file_path, backup_wal_file_path, is_compress,
74+
overwrite);
7675
elog(INFO, "pg_probackup archive-push completed successfully");
7776

7877
return 0;

src/backup.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,22 +1867,6 @@ checkpoint_timeout(void)
18671867
return val_int;
18681868
}
18691869

1870-
/*
1871-
* Return true if the path is a existing regular file.
1872-
*/
1873-
bool
1874-
fileExists(const char *path)
1875-
{
1876-
struct stat buf;
1877-
1878-
if (stat(path, &buf) == -1 && errno == ENOENT)
1879-
return false;
1880-
else if (!S_ISREG(buf.st_mode))
1881-
return false;
1882-
else
1883-
return true;
1884-
}
1885-
18861870
/*
18871871
* Notify end of backup to server when "backup_label" is in the root directory
18881872
* of the DB cluster.

src/data.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,8 @@ copy_meta(const char *from_path, const char *to_path, bool unlink_on_error)
758758
* Copy WAL segment from pgdata to archive catalog with possible compression.
759759
*/
760760
void
761-
push_wal_file(const char *from_path, const char *to_path, bool is_compress)
761+
push_wal_file(const char *from_path, const char *to_path, bool is_compress,
762+
bool overwrite)
762763
{
763764
FILE *in = NULL;
764765
FILE *out;
@@ -781,6 +782,10 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress)
781782
if (is_compress)
782783
{
783784
snprintf(gz_to_path, sizeof(gz_to_path), "%s.gz", to_path);
785+
786+
if (!overwrite && fileExists(gz_to_path))
787+
elog(ERROR, "WAL segment \"%s\" already exists.", gz_to_path);
788+
784789
gz_out = gzopen(gz_to_path, "wb");
785790
if (gzsetparams(gz_out, compress_level, Z_DEFAULT_STRATEGY) != Z_OK)
786791
elog(ERROR, "Cannot set compression level %d to file \"%s\": %s",
@@ -791,6 +796,9 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress)
791796
else
792797
#endif
793798
{
799+
if (!overwrite && fileExists(to_path))
800+
elog(ERROR, "WAL segment \"%s\" already exists.", to_path);
801+
794802
out = fopen(to_path, "w");
795803
if (out == NULL)
796804
elog(ERROR, "Cannot open destination WAL file \"%s\": %s",

src/dir.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ static char *pgdata_exclude_files[] =
8080
NULL
8181
};
8282

83-
pgFile *pgFileNew(const char *path, bool omit_symlink);
8483
static int BlackListCompare(const void *str1, const void *str2);
8584

8685
static void dir_list_file_internal(parray *files, const char *root,
@@ -947,3 +946,19 @@ dir_is_empty(const char *path)
947946

948947
return true;
949948
}
949+
950+
/*
951+
* Return true if the path is a existing regular file.
952+
*/
953+
bool
954+
fileExists(const char *path)
955+
{
956+
struct stat buf;
957+
958+
if (stat(path, &buf) == -1 && errno == ENOENT)
959+
return false;
960+
else if (!S_ISREG(buf.st_mode))
961+
return false;
962+
else
963+
return true;
964+
}

src/help.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ help_pg_probackup(void)
127127
printf(_(" --wal-file-path=wal-file-path\n"));
128128
printf(_(" --wal-file-name=wal-file-name\n"));
129129
printf(_(" [--compress [--compress-level=compress-level]]\n"));
130+
printf(_(" [--overwrite]\n"));
130131

131132
printf(_("\n %s archive-get -B backup-dir --instance=instance_name\n"), PROGRAM_NAME);
132133
printf(_(" --wal-file-path=wal-file-path\n"));
@@ -365,6 +366,7 @@ help_archive_push(void)
365366
printf(_(" --wal-file-path=wal-file-path\n"));
366367
printf(_(" --wal-file-name=wal-file-name\n"));
367368
printf(_(" [--compress [--compress-level=compress-level]]\n\n"));
369+
printf(_(" [--overwrite]\n"));
368370

369371
printf(_(" -B, --backup-path=backup-path location of the backup storage area\n"));
370372
printf(_(" --instance=instance_name name of the instance to delete\n"));
@@ -375,6 +377,7 @@ help_archive_push(void)
375377
printf(_(" --compress compress WAL file during archiving\n"));
376378
printf(_(" --compress-level=compress-level\n"));
377379
printf(_(" level of compression [0-9]\n"));
380+
printf(_(" --overwrite overwrite archived WAL file\n"));
378381
}
379382

380383
static void

src/pg_probackup.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ uint64 system_identifier = 0;
8585
/* archive push options */
8686
static char *wal_file_path;
8787
static char *wal_file_name;
88+
static bool file_overwrite = false;
8889

8990
/* current settings */
9091
pgBackup current;
@@ -161,6 +162,7 @@ static pgut_option options[] =
161162
/* archive-push options */
162163
{ 's', 160, "wal-file-path", &wal_file_path, SOURCE_CMDLINE },
163164
{ 's', 161, "wal-file-name", &wal_file_name, SOURCE_CMDLINE },
165+
{ 'b', 162, "overwrite", &file_overwrite, SOURCE_CMDLINE },
164166
{ 0 }
165167
};
166168

@@ -410,7 +412,7 @@ main(int argc, char *argv[])
410412
switch (backup_subcmd)
411413
{
412414
case ARCHIVE_PUSH:
413-
return do_archive_push(wal_file_path, wal_file_name);
415+
return do_archive_push(wal_file_path, wal_file_name, file_overwrite);
414416
case ARCHIVE_GET:
415417
return do_archive_get(wal_file_path, wal_file_name);
416418
case ADD_INSTANCE:

src/pg_probackup.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ extern const char *pgdata_exclude_dir[];
316316
extern int do_backup(time_t start_time);
317317
extern BackupMode parse_backup_mode(const char *value);
318318
extern const char *deparse_backup_mode(BackupMode mode);
319-
extern bool fileExists(const char *path);
320319
extern void process_block_change(ForkNumber forknum, RelFileNode rnode,
321320
BlockNumber blkno);
322321

@@ -343,7 +342,8 @@ extern int do_init(void);
343342
extern int do_add_instance(void);
344343

345344
/* in archive.c */
346-
extern int do_archive_push(char *wal_file_path, char *wal_file_name);
345+
extern int do_archive_push(char *wal_file_path, char *wal_file_name,
346+
bool overwrite);
347347
extern int do_archive_get(char *wal_file_path, char *wal_file_name);
348348

349349

@@ -409,6 +409,8 @@ extern parray *dir_read_file_list(const char *root, const char *file_txt);
409409
extern int dir_create_dir(const char *path, mode_t mode);
410410
extern bool dir_is_empty(const char *path);
411411

412+
extern bool fileExists(const char *path);
413+
412414
extern pgFile *pgFileNew(const char *path, bool omit_symlink);
413415
extern pgFile *pgFileInit(const char *path);
414416
extern void pgFileDelete(pgFile *file);
@@ -428,7 +430,7 @@ extern void restore_data_file(const char *from_root, const char *to_root,
428430
extern bool copy_file(const char *from_root, const char *to_root,
429431
pgFile *file);
430432
extern void push_wal_file(const char *from_path, const char *to_path,
431-
bool is_compress);
433+
bool is_compress, bool overwrite);
432434
extern void get_wal_file(const char *from_path, const char *to_path);
433435

434436
extern bool calc_file_checksum(pgFile *file);

0 commit comments

Comments
 (0)