-
Notifications
You must be signed in to change notification settings - Fork 78
/
restore.c
1580 lines (1346 loc) · 42.2 KB
/
restore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* restore.c: restore DB cluster and archived WAL.
*
* Copyright (c) 2009-2023, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
*
*-------------------------------------------------------------------------
*/
#include "pg_rman.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "catalog/pg_control.h"
#include "common/controldata_utils.h"
#include "common/fe_memutils.h"
#define POSTGRES_CONF "postgresql.conf"
#define POSTGRES_CONF_TMP "postgresql.conf.pg_rman.tmp"
#define PG_RMAN_RECOVERY_CONF "pg_rman_recovery.conf"
#define PG_RMAN_COMMENT "# added by pg_rman"
static void backup_online_files(bool re_recovery);
static void restore_online_files(void);
static void restore_database(pgBackup *backup);
static void restore_archive_logs(pgBackup *backup, bool is_hard_copy);
static void append_include_directive_for_pg_rman(void);
static void include_recovery_configuration(void);
static void configure_recovery_options(const char *target_time,
const char *target_xid,
const char *target_inclusive,
const char *target_action,
TimeLineID target_tli,
bool target_tli_latest);
static void create_recovery_configuration_file(const char *target_time,
const char *target_xid,
const char *target_inclusive,
const char *target_action,
TimeLineID target_tli,
bool target_tli_latest);
static void create_recovery_signal(void);
static void remove_include_directive_for_pg_rman(void);
static void remove_standby_signal(void);
static pgRecoveryTarget *checkIfCreateRecoveryConf(const char *target_time,
const char *target_xid,
const char *target_inclusive,
const char *target_action);
static parray * readTimeLineHistory(TimeLineID targetTLI);
static bool satisfy_timeline(const parray *timelines, const pgBackup *backup);
static bool satisfy_recovery_target(const pgBackup *backup, const pgRecoveryTarget *rt);
static TimeLineID get_fullbackup_timeline(parray *backups, const pgRecoveryTarget *rt);
static TimeLineID parse_target_timeline(const char *target_tli_string, TimeLineID cur_tli,
bool *target_tli_latest);
static TimeLineID findNewestTimeLine(TimeLineID startTLI);
static bool existsTimeLineHistory(TimeLineID probeTLI);
static void print_backup_id(const pgBackup *backup);
static void search_next_wal(const char *path, uint32 *needId, uint32 *needSeg, parray *timelines);
static int wal_segment_size = 0;
int
do_restore(const char *target_time,
const char *target_xid,
const char *target_inclusive,
const char *target_tli_string,
const char *target_action,
bool is_hard_copy)
{
int i;
int base_index; /* index of base (full) backup */
int last_restored_index; /* index of last restored database backup */
int ret;
TimeLineID target_tli;
bool target_tli_latest = false;
TimeLineID cur_tli;
TimeLineID backup_tli;
parray *backups;
pgBackup *base_backup = NULL;
parray *files;
parray *timelines;
char timeline_dir[MAXPGPATH];
char timestamp[20];
uint32 needId = 0;
uint32 needSeg = 0;
pgRecoveryTarget *rt = NULL;
ControlFileData *controlFile;
bool crc_ok;
char ControlFilePath[MAXPGPATH];
/* PGDATA and ARCLOG_PATH are always required */
if (pgdata == NULL)
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("required parameter not specified: PGDATA (-D, --pgdata)")));
if (arclog_path == NULL)
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("required parameter not specified: ARCLOG_PATH (-A, --arclog-path)")));
if (srvlog_path == NULL)
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("required parameter not specified: SRVLOG_PATH (-S, --srvlog-path)")));
/* update pgconf_path if user didn't specify */
if (pgconf_path == NULL)
pgconf_path = pgdata;
if (verbose)
{
printf(_("========================================\n"));
printf(_("restore start\n"));
}
/* get exclusive lock of backup catalog */
ret = catalog_lock();
if (ret == -1)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not lock backup catalog")));
else if (ret == 1)
ereport(ERROR,
(errcode(ERROR_ALREADY_RUNNING),
errmsg("could not lock backup catalog"),
errdetail("Another pg_rman is just running.")));
/* confirm the PostgreSQL server is not running */
if (is_pg_running())
ereport(ERROR,
(errcode(ERROR_PG_RUNNING),
errmsg("PostgreSQL server is running"),
errhint("Please stop PostgreSQL server before executing restore.")));
rt = checkIfCreateRecoveryConf(target_time, target_xid, target_inclusive, target_action);
if(rt == NULL)
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("could not create recovery.conf or "
"add recovery-related options to postgresql.conf(after PG12)"),
errdetail("The specified options are invalid.")));
/* get list of backups. (index == 0) is the last backup */
backups = catalog_get_backup_list(NULL);
if(!backups)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not get list of backup already taken")));
/* get wal_segment_size from pg_control file, it is needed for check option. */
if (check)
{
snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", pgdata);
if (fileExists(ControlFilePath))
{
controlFile = get_controlfile(pgdata, &crc_ok);
if (!crc_ok)
ereport(ERROR,
(errmsg("control file appears to be corrupt"),
errdetail("Calculated CRC checksum does not match value stored in file.")));
wal_segment_size = controlFile->xlog_seg_size;
pg_free(controlFile);
}
else
{
elog(ERROR, _("pg_controldata file \"%s\" does not exist"),
ControlFilePath);
}
}
cur_tli = get_current_timeline();
elog(DEBUG, "the current timeline ID of database cluster is %d", cur_tli);
backup_tli = get_fullbackup_timeline(backups, rt);
elog(DEBUG, "the timeline ID of latest full backup is %d", backup_tli);
/*
* determine target timeline;
* first parse the user specified string value for the target timeline
* passed in via --recovery-target-timeline option. Need this because
* the value 'latest' is also supported.
*/
if(target_tli_string)
{
target_tli = parse_target_timeline(target_tli_string, cur_tli, &target_tli_latest);
elog(INFO, "the specified target timeline ID is %d", target_tli);
}
else
{
elog(INFO, "the recovery target timeline ID is not given");
if (cur_tli != 0)
{
target_tli = cur_tli;
elog(INFO, "use timeline ID of current database cluster as recovery target: %d",
cur_tli);
}
else
{
backup_tli = get_fullbackup_timeline(backups, rt);
target_tli = backup_tli;
elog(INFO, "use timeline ID of latest full backup as recovery target: %d",
backup_tli);
}
}
/*
* restore timeline history files and get timeline branches can reach
* recovery target point.
*/
elog(INFO, "calculating timeline branches to be used to recovery target point");
join_path_components(timeline_dir, backup_path, TIMELINE_HISTORY_DIR);
dir_copy_files(timeline_dir, arclog_path);
timelines = readTimeLineHistory(target_tli);
/* find last full backup which can be used as base backup. */
elog(INFO, "searching latest full backup which can be used as restore start point");
for (i = 0; i < parray_num(backups); i++)
{
base_backup = (pgBackup *) parray_get(backups, i);
if (base_backup->backup_mode < BACKUP_MODE_FULL ||
base_backup->status != BACKUP_STATUS_OK)
continue;
#ifndef HAVE_LIBZ
/* Make sure we won't need decompression we haven't got */
if (base_backup->compress_data &&
(HAVE_DATABASE(base_backup) || HAVE_ARCLOG(base_backup)))
{
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not restore from compressed backup"),
errdetail("Compression is not supported in this installation.")));
}
#endif
if (satisfy_timeline(timelines, base_backup) && satisfy_recovery_target(base_backup, rt))
{
time2iso(timestamp, lengthof(timestamp), base_backup->start_time);
elog(INFO, "found the full backup can be used as base in recovery: \"%s\"",
timestamp);
goto base_backup_found;
}
}
/* no full backup found, can't restore */
ereport(ERROR,
(errcode(ERROR_NO_BACKUP),
errmsg("cannot do restore"),
errdetail("There is no valid full backup which can be used for given recovery condition.")));
base_backup_found:
/* first off, backup online WAL and serverlog */
backup_online_files(cur_tli != 0 && cur_tli != backup_tli);
/*
* Clear restore destination, but don't remove $PGDATA.
* To remove symbolic link, get file list with "omit_symlink = false".
*
* Doing it *after* a good base backup is found so that we don't end up
* in a situation where the target data directory is already deleted
* but we could not find a valid base backup based on user specified
* restore options (perhaps a mistake on user's part but we should be
* cautious.)
*/
if (!check)
{
int x;
if (verbose)
printf(_("----------------------------------------\n"));
elog(INFO, "clearing restore destination");
files = parray_new();
dir_list_file(files, pgdata, NULL, false, false);
parray_qsort(files, pgFileComparePathDesc); /* delete from leaf */
for (x = 0; x < parray_num(files); x++)
{
pgFile *file = (pgFile *) parray_get(files, i);
pgFileDelete(file);
}
parray_walk(files, pgFileFree);
parray_free(files);
}
/* OK, now proceed to restoring the backup */
base_index = i;
if (verbose)
print_backup_id(base_backup);
/* restore base backup */
restore_database(base_backup);
last_restored_index = base_index;
/* restore following incremental backup */
if (verbose)
printf(_("----------------------------------------\n"));
elog(INFO, "searching incremental backup to be restored");
for (i = base_index - 1; i >= 0; i--)
{
pgBackup *backup = (pgBackup *) parray_get(backups, i);
/* don't use incomplete nor different timeline backup */
if (backup->status != BACKUP_STATUS_OK ||
backup->tli != base_backup->tli)
continue;
/* use database backup only */
if (backup->backup_mode != BACKUP_MODE_INCREMENTAL)
continue;
/* is the backup is necessary for restore to target timeline ? */
if (!satisfy_timeline(timelines, backup) || !satisfy_recovery_target(backup, rt))
continue;
if (verbose)
print_backup_id(backup);
time2iso(timestamp, lengthof(timestamp), backup->start_time);
elog(DEBUG, "found the incremental backup can be used in recovery: \"%s\"",
timestamp);
restore_database(backup);
last_restored_index = i;
}
/*
* Restore archived WAL which backed up with or after last restored backup.
* We don't check the backup->tli because a backup of archived WAL
* can contain WALs which were archived in multiple timeline.
*/
if (check)
{
pgBackup *backup = (pgBackup *) parray_get(backups, last_restored_index);
needId = (uint32) (backup->start_lsn >> 32);
needSeg = (uint32) backup->start_lsn / wal_segment_size;
}
if (verbose)
printf(_("----------------------------------------\n"));
elog(INFO, "searching backup which contained archived WAL files to be restored");
for (i = last_restored_index; i >= 0; i--)
{
pgBackup *backup = (pgBackup *) parray_get(backups, i);
/* don't use incomplete backup */
if (backup->status != BACKUP_STATUS_OK)
continue;
if (!HAVE_ARCLOG(backup))
continue;
/* care timeline junction */
if (!satisfy_timeline(timelines, backup))
continue;
restore_archive_logs(backup, is_hard_copy);
if (check)
{
char xlogpath[MAXPGPATH];
pgBackupGetPath(backup, xlogpath, lengthof(xlogpath), ARCLOG_DIR);
search_next_wal(xlogpath, &needId, &needSeg, timelines);
}
}
/* copy online WAL backup to $PGDATA/pg_wal */
restore_online_files();
if (check)
{
char xlogpath[MAXPGPATH];
if (verbose)
printf(_("searching archived WAL\n"));
search_next_wal(arclog_path, &needId, &needSeg, timelines);
if (verbose)
printf(_("searching online WAL\n"));
join_path_components(xlogpath, pgdata, PG_XLOG_DIR);
search_next_wal(xlogpath, &needId, &needSeg, timelines);
if (verbose)
printf(_("all necessary files are found.\n"));
}
/* configure recovery-related options */
configure_recovery_options(target_time, target_xid, target_inclusive,
target_action, target_tli, target_tli_latest);
/* release catalog lock */
catalog_unlock();
/* cleanup */
parray_walk(backups, pgBackupFree);
parray_free(backups);
/* print restore complete message */
if (verbose && !check)
{
printf(_("all restore completed\n"));
printf(_("========================================\n"));
}
if (!check)
ereport(INFO,
(errmsg("restore complete"),
errhint("Recovery will start automatically when the PostgreSQL server is started. After the recovery is done, we recommend to remove recovery-related parameters configured by pg_rman.")));
return 0;
}
/*
* Validate and restore backup.
*/
void
restore_database(pgBackup *backup)
{
char timestamp[100];
char path[MAXPGPATH];
char list_path[MAXPGPATH];
int ret;
parray *files;
int i;
int num_skipped = 0;
/* confirm block size compatibility */
if (backup->block_size != BLCKSZ)
ereport(ERROR,
(errcode(ERROR_PG_INCOMPATIBLE),
errmsg("BLCKSZ(%d) is not compatible (%d expected)",
backup->block_size, BLCKSZ)));
if (backup->wal_block_size != XLOG_BLCKSZ)
ereport(ERROR,
(errcode(ERROR_PG_INCOMPATIBLE),
errmsg("XLOG_BLCKSZ(%d) is not compatible (%d expected)",
backup->wal_block_size, XLOG_BLCKSZ)));
time2iso(timestamp, lengthof(timestamp), backup->start_time);
if (verbose && !check)
{
printf(_("----------------------------------------\n"));
}
/*
* Validate backup files with its size, because load of CRC calculation is
* not light.
*/
pgBackupValidate(backup, true, false, true);
if (backup->backup_mode == BACKUP_MODE_FULL)
elog(INFO, "restoring database files from the full mode backup \"%s\"",
timestamp);
else if (backup->backup_mode == BACKUP_MODE_INCREMENTAL)
elog(INFO, "restoring database files from the incremental mode backup \"%s\"",
timestamp);
/* make directories and symbolic links */
pgBackupGetPath(backup, path, lengthof(path), MKDIRS_SH_FILE);
if (!check)
{
char pwd[MAXPGPATH];
/* keep original directory */
if (getcwd(pwd, sizeof(pwd)) == NULL)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not get current working directory: %s", strerror(errno))));
/* create pgdata directory */
dir_create_dir(pgdata, DIR_PERMISSION);
/* change directory to pgdata */
if (chdir(pgdata))
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not change directory: %s", strerror(errno))));
/* Execute mkdirs.sh */
ret = system(path);
if (ret != 0)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not execute mkdirs.sh: %s", strerror(errno))));
/* go back to original directory */
if (chdir(pwd))
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not change directory: %s", strerror(errno))));
}
/*
* get list of files which need to be restored.
*/
pgBackupGetPath(backup, path, lengthof(path), DATABASE_DIR);
pgBackupGetPath(backup, list_path, lengthof(list_path), DATABASE_FILE_LIST);
files = dir_read_file_list(path, list_path);
for (i = parray_num(files) - 1; i >= 0; i--)
{
pgFile *file = (pgFile *) parray_get(files, i);
/* remove files which are not backed up */
if (file->write_size == BYTES_INVALID)
pgFileFree(parray_remove(files, i));
}
/* restore files into $PGDATA */
for (i = 0; i < parray_num(files); i++)
{
char from_root[MAXPGPATH];
pgFile *file = (pgFile *) parray_get(files, i);
pgBackupGetPath(backup, from_root, lengthof(from_root), DATABASE_DIR);
/* check for interrupt */
if (interrupted)
ereport(FATAL,
(errcode(ERROR_INTERRUPTED),
errmsg("interrupted during restore database")));
/* print progress in verbose mode */
if (verbose && !check)
printf(_("(%d/%lu) %s "), i + 1, (unsigned long) parray_num(files),
file->path + strlen(from_root) + 1);
/* directories are created with mkdirs.sh */
if (S_ISDIR(file->mode))
{
num_skipped++;
if (verbose && !check)
printf(_("directory, skip\n"));
goto show_progress;
}
/* not backed up */
if (file->write_size == BYTES_INVALID)
{
num_skipped++;
if (verbose && !check)
printf(_("not backed up, skip\n"));
goto show_progress;
}
/* restore file */
if (!check)
restore_data_file(from_root, pgdata, file, backup->compress_data);
/* print size of restored file */
if (verbose && !check)
{
printf(_("restored %lu\n"), (unsigned long) file->write_size);
continue;
}
show_progress:
/* print progress in non-verbose format */
if (progress)
{
fprintf(stderr, _("Processed %d of %lu files, skipped %d"),
i + 1, (unsigned long) parray_num(files), num_skipped);
if(i + 1 < (unsigned long) parray_num(files))
fprintf(stderr, "\r");
else
fprintf(stderr, "\n");
}
}
/* Delete files which are not in file list. */
if (!check)
{
parray *files_now;
parray_walk(files, pgFileFree);
parray_free(files);
/* re-read file list to change base path to $PGDATA */
files = dir_read_file_list(pgdata, list_path);
parray_qsort(files, pgFileComparePathDesc);
/* get list of files restored to pgdata */
files_now = parray_new();
dir_list_file(files_now, pgdata, pgdata_exclude, true, false);
/* to delete from leaf, sort in reversed order */
parray_qsort(files_now, pgFileComparePathDesc);
for (i = 0; i < parray_num(files_now); i++)
{
pgFile *file = (pgFile *) parray_get(files_now, i);
/* If the file is not in the file list, delete it */
if (parray_bsearch(files, file, pgFileComparePathDesc) == NULL)
{
if (verbose)
printf(_(" delete %s\n"), file->path + strlen(pgdata) + 1);
pgFileDelete(file);
}
}
parray_walk(files_now, pgFileFree);
parray_free(files_now);
}
/* remove postmaster.pid */
snprintf(path, lengthof(path), "%s/postmaster.pid", pgdata);
if (remove(path) == -1 && errno != ENOENT)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not remove postmaster.pid: %s", strerror(errno))));
/* cleanup */
parray_walk(files, pgFileFree);
parray_free(files);
if (verbose && !check)
printf(_("restore backup completed\n"));
}
/*
* Restore archived WAL by creating symbolic link which linked to backup WAL in
* archive directory.
*/
void
restore_archive_logs(pgBackup *backup, bool is_hard_copy)
{
int i;
int num_skipped = 0;
char timestamp[100];
parray *files;
char path[MAXPGPATH];
char list_path[MAXPGPATH];
char base_path[MAXPGPATH];
time2iso(timestamp, lengthof(timestamp), backup->start_time);
if (verbose && !check)
{
printf(_("----------------------------------------\n"));
}
/*
* Validate backup files with its size, because load of CRC calculation is
* not light.
*/
pgBackupValidate(backup, true, false, false);
elog(INFO,_("restoring WAL files from backup \"%s\""), timestamp);
pgBackupGetPath(backup, list_path, lengthof(list_path), ARCLOG_FILE_LIST);
pgBackupGetPath(backup, base_path, lengthof(list_path), ARCLOG_DIR);
files = dir_read_file_list(base_path, list_path);
for (i = 0; i < parray_num(files); i++)
{
pgFile *file = (pgFile *) parray_get(files, i);
/* check for interrupt */
if (interrupted)
ereport(FATAL,
(errcode(ERROR_INTERRUPTED),
errmsg("interrupted during restore WAL")));
/* print progress */
join_path_components(path, arclog_path, file->path + strlen(base_path) + 1);
if (verbose && !check)
printf(_("(%d/%lu) %s "), i + 1, (unsigned long) parray_num(files),
file->path + strlen(base_path) + 1);
/* skip files which are not in backup */
if (file->write_size == BYTES_INVALID)
{
if (verbose && !check)
printf(_("skip(not backed up)\n"));
goto show_progress;
}
/*
* skip timeline history files because timeline history files will be
* restored from $BACKUP_PATH/timeline_history.
*/
if (strstr(file->path, ".history") ==
file->path + strlen(file->path) - strlen(".history"))
{
if (verbose && !check)
printf(_("skip(timeline history)\n"));
goto show_progress;
}
if (!check)
{
if (backup->compress_data)
{
copy_file(base_path, arclog_path, file, DECOMPRESSION);
if (verbose)
printf(_("decompressed\n"));
goto show_progress;
}
/* even same file exist, use backup file */
if ((remove(path) == -1) && errno != ENOENT)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not remove file \"%s\": %s", path, strerror(errno))));
if (!is_hard_copy)
{
/* create symlink */
if ((symlink(file->path, path) == -1))
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not create link to \"%s\": %s",
file->path, strerror(errno))));
if (verbose)
printf(_("linked\n"));
}
else
{
/* create hard-copy */
if (!copy_file(base_path, arclog_path, file, NO_COMPRESSION))
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not copy to \"%s\": %s",
file->path, strerror(errno))));
if (verbose)
printf(_("copied\n"));
}
show_progress:
/* print progress in non-verbose format */
if (progress)
{
fprintf(stderr, _("Processed %d of %lu files, skipped %d"),
i + 1, (unsigned long) parray_num(files), num_skipped);
if(i + 1 < (unsigned long) parray_num(files))
fprintf(stderr, "\r");
else
fprintf(stderr, "\n");
}
}
}
parray_walk(files, pgFileFree);
parray_free(files);
}
static void
configure_recovery_options(const char *target_time,
const char *target_xid,
const char *target_inclusive,
const char *target_action,
TimeLineID target_tli,
bool target_tli_latest)
{
/*
* Check if postgresql.conf exists in the restored data directory
* because a user manages postgresql's configuration files in a
* directory different from the data directory using the GUC
* `data_directory` parameter. If so, recovery-related parameters
* will not work so that user must manage them manually.
*/
char path[MAXPGPATH];
snprintf(path, lengthof(path), "%s/%s", pgconf_path, POSTGRES_CONF);
if (!fileExists(path))
{
elog(WARNING,
"recovery-related configuration is skipped because postgresql.conf doesn't exist in %s",
pgconf_path);
return;
}
/*
* Configure recovery-related parameters.
*
* 1. Create the file for recovery-related parameters
*
* 2. Append an 'include' directive to include the file.
* If the 'include' directive configured by pg_rman exists,
* remove it first. The reason why to use an 'include' directive is to
* make it easy for users to distinguish it.
*
* Note: It keeps the user's configuration. There are two reasons.
* The first is to avoid making a user puzzled. The second is that
* there is no problem because pg_rman appends the 'include' directive
* at the last of postgresql.conf every time so that the pg_rman's
* configurations work as valid values.
*/
create_recovery_configuration_file(target_time, target_xid, target_inclusive,
target_action, target_tli, target_tli_latest);
include_recovery_configuration();
/* Create recovery.signal file */
create_recovery_signal();
/*
* Remove if standby.signal file exists because pg_rman doesn’t treat
* the backup as restoring on standby automatically now.
*/
remove_standby_signal();
}
static void
remove_include_directive_for_pg_rman()
{
char path[MAXPGPATH];
char tmppath[MAXPGPATH];
char fline[MAXPGPATH];
FILE *r_fd, *w_fd;
if (verbose && !check)
{
printf(_("----------------------------------------\n"));
}
snprintf(path, lengthof(path), "%s/%s", pgconf_path, POSTGRES_CONF);
snprintf(tmppath, lengthof(path), "%s/%s", pgconf_path, POSTGRES_CONF_TMP);
elog(INFO, "remove an 'include' directive added by pg_rman in %s if exists", POSTGRES_CONF);
if (!check)
{
elog(DEBUG, "make temporary file \"%s\"", tmppath);
if ((r_fd = fopen(path, "rt")) == NULL)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not open file \"%s\": %s", path, strerror(errno))));
if ((w_fd = fopen(tmppath, "w")) == NULL)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not open file \"%s\": %s", tmppath, strerror(errno))));
while (r_fd && fgets(fline, sizeof(fline), r_fd) != NULL)
{
elog(DEBUG, "%s", fline);
/* skip the lines which pg_rman configured */
if (strstr(fline, "include") && strstr(fline, PG_RMAN_RECOVERY_CONF))
continue;
fprintf(w_fd, "%s", fline);
}
fclose(r_fd);
fclose(w_fd);
elog(DEBUG, "overwrite file \"%s\" with \"%s\"", path, tmppath);
if (rename(tmppath, path) != 0)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not overwrite file \"%s\" with \"%s\": %s",
path, tmppath, strerror(errno))));
}
}
static void
create_recovery_configuration_file(const char *target_time,
const char *target_xid,
const char *target_inclusive,
const char *target_action,
TimeLineID target_tli,
bool target_tli_latest)
{
char path[MAXPGPATH];
FILE *fp;
if (verbose && !check)
{
printf(_("----------------------------------------\n"));
}
snprintf(path, lengthof(path), "%s/%s", pgconf_path, PG_RMAN_RECOVERY_CONF);
elog(INFO, "create %s for recovery-related parameters.", PG_RMAN_RECOVERY_CONF);
if (!check)
{
/* overwrite if exists */
if ((fp = fopen(path, "w")) == NULL)
ereport(ERROR,
(ERROR_SYSTEM,
errmsg("could not create file \"%s\": %s", path, strerror(errno))));
fprintf(fp, "%s %s\n", PG_RMAN_COMMENT, PROGRAM_VERSION);
fprintf(fp, "restore_command = 'cp %s/%%f %%p'\n", arclog_path);
if (target_time)
fprintf(fp, "recovery_target_time = '%s'\n", target_time);
if (target_xid)
fprintf(fp, "recovery_target_xid = '%s'\n", target_xid);
if (target_inclusive)
fprintf(fp, "recovery_target_inclusive = '%s'\n", target_inclusive);
if(target_tli_latest)
fprintf(fp, "recovery_target_timeline = 'latest'\n");
else
fprintf(fp, "recovery_target_timeline = '%u'\n", target_tli);
if (target_action)
fprintf(fp, "recovery_target_action = '%s'\n", target_action);
fclose(fp);
}
}
static void
append_include_directive_for_pg_rman()
{
char path[MAXPGPATH];
FILE *fp;
if (verbose && !check)
{
printf(_("----------------------------------------\n"));
}
snprintf(path, lengthof(path), "%s/%s", pgconf_path, POSTGRES_CONF);
elog(INFO, "append an 'include' directive in %s for %s", POSTGRES_CONF, PG_RMAN_RECOVERY_CONF);
if (!check)
{
fp = fopen(path, "a");
if (fp == NULL)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not open \"%s\": %s", path, strerror(errno))));
fprintf(fp, "include = '%s' %s %s\n", PG_RMAN_RECOVERY_CONF, PG_RMAN_COMMENT, PROGRAM_VERSION);
fclose(fp);
}
}
static void
include_recovery_configuration(void)
{
remove_include_directive_for_pg_rman();
append_include_directive_for_pg_rman();
}
static void
create_recovery_signal(void)
{
char path[MAXPGPATH];
FILE *fp;
if (verbose && !check)
printf(_("----------------------------------------\n"));
elog(INFO, _("generating recovery.signal"));
if (!check)
{
snprintf(path, lengthof(path), "%s/recovery.signal", pgdata);
fp = fopen(path, "wt");
fprintf(fp, "# recovery.signal generated by pg_rman %s\n",
PROGRAM_VERSION);
fclose(fp);
}
}
static void
remove_standby_signal(void)
{
char path[MAXPGPATH];
if (verbose && !check)
printf(_("----------------------------------------\n"));
elog(INFO, _("removing standby.signal if exists to restore as primary"));
if (!check)
{
if (get_standby_signal_filepath(path, sizeof(path)))
{
if (remove(path))
{
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not remove \"%s\": %s", path,
strerror(errno))));
}
ereport(INFO,
(errmsg("removed standby.signal"),
errhint("if you want to start as standby, additional manual "
"setups to make standby.signal and so on are required")));
}
}
}
static void
backup_online_files(bool re_recovery)
{