27
27
#include "common/file_perm.h"
28
28
#include "common/file_utils.h"
29
29
#include "common/restricted_token.h"
30
+ #include "fe_utils/recovery_gen.h"
30
31
#include "getopt_long.h"
31
32
#include "storage/bufpage.h"
32
33
@@ -41,6 +42,7 @@ static void syncTargetDirectory(void);
41
42
static void sanityChecks (void );
42
43
static void findCommonAncestorTimeline (XLogRecPtr * recptr , int * tliIndex );
43
44
static void ensureCleanShutdown (const char * argv0 );
45
+ static void disconnect_atexit (void );
44
46
45
47
static ControlFileData ControlFile_target ;
46
48
static ControlFileData ControlFile_source ;
@@ -76,6 +78,8 @@ usage(const char *progname)
76
78
printf (_ (" -D, --target-pgdata=DIRECTORY existing data directory to modify\n" ));
77
79
printf (_ (" --source-pgdata=DIRECTORY source data directory to synchronize with\n" ));
78
80
printf (_ (" --source-server=CONNSTR source server to synchronize with\n" ));
81
+ printf (_ (" -R, --write-recovery-conf write configuration for replication\n"
82
+ " (requires --source-server)\n" ));
79
83
printf (_ (" -n, --dry-run stop before modifying anything\n" ));
80
84
printf (_ (" -N, --no-sync do not wait for changes to be written\n"
81
85
" safely to disk\n" ));
@@ -94,6 +98,7 @@ main(int argc, char **argv)
94
98
static struct option long_options [] = {
95
99
{"help" , no_argument , NULL , '?' },
96
100
{"target-pgdata" , required_argument , NULL , 'D' },
101
+ {"write-recovery-conf" , no_argument , NULL , 'R' },
97
102
{"source-pgdata" , required_argument , NULL , 1 },
98
103
{"source-server" , required_argument , NULL , 2 },
99
104
{"no-ensure-shutdown" , no_argument , NULL , 44 },
@@ -118,6 +123,7 @@ main(int argc, char **argv)
118
123
XLogRecPtr endrec ;
119
124
TimeLineID endtli ;
120
125
ControlFileData ControlFile_new ;
126
+ bool writerecoveryconf = false;
121
127
122
128
pg_logging_init (argv [0 ]);
123
129
set_pglocale_pgservice (argv [0 ], PG_TEXTDOMAIN ("pg_rewind" ));
@@ -138,7 +144,7 @@ main(int argc, char **argv)
138
144
}
139
145
}
140
146
141
- while ((c = getopt_long (argc , argv , "D:nNP " , long_options , & option_index )) != -1 )
147
+ while ((c = getopt_long (argc , argv , "D:nNPR " , long_options , & option_index )) != -1 )
142
148
{
143
149
switch (c )
144
150
{
@@ -158,6 +164,10 @@ main(int argc, char **argv)
158
164
do_sync = false;
159
165
break ;
160
166
167
+ case 'R' :
168
+ writerecoveryconf = true;
169
+ break ;
170
+
161
171
case 3 :
162
172
debug = true;
163
173
pg_logging_set_level (PG_LOG_DEBUG );
@@ -200,6 +210,13 @@ main(int argc, char **argv)
200
210
exit (1 );
201
211
}
202
212
213
+ if (writerecoveryconf && connstr_source == NULL )
214
+ {
215
+ pg_log_error ("no source server information (--source--server) specified for --write-recovery-conf" );
216
+ fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ), progname );
217
+ exit (1 );
218
+ }
219
+
203
220
if (optind < argc )
204
221
{
205
222
pg_log_error ("too many command-line arguments (first is \"%s\")" ,
@@ -236,6 +253,8 @@ main(int argc, char **argv)
236
253
237
254
umask (pg_mode_mask );
238
255
256
+ atexit (disconnect_atexit );
257
+
239
258
/* Connect to remote server */
240
259
if (connstr_source )
241
260
libpqConnect (connstr_source );
@@ -322,6 +341,9 @@ main(int argc, char **argv)
322
341
if (!rewind_needed )
323
342
{
324
343
pg_log_info ("no rewind required" );
344
+ if (writerecoveryconf )
345
+ WriteRecoveryConfig (conn , datadir_target ,
346
+ GenerateRecoveryConfig (conn , NULL ));
325
347
exit (0 );
326
348
}
327
349
@@ -419,6 +441,10 @@ main(int argc, char **argv)
419
441
pg_log_info ("syncing target data directory" );
420
442
syncTargetDirectory ();
421
443
444
+ if (writerecoveryconf )
445
+ WriteRecoveryConfig (conn , datadir_target ,
446
+ GenerateRecoveryConfig (conn , NULL ));
447
+
422
448
pg_log_info ("Done!" );
423
449
424
450
return 0 ;
@@ -828,3 +854,10 @@ ensureCleanShutdown(const char *argv0)
828
854
pg_fatal ("Command was: %s" , cmd );
829
855
}
830
856
}
857
+
858
+ static void
859
+ disconnect_atexit (void )
860
+ {
861
+ if (conn != NULL )
862
+ PQfinish (conn );
863
+ }
0 commit comments