-
Notifications
You must be signed in to change notification settings - Fork 78
/
backup.c
2144 lines (1860 loc) · 58.6 KB
/
backup.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
/*-------------------------------------------------------------------------
*
* backup.c: backup DB cluster, archived WAL, serverlog.
*
* Copyright (c) 2009-2023, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
*
*-------------------------------------------------------------------------
*/
#include "pg_rman.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <dirent.h>
#include <time.h>
#include <math.h>
#include "catalog/pg_control.h"
#include "common/controldata_utils.h"
#include "pgut/pgut-port.h"
#define TIMEOUT_ARCHIVE 10 /* wait 10 sec until WAL archive complete */
static bool in_backup = false; /* TODO: more robust logic */
static parray *cleanup_list; /* list of command to execute at error processing for snapshot */
/*
* data_checksum_enabled as read from the control file of the database
* cluster. Exposed for use in data.c.
*/
bool data_checksum_enabled = false;
static void init_data_checksum_enabled(void);
/*
* Backup routines
*/
static void backup_cleanup(bool fatal, void *userdata);
static void delete_old_files(const char *root, parray *files, int keep_files,
int keep_days, bool is_arclog);
static void backup_files(const char *from_root, const char *to_root,
parray *files, parray *prev_files, const XLogRecPtr *lsn, bool compress, const char *prefix);
static parray *do_backup_database(parray *backup_list, pgBackupOption bkupopt);
static parray *do_backup_arclog(parray *backup_list);
static parray *do_backup_srvlog(parray *backup_list);
static void confirm_block_size(const char *name, int blcksz);
static void pg_backup_start(const char *label, bool smooth, pgBackup *backup);
static parray *pg_backup_stop(pgBackup *backup);
static void pg_switch_wal(pgBackup *backup);
static void get_lsn(PGresult *res, TimeLineID *timeline, XLogRecPtr *lsn);
static void get_xid(PGresult *res, uint32 *xid);
static bool execute_restartpoint(pgBackupOption bkupopt, pgBackup *backup);
static void delete_arclog_link(void);
static void delete_online_wal_backup(void);
static bool dirExists(const char *path);
static void execute_freeze(void);
static void execute_unfreeze(void);
static void execute_split(parray *tblspc_list);
static void execute_resync(void);
static void execute_mount(parray *tblspcmp_list);
static void execute_umount(void);
static void execute_script(const char *mode, bool is_cleanup, parray *output);
static void snapshot_cleanup(bool fatal, void *userdata);
static void add_files(parray *files, const char *root, bool add_root, bool is_pgdata);
static int strCompare(const void *str1, const void *str2);
static void create_file_list(parray *files, const char *root, const char *prefix, bool is_append);
static void check_server_version(void);
static int wal_segment_size = 0;
/*
* Take a backup of database.
*/
static parray *
do_backup_database(parray *backup_list, pgBackupOption bkupopt)
{
int i;
parray *files; /* backup file list from non-snapshot */
parray *prev_files = NULL; /* file list of previous database backup */
FILE *fp;
char path[MAXPGPATH];
char label[1024];
XLogRecPtr *lsn = NULL;
char prev_file_txt[MAXPGPATH]; /* path of the previous backup list file */
/* repack the options */
bool smooth_checkpoint = bkupopt.smooth_checkpoint;
check_server_version();
init_data_checksum_enabled();
if (!HAVE_DATABASE(¤t))
{
/* check if arclog backup. if arclog backup and no suitable full backup, */
if (HAVE_ARCLOG(¤t))
{
pgBackup *prev_backup;
/* find last completed database backup */
prev_backup = catalog_get_last_data_backup(backup_list);
if (prev_backup == NULL)
{
if (current.full_backup_on_error)
{
ereport(NOTICE,
(errmsg("turn to take a full backup"),
errdetail("There is no validated full backup with current timeline.")));
current.backup_mode = BACKUP_MODE_FULL;
}
else
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("cannot take an incremental backup"),
errdetail("There is no validated full backup with current timeline."),
errhint("Please take a full backup and validate it before doing an archive backup. "
"Or use with --full-backup-on-error command line option.")));
}
else
return NULL;
}
}
elog(INFO, _("copying database files"));
/* initialize size summary */
current.total_data_bytes = 0;
current.read_data_bytes = 0;
/* notify start of backup to PostgreSQL server */
time2iso(label, lengthof(label), current.start_time);
strncat(label, " with pg_rman", lengthof(label) - strlen(label) - 1);
pg_backup_start(label, smooth_checkpoint, ¤t);
/* Execute restartpoint on standby once replay reaches the backup LSN */
if (current.is_from_standby && !execute_restartpoint(bkupopt, ¤t))
{
/*
* Disconnecting automatically aborts a non-exclusive backup, so no
* need to call pg_backup_stop() do it for us.
*/
disconnect();
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not execute restartpoint")));
}
/*
* Generate mkdirs.sh required to recreate the directory structure of
* PGDATA when restoring. Omits $PGDATA from being listed in the
* commands. Note that the resulting mkdirs.sh file is part of the
* backup.
*/
files = parray_new();
dir_list_file(files, pgdata, NULL, false, false);
if (!check)
{
pgBackupGetPath(¤t, path, lengthof(path), MKDIRS_SH_FILE);
fp = fopen(path, "wt");
if (fp == NULL)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not open make directory script \"%s\": %s",
path, strerror(errno))));
dir_print_mkdirs_sh(fp, files, pgdata);
fclose(fp);
if (chmod(path, DIR_PERMISSION) == -1)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not change mode of \"%s\": %s", path,
strerror(errno))));
}
/* Free no longer needed memory. */
parray_walk(files, pgFileFree);
parray_free(files);
files = NULL;
/*
* To take incremental backup, the file list of the latest validated
* full database backup is needed.
* TODO: fix for issue #154
* When a backup list is deleted with rm command or pg_rman's delete command with '--force' option,
* pg_rman can't detect there is a missing piece of backup.
* We need the way tracing the backup chains or something else...
*/
if (current.backup_mode < BACKUP_MODE_FULL)
{
pgBackup *prev_backup;
uint32 xlogid, xrecoff;
/* find last completed database backup */
prev_backup = catalog_get_last_data_backup(backup_list);
if (prev_backup == NULL || prev_backup->tli != current.tli)
{
if (current.full_backup_on_error)
{
ereport(NOTICE,
(errmsg("turn to take a full backup"),
errdetail("There is no validated full backup with current timeline.")));
current.backup_mode = BACKUP_MODE_FULL;
}
else
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("cannot take an incremental backup"),
errdetail("There is no validated full backup with current timeline."),
errhint("Please take a full backup and validate it before doing an incremental backup. "
"Or use with --full-backup-on-error command line option.")));
}
else
{
pgBackupGetPath(prev_backup, prev_file_txt, lengthof(prev_file_txt),
DATABASE_FILE_LIST);
prev_files = dir_read_file_list(pgdata, prev_file_txt);
/*
* Do backup only pages having larger LSN than previous backup.
*/
lsn = &prev_backup->start_lsn;
xlogid = (uint32) (*lsn >> 32);
xrecoff = (uint32) *lsn;
elog(DEBUG, _("backup only the page updated after LSN(%X/%08X)"),
xlogid, xrecoff);
}
}
/* initialize backup list from non-snapshot */
files = parray_new();
/*
* Check the existence of the snapshot-script and perform backup using the
* snapshot_script if one is provided.
*/
join_path_components(path, backup_path, SNAPSHOT_SCRIPT_FILE);
if (!fileExists(path))
{
/* Nope. Perform a simple backup by copying files. */
parray *stop_backup_files;
/*
* List files contained in PGDATA, omitting all sub-directories beside
* base, global and pg_tblspc. Omits $PGDATA from the paths.
*/
add_files(files, pgdata, false, true);
if (current.backup_mode == BACKUP_MODE_FULL)
elog(DEBUG, "taking full backup of database files");
else if (current.backup_mode == BACKUP_MODE_INCREMENTAL)
elog(DEBUG, "taking incremental backup of database files");
/* Construct the directory for this backup within BACKUP_PATH. */
pgBackupGetPath(¤t, path, lengthof(path), DATABASE_DIR);
/* Save the files listed above. */
backup_files(pgdata, path, files, prev_files, lsn, current.compress_data, NULL);
/*
* Notify end of backup and save the backup_label and tablespace_map
* files.
*/
stop_backup_files = pg_backup_stop(¤t);
/* stop_backup_files must be listed in file_database.txt. */
files = parray_concat(stop_backup_files, files);
/*
* Construct file_database.txt listing all files we just saved under
* DATABASE_DIR.
*/
create_file_list(files, pgdata, NULL, false);
}
else
{
/* Use snapshot_script. */
parray *tblspc_list; /* list of name of TABLESPACE backup from snapshot */
parray *tblspcmp_list; /* list of mounted directory of TABLESPACE in snapshot volume */
PGresult *tblspc_res; /* contain spcname and oid in TABLESPACE */
parray *stop_backup_files; /* list of files that pg_backup_stop() wrote */
/* if backup is from standby, snapshot backup is unsupported */
if (current.is_from_standby)
{
/*
* Disconnecting automatically aborts a non-exclusive backup, so no
* need to call pg_backup_stop() do it for us.
*/
disconnect();
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("cannot take a backup"),
errdetail("Taking backup from standby server with snapshot-script is not supported")));
}
tblspc_list = parray_new();
tblspcmp_list = parray_new();
cleanup_list = parray_new();
/*
* append 'pg_tblspc' to list of directory excluded from copy.
* because DB cluster and TABLESPACE are copied separately.
*/
for (i = 0; pgdata_exclude[i]; i++); /* find first empty slot */
pgdata_exclude[i] = PG_TBLSPC_DIR;
/* set the error processing for the snapshot */
pgut_atexit_push(snapshot_cleanup, cleanup_list);
/* create snapshot volume */
if (!check)
{
/* freeze I/O of the file-system */
execute_freeze();
/* create the snapshot, and obtain the name of TABLESPACE backup from snapshot */
execute_split(tblspc_list);
/* unfreeze I/O of the file-system */
execute_unfreeze();
}
/*
* when DB cluster is not contained in the backup from the snapshot,
* DB cluster is added to the backup file list from non-snapshot.
*/
parray_qsort(tblspc_list, strCompare);
if (parray_bsearch(tblspc_list, "PG-DATA", strCompare) == NULL)
add_files(files, pgdata, false, true);
else
/* remove the detected tablespace("PG-DATA") from tblspc_list */
parray_rm(tblspc_list, "PG-DATA", strCompare);
/*
* select the TABLESPACE backup from non-snapshot,
* and append TABLESPACE to the list backup from non-snapshot.
* TABLESPACE name and oid is obtained by inquiring of the database.
*/
Assert(connection != NULL);
tblspc_res = execute("SELECT spcname, oid FROM pg_tablespace WHERE "
"spcname NOT IN ('pg_default', 'pg_global') ORDER BY spcname ASC", 0, NULL);
for (i = 0; i < PQntuples(tblspc_res); i++)
{
char *name = PQgetvalue(tblspc_res, i, 0);
char *oid = PQgetvalue(tblspc_res, i, 1);
/* when not found, append it to the backup list from non-snapshot */
if (parray_bsearch(tblspc_list, name, strCompare) == NULL)
{
char dir[MAXPGPATH];
join_path_components(dir, pgdata, PG_TBLSPC_DIR);
join_path_components(dir, dir, oid);
add_files(files, dir, true, false);
}
else
/* remove the detected tablespace from tblspc_list */
parray_rm(tblspc_list, name, strCompare);
}
/*
* tblspc_list is not empty,
* so snapshot-script output the tablespace name that not exist.
*/
if (parray_num(tblspc_list) > 0)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("snapshot-script output the name of tablespace that not exist")));
/* clear array */
parray_walk(tblspc_list, free);
parray_free(tblspc_list);
/* backup files from non-snapshot */
pgBackupGetPath(¤t, path, lengthof(path), DATABASE_DIR);
backup_files(pgdata, path, files, prev_files, lsn, current.compress_data, NULL);
/*
* Notify end of backup and write backup_label and tablespace_map
* files to backup destination directory.
*/
stop_backup_files = pg_backup_stop(¤t);
files = parray_concat(stop_backup_files, files);
/* create file list of non-snapshot objects */
create_file_list(files, pgdata, NULL, false);
/* mount snapshot volume to file-system, and obtain that mounted directory */
if (!check)
execute_mount(tblspcmp_list);
/* backup files from snapshot volume */
for (i = 0; i < parray_num(tblspcmp_list); i++)
{
char *spcname;
char *mp = NULL;
char *item = (char *) parray_get(tblspcmp_list, i);
parray *snapshot_files = parray_new();
/*
* obtain the TABLESPACE name and the directory where it is stored.
* Note: strtok() replace the delimiter to '\0'. but no problem because
* it doesn't use former value
*/
if ((spcname = strtok(item, "=")) == NULL || (mp = strtok(NULL, "\0")) == NULL)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("snapshot-script output illegal format: %s", item)));
if (verbose)
{
printf(_("========================================\n"));
printf(_("backup files from snapshot: \"%s\"\n"), spcname);
}
/* tablespace storage directory not exist */
if (!dirExists(mp))
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("tablespace storage directory doesn't exist: %s", mp)));
/*
* create the previous backup file list to take incremental backup
* from the snapshot volume.
*/
if (prev_files != NULL)
prev_files = dir_read_file_list(mp, prev_file_txt);
/* when DB cluster is backup from snapshot, it backup from the snapshot */
if (strcmp(spcname, "PG-DATA") == 0)
{
/* append DB cluster to backup file list */
add_files(snapshot_files, mp, false, true);
/* backup files of DB cluster from snapshot volume */
backup_files(mp, path, snapshot_files, prev_files, lsn, current.compress_data, NULL);
/* create file list of snapshot objects (DB cluster) */
create_file_list(snapshot_files, mp, NULL, true);
/* remove the detected tablespace("PG-DATA") from tblspcmp_list */
parray_rm(tblspcmp_list, "PG-DATA", strCompare);
i--;
}
/* backup TABLESPACE from snapshot volume */
else
{
int j;
/*
* obtain the oid from TABLESPACE information acquired by inquiring of database.
* and do backup files of TABLESPACE from snapshot volume.
*/
for (j = 0; j < PQntuples(tblspc_res); j++)
{
char dest[MAXPGPATH];
char prefix[MAXPGPATH];
char *name = PQgetvalue(tblspc_res, j, 0);
char *oid = PQgetvalue(tblspc_res, j, 1);
if (strcmp(spcname, name) == 0)
{
/* append TABLESPACE to backup file list */
add_files(snapshot_files, mp, true, false);
/* backup files of TABLESPACE from snapshot volume */
join_path_components(prefix, PG_TBLSPC_DIR, oid);
join_path_components(dest, path, prefix);
backup_files(mp, dest, snapshot_files, prev_files, lsn, current.compress_data, prefix);
/* create file list of snapshot objects (TABLESPACE) */
create_file_list(snapshot_files, mp, prefix, true);
/* remove the detected tablespace("PG-DATA") from tblspcmp_list */
parray_rm(tblspcmp_list, spcname, strCompare);
i--;
break;
}
}
}
parray_concat(files, snapshot_files);
}
/*
* tblspcmp_list is not empty,
* so snapshot-script output the tablespace name that not exist.
*/
if (parray_num(tblspcmp_list) > 0)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("snapshot-script output the name of tablespace that not exist")));
/* clear array */
parray_walk(tblspcmp_list, free);
parray_free(tblspcmp_list);
/* snapshot became unnecessary, annul the snapshot */
if (!check)
{
/* unmount directory of mounted snapshot volume */
execute_umount();
/* annul the snapshot */
execute_resync();
}
/* unset the error processing for the snapshot */
pgut_atexit_pop(snapshot_cleanup, cleanup_list);
/* don't use 'parray_walk'. element of parray not allocate memory by malloc */
parray_free(cleanup_list);
PQclear(tblspc_res);
}
/* Update various size fields in current. */
for (i = 0; i < parray_num(files); i++)
{
pgFile *file = (pgFile *) parray_get(files, i);
if (!S_ISREG(file->mode))
continue;
current.total_data_bytes += file->size;
current.read_data_bytes += file->read_size;
if (file->write_size != BYTES_INVALID)
current.write_bytes += file->write_size;
}
if (verbose)
{
printf(_("database backup completed(read: " INT64_FORMAT " write: " INT64_FORMAT ")\n"),
current.read_data_bytes, current.write_bytes);
printf(_("========================================\n"));
}
return files;
}
/*
* Connects to the standby server described by command line options,
* waits for the minimum WAL location required by this backup to be replayed,
* and finally performs a restartpoint.
*
* Returns false if could not connect to the statndby server, although that
* currently never happens, because pgut_connect() errors out anyway.
*/
static bool
execute_restartpoint(pgBackupOption bkupopt, pgBackup *backup)
{
PGconn *sby_conn = NULL;
PGresult *res;
XLogRecPtr replayed_lsn;
int sleep_time = 1;
/*
* Change connection to standby server, so that any commands executed from
* this point on are sent to the standby server.
*/
pgut_set_host(bkupopt.standby_host);
pgut_set_port(bkupopt.standby_port);
sby_conn = save_connection();
if (!sby_conn)
{
restore_saved_connection();
return false;
}
while (1)
{
uint32 xlogid, xrecoff;
/*
* Wait for standby server to replay WAL up to the LSN returned by
* pg_backup_start()
*/
res = execute("SELECT * FROM pg_last_wal_replay_lsn()", 0, NULL);
sscanf(PQgetvalue(res, 0, 0), "%X/%X", &xlogid, &xrecoff);
PQclear(res);
replayed_lsn = (XLogRecPtr) ((uint64) xlogid << 32) | xrecoff;
if (replayed_lsn >= backup->start_lsn)
break;
sleep(sleep_time);
/* next sleep_time is increasing by 2 times. */
/* ex: 1, 2, 4, 8, 16, 32, 60, 60, 60... */
sleep_time = (sleep_time < 32) ? sleep_time * 2 : 60;
}
/* Perform the restartpoint */
command("CHECKPOINT", 0, NULL);
/*
* Done sending commands to standby, restore our connection to primary.
*/
restore_saved_connection();
return true;
}
/*
* backup archived WAL incrementally.
*/
static parray *
do_backup_arclog(parray *backup_list)
{
int i;
parray *files;
parray *prev_files = NULL; /* file list of previous database backup */
FILE *fp;
char path[MAXPGPATH];
char timeline_dir[MAXPGPATH];
char prev_file_txt[MAXPGPATH];
pgBackup *prev_backup;
int64 arclog_write_bytes = 0;
char last_wal[MAXPGPATH];
if (!HAVE_ARCLOG(¤t) || check)
return NULL;
if (verbose)
{
printf(_("========================================\n"));
}
elog(INFO, _("copying archived WAL files"));
/* initialize size summary */
current.read_arclog_bytes = 0;
/* switch xlog if database is not backed up */
if (((uint32) current.stop_lsn) == 0)
pg_switch_wal(¤t);
/*
* To take incremental backup, the file list of the last completed database
* backup is needed.
*/
prev_backup = catalog_get_last_arclog_backup(backup_list);
if (prev_backup == NULL)
elog(DEBUG, "turn to take a full backup of archived WAL files");
if (prev_backup)
{
pgBackupGetPath(prev_backup, prev_file_txt, lengthof(prev_file_txt),
ARCLOG_FILE_LIST);
prev_files = dir_read_file_list(arclog_path, prev_file_txt);
}
/* list files with the logical path. omit ARCLOG_PATH */
files = parray_new();
dir_list_file(files, arclog_path, NULL, true, false);
/* remove WALs archived after pg_backup_stop()/pg_switch_wal() */
xlog_fname(last_wal, lengthof(last_wal), current.tli, ¤t.stop_lsn,
wal_segment_size);
for (i = 0; i < parray_num(files); i++)
{
pgFile *file = (pgFile *) parray_get(files, i);
char *fname;
if ((fname = last_dir_separator(file->path)))
fname++;
else
fname = file->path;
/* to backup backup history files, compare tli/lsn portion only */
if (strncmp(fname, last_wal, 24) > 0)
{
parray_remove(files, i);
i--;
}
}
elog(DEBUG, "taking backup of archived WAL files");
pgBackupGetPath(¤t, path, lengthof(path), ARCLOG_DIR);
backup_files(arclog_path, path, files, prev_files, NULL,
current.compress_data, NULL);
/* create file list */
if (!check)
{
pgBackupGetPath(¤t, path, lengthof(path), ARCLOG_FILE_LIST);
fp = fopen(path, "wt");
if (fp == NULL)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not open file list \"%s\": %s", path,
strerror(errno))));
dir_print_file_list(fp, files, arclog_path, NULL);
fclose(fp);
}
/* print summary of size of backup files */
for (i = 0; i < parray_num(files); i++)
{
pgFile *file = (pgFile *) parray_get(files, i);
if (!S_ISREG(file->mode))
continue;
current.read_arclog_bytes += file->read_size;
if (file->write_size != BYTES_INVALID)
{
current.write_bytes += file->write_size;
arclog_write_bytes += file->write_size;
}
}
/*
* Backup timeline history files to special directory.
* We do this after create file list, because copy_file() update
* pgFile->write_size to actual size.
*/
join_path_components(timeline_dir, backup_path, TIMELINE_HISTORY_DIR);
for (i = 0; i < parray_num(files); i++)
{
pgFile *file = (pgFile *) parray_get(files, i);
if (!S_ISREG(file->mode))
continue;
if (strstr(file->path, ".history") ==
file->path + strlen(file->path) - strlen(".history"))
{
elog(DEBUG, _("(timeline history) %s"), file->path);
copy_file(arclog_path, timeline_dir, file, NO_COMPRESSION);
}
}
if (verbose)
{
printf(_("archived WAL backup completed(read: " INT64_FORMAT " write: " INT64_FORMAT ")\n"),
current.read_arclog_bytes, arclog_write_bytes);
printf(_("========================================\n"));
}
return files;
}
/*
* Take a backup of serverlog.
*/
static parray *
do_backup_srvlog(parray *backup_list)
{
int i;
parray *files;
parray *prev_files = NULL; /* file list of previous database backup */
FILE *fp;
char path[MAXPGPATH];
char prev_file_txt[MAXPGPATH];
pgBackup *prev_backup;
int64 srvlog_write_bytes = 0;
if (!current.with_serverlog)
return NULL;
if (verbose)
{
printf(_("========================================\n"));
}
elog(INFO, _("copying server log files"));
/* initialize size summary */
current.read_srvlog_bytes = 0;
/*
* To take incremental backup, the file list of the last completed database
* backup is needed.
*/
prev_backup = catalog_get_last_srvlog_backup(backup_list);
if (prev_backup == NULL)
elog(DEBUG, "turn to take a full backup of server log files");
if (prev_backup)
{
pgBackupGetPath(prev_backup, prev_file_txt, lengthof(prev_file_txt),
SRVLOG_FILE_LIST);
prev_files = dir_read_file_list(srvlog_path, prev_file_txt);
}
/* list files with the logical path. omit SRVLOG_PATH */
files = parray_new();
dir_list_file(files, srvlog_path, NULL, true, false);
pgBackupGetPath(¤t, path, lengthof(path), SRVLOG_DIR);
backup_files(srvlog_path, path, files, prev_files, NULL, false, NULL);
/* create file list */
if (!check)
{
pgBackupGetPath(¤t, path, lengthof(path), SRVLOG_FILE_LIST);
fp = fopen(path, "wt");
if (fp == NULL)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not open file list \"%s\": %s", path,
strerror(errno))));
dir_print_file_list(fp, files, srvlog_path, NULL);
fclose(fp);
}
/* print summary of size of backup mode files */
for (i = 0; i < parray_num(files); i++)
{
pgFile *file = (pgFile *) parray_get(files, i);
if (!S_ISREG(file->mode))
continue;
current.read_srvlog_bytes += file->read_size;
if (file->write_size != BYTES_INVALID)
{
current.write_bytes += file->write_size;
srvlog_write_bytes += file->write_size;
}
}
if (verbose)
{
printf(_("serverlog backup completed(read: " INT64_FORMAT " write: " INT64_FORMAT ")\n"),
current.read_srvlog_bytes, srvlog_write_bytes);
printf(_("========================================\n"));
}
return files;
}
int
do_backup(pgBackupOption bkupopt)
{
parray *backup_list;
parray *files_database;
parray *files_arclog;
parray *files_srvlog;
int ret;
char path[MAXPGPATH];
/* repack the necesary options */
int keep_arclog_files = bkupopt.keep_arclog_files;
int keep_arclog_days = bkupopt.keep_arclog_days;
int keep_srvlog_files = bkupopt.keep_srvlog_files;
int keep_srvlog_days = bkupopt.keep_srvlog_days;
int keep_data_generations = bkupopt.keep_data_generations;
int keep_data_days = bkupopt.keep_data_days;
ControlFileData *controlFile;
bool crc_ok;
/* PGDATA and BACKUP_MODE are always required */
if (pgdata == NULL)
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("required parameter not specified: PGDATA (-D, --pgdata)")));
if (current.backup_mode == BACKUP_MODE_INVALID)
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("required parameter not specified: BACKUP_MODE (-b, --backup-mode)")));
/* ARCLOG_PATH is required only when backup archive WAL */
if (HAVE_ARCLOG(¤t) && arclog_path == NULL)
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("required parameter not specified: ARCLOG_PATH (-A, --arclog-path)")));
/* SRVLOG_PATH is required only when backup serverlog */
if (current.with_serverlog && srvlog_path == NULL)
ereport(ERROR,
(errcode(ERROR_ARGS),
errmsg("required parameter not specified: SRVLOG_PATH (-S, --srvlog-path)")));
/*
* If we are taking backup from standby
* (ie, $PGDATA has recovery.conf or standby.signal),
* check required parameters (ie, standby connection info).
*/
if (get_standby_signal_filepath(path, sizeof(path)))
{
if (!bkupopt.standby_host || !bkupopt.standby_port)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("please specify both standby host and port")));
current.is_from_standby = true;
}
else
current.is_from_standby = false;
#ifndef HAVE_LIBZ
if (current.compress_data)
{
ereport(WARNING,
(errmsg("this pg_rman build does not support compression"),
errhint("Please build PostgreSQL with zlib to use compression.")));
current.compress_data = false;
}
#endif
controlFile = get_controlfile(pgdata, &crc_ok);
if (!crc_ok)
ereport(WARNING,
(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);
/* Check that we're working with the correct database cluster */
check_system_identifier();
/* show configuration actually used */
if (verbose)
{
printf(_("========================================\n"));
printf(_("backup start\n"));
printf(_("----------------------------------------\n"));
pgBackupWriteConfigSection(stderr, ¤t);
printf(_("----------------------------------------\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. Skip this backup.")));
/* initialize backup result */
current.status = BACKUP_STATUS_RUNNING;
current.tli = 0; /* get from result of pg_backup_start() */
current.start_lsn = current.stop_lsn = (XLogRecPtr) 0;
current.start_time = time(NULL);
current.end_time = (time_t) 0;
current.total_data_bytes = BYTES_INVALID;
current.read_data_bytes = BYTES_INVALID;
current.read_arclog_bytes = BYTES_INVALID;
current.read_srvlog_bytes = BYTES_INVALID;
current.write_bytes = 0; /* write_bytes is valid always */
current.block_size = BLCKSZ;
current.wal_block_size = XLOG_BLCKSZ;
current.recovery_xid = 0;
current.recovery_time = (time_t) 0;
/* create backup directory and backup.ini */
if (!check)
{
if (pgBackupCreateDir(¤t))
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not create backup directory")));
pgBackupWriteIni(¤t);
}
elog(DEBUG, "destination directories of backup are initialized");
/* get list of backups already taken */
backup_list = catalog_get_backup_list(NULL);
if(!backup_list)
ereport(ERROR,
(errcode(ERROR_SYSTEM),
errmsg("could not get list of backup already taken")));
/* set the error processing function for the backup process */
pgut_atexit_push(backup_cleanup, NULL);
/*
* Signal for backup_cleanup() that there may actually be some cleanup
* for it to do from this point on.
*/
in_backup = true;
/* backup data */
files_database = do_backup_database(backup_list, bkupopt);
/* backup archived WAL */
files_arclog = do_backup_arclog(backup_list);
/* backup serverlog */
files_srvlog = do_backup_srvlog(backup_list);
pgut_atexit_pop(backup_cleanup, NULL);
/* update backup status to DONE */
current.end_time = time(NULL);
current.status = BACKUP_STATUS_DONE;
if (!check)
pgBackupWriteIni(¤t);
if (verbose)
{
if (TOTAL_READ_SIZE(¤t) == 0)
printf(_("nothing to backup\n"));
else
printf(_("all backup completed(read: " INT64_FORMAT " write: "
INT64_FORMAT ")\n"),
TOTAL_READ_SIZE(¤t), current.write_bytes);
printf(_("========================================\n"));
}
ereport(INFO,
(errmsg("backup complete")));
ereport(INFO,
(errmsg("Please execute 'pg_rman validate' to verify the files are correctly copied.")));
/*