@@ -171,10 +171,13 @@ catchup_preflight_checks(PGNodeInfo *source_node_info, PGconn *source_conn,
171171
172172if (current .backup_mode != BACKUP_MODE_FULL )
173173{
174- dest_id = get_system_identifier (dest_pgdata ,FIO_LOCAL_HOST , false);
174+ ControlFileData dst_control ;
175+ get_control_file_or_back_file (dest_pgdata ,FIO_LOCAL_HOST ,& dst_control );
176+ dest_id = dst_control .system_identifier ;
177+
175178if (source_conn_id != dest_id )
176- elog (ERROR ,"Database identifiers mismatch: we connected to DB id %lu , but in \"%s\" we found id %lu " ,
177- source_conn_id ,dest_pgdata ,dest_id );
179+ elog (ERROR ,"Database identifiers mismatch: we connected to DB id %llu , but in \"%s\" we found id %llu " ,
180+ ( long long ) source_conn_id ,dest_pgdata ,( long long ) dest_id );
178181}
179182}
180183
@@ -640,6 +643,9 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads,
640643ssize_t transfered_walfiles_bytes = 0 ;
641644char pretty_source_bytes [20 ];
642645
646+ char dest_pg_control_fullpath [MAXPGPATH ];
647+ char dest_pg_control_bak_fullpath [MAXPGPATH ];
648+
643649source_conn = catchup_init_state (& source_node_info ,source_pgdata ,dest_pgdata );
644650catchup_preflight_checks (& source_node_info ,source_conn ,source_pgdata ,dest_pgdata );
645651
@@ -935,6 +941,9 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads,
935941Assert (file -> external_dir_num == 0 );
936942if (pg_strcasecmp (file -> name ,RELMAPPER_FILENAME )== 0 )
937943redundant = true;
944+ /* global/pg_control.pbk.bak is always keeped, because it's needed for restart failed incremental restore */
945+ if (pg_strcasecmp (file -> rel_path ,XLOG_CONTROL_BAK_FILE )== 0 )
946+ redundant = false;
938947
939948/* if file does not exists in destination list, then we can safely unlink it */
940949if (redundant )
@@ -966,6 +975,28 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads,
966975if (dest_filelist )
967976parray_qsort (dest_filelist ,pgFileCompareRelPathWithExternal );
968977
978+ join_path_components (dest_pg_control_fullpath ,dest_pgdata ,XLOG_CONTROL_FILE );
979+ join_path_components (dest_pg_control_bak_fullpath ,dest_pgdata ,XLOG_CONTROL_BAK_FILE );
980+ /*
981+ * rename (if it exist) dest control file before restoring
982+ * if it doesn't exist, that mean, that we already restoring in a previously failed
983+ * pgdata, where XLOG_CONTROL_BAK_FILE exist
984+ */
985+ if (current .backup_mode != BACKUP_MODE_FULL && !dry_run )
986+ {
987+ if (!fio_access (dest_pg_control_fullpath ,F_OK ,FIO_LOCAL_HOST ))
988+ {
989+ pgFile * dst_control ;
990+ dst_control = pgFileNew (dest_pg_control_bak_fullpath ,XLOG_CONTROL_BAK_FILE ,
991+ true,0 ,FIO_BACKUP_HOST );
992+
993+ if (!fio_access (dest_pg_control_bak_fullpath ,F_OK ,FIO_LOCAL_HOST ))
994+ fio_delete (dst_control -> mode ,dest_pg_control_bak_fullpath ,FIO_LOCAL_HOST );
995+ fio_rename (dest_pg_control_fullpath ,dest_pg_control_bak_fullpath ,FIO_LOCAL_HOST );
996+ pgFileFree (dst_control );
997+ }
998+ }
999+
9691000/* run copy threads */
9701001elog (INFO ,"Start transferring data files" );
9711002time (& start_time );
@@ -985,6 +1016,15 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads,
9851016copy_pgcontrol_file (from_fullpath ,FIO_DB_HOST ,
9861017to_fullpath ,FIO_LOCAL_HOST ,source_pg_control_file );
9871018transfered_datafiles_bytes += source_pg_control_file -> size ;
1019+
1020+ /* Now backup control file can be deled */
1021+ if (current .backup_mode != BACKUP_MODE_FULL && !fio_access (dest_pg_control_bak_fullpath ,F_OK ,FIO_LOCAL_HOST )){
1022+ pgFile * dst_control ;
1023+ dst_control = pgFileNew (dest_pg_control_bak_fullpath ,XLOG_CONTROL_BAK_FILE ,
1024+ true,0 ,FIO_BACKUP_HOST );
1025+ fio_delete (dst_control -> mode ,dest_pg_control_bak_fullpath ,FIO_LOCAL_HOST );
1026+ pgFileFree (dst_control );
1027+ }
9881028}
9891029
9901030if (!catchup_isok && !dry_run )