Skip to content

Commit cb939f6

Browse files
author
Michael Paquier
committed
Block backup operations for standbys
Backup from standbys should use a method based on replication protocol in a way similar to what is done in pg_basebackup, as it cannot use pg_start/stop_backup. As I am not sure what would be the right approach by the way, it is better for the time being to block backups taken from a standby. It does not penalize the functionality though as taking disk snapshots is not forbidden either, and a user can still recover from that. This commit removes at the same time some home-made functions that created custom backup label files, this is not relyable, especially if Postgres core format for this file changes across versions. Removing them at least will save from some bugs for sure.
1 parent 85f0c1e commit cb939f6

File tree

3 files changed

+29
-98
lines changed

3 files changed

+29
-98
lines changed

backup.c

Lines changed: 28 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ static void backup_files(const char *from_root, const char *to_root,
4343
static parray *do_backup_database(parray *backup_list, pgBackupOption bkupopt);
4444
static parray *do_backup_arclog(parray *backup_list);
4545
static parray *do_backup_srvlog(parray *backup_list);
46-
static void remove_stopinfo_from_backup_label(char *history_file, char *bkup_label);
47-
static void make_backup_label(parray *backup_list);
4846
static void confirm_block_size(const char *name, int blcksz);
4947
static void pg_start_backup(const char *label, bool smooth, pgBackup *backup);
5048
static void pg_stop_backup(pgBackup *backup);
5149
static void pg_switch_xlog(pgBackup *backup);
50+
static bool pg_is_standby(void);
5251
static void get_lsn(PGresult *res, XLogRecPtr *lsn);
5352
static void get_xid(PGresult *res, uint32 *xid);
5453

@@ -86,6 +85,10 @@ do_backup_database(parray *backup_list, pgBackupOption bkupopt)
8685
if (current.backup_mode == BACKUP_MODE_ARCHIVE)
8786
return NULL;
8887

88+
/* Block backup operations on a standby */
89+
if (pg_is_standby())
90+
elog(ERROR_SYSTEM, _("Backup cannot run on a standby."));
91+
8992
elog(INFO, _("database backup start"));
9093

9194
/* Initialize size summary */
@@ -125,12 +128,8 @@ do_backup_database(parray *backup_list, pgBackupOption bkupopt)
125128
if (!fileExists(path))
126129
has_backup_label = false;
127130

128-
snprintf(path, lengthof(path), "%s/recovery.conf", pgdata);
129-
make_native_path(path);
130-
if (fileExists(path))
131-
current.is_from_standby = true;
132-
133-
if (!has_backup_label && !current.is_from_standby)
131+
/* Leave if no backup file */
132+
if (!has_backup_label)
134133
{
135134
if (verbose)
136135
printf(_("backup_label does not exist, stop backup\n"));
@@ -386,11 +385,6 @@ do_backup_database(parray *backup_list, pgBackupOption bkupopt)
386385
/* notify end of backup */
387386
pg_stop_backup(&current);
388387

389-
/* if backup is from standby, making backup_label from */
390-
/* backup.history file. */
391-
if (current.is_from_standby)
392-
make_backup_label(files);
393-
394388
/* create file list */
395389
create_file_list(files, pgdata, NULL, false);
396390
}
@@ -439,6 +433,10 @@ do_backup_arclog(parray *backup_list)
439433
current.backup_mode == BACKUP_MODE_INCREMENTAL ||
440434
current.backup_mode == BACKUP_MODE_FULL);
441435

436+
/* Block backup operations on a standby */
437+
if (pg_is_standby())
438+
elog(ERROR_SYSTEM, _("Backup cannot run on a standby."));
439+
442440
if (verbose)
443441
{
444442
printf(_("========================================\n"));
@@ -455,10 +453,6 @@ do_backup_arclog(parray *backup_list)
455453
if ((uint32) current.stop_lsn == 0)
456454
pg_switch_xlog(&current);
457455

458-
/* Archive backup is not available for a standby */
459-
if (current.is_from_standby)
460-
elog(ERROR_SYSTEM, _("Archive backup not allowed on a standby node"));
461-
462456
/*
463457
* Check if there is a full backup present on current timeline.
464458
* For an incremental or full backup, we are sure that there is one
@@ -584,6 +578,10 @@ do_backup_srvlog(parray *backup_list)
584578
if (!current.with_serverlog)
585579
return NULL;
586580

581+
/* Block backup operations on a standby */
582+
if (pg_is_standby())
583+
elog(ERROR_SYSTEM, _("Backup cannot run on a standby."));
584+
587585
if (verbose)
588586
{
589587
printf(_("========================================\n"));
@@ -739,7 +737,6 @@ do_backup(pgBackupOption bkupopt)
739737
current.wal_block_size = XLOG_BLCKSZ;
740738
current.recovery_xid = 0;
741739
current.recovery_time = (time_t) 0;
742-
current.is_from_standby = false;
743740

744741
/* create backup directory and backup.ini */
745742
if (!check)
@@ -843,80 +840,6 @@ do_backup(pgBackupOption bkupopt)
843840
return 0;
844841
}
845842

846-
void
847-
remove_stopinfo_from_backup_label(char *history_file, char *bkup_label)
848-
{
849-
FILE *read;
850-
FILE *write;
851-
char buf[MAXPGPATH * 2];
852-
853-
if ((read = fopen(history_file, "r")) == NULL)
854-
elog(ERROR_SYSTEM,
855-
_("can't open backup history file for standby backup."));
856-
if ((write = fopen(bkup_label, "w")) == NULL)
857-
elog(ERROR_SYSTEM,
858-
_("can't open backup_label file for standby backup."));
859-
while (fgets(buf, lengthof(buf), read) != NULL)
860-
{
861-
if (strstr(buf, "STOP") - buf == 0)
862-
continue;
863-
fputs(buf, write);
864-
}
865-
fclose(write);
866-
fclose(read);
867-
}
868-
869-
/*
870-
* creating backup_label from backup.history for standby backup.
871-
*/
872-
void
873-
make_backup_label(parray *backup_list)
874-
{
875-
char dest_path[MAXPGPATH];
876-
char src_bkup_history_file[MAXPGPATH];
877-
char dst_bkup_label_file[MAXPGPATH];
878-
char original_bkup_label_file[MAXPGPATH];
879-
parray *bkuped_arc_files = NULL;
880-
int i;
881-
882-
pgBackupGetPath(&current, dest_path, lengthof(dest_path), DATABASE_DIR);
883-
bkuped_arc_files = parray_new();
884-
dir_list_file(bkuped_arc_files, arclog_path, NULL, true, false);
885-
886-
for (i = parray_num(bkuped_arc_files) - 1; i >= 0; i--)
887-
{
888-
char *current_arc_fname;
889-
pgFile *current_arc_file;
890-
891-
current_arc_file = (pgFile *) parray_get(bkuped_arc_files, i);
892-
current_arc_fname = last_dir_separator(current_arc_file->path) + 1;
893-
894-
if(strlen(current_arc_fname) <= 24) continue;
895-
896-
copy_file(arclog_path, dest_path, current_arc_file, NO_COMPRESSION);
897-
join_path_components(src_bkup_history_file, dest_path, current_arc_fname);
898-
join_path_components(dst_bkup_label_file, dest_path, PG_BACKUP_LABEL_FILE);
899-
join_path_components(original_bkup_label_file, pgdata, PG_BACKUP_LABEL_FILE);
900-
remove_stopinfo_from_backup_label(src_bkup_history_file, dst_bkup_label_file);
901-
902-
dir_list_file(backup_list, dst_bkup_label_file, NULL, false, true);
903-
for (i = 0; i < parray_num(backup_list); i++)
904-
{
905-
pgFile *file = (pgFile *)parray_get(backup_list, i);
906-
if (strcmp(file->path, dst_bkup_label_file) == 0)
907-
{
908-
struct stat st;
909-
stat(dst_bkup_label_file, &st);
910-
file->write_size = st.st_size;
911-
file->crc = pgFileGetCRC(file);
912-
strcpy(file->path, original_bkup_label_file);
913-
}
914-
}
915-
parray_qsort(backup_list, pgFileComparePath);
916-
break;
917-
}
918-
}
919-
920843
/*
921844
* get server version and confirm block sizes.
922845
*/
@@ -1080,6 +1003,19 @@ pg_switch_xlog(pgBackup *backup)
10801003
"SELECT * FROM pg_switch_xlog()");
10811004
}
10821005

1006+
/*
1007+
* Check if node is a standby by looking at the presence of
1008+
* recovery.conf.
1009+
*/
1010+
static bool
1011+
pg_is_standby(void)
1012+
{
1013+
char path[MAXPGPATH];
1014+
snprintf(path, lengthof(path), "%s/recovery.conf", pgdata);
1015+
make_native_path(path);
1016+
return fileExists(path);
1017+
}
1018+
10831019
/*
10841020
* Get LSN from result of pg_start_backup() or pg_stop_backup().
10851021
*/

pg_rman.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,6 @@ typedef struct pgBackup
154154
/* data/wal block size for compatibility check */
155155
uint32 block_size;
156156
uint32 wal_block_size;
157-
158-
/* if backup from standby or not */
159-
bool is_from_standby;
160-
161157
} pgBackup;
162158

163159
typedef struct pgBackupOption

pg_rman.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ Incremental backup
8282
Backup only files or pages modified after the last verified backup.
8383

8484
Archive WAL backup
85-
Backup only archive WAL files. Archive backup is not available for
86-
a standby node.
85+
Backup only archive WAL files.
8786

8887
It is recommended to verify backup files as soon as possible after backup.
8988
Unverified backup cannot be used in restore and in incremental backup.

0 commit comments

Comments
 (0)