@@ -64,7 +64,7 @@ static char *target_inclusive;
64
64
static TimeLineID target_tli ;
65
65
static bool target_immediate ;
66
66
static char * target_name = NULL ;
67
- static char * target_action = NULL ;;
67
+ static char * target_action = NULL ;
68
68
69
69
static pgRecoveryTarget * recovery_target_options = NULL ;
70
70
@@ -99,9 +99,10 @@ ShowFormat show_format = SHOW_PLAIN;
99
99
100
100
/* current settings */
101
101
pgBackup current ;
102
- ProbackupSubcmd backup_subcmd ;
102
+ ProbackupSubcmd backup_subcmd = NO_CMD ;
103
103
104
- bool help = false;
104
+ static bool help_opt = false;
105
+ static bool version_opt = false;
105
106
106
107
static void opt_backup_mode (pgut_option * opt , const char * arg );
107
108
static void opt_log_level_console (pgut_option * opt , const char * arg );
@@ -114,7 +115,8 @@ static void compress_init(void);
114
115
static pgut_option options [] =
115
116
{
116
117
/* directory options */
117
- { 'b' , 1 , "help" , & help , SOURCE_CMDLINE },
118
+ { 'b' , 1 , "help" , & help_opt , SOURCE_CMDLINE },
119
+ { 'b' , 'V' , "version" , & version_opt , SOURCE_CMDLINE },
118
120
{ 's' , 'D' , "pgdata" , & pgdata , SOURCE_CMDLINE },
119
121
{ 's' , 'B' , "backup-path" , & backup_path , SOURCE_CMDLINE },
120
122
/* common options */
@@ -152,7 +154,7 @@ static pgut_option options[] =
152
154
{ 'b' , 131 , "expired" , & delete_expired , SOURCE_CMDLINE },
153
155
{ 'b' , 132 , "all" , & apply_to_all , SOURCE_CMDLINE },
154
156
/* TODO not implemented yet */
155
- { 'b' , 133 , "force" , & force_delete , SOURCE_CMDLINE },
157
+ { 'b' , 133 , "force" , & force_delete , SOURCE_CMDLINE },
156
158
/* retention options */
157
159
{ 'u' , 134 , "retention-redundancy" , & retention_redundancy , SOURCE_CMDLINE },
158
160
{ 'u' , 135 , "retention-window" , & retention_window , SOURCE_CMDLINE },
@@ -198,6 +200,9 @@ main(int argc, char *argv[])
198
200
/* Check if backup_path is directory. */
199
201
struct stat stat_buf ;
200
202
int rc ;
203
+ int i ,
204
+ len = 0 ,
205
+ allocated = 0 ;
201
206
202
207
/* initialize configuration */
203
208
pgBackup_init (& current );
@@ -210,106 +215,94 @@ main(int argc, char *argv[])
210
215
*/
211
216
main_tid = pthread_self ();
212
217
213
- /* Parse subcommands and non-subcommand options */
214
- if (argc > 1 )
215
- {
216
- if (strcmp (argv [1 ], "archive-push" ) == 0 )
217
- backup_subcmd = ARCHIVE_PUSH ;
218
- else if (strcmp (argv [1 ], "archive-get" ) == 0 )
219
- backup_subcmd = ARCHIVE_GET ;
220
- else if (strcmp (argv [1 ], "add-instance" ) == 0 )
221
- backup_subcmd = ADD_INSTANCE ;
222
- else if (strcmp (argv [1 ], "del-instance" ) == 0 )
223
- backup_subcmd = DELETE_INSTANCE ;
224
- else if (strcmp (argv [1 ], "init" ) == 0 )
225
- backup_subcmd = INIT ;
226
- else if (strcmp (argv [1 ], "backup" ) == 0 )
227
- backup_subcmd = BACKUP ;
228
- else if (strcmp (argv [1 ], "restore" ) == 0 )
229
- backup_subcmd = RESTORE ;
230
- else if (strcmp (argv [1 ], "validate" ) == 0 )
231
- backup_subcmd = VALIDATE ;
232
- else if (strcmp (argv [1 ], "show" ) == 0 )
233
- backup_subcmd = SHOW ;
234
- else if (strcmp (argv [1 ], "delete" ) == 0 )
235
- backup_subcmd = DELETE_SUBCMD ;
236
- else if (strcmp (argv [1 ], "set-config" ) == 0 )
237
- backup_subcmd = SET_CONFIG ;
238
- else if (strcmp (argv [1 ], "show-config" ) == 0 )
239
- backup_subcmd = SHOW_CONFIG ;
240
- else if (strcmp (argv [1 ], "--help" ) == 0
241
- || strcmp (argv [1 ], "help" ) == 0
242
- || strcmp (argv [1 ], "-?" ) == 0 )
243
- {
244
- if (argc > 2 )
245
- help_command (argv [2 ]);
246
- else
247
- help_pg_probackup ();
248
- }
249
- else if (strcmp (argv [1 ], "--version" ) == 0
250
- || strcmp (argv [1 ], "version" ) == 0
251
- || strcmp (argv [1 ], "-V" ) == 0 )
252
- {
253
- if (argc == 2 )
254
- {
255
- #ifdef PGPRO_VERSION
256
- fprintf (stderr , "%s %s (Postgres Pro %s %s)\n" ,
257
- PROGRAM_NAME , PROGRAM_VERSION ,
258
- PGPRO_VERSION , PGPRO_EDITION );
259
- #else
260
- fprintf (stderr , "%s %s (PostgreSQL %s)\n" ,
261
- PROGRAM_NAME , PROGRAM_VERSION , PG_VERSION );
262
- #endif
263
- exit (0 );
264
- }
265
- else if (strcmp (argv [2 ], "--help" ) == 0 )
266
- help_command (argv [1 ]);
267
- else
268
- elog (ERROR , "Invalid arguments for \"%s\" subcommand" , argv [1 ]);
269
- }
270
- else
271
- elog (ERROR , "Unknown subcommand" );
272
- }
273
-
274
218
/*
275
219
* Make command string before getopt_long() will call. It permutes the
276
220
* content of argv.
277
221
*/
278
- if (backup_subcmd == BACKUP ||
279
- backup_subcmd == RESTORE ||
280
- backup_subcmd == VALIDATE ||
281
- backup_subcmd == DELETE_SUBCMD )
282
- {
283
- int i ,
284
- len = 0 ,
285
- allocated = 0 ;
222
+ allocated = sizeof (char ) * MAXPGPATH ;
223
+ command = (char * ) palloc (allocated );
286
224
287
- allocated = sizeof (char ) * MAXPGPATH ;
288
- command = (char * ) palloc (allocated );
225
+ for (i = 0 ; i < argc ; i ++ )
226
+ {
227
+ int arglen = strlen (argv [i ]);
289
228
290
- for ( i = 0 ; i < argc ; i ++ )
229
+ if ( arglen + len > allocated )
291
230
{
292
- int arglen = strlen (argv [i ]);
293
-
294
- if (arglen + len > allocated )
295
- {
296
- allocated *= 2 ;
297
- command = repalloc (command , allocated );
298
- }
299
-
300
- strncpy (command + len , argv [i ], arglen );
301
- len += arglen ;
302
- command [len ++ ] = ' ' ;
231
+ allocated *= 2 ;
232
+ command = repalloc (command , allocated );
303
233
}
304
234
305
- command [len ] = '\0' ;
235
+ strncpy (command + len , argv [i ], arglen );
236
+ len += arglen ;
237
+ command [len ++ ] = ' ' ;
306
238
}
307
239
240
+ command [len ] = '\0' ;
241
+
308
242
/* Parse command line arguments */
309
243
pgut_getopt (argc , argv , options );
310
244
311
- if (help )
312
- help_command (argv [2 ]);
245
+ /* Process a command */
246
+ if (optind < argc )
247
+ {
248
+ if (strcmp (argv [optind ], "archive-push" ) == 0 )
249
+ backup_subcmd = ARCHIVE_PUSH_CMD ;
250
+ else if (strcmp (argv [optind ], "archive-get" ) == 0 )
251
+ backup_subcmd = ARCHIVE_GET_CMD ;
252
+ else if (strcmp (argv [optind ], "add-instance" ) == 0 )
253
+ backup_subcmd = ADD_INSTANCE_CMD ;
254
+ else if (strcmp (argv [optind ], "del-instance" ) == 0 )
255
+ backup_subcmd = DELETE_INSTANCE_CMD ;
256
+ else if (strcmp (argv [optind ], "init" ) == 0 )
257
+ backup_subcmd = INIT_CMD ;
258
+ else if (strcmp (argv [optind ], "backup" ) == 0 )
259
+ backup_subcmd = BACKUP_CMD ;
260
+ else if (strcmp (argv [optind ], "restore" ) == 0 )
261
+ backup_subcmd = RESTORE_CMD ;
262
+ else if (strcmp (argv [optind ], "validate" ) == 0 )
263
+ backup_subcmd = VALIDATE_CMD ;
264
+ else if (strcmp (argv [optind ], "show" ) == 0 )
265
+ backup_subcmd = SHOW_CMD ;
266
+ else if (strcmp (argv [optind ], "delete" ) == 0 )
267
+ backup_subcmd = DELETE_CMD ;
268
+ else if (strcmp (argv [optind ], "set-config" ) == 0 )
269
+ backup_subcmd = SET_CONFIG_CMD ;
270
+ else if (strcmp (argv [optind ], "show-config" ) == 0 )
271
+ backup_subcmd = SHOW_CONFIG_CMD ;
272
+ else if (strcmp (argv [optind ], "help" ) == 0 )
273
+ {
274
+ if (argc - optind < 2 )
275
+ help_pg_probackup ();
276
+ else
277
+ help_command (argv [optind + 1 ]);
278
+ }
279
+ else if (strcmp (argv [optind ], "version" ) == 0 )
280
+ version_opt = true;
281
+ else
282
+ elog (ERROR , "Unknown subcommand \"%s\"" , argv [optind ]);
283
+ }
284
+
285
+ if (help_opt )
286
+ {
287
+ if (backup_subcmd == NO_CMD || argc - optind < 1 )
288
+ help_pg_probackup ();
289
+ else
290
+ help_command (argv [optind ]);
291
+ }
292
+ else if (version_opt )
293
+ {
294
+ #ifdef PGPRO_VERSION
295
+ fprintf (stderr , "%s %s (Postgres Pro %s %s)\n" ,
296
+ PROGRAM_NAME , PROGRAM_VERSION ,
297
+ PGPRO_VERSION , PGPRO_EDITION );
298
+ #else
299
+ fprintf (stderr , "%s %s (PostgreSQL %s)\n" ,
300
+ PROGRAM_NAME , PROGRAM_VERSION , PG_VERSION );
301
+ #endif
302
+ return 0 ;
303
+ }
304
+ else if (backup_subcmd == NO_CMD )
305
+ elog (ERROR , "No subcommand specified" );
313
306
314
307
/* backup_path is required for all pg_probackup commands except help */
315
308
if (backup_path == NULL )
@@ -343,7 +336,7 @@ main(int argc, char *argv[])
343
336
}
344
337
345
338
/* Option --instance is required for all commands except init and show */
346
- if (backup_subcmd != INIT && backup_subcmd != SHOW && backup_subcmd != VALIDATE )
339
+ if (backup_subcmd != INIT_CMD && backup_subcmd != SHOW_CMD && backup_subcmd != VALIDATE_CMD )
347
340
{
348
341
if (instance_name == NULL )
349
342
elog (ERROR , "required parameter not specified: --instance" );
@@ -363,7 +356,7 @@ main(int argc, char *argv[])
363
356
* for all commands except init, which doesn't take this parameter
364
357
* and add-instance which creates new instance.
365
358
*/
366
- if (backup_subcmd != INIT && backup_subcmd != ADD_INSTANCE )
359
+ if (backup_subcmd != INIT_CMD && backup_subcmd != ADD_INSTANCE_CMD )
367
360
{
368
361
if (access (backup_instance_path , F_OK ) != 0 )
369
362
elog (ERROR , "Instance '%s' does not exist in this backup catalog" ,
@@ -375,7 +368,7 @@ main(int argc, char *argv[])
375
368
* Read options from env variables or from config file,
376
369
* unless we're going to set them via set-config.
377
370
*/
378
- if (instance_name && backup_subcmd != SET_CONFIG )
371
+ if (instance_name && backup_subcmd != SET_CONFIG_CMD )
379
372
{
380
373
/* Read environment variables */
381
374
pgut_getopt_env (options );
@@ -398,10 +391,10 @@ main(int argc, char *argv[])
398
391
/* Sanity check of --backup-id option */
399
392
if (backup_id_string_param != NULL )
400
393
{
401
- if (backup_subcmd != RESTORE
402
- && backup_subcmd != VALIDATE
403
- && backup_subcmd != DELETE_SUBCMD
404
- && backup_subcmd != SHOW )
394
+ if (backup_subcmd != RESTORE_CMD
395
+ && backup_subcmd != VALIDATE_CMD
396
+ && backup_subcmd != DELETE_CMD
397
+ && backup_subcmd != SHOW_CMD )
405
398
elog (ERROR , "Cannot use -i (--backup-id) option together with the '%s' command" ,
406
399
argv [1 ]);
407
400
@@ -429,7 +422,7 @@ main(int argc, char *argv[])
429
422
pgdata_exclude_dir [i ] = "pg_log" ;
430
423
}
431
424
432
- if (backup_subcmd == VALIDATE || backup_subcmd == RESTORE )
425
+ if (backup_subcmd == VALIDATE_CMD || backup_subcmd == RESTORE_CMD )
433
426
{
434
427
/* parse all recovery target options into recovery_target_options structure */
435
428
recovery_target_options = parseRecoveryTargetOptions (target_time , target_xid ,
@@ -445,17 +438,17 @@ main(int argc, char *argv[])
445
438
/* do actual operation */
446
439
switch (backup_subcmd )
447
440
{
448
- case ARCHIVE_PUSH :
441
+ case ARCHIVE_PUSH_CMD :
449
442
return do_archive_push (wal_file_path , wal_file_name , file_overwrite );
450
- case ARCHIVE_GET :
443
+ case ARCHIVE_GET_CMD :
451
444
return do_archive_get (wal_file_path , wal_file_name );
452
- case ADD_INSTANCE :
445
+ case ADD_INSTANCE_CMD :
453
446
return do_add_instance ();
454
- case DELETE_INSTANCE :
447
+ case DELETE_INSTANCE_CMD :
455
448
return do_delete_instance ();
456
- case INIT :
449
+ case INIT_CMD :
457
450
return do_init ();
458
- case BACKUP :
451
+ case BACKUP_CMD :
459
452
{
460
453
const char * backup_mode ;
461
454
time_t start_time ;
@@ -470,20 +463,20 @@ main(int argc, char *argv[])
470
463
471
464
return do_backup (start_time );
472
465
}
473
- case RESTORE :
466
+ case RESTORE_CMD :
474
467
return do_restore_or_validate (current .backup_id ,
475
468
recovery_target_options ,
476
469
true);
477
- case VALIDATE :
470
+ case VALIDATE_CMD :
478
471
if (current .backup_id == 0 && target_time == 0 && target_xid == 0 )
479
472
return do_validate_all ();
480
473
else
481
474
return do_restore_or_validate (current .backup_id ,
482
475
recovery_target_options ,
483
476
false);
484
- case SHOW :
477
+ case SHOW_CMD :
485
478
return do_show (current .backup_id );
486
- case DELETE_SUBCMD :
479
+ case DELETE_CMD :
487
480
if (delete_expired && backup_id_string_param )
488
481
elog (ERROR , "You cannot specify --delete-expired and --backup-id options together" );
489
482
if (!delete_expired && !delete_wal && !backup_id_string_param )
@@ -494,10 +487,13 @@ main(int argc, char *argv[])
494
487
return do_retention_purge ();
495
488
else
496
489
return do_delete (current .backup_id );
497
- case SHOW_CONFIG :
490
+ case SHOW_CONFIG_CMD :
498
491
return do_configure (true);
499
- case SET_CONFIG :
492
+ case SET_CONFIG_CMD :
500
493
return do_configure (false);
494
+ case NO_CMD :
495
+ /* Should not happen */
496
+ elog (ERROR , "Unknown subcommand" );
501
497
}
502
498
503
499
return 0 ;
@@ -561,7 +557,7 @@ compress_init(void)
561
557
if (compress_shortcut )
562
558
compress_alg = ZLIB_COMPRESS ;
563
559
564
- if (backup_subcmd != SET_CONFIG )
560
+ if (backup_subcmd != SET_CONFIG_CMD )
565
561
{
566
562
if (compress_level != DEFAULT_COMPRESS_LEVEL
567
563
&& compress_alg == NOT_DEFINED_COMPRESS )
@@ -574,7 +570,7 @@ compress_init(void)
574
570
if (compress_level == 0 )
575
571
compress_alg = NOT_DEFINED_COMPRESS ;
576
572
577
- if (backup_subcmd == BACKUP || backup_subcmd == ARCHIVE_PUSH )
573
+ if (backup_subcmd == BACKUP_CMD || backup_subcmd == ARCHIVE_PUSH_CMD )
578
574
{
579
575
#ifndef HAVE_LIBZ
580
576
if (compress_alg == ZLIB_COMPRESS )
0 commit comments