@@ -43,12 +43,11 @@ static void backup_files(const char *from_root, const char *to_root,
4343static parray * do_backup_database (parray * backup_list ,pgBackupOption bkupopt );
4444static parray * do_backup_arclog (parray * backup_list );
4545static 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 );
4846static void confirm_block_size (const char * name ,int blcksz );
4947static void pg_start_backup (const char * label ,bool smooth ,pgBackup * backup );
5048static void pg_stop_backup (pgBackup * backup );
5149static void pg_switch_xlog (pgBackup * backup );
50+ static bool pg_is_standby (void );
5251static void get_lsn (PGresult * res ,XLogRecPtr * lsn );
5352static void get_xid (PGresult * res ,uint32 * xid );
5453
@@ -86,6 +85,10 @@ do_backup_database(parray *backup_list, pgBackupOption bkupopt)
8685if (current .backup_mode == BACKUP_MODE_ARCHIVE )
8786return 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+
8992elog (INFO ,_ ("database backup start" ));
9093
9194/* Initialize size summary */
@@ -125,12 +128,8 @@ do_backup_database(parray *backup_list, pgBackupOption bkupopt)
125128if (!fileExists (path ))
126129has_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{
135134if (verbose )
136135printf (_ ("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 */
387386pg_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 */
395389create_file_list (files ,pgdata ,NULL , false);
396390}
@@ -439,6 +433,10 @@ do_backup_arclog(parray *backup_list)
439433current .backup_mode == BACKUP_MODE_INCREMENTAL ||
440434current .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+
442440if (verbose )
443441{
444442printf (_ ("========================================\n" ));
@@ -455,10 +453,6 @@ do_backup_arclog(parray *backup_list)
455453if ((uint32 )current .stop_lsn == 0 )
456454pg_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)
584578if (!current .with_serverlog )
585579return 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+
587585if (verbose )
588586{
589587printf (_ ("========================================\n" ));
@@ -739,7 +737,6 @@ do_backup(pgBackupOption bkupopt)
739737current .wal_block_size = XLOG_BLCKSZ ;
740738current .recovery_xid = 0 ;
741739current .recovery_time = (time_t )0 ;
742- current .is_from_standby = false;
743740
744741/* create backup directory and backup.ini */
745742if (!check )
@@ -843,80 +840,6 @@ do_backup(pgBackupOption bkupopt)
843840return 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 */