@@ -475,6 +475,8 @@ do_backup_instance(void)
475
475
pgBackup * prev_backup = NULL ;
476
476
parray * prev_backup_filelist = NULL ;
477
477
478
+ pgFile * pg_control = NULL ;
479
+
478
480
elog (LOG , "Database backup start" );
479
481
480
482
/* Initialize size summary */
@@ -754,9 +756,37 @@ do_backup_instance(void)
754
756
parray_free (prev_backup_filelist );
755
757
}
756
758
759
+ /* Copy pg_control in case of backup from replica >= 9.6 */
760
+ if (current .from_replica && !exclusive_backup )
761
+ {
762
+ for (i = 0 ; i < parray_num (backup_files_list ); i ++ )
763
+ {
764
+ pgFile * tmp_file = (pgFile * ) parray_get (backup_files_list , i );
765
+
766
+ if (strcmp (tmp_file -> name , "pg_control" ) == 0 )
767
+ {
768
+ pg_control = tmp_file ;
769
+ break ;
770
+ }
771
+ }
772
+
773
+ if (!pg_control )
774
+ elog (ERROR , "Failed to locate pg_control in copied files" );
775
+
776
+ if (is_remote_backup )
777
+ remote_copy_file (NULL , pg_control );
778
+ else
779
+ if (!copy_file (pgdata , database_path , pg_control ))
780
+ elog (ERROR , "Failed to copy pg_control" );
781
+ }
782
+
783
+
757
784
/* Notify end of backup */
758
785
pg_stop_backup (& current );
759
786
787
+ if (current .from_replica && !exclusive_backup )
788
+ set_min_recovery_point (pg_control , database_path , current .stop_lsn );
789
+
760
790
/* Add archived xlog files into the list of files of this backup */
761
791
if (stream_wal )
762
792
{
@@ -883,7 +913,7 @@ do_backup(time_t start_time)
883
913
}
884
914
}
885
915
886
- if (current .from_replica )
916
+ if (current .from_replica && exclusive_backup )
887
917
{
888
918
/* Check master connection options */
889
919
if (master_host == NULL )
@@ -1089,8 +1119,11 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
1089
1119
1090
1120
params [0 ] = label ;
1091
1121
1092
- /* For replica we call pg_start_backup() on master */
1093
- conn = (backup -> from_replica ) ? master_conn : backup_conn ;
1122
+ /* For 9.5 replica we call pg_start_backup() on master */
1123
+ if (backup -> from_replica && exclusive_backup )
1124
+ conn = master_conn ;
1125
+ else
1126
+ conn = backup_conn ;
1094
1127
1095
1128
/* 2nd argument is 'fast'*/
1096
1129
params [1 ] = smooth ? "false" : "true" ;
@@ -1118,16 +1151,21 @@ pg_start_backup(const char *label, bool smooth, pgBackup *backup)
1118
1151
1119
1152
PQclear (res );
1120
1153
1121
- if (current .backup_mode == BACKUP_MODE_DIFF_PAGE )
1154
+ if (current .backup_mode == BACKUP_MODE_DIFF_PAGE &&
1155
+ (!(backup -> from_replica && !exclusive_backup )))
1122
1156
/*
1123
1157
* Switch to a new WAL segment. It is necessary to get archived WAL
1124
1158
* segment, which includes start LSN of current backup.
1159
+ * Don`t do this for replica backups unless it`s PG 9.5
1125
1160
*/
1126
1161
pg_switch_wal (conn );
1127
1162
1163
+ //elog(INFO, "START LSN: %X/%X",
1164
+ // (uint32) (backup->start_lsn >> 32), (uint32) (backup->start_lsn));
1165
+
1128
1166
if (current .backup_mode == BACKUP_MODE_DIFF_PAGE )
1129
1167
/* In PAGE mode wait for current segment... */
1130
- wait_wal_lsn (backup -> start_lsn , true, false);
1168
+ wait_wal_lsn (backup -> start_lsn , true, false);
1131
1169
/*
1132
1170
* Do not wait start_lsn for stream backup.
1133
1171
* Because WAL streaming will start after pg_start_backup() in stream
@@ -1669,7 +1707,7 @@ pg_stop_backup(pgBackup *backup)
1669
1707
PGresult * tablespace_map_content = NULL ;
1670
1708
uint32 lsn_hi ;
1671
1709
uint32 lsn_lo ;
1672
- XLogRecPtr restore_lsn = InvalidXLogRecPtr ;
1710
+ // XLogRecPtr restore_lsn = InvalidXLogRecPtr;
1673
1711
int pg_stop_backup_timeout = 0 ;
1674
1712
char path [MAXPGPATH ];
1675
1713
char backup_label [MAXPGPATH ];
@@ -1689,16 +1727,21 @@ pg_stop_backup(pgBackup *backup)
1689
1727
if (!backup_in_progress )
1690
1728
elog (ERROR , "backup is not in progress" );
1691
1729
1692
- /* For replica we call pg_stop_backup() on master */
1693
- conn = (current .from_replica ) ? master_conn : backup_conn ;
1730
+ /* For 9.5 replica we call pg_stop_backup() on master */
1731
+ if (current .from_replica && exclusive_backup )
1732
+ conn = master_conn ;
1733
+ else
1734
+ conn = backup_conn ;
1694
1735
1695
1736
/* Remove annoying NOTICE messages generated by backend */
1696
1737
res = pgut_execute (conn , "SET client_min_messages = warning;" ,
1697
1738
0 , NULL );
1698
1739
PQclear (res );
1699
1740
1700
- /* Create restore point */
1701
- if (backup != NULL )
1741
+ /* Create restore point
1742
+ * only if it`s backup from master, or exclusive replica(wich connects to master)
1743
+ */
1744
+ if (backup != NULL && (!current .from_replica || (current .from_replica && exclusive_backup )))
1702
1745
{
1703
1746
const char * params [1 ];
1704
1747
char name [1024 ];
@@ -1716,7 +1759,7 @@ pg_stop_backup(pgBackup *backup)
1716
1759
/* Extract timeline and LSN from the result */
1717
1760
XLogDataFromLSN (PQgetvalue (res , 0 , 0 ), & lsn_hi , & lsn_lo );
1718
1761
/* Calculate LSN */
1719
- restore_lsn = ((uint64 ) lsn_hi ) << 32 | lsn_lo ;
1762
+ // restore_lsn = ((uint64) lsn_hi) << 32 | lsn_lo;
1720
1763
PQclear (res );
1721
1764
}
1722
1765
@@ -1830,10 +1873,10 @@ pg_stop_backup(pgBackup *backup)
1830
1873
/* Calculate LSN */
1831
1874
stop_backup_lsn = ((uint64 ) lsn_hi ) << 32 | lsn_lo ;
1832
1875
1833
- if (!XRecOffIsValid (stop_backup_lsn ))
1834
- {
1835
- stop_backup_lsn = restore_lsn ;
1836
- }
1876
+ // if (!XRecOffIsValid(stop_backup_lsn))
1877
+ // {
1878
+ // stop_backup_lsn = restore_lsn;
1879
+ // }
1837
1880
1838
1881
if (!XRecOffIsValid (stop_backup_lsn ))
1839
1882
elog (ERROR , "Invalid stop_backup_lsn value %X/%X" ,
@@ -1939,7 +1982,7 @@ pg_stop_backup(pgBackup *backup)
1939
1982
stream_xlog_path [MAXPGPATH ];
1940
1983
1941
1984
/* Wait for stop_lsn to be received by replica */
1942
- if (backup -> from_replica )
1985
+ if (current . from_replica )
1943
1986
wait_replica_wal_lsn (stop_backup_lsn , false);
1944
1987
/*
1945
1988
* Wait for stop_lsn to be archived or streamed.
@@ -1962,10 +2005,12 @@ pg_stop_backup(pgBackup *backup)
1962
2005
1963
2006
elog (LOG , "Getting the Recovery Time from WAL" );
1964
2007
2008
+ /* iterate over WAL from stop_backup lsn to start_backup lsn */
1965
2009
if (!read_recovery_info (xlog_path , backup -> tli , xlog_seg_size ,
1966
2010
backup -> start_lsn , backup -> stop_lsn ,
1967
2011
& backup -> recovery_time , & backup -> recovery_xid ))
1968
2012
{
2013
+ elog (LOG , "Failed to find Recovery Time in WAL. Forced to trust current_timestamp" );
1969
2014
backup -> recovery_time = recovery_time ;
1970
2015
backup -> recovery_xid = recovery_xid ;
1971
2016
}
@@ -2074,7 +2119,7 @@ backup_files(void *arg)
2074
2119
elog (ERROR , "interrupted during backup" );
2075
2120
2076
2121
if (progress )
2077
- elog (LOG , "Progress: (%d/%d). Process file \"%s\"" ,
2122
+ elog (INFO , "Progress: (%d/%d). Process file \"%s\"" ,
2078
2123
i + 1 , n_backup_files_list , file -> path );
2079
2124
2080
2125
/* stat file to check its current state */
@@ -2168,7 +2213,7 @@ backup_files(void *arg)
2168
2213
file -> path , file -> write_size );
2169
2214
}
2170
2215
else
2171
- elog (LOG , "unexpected file type %d" , buf .st_mode );
2216
+ elog (WARNING , "unexpected file type %d" , buf .st_mode );
2172
2217
}
2173
2218
2174
2219
/* Close connection */
0 commit comments