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>
17 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
32 #include <rx/rx_globals.h>
34 #include <afs/bubasics.h>
36 #include <afs/afsutil.h>
38 #include <afs/cellconfig.h>
43 #include <afs/volser.h> /*VLDB_MAXSERVERS */
44 #include <afs/com_err.h>
48 #include "bc.h" /*Backup Coordinator structs and defs */
51 int localauth, interact;
54 extern int bc_AddDumpCmd();
55 extern int bc_AddHostCmd();
56 extern int bc_AddVolEntryCmd();
57 extern int bc_AddVolSetCmd();
58 /* extern int bc_CheckDumpStatCmd(); */
59 extern int bc_DeleteDumpCmd();
60 extern int bc_DeleteHostCmd();
61 extern int bc_DeleteVolEntryCmd();
62 extern int bc_DeleteVolSetCmd();
63 extern int bc_DiskRestoreCmd();
64 extern int bc_VolsetRestoreCmd();
65 extern int bc_DumpCmd();
66 extern int bc_GetTapeStatusCmd();
67 extern int bc_JobsCmd();
68 extern int bc_KillCmd();
69 extern int bc_LabelTapeCmd();
70 extern int bc_ListDumpScheduleCmd();
71 extern int bc_ListHostsCmd();
72 extern int bc_ListVolSetCmd();
73 extern int bc_QuitCmd();
74 extern int bc_ReadLabelCmd();
75 extern int bc_ScanDumpsCmd();
76 extern int bc_SetExpCmd();
77 extern int bc_VolRestoreCmd();
78 extern int bc_dblookupCmd();
79 extern int bc_dbVerifyCmd();
80 extern int bc_deleteDumpCmd();
81 extern int bc_dumpInfoCmd();
82 extern int bc_restoreDbCmd();
83 extern int bc_saveDbCmd();
86 * Global configuration information for the Backup Coordinator.
88 struct bc_config *bc_globalConfig; /*Ptr to global BC configuration info */
90 struct ubik_client *cstruct; /* Ptr to Ubik client structure */
91 struct ktc_token ttoken; /* The token */
93 static const char *DefaultConfDir; /*Default backup config directory */
94 static int bcInit = 0; /* backupInit called yet ? */
95 char *whoami = "backup";
97 /* dummy routine for the audit work. It should do nothing since audits */
98 /* occur at the server level and bos is not a server. */
105 * Initialize all the error tables that may be used by com_err
111 initialize_ACFG_error_table();
112 initialize_KA_error_table();
113 initialize_RXK_error_table();
114 initialize_CMD_error_table();
115 initialize_VL_error_table();
116 initialize_BUTM_error_table();
117 initialize_VOLS_error_table();
118 initialize_BUTC_error_table();
119 initialize_BUTX_error_table();
120 initialize_BUDB_error_table();
121 initialize_BUCD_error_table();
122 initialize_KTC_error_table();
126 * got to account for the errors which are volume related but
127 * not dealt with by standard errno and com_err stuff.
133 if (((code <= VMOVED) && (code >= VSALVAGE)) || (code < 0)) {
136 fprintf(STDERR, "Possible communication failure\n");
139 fprintf(STDERR, "Volume needs salvage\n");
142 fprintf(STDERR, "Bad vnode number quoted\n");
146 "Volume not attached, does not exist, or not on line\n");
149 fprintf(STDERR, "Volume already exists\n");
152 fprintf(STDERR, "Volume is not in service\n");
155 fprintf(STDERR, "Volume is off line\n");
158 fprintf(STDERR, "Volume is already on line\n");
161 fprintf(STDERR, "Partition is full\n");
164 fprintf(STDERR, "Volume max quota exceeded\n");
167 fprintf(STDERR, "Volume temporarily unavailable\n");
170 fprintf(STDERR, "Volume has moved to another server\n");
179 /* Return true if line is all whitespace */
182 register char *aline;
186 while (tc = *aline++)
187 if ((tc != ' ') && (tc != '\t') && (tc != '\n'))
195 * initialize configuration information that is stored as text blocks
201 udbClientTextP ctPtr;
205 extern struct bc_config *bc_globalConfig;
207 mkdir(DefaultConfDir, 777); /* temporary */
209 /* initialize the client text structures */
210 ctPtr = &bc_globalConfig->configText[0];
212 for (i = 0; i < TB_NUM; i++) {
213 memset(ctPtr, 0, sizeof(*ctPtr));
215 ctPtr->textVersion = -1;
222 /*----------------------------------------------------------------------------
226 * Routine that is called when the backup coordinator is invoked, responsible for
227 * basic initialization and some command line parsing.
230 * Zero (but may exit the entire program on error!)
233 * Nothing interesting.
236 * Initializes this program.
237 *----------------------------------------------------------------------------
243 register afs_int32 code;
244 static int initd = 0; /* ever called? */
246 PROCESS pid; /* LWP process ID */
248 extern statusWatcher();
251 initialize_CMD_error_table();
253 /* don't run more than once */
255 com_err(whoami, 0, "Backup already initialized.");
260 code = bc_InitConfig(DefaultConfDir);
262 com_err(whoami, code,
263 "Can't initialize from config files in directory '%s'",
271 code = LWP_InitializeProcessSupport(LWP_NORMAL_PRIORITY, &pid);
273 com_err(whoami, code, "; Can't initialize LWP");
277 code = rx_Init(htons(0));
279 com_err(whoami, code, "; Can't initialize Rx");
283 rx_SetRxDeadTime(60);
285 /* VLDB initialization */
286 code = vldbClientInit(0, localauth, tcell, &cstruct, &ttoken);
290 /* Backup database initialization */
291 code = udbClientInit(0, localauth, tcell);
295 /* setup status monitoring thread */
298 LWP_CreateProcess(statusWatcher, 20480, LWP_NORMAL_PRIORITY,
299 (void *)2, "statusWatcher", &watcherPid);
301 com_err(whoami, code, "; Can't create status monitor task");
308 /*----------------------------------------------------------------------------
312 * Make sure we mark down when we need to exit the Backup Coordinator.
313 * Specifically, we don't want to continue when our help and apropos
314 * routines are called from the command line.
317 * as : Ptr to the command syntax descriptor.
323 * This routine is called right before each of the Backup Coordinator
324 * opcode routines are invoked.
326 *----------------------------------------------------------------------------
331 register struct cmd_syndesc *as;
335 /* Handling the command line opcode */
337 localauth = ((as && as->parms[14].items) ? 1 : 0);
338 if (as && as->parms[15].items)
339 strcpy(tcell, as->parms[15].items->data);
345 com_err(whoami, code, "; Can't initialize backup");
349 /* Get initial information from the database */
350 code = bc_InitTextConfig();
352 com_err(whoami, code,
353 "; Can't obtain configuration text from backup database");
365 #include "AFS_component_version_number.c"
367 #define MAXRECURSION 20
368 extern int dontExecute; /* declared in commands.c */
369 extern char *loadFile; /* declared in commands.c */
370 char lineBuffer[1024]; /* Line typed in by user or read from load file */
373 * This will dispatch a command. It holds a recursive loop for the
374 * "dump -file" option. This option reads backup commands from a file.
376 * Cannot put this code on other side of cmd_Dispatch call (in
377 * commands.c) because when make a dispatch call when in a dispatch
378 * call, environment is mucked up.
380 * To avoid multiple processes stepping on each other in the dispatch code,
381 * put a lock around it so only 1 process gets in at a time.
384 struct Lock dispatchLock; /* lock on the Dispatch call */
385 #define lock_Dispatch() ObtainWriteLock(&dispatchLock)
386 #define unlock_Dispatch() ReleaseWriteLock(&dispatchLock)
389 doDispatch(targc, targv, dispatchCount)
392 afs_int32 dispatchCount; /* to prevent infinite recursion */
400 int noExecute; /* local capy of global variable */
401 char *internalLoadFile;
406 code = cmd_Dispatch(targc, targv);
407 internalLoadFile = loadFile;
411 if (internalLoadFile) { /* Load a file in */
412 if (dispatchCount > MAXRECURSION) { /* Beware recursive loops. */
413 com_err(whoami, 0, "Potential recursion: will not load file %s",
419 fd = fopen(internalLoadFile, "r"); /* Open the load file */
421 com_err(whoami, errno, "; Cannot open file %s", internalLoadFile);
426 noExecute = dontExecute;
428 printf("Would have executed the following commands:\n");
431 while (fgets(lineBuffer, sizeof(lineBuffer) - 1, fd)) { /* Get commands from file */
434 i = strlen(lineBuffer) - 1;
435 if (lineBuffer[i] == '\n') /* Drop return at end of line */
436 lineBuffer[i] = '\0';
439 printf(" %s\n", lineBuffer); /* echo */
441 printf("------> %s\n", lineBuffer); /* echo */
443 if (!LineIsBlank(lineBuffer) && /* Skip if blank line */
444 (lineBuffer[0] != '#') && /* or comment */
445 (!noExecute)) { /* or no execute */
446 c = cmd_ParseLine(lineBuffer, sargv, &sargc, MAXV);
448 com_err(whoami, c, "; Can't parse line");
450 doDispatch(sargc, sargv, dispatchCount + 1); /* Recursive - ignore error */
451 cmd_FreeArgv(sargv); /* Free up arguments */
460 if (internalLoadFile)
461 free(internalLoadFile);
466 bc_interactCmd(as, arock)
467 struct cmd_syndesc *as;
476 register struct cmd_syndesc *ts;
479 cmd_AddParm(ts, "-localauth", CMD_FLAG, CMD_OPTIONAL,
480 "local authentication");
481 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
489 char *targv[MAXV]; /*Ptr to parsed argv stuff */
490 afs_int32 targc; /*Num parsed arguments */
491 afs_int32 code; /*Return code */
492 register struct cmd_syndesc *ts; /*Ptr to parsed command line */
498 * The following signal action for AIX is necessary so that in case of a
499 * crash (i.e. core is generated) we can include the user's data section
500 * in the core dump. Unfortunately, by default, only a partial core is
501 * generated which, in many cases, isn't too useful.
503 struct sigaction nsa;
505 sigemptyset(&nsa.sa_mask);
506 nsa.sa_handler = SIG_DFL;
507 nsa.sa_flags = SA_FULLDUMP;
508 sigaction(SIGSEGV, &nsa, NULL);
510 Lock_Init(&dispatchLock);
511 InitErrTabs(); /* init all the error tables which may be used */
513 /* setup the default backup dir */
514 DefaultConfDir = AFSDIR_SERVER_BACKUP_DIRPATH;
515 /* Get early warning if the command is interacive mode or not */
516 interact = (((argc < 2) || (argv[1][0] == '-')) ? 1 : 0);
518 cmd_SetBeforeProc(MyBeforeProc, NULL);
520 ts = cmd_CreateSyntax("dump", bc_DumpCmd, 0, "start dump");
521 cmd_AddParm(ts, "-volumeset", CMD_SINGLE, CMD_OPTIONAL,
523 cmd_AddParm(ts, "-dump", CMD_SINGLE, CMD_OPTIONAL, "dump level name");
524 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
526 cmd_AddParm(ts, "-at", CMD_LIST, CMD_OPTIONAL, "Date/time to start dump");
527 cmd_AddParm(ts, "-append", CMD_FLAG, CMD_OPTIONAL,
528 "append to existing dump set");
529 cmd_AddParm(ts, "-n", CMD_FLAG, CMD_OPTIONAL, "don't really execute it");
530 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "load file");
534 ts = cmd_CreateSyntax("volrestore", bc_VolRestoreCmd, 0,
536 cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED,
537 "destination machine");
538 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED,
539 "destination partition");
540 cmd_AddParm(ts, "-volume", CMD_LIST, CMD_REQUIRED,
541 "volume(s) to restore");
542 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL,
543 "new volume name extension");
544 cmd_AddParm(ts, "-date", CMD_LIST, CMD_OPTIONAL,
545 "date from which to restore");
546 cmd_AddParm(ts, "-portoffset", CMD_LIST, CMD_OPTIONAL, "TC port offsets");
547 cmd_AddParm(ts, "-n", CMD_FLAG, CMD_OPTIONAL, "don't really execute it");
551 ts = cmd_CreateSyntax("diskrestore", bc_DiskRestoreCmd, 0,
552 "restore partition");
553 cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED,
554 "machine to restore");
555 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED,
556 "partition to restore");
557 cmd_AddParm(ts, "-portoffset", CMD_LIST, CMD_OPTIONAL, "TC port offset");
559 cmd_AddParm(ts, "-newserver", CMD_SINGLE, CMD_OPTIONAL,
560 "destination machine");
561 cmd_AddParm(ts, "-newpartition", CMD_SINGLE, CMD_OPTIONAL,
562 "destination partition");
563 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL,
564 "new volume name extension");
565 cmd_AddParm(ts, "-n", CMD_FLAG, CMD_OPTIONAL, "don't really execute it");
569 ts = cmd_CreateSyntax("quit", bc_QuitCmd, 0, "leave the program");
571 ts = cmd_CreateSyntax("volsetrestore", bc_VolsetRestoreCmd, 0,
572 "restore a set of volumes");
573 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "volume set name");
574 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "file name");
575 cmd_AddParm(ts, "-portoffset", CMD_LIST, CMD_OPTIONAL, "TC port offset");
576 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL,
577 "new volume name extension");
578 cmd_AddParm(ts, "-n", CMD_FLAG, CMD_OPTIONAL, "don't really execute it");
582 ts = cmd_CreateSyntax("addhost", bc_AddHostCmd, 0, "add host to config");
583 cmd_AddParm(ts, "-tapehost", CMD_SINGLE, CMD_REQUIRED,
584 "tape machine name");
585 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
590 ts = cmd_CreateSyntax("delhost", bc_DeleteHostCmd, 0,
591 "delete host to config");
592 cmd_AddParm(ts, "-tapehost", CMD_SINGLE, CMD_REQUIRED,
593 "tape machine name");
594 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
599 ts = cmd_CreateSyntax("listhosts", bc_ListHostsCmd, 0,
600 "list config hosts");
604 ts = cmd_CreateSyntax("jobs", bc_JobsCmd, 0, "list running jobs");
606 ts = cmd_CreateSyntax("kill", bc_KillCmd, 0, "kill running job");
607 cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_REQUIRED,
608 "job ID or dump set name");
610 ts = cmd_CreateSyntax("listvolsets", bc_ListVolSetCmd, 0,
612 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL, "volume set name");
616 ts = cmd_CreateSyntax("listdumps", bc_ListDumpScheduleCmd, 0,
617 "list dump schedules");
621 ts = cmd_CreateSyntax("addvolset", bc_AddVolSetCmd, 0,
622 "create a new volume set");
623 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_REQUIRED, "volume set name");
624 cmd_AddParm(ts, "-temporary", CMD_FLAG, CMD_OPTIONAL,
625 "temporary volume set");
629 ts = cmd_CreateSyntax("status", bc_GetTapeStatusCmd, 0,
630 "get tape coordinator status");
631 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
636 ts = cmd_CreateSyntax("delvolset", bc_DeleteVolSetCmd, 0,
637 "delete a volume set");
638 cmd_AddParm(ts, "-name", CMD_LIST, CMD_REQUIRED, "volume set name");
642 ts = cmd_CreateSyntax("addvolentry", bc_AddVolEntryCmd, 0,
643 "add a new volume entry");
644 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_REQUIRED, "volume set name");
645 cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED, "machine name");
646 cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED, "partition name");
647 cmd_AddParm(ts, "-volumes", CMD_SINGLE, CMD_REQUIRED,
648 "volume name (regular expression)");
652 ts = cmd_CreateSyntax("delvolentry", bc_DeleteVolEntryCmd, 0,
653 "delete a volume set sub-entry");
654 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_REQUIRED, "volume set name");
655 cmd_AddParm(ts, "-entry", CMD_SINGLE, CMD_REQUIRED, "volume set index");
659 ts = cmd_CreateSyntax("adddump", bc_AddDumpCmd, 0, "add dump schedule");
660 cmd_AddParm(ts, "-dump", CMD_LIST, CMD_REQUIRED, "dump level name");
661 cmd_AddParm(ts, "-expires", CMD_LIST, CMD_OPTIONAL, "expiration date");
665 ts = cmd_CreateSyntax("deldump", bc_DeleteDumpCmd, 0,
666 "delete dump schedule");
667 cmd_AddParm(ts, "-dump", CMD_SINGLE, CMD_REQUIRED, "dump level name");
671 ts = cmd_CreateSyntax("labeltape", bc_LabelTapeCmd, 0, "label a tape");
672 cmd_AddParm(ts, "-name", CMD_SINGLE, CMD_OPTIONAL,
673 "AFS tape name, defaults to NULL");
674 cmd_AddParm(ts, "-size", CMD_SINGLE, CMD_OPTIONAL,
675 "tape size in Kbytes, defaults to size in tapeconfig");
676 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
678 cmd_AddParm(ts, "-pname", CMD_SINGLE, CMD_OPTIONAL,
679 "permanent tape name");
683 ts = cmd_CreateSyntax("readlabel", bc_ReadLabelCmd, 0,
684 "read the label on tape");
685 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
690 ts = cmd_CreateSyntax("scantape", bc_ScanDumpsCmd, 0,
691 "dump information recovery from tape");
692 cmd_AddParm(ts, "-dbadd", CMD_FLAG, CMD_OPTIONAL,
693 "add information to the database");
694 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
699 ts = cmd_CreateSyntax("volinfo", bc_dblookupCmd, 0,
700 "query the backup database");
701 cmd_AddParm(ts, "-volume", CMD_SINGLE, CMD_REQUIRED, "volume name");
705 ts = cmd_CreateSyntax("setexp", bc_SetExpCmd, 0,
706 "set/clear dump expiration dates");
707 cmd_AddParm(ts, "-dump", CMD_LIST, CMD_REQUIRED, "dump level name");
708 cmd_AddParm(ts, "-expires", CMD_LIST, CMD_OPTIONAL, "expiration date");
712 ts = cmd_CreateSyntax("savedb", bc_saveDbCmd, 0, "save backup database");
713 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
715 cmd_AddParm(ts, "-archive", CMD_LIST, CMD_OPTIONAL, "date time");
719 ts = cmd_CreateSyntax("restoredb", bc_restoreDbCmd, 0,
720 "restore backup database");
721 cmd_AddParm(ts, "-portoffset", CMD_SINGLE, CMD_OPTIONAL,
726 ts = cmd_CreateSyntax("dumpinfo", bc_dumpInfoCmd, 0,
727 "provide information about a dump in the database");
728 cmd_AddParm(ts, "-ndumps", CMD_SINGLE, CMD_OPTIONAL, "no. of dumps");
729 cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "dump id");
730 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL,
731 "detailed description");
735 ts = cmd_CreateSyntax("dbverify", bc_dbVerifyCmd, 0,
736 "check ubik database integrity");
737 cmd_AddParm(ts, "-detail", CMD_FLAG, CMD_OPTIONAL, "additional details");
741 ts = cmd_CreateSyntax("deletedump", bc_deleteDumpCmd, 0,
742 "delete dumps from the database");
743 cmd_AddParm(ts, "-dumpid", CMD_LIST, CMD_OPTIONAL, "dump id");
744 cmd_AddParm(ts, "-from", CMD_LIST, CMD_OPTIONAL, "date time");
745 cmd_AddParm(ts, "-to", CMD_LIST, CMD_OPTIONAL, "date time");
746 cmd_AddParm(ts, "-port", CMD_SINGLE, CMD_OPTIONAL, "TC port offset");
747 cmd_AddParm(ts, "-groupid", CMD_SINGLE, CMD_OPTIONAL, "group ID");
748 cmd_AddParm(ts, "-dbonly", CMD_FLAG, CMD_OPTIONAL,
749 "delete the dump from the backup database only");
750 cmd_AddParm(ts, "-force", CMD_FLAG, CMD_OPTIONAL,
751 "always delete from backup database");
752 cmd_AddParm(ts, "-noexecute", CMD_FLAG, CMD_OPTIONAL,
753 "Just list the dumps");
758 ts = cmd_CreateSyntax("interactive", bc_interactCmd, 0,
759 "enter interactive mode");
763 * Now execute the command.
767 targv[targc++] = argv[0];
769 targv[targc++] = "interactive";
770 for (i = 1; i < argc; i++)
771 targv[targc++] = argv[i];
773 code = doDispatch(targc, targv, 1);
775 if (!interact || !bcInit) { /* Non-interactive mode */
779 code = bc_WaitForNoJobs(); /* wait for any jobs to finish */
780 exit(code); /* and exit */
783 /* Iterate on command lines, interpreting user commands (interactive mode) */
791 while ((ret = LWP_GetLine(lineBuffer, sizeof(lineBuffer))) == 0)
792 printf("%s: Command line too long\n", whoami); /* line too long */
795 return 0; /* Got EOF */
797 if (!LineIsBlank(lineBuffer)) {
798 code = cmd_ParseLine(lineBuffer, targv, &targc, MAXV);
800 com_err(whoami, code, "; Can't parse line: '%s'",
801 error_message(code));
803 doDispatch(targc, targv, 1);