4747do_restore_or_validate (time_t target_backup_id , pgRecoveryTarget * rt ,
4848 bool is_restore )
4949{
50- int i ;
50+ int i = 0 ;
5151 parray * backups ;
5252 pgBackup * current_backup = NULL ;
5353 pgBackup * dest_backup = NULL ;
@@ -79,9 +79,10 @@ do_restore_or_validate(time_t target_backup_id, pgRecoveryTarget *rt,
7979 backups = catalog_get_backup_list (INVALID_BACKUP_ID );
8080
8181 /* Find backup range we should restore or validate. */
82- for ( i = 0 ; i < parray_num (backups ); i ++ )
82+ while (( i < parray_num (backups )) && ! dest_backup )
8383 {
8484 current_backup = (pgBackup * ) parray_get (backups , i );
85+ i ++ ;
8586
8687 /* Skip all backups which started after target backup */
8788 if (target_backup_id && current_backup -> start_time > target_backup_id )
@@ -93,7 +94,6 @@ do_restore_or_validate(time_t target_backup_id, pgRecoveryTarget *rt,
9394 */
9495
9596 if (is_restore &&
96- !dest_backup &&
9797 target_backup_id == INVALID_BACKUP_ID &&
9898 current_backup -> status != BACKUP_STATUS_OK )
9999 {
@@ -107,8 +107,7 @@ do_restore_or_validate(time_t target_backup_id, pgRecoveryTarget *rt,
107107 * ensure that it satisfies recovery target.
108108 */
109109 if ((target_backup_id == current_backup -> start_time
110- || target_backup_id == INVALID_BACKUP_ID )
111- && !dest_backup )
110+ || target_backup_id == INVALID_BACKUP_ID ))
112111 {
113112
114113 /* backup is not ok,
@@ -161,37 +160,42 @@ do_restore_or_validate(time_t target_backup_id, pgRecoveryTarget *rt,
161160 * Save it as dest_backup
162161 */
163162 dest_backup = current_backup ;
164- dest_backup_index = i ;
163+ dest_backup_index = i - 1 ;
164+ }
165+ }
166+
167+ if (dest_backup == NULL )
168+ elog (ERROR , "Backup satisfying target options is not found." );
169+
170+ /* If we already found dest_backup, look for full backup. */
171+ if (dest_backup )
172+ {
173+ base_full_backup = current_backup ;
174+
175+ if (current_backup -> backup_mode != BACKUP_MODE_FULL )
176+ {
177+ base_full_backup = find_parent_backup (current_backup );
178+
179+ if (base_full_backup == NULL )
180+ elog (ERROR , "Valid full backup for backup %s is not found." ,
181+ base36enc (current_backup -> start_time ));
165182 }
166183
167- /* If we already found dest_backup, look for full backup. */
168- if (dest_backup )
184+ /*
185+ * We have found full backup by link,
186+ * now we need to walk the list to find its index.
187+ *
188+ * TODO I think we should rewrite it someday to use double linked list
189+ * and avoid relying on sort order anymore.
190+ */
191+ for (i = dest_backup_index ; i < parray_num (backups ); i ++ )
169192 {
170- if (current_backup -> backup_mode == BACKUP_MODE_FULL )
193+ pgBackup * temp_backup = (pgBackup * ) parray_get (backups , i );
194+ if (temp_backup -> start_time == base_full_backup -> start_time )
171195 {
172- if (current_backup -> status != BACKUP_STATUS_OK )
173- {
174- /* Full backup revalidation can be done only for DONE and CORRUPT */
175- if (current_backup -> status == BACKUP_STATUS_DONE ||
176- current_backup -> status == BACKUP_STATUS_CORRUPT )
177- elog (WARNING , "base backup %s for given backup %s is in %s status, trying to revalidate" ,
178- base36enc_dup (current_backup -> start_time ),
179- base36enc_dup (dest_backup -> start_time ),
180- status2str (current_backup -> status ));
181- else
182- elog (ERROR , "base backup %s for given backup %s is in %s status" ,
183- base36enc_dup (current_backup -> start_time ),
184- base36enc_dup (dest_backup -> start_time ),
185- status2str (current_backup -> status ));
186- }
187- /* We found both dest and base backups. */
188- base_full_backup = current_backup ;
189196 base_full_backup_index = i ;
190197 break ;
191198 }
192- else
193- /* It`s ok to skip incremental backup */
194- continue ;
195199 }
196200 }
197201
0 commit comments