2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
19 #include <rx/rx_globals.h>
21 #include <afs/bubasics.h>
22 #include <afs/afsutil.h>
24 #include <afs/cellconfig.h>
29 #include <afs/volser.h> /*VLDB_MAXSERVERS */
30 #include <afs/com_err.h>
33 #include <afs/kautils.h>
34 #include <afs/vlserver.h>
37 #include <afs/tcdata.h>
39 #include "bc.h" /*Backup Coordinator structs and defs */
40 #include "bucoord_internal.h"
41 #include "bucoord_prototypes.h"
43 int localauth, interact;
47 * Global configuration information for the Backup Coordinator.
49 struct bc_config *bc_globalConfig; /*Ptr to global BC configuration info */
51 struct ubik_client *cstruct; /* Ptr to Ubik client structure */
52 time_t tokenExpires; /* The token's expiration time */
54 static const char *DefaultConfDir; /*Default backup config directory */
55 static int bcInit = 0; /* backupInit called yet ? */
56 char *whoami = "backup";
58 /* dummy routine for the audit work. It should do nothing since audits */
59 /* occur at the server level and bos is not a server. */
67 * Initialize all the error tables that may be used by com_err
73 initialize_ACFG_error_table();
74 initialize_KA_error_table();
75 initialize_RXK_error_table();
76 initialize_CMD_error_table();
77 initialize_VL_error_table();
78 initialize_BUTM_error_table();
79 initialize_VOLS_error_table();
80 initialize_BUTC_error_table();
81 initialize_BUTX_error_table();
82 initialize_BUDB_error_table();
83 initialize_BUCD_error_table();
84 initialize_KTC_error_table();
88 * got to account for the errors which are volume related but
89 * not dealt with by standard errno and com_err stuff.
92 bc_HandleMisc(afs_int32 code)
94 if (((code <= VMOVED) && (code >= VSALVAGE)) || (code < 0)) {
97 fprintf(STDERR, "Possible communication failure\n");
100 fprintf(STDERR, "Volume needs salvage\n");
103 fprintf(STDERR, "Bad vnode number quoted\n");
107 "Volume not attached, does not exist, or not on line\n");
110 fprintf(STDERR, "Volume already exists\n");
113 fprintf(STDERR, "Volume is not in service\n");
116 fprintf(STDERR, "Volume is off line\n");
119 fprintf(STDERR, "Volume is already on line\n");
122 fprintf(STDERR, "Partition is full\n");
125 fprintf(STDERR, "Volume max quota exceeded\n");
128 fprintf(STDERR, "Volume temporarily unavailable\n");
131 fprintf(STDERR, "Volume has moved to another server\n");
140 /* Return true if line is all whitespace */
142 LineIsBlank(char *aline)
146 while ((tc = *aline++))
147 if ((tc != ' ') && (tc != '\t') && (tc != '\n'))
155 * initialize configuration information that is stored as text blocks
159 bc_InitTextConfig(void)
161 udbClientTextP ctPtr;
164 extern struct bc_config *bc_globalConfig;
166 mkdir(DefaultConfDir, 777); /* temporary */
168 /* initialize the client text structures */
169 ctPtr = &bc_globalConfig->configText[0];
171 for (i = 0; i < TB_NUM; i++) {
172 memset(ctPtr, 0, sizeof(*ctPtr));
174 ctPtr->textVersion = -1;
181 /*----------------------------------------------------------------------------
185 * Routine that is called when the backup coordinator is invoked, responsible for
186 * basic initialization and some command line parsing.
189 * Zero (but may exit the entire program on error!)
192 * Nothing interesting.
195 * Initializes this program.
196 *----------------------------------------------------------------------------
203 static int initd = 0; /* ever called? */
205 PROCESS pid; /* LWP process ID */
208 initialize_CMD_error_table();
210 /* don't run more than once */
212 afs_com_err(whoami, 0, "Backup already initialized.");
217 code = bc_InitConfig((char *)DefaultConfDir);
219 afs_com_err(whoami, code,
220 "Can't initialize from config files in directory '%s'",
228 code = LWP_InitializeProcessSupport(LWP_NORMAL_PRIORITY, &pid);
230 afs_com_err(whoami, code, "; Can't initialize LWP");
234 code = rx_Init(htons(0));
236 afs_com_err(whoami, code, "; Can't initialize Rx");
240 rx_SetRxDeadTime(60);
242 /* VLDB initialization */
243 code = vldbClientInit(0, localauth, tcell, &cstruct, &tokenExpires);
247 /* Backup database initialization */
248 code = udbClientInit(0, localauth, tcell);
252 /* setup status monitoring thread */
255 LWP_CreateProcess(statusWatcher, 20480, LWP_NORMAL_PRIORITY,
256 (void *)2, "statusWatcher", &watcherPid);
258 afs_com_err(whoami, code, "; Can't create status monitor task");
265 /*----------------------------------------------------------------------------
269 * Make sure we mark down when we need to exit the Backup Coordinator.
270 * Specifically, we don't want to continue when our help and apropos
271 * routines are called from the command line.
274 * as : Ptr to the command syntax descriptor.
280 * This routine is called right before each of the Backup Coordinator
281 * opcode routines are invoked.
283 *----------------------------------------------------------------------------
287 MyBeforeProc(struct cmd_syndesc *as, void *arock)
291 /* Handling the command line opcode */
293 localauth = ((as && as->parms[14].items) ? 1 : 0);
294 if (as && as->parms[15].items)
295 strcpy(tcell, as->parms[15].items->data);
301 afs_com_err(whoami, code, "; Can't initialize backup");
305 /* Get initial information from the database */
306 code = bc_InitTextConfig();
308 afs_com_err(whoami, code,
309 "; Can't obtain configuration text from backup database");
321 #include "AFS_component_version_number.c"
323 #define MAXRECURSION 20
324 extern int dontExecute; /* declared in commands.c */
325 extern char *loadFile; /* declared in commands.c */
326 char lineBuffer[1024]; /* Line typed in by user or read from load file */
329 * This will dispatch a command. It holds a recursive loop for the
330 * "dump -file" option. This option reads backup commands from a file.
332 * Cannot put this code on other side of cmd_Dispatch call (in
333 * commands.c) because when make a dispatch call when in a dispatch
334 * call, environment is mucked up.
336 * To avoid multiple processes stepping on each other in the dispatch code,
337 * put a lock around it so only 1 process gets in at a time.
340 struct Lock dispatchLock; /* lock on the Dispatch call */
341 #define lock_Dispatch() ObtainWriteLock(&dispatchLock)
342 #define unlock_Dispatch() ReleaseWriteLock(&dispatchLock)
345 doDispatch(afs_int32 targc,
347 afs_int32 dispatchCount) /* to prevent infinite recursion */
355 int noExecute; /* local capy of global variable */
356 char *internalLoadFile;
361 code = cmd_Dispatch(targc, targv);
362 internalLoadFile = loadFile;
366 if (internalLoadFile) { /* Load a file in */
367 if (dispatchCount > MAXRECURSION) { /* Beware recursive loops. */
368 afs_com_err(whoami, 0, "Potential recursion: will not load file %s",
374 fd = fopen(internalLoadFile, "r"); /* Open the load file */
376 afs_com_err(whoami, errno, "; Cannot open file %s", internalLoadFile);
381 noExecute = dontExecute;
383 printf("Would have executed the following commands:\n");
386 while (fgets(lineBuffer, sizeof(lineBuffer) - 1, fd)) { /* Get commands from file */
389 i = strlen(lineBuffer) - 1;
390 if (lineBuffer[i] == '\n') /* Drop return at end of line */
391 lineBuffer[i] = '\0';
394 printf(" %s\n", lineBuffer); /* echo */
396 printf("------> %s\n", lineBuffer); /* echo */
398 if (!LineIsBlank(lineBuffer) && /* Skip if blank line */
399 (lineBuffer[0] != '#') && /* or comment */
400 (!noExecute)) { /* or no execute */
401 c = cmd_ParseLine(lineBuffer, sargv, &sargc, MAXV);
403 afs_com_err(whoami, c, "; Can't parse line");
405 doDispatch(sargc, sargv, dispatchCount + 1); /* Recursive - ignore error */
406 cmd_FreeArgv(sargv); /* Free up arguments */
415 if (internalLoadFile)
416 free(internalLoadFile);
421 bc_interactCmd(struct cmd_syndesc *as, void *arock)
428 add_std_args(struct cmd_syndesc *ts)
431 cmd_AddParm(ts, "-localauth", CMD_FLAG, CMD_OPTIONAL,
432 "local authentication");
433 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
437 main(int argc, char **argv)
439 char *targv[MAXV]; /*Ptr to parsed argv stuff */
440 afs_int32 targc; /*Num parsed arguments */
441 afs_int32 code; /*Return code */
442 struct cmd_syndesc *ts; /*Ptr to parsed command line */
448 * The following signal action for AIX is necessary so that in case of a
449 * crash (i.e. core is generated) we can include the user's data section
450 * in the core dump. Unfortunately, by default, only a partial core is
451 * generated which, in many cases, isn't too useful.
453 struct sigaction nsa;
455 sigemptyset(&nsa.sa_mask);
456 nsa.sa_handler = SIG_DFL;
457 nsa.sa_flags = SA_FULLDUMP;
458 sigaction(SIGSEGV, &nsa, NULL);
460 Lock_Init(&dispatchLock);
461 InitErrTabs(); /* init all the error tables which may be used */
463 /* setup the default backup dir */
464 DefaultConfDir = AFSDIR_SERVER_BACKUP_DIRPATH;
465 /* Get early warning if the command is interacive mode or not */
466 interact = (((argc < 2) || (argv[1][0] == '-')) ? 1 : 0);
468 cmd_SetBeforeProc(MyBeforeProc, NULL);
470 ts = cmd_CreateSyntax("dump", bc_DumpCmd, NULL, "start dump");
471 cmd_AddParm(ts, "-volumeset", CMD_SINGLE, CMD_OPTIONAL,
473 cmd_AddParm(ts, "-dump", CMD_SINGLE, CMD_OPTIONAL, "dump level name");
474 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
476 cmd_AddParm(ts, "-at", CMD_LIST, CMD_OPTIONAL, "Date/time to start dump");
477 cmd_AddParm(ts, "-append", CMD_FLAG, CMD_OPTIONAL,
478 "append to existing dump set");
479 cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL,
480 "list what would be done, don't do it");
481 cmd_AddParmAlias(ts, 5, "-n");
482 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "load file");
486 ts = cmd_CreateSyntax("volrestore", bc_VolRestoreCmd, NULL,
488 cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED,
489 "destination machine");
490 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED,
491 "destination partition");
492 cmd_AddParm(ts, "-volume", CMD_LIST, CMD_REQUIRED,
493 "volume(s) to restore");
494 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL,
495 "new volume name extension");
496 cmd_AddParm(ts, "-date", CMD_LIST, CMD_OPTIONAL,
497 "date from which to restore");
498 cmd_AddParm(ts, "-portoffset", CMD_LIST, CMD_OPTIONAL, "TC port offsets");
499 cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL,
500 "list what would be done, don't do it");
501 cmd_AddParmAlias(ts, 6, "-n");
502 cmd_AddParm(ts, "-usedump", CMD_SINGLE, CMD_OPTIONAL,
503 "specify the dumpID to restore from");
507 ts = cmd_CreateSyntax("diskrestore", bc_DiskRestoreCmd, NULL,
508 "restore partition");
509 cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED,
510 "machine to restore");
511 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED,
512 "partition to restore");
513 cmd_AddParm(ts, "-portoffset", CMD_LIST, CMD_OPTIONAL, "TC port offset");
515 cmd_AddParm(ts, "-newserver", CMD_SINGLE, CMD_OPTIONAL,
516 "destination machine");
517 cmd_AddParm(ts, "-newpartition", CMD_SINGLE, CMD_OPTIONAL,
518 "destination partition");
519 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL,
520 "new volume name extension");
521 cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL,
522 "list what would be done, don't do it");
523 cmd_AddParmAlias(ts, 11, "-n");
527 ts = cmd_CreateSyntax("quit", bc_QuitCmd, NULL, "leave the program");
529 ts = cmd_CreateSyntax("volsetrestore", bc_VolsetRestoreCmd, NULL,
530 "restore a set of volumes");
531 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "volume set name");
532 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "file name");
533 cmd_AddParm(ts, "-portoffset", CMD_LIST, CMD_OPTIONAL, "TC port offset");
534 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL,
535 "new volume name extension");
536 cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL,
537 "list what would be done, don't do it");
538 cmd_AddParmAlias(ts, 4, "-n");
542 ts = cmd_CreateSyntax("addhost", bc_AddHostCmd, NULL, "add host to config");
543 cmd_AddParm(ts, "-tapehost", CMD_SINGLE, CMD_REQUIRED,
544 "tape machine name");
545 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
550 ts = cmd_CreateSyntax("delhost", bc_DeleteHostCmd, NULL,
551 "delete host to config");
552 cmd_AddParm(ts, "-tapehost", CMD_SINGLE, CMD_REQUIRED,
553 "tape machine name");
554 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
559 ts = cmd_CreateSyntax("listhosts", bc_ListHostsCmd, NULL,
560 "list config hosts");
564 ts = cmd_CreateSyntax("jobs", bc_JobsCmd, NULL, "list running jobs");
566 ts = cmd_CreateSyntax("kill", bc_KillCmd, NULL, "kill running job");
567 cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_REQUIRED,
568 "job ID or dump set name");
570 ts = cmd_CreateSyntax("listvolsets", bc_ListVolSetCmd, NULL,
572 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "volume set name");
576 ts = cmd_CreateSyntax("listdumps", bc_ListDumpScheduleCmd, NULL,
577 "list dump schedules");
581 ts = cmd_CreateSyntax("addvolset", bc_AddVolSetCmd, NULL,
582 "create a new volume set");
583 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_REQUIRED, "volume set name");
584 cmd_AddParm(ts, "-temporary", CMD_FLAG, CMD_OPTIONAL,
585 "temporary volume set");
589 ts = cmd_CreateSyntax("status", bc_GetTapeStatusCmd, NULL,
590 "get tape coordinator status");
591 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
596 ts = cmd_CreateSyntax("delvolset", bc_DeleteVolSetCmd, NULL,
597 "delete a volume set");
598 cmd_AddParm(ts, "-name", CMD_LIST, CMD_REQUIRED, "volume set name");
602 ts = cmd_CreateSyntax("addvolentry", bc_AddVolEntryCmd, NULL,
603 "add a new volume entry");
604 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_REQUIRED, "volume set name");
605 cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED, "machine name");
606 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED, "partition name");
607 cmd_AddParm(ts, "-volumes", CMD_SINGLE, CMD_REQUIRED,
608 "volume name (regular expression)");
612 ts = cmd_CreateSyntax("delvolentry", bc_DeleteVolEntryCmd, NULL,
613 "delete a volume set sub-entry");
614 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_REQUIRED, "volume set name");
615 cmd_AddParm(ts, "-entry", CMD_SINGLE, CMD_REQUIRED, "volume set index");
619 ts = cmd_CreateSyntax("adddump", bc_AddDumpCmd, NULL, "add dump schedule");
620 cmd_AddParm(ts, "-dump", CMD_LIST, CMD_REQUIRED, "dump level name");
621 cmd_AddParm(ts, "-expires", CMD_LIST, CMD_OPTIONAL, "expiration date");
625 ts = cmd_CreateSyntax("deldump", bc_DeleteDumpCmd, NULL,
626 "delete dump schedule");
627 cmd_AddParm(ts, "-dump", CMD_SINGLE, CMD_REQUIRED, "dump level name");
631 ts = cmd_CreateSyntax("labeltape", bc_LabelTapeCmd, NULL, "label a tape");
632 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL,
633 "AFS tape name, defaults to NULL");
634 cmd_AddParm(ts, "-size", CMD_SINGLE, CMD_OPTIONAL,
635 "tape size in Kbytes, defaults to size in tapeconfig");
636 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
638 cmd_AddParm(ts, "-pname", CMD_SINGLE, CMD_OPTIONAL,
639 "permanent tape name");
643 ts = cmd_CreateSyntax("readlabel", bc_ReadLabelCmd, NULL,
644 "read the label on tape");
645 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
650 ts = cmd_CreateSyntax("scantape", bc_ScanDumpsCmd, NULL,
651 "dump information recovery from tape");
652 cmd_AddParm(ts, "-dbadd", CMD_FLAG, CMD_OPTIONAL,
653 "add information to the database");
654 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
659 ts = cmd_CreateSyntax("volinfo", bc_dblookupCmd, NULL,
660 "query the backup database");
661 cmd_AddParm(ts, "-volume", CMD_SINGLE, CMD_REQUIRED, "volume name");
665 ts = cmd_CreateSyntax("setexp", bc_SetExpCmd, NULL,
666 "set/clear dump expiration dates");
667 cmd_AddParm(ts, "-dump", CMD_LIST, CMD_REQUIRED, "dump level name");
668 cmd_AddParm(ts, "-expires", CMD_LIST, CMD_OPTIONAL, "expiration date");
672 ts = cmd_CreateSyntax("savedb", bc_saveDbCmd, NULL, "save backup database");
673 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
675 cmd_AddParm(ts, "-archive", CMD_LIST, CMD_OPTIONAL, "date time");
679 ts = cmd_CreateSyntax("restoredb", bc_restoreDbCmd, NULL,
680 "restore backup database");
681 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
686 ts = cmd_CreateSyntax("dumpinfo", bc_dumpInfoCmd, NULL,
687 "provide information about a dump in the database");
688 cmd_AddParm(ts, "-ndumps", CMD_SINGLE, CMD_OPTIONAL, "no. of dumps");
689 cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "dump id");
690 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL,
691 "detailed description");
695 ts = cmd_CreateSyntax("dbverify", bc_dbVerifyCmd, NULL,
696 "check ubik database integrity");
697 cmd_AddParm(ts, "-detail", CMD_FLAG, CMD_OPTIONAL, "additional details");
701 ts = cmd_CreateSyntax("deletedump", bc_deleteDumpCmd, NULL,
702 "delete dumps from the database");
703 cmd_AddParm(ts, "-dumpid", CMD_LIST, CMD_OPTIONAL, "dump id");
704 cmd_AddParm(ts, "-from", CMD_LIST, CMD_OPTIONAL, "date time");
705 cmd_AddParm(ts, "-to", CMD_LIST, CMD_OPTIONAL, "date time");
706 cmd_AddParm(ts, "-port", CMD_SINGLE, CMD_OPTIONAL, "TC port offset");
707 cmd_AddParm(ts, "-groupid", CMD_SINGLE, CMD_OPTIONAL, "group ID");
708 cmd_AddParm(ts, "-dbonly", CMD_FLAG, CMD_OPTIONAL,
709 "delete the dump from the backup database only");
710 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
711 "always delete from backup database");
712 cmd_AddParm(ts, "-noexecute", CMD_FLAG, CMD_OPTIONAL|CMD_HIDDEN, "");
713 cmd_AddParm(ts, "-dryrun", CMD_FLAG, CMD_OPTIONAL,
714 "list the dumps, don't delete anything");
715 cmd_AddParmAlias(ts, 8, "-n");
720 ts = cmd_CreateSyntax("interactive", bc_interactCmd, NULL,
721 "enter interactive mode");
725 * Now execute the command.
729 targv[targc++] = argv[0];
731 targv[targc++] = "interactive";
732 for (i = 1; i < argc; i++)
733 targv[targc++] = argv[i];
735 code = doDispatch(targc, targv, 1);
737 if (!interact || !bcInit) { /* Non-interactive mode */
741 code = bc_WaitForNoJobs(); /* wait for any jobs to finish */
742 exit(code); /* and exit */
745 /* Iterate on command lines, interpreting user commands (interactive mode) */
753 while ((ret = LWP_GetLine(lineBuffer, sizeof(lineBuffer))) == 0)
754 printf("%s: Command line too long\n", whoami); /* line too long */
757 return 0; /* Got EOF */
759 if (!LineIsBlank(lineBuffer)) {
760 code = cmd_ParseLine(lineBuffer, targv, &targc, MAXV);
762 afs_com_err(whoami, code, "; Can't parse line: '%s'",
763 afs_error_message(code));
765 doDispatch(targc, targv, 1);