3 * Bacula Director -- Run Command
5 * Kern Sibbald, December MMI
11 Copyright (C) 2001-2004 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 /* Imported subroutines */
35 /* Imported variables */
36 extern struct s_kw ReplaceOptions[];
39 * For Backup and Verify Jobs
40 * run [job=]<job-name> level=<level-name>
43 * run <job-name> jobid=nn
46 int run_cmd(UAContext *ua, const char *cmd)
49 char *job_name, *level_name, *jid, *store_name, *pool_name;
50 char *where, *fileset_name, *client_name, *bootstrap;
52 char *when, *verify_job_name;
54 int i, j, opt, files = 0;
57 JOB *verify_job = NULL;
59 CLIENT *client = NULL;
60 FILESET *fileset = NULL;
62 static const char *kw[] = { /* command line arguments */
63 N_("job"), /* Used in a switch() */
67 N_("fileset"), /* 4 */
69 N_("storage"), /* 6 */
73 N_("bootstrap"), /* 10 */
74 N_("replace"), /* 11 */
76 N_("priority"), /* 13 */
77 N_("yes"), /* 14 -- if you change this change YES_POS too */
78 N_("verifyjob"), /* 15 */
79 N_("files"), /* 16 number of files to restore */
99 verify_job_name = NULL;
101 for (i=1; i<ua->argc; i++) {
102 Dmsg2(200, "Doing arg %d = %s\n", i, ua->argk[i]);
104 /* Keep looking until we find a good keyword */
105 for (j=0; !kw_ok && kw[j]; j++) {
106 if (strcasecmp(ua->argk[i], _(kw[j])) == 0) {
107 /* Note, yes and run have no value, so do not err */
108 if (!ua->argv[i] && j != YES_POS /*yes*/) {
109 bsendmsg(ua, _("Value missing for keyword %s\n"), ua->argk[i]);
112 Dmsg1(200, "Got keyword=%s\n", kw[j]);
116 bsendmsg(ua, _("Job name specified twice.\n"));
119 job_name = ua->argv[i];
124 bsendmsg(ua, _("JobId specified twice.\n"));
133 bsendmsg(ua, _("Client specified twice.\n"));
136 client_name = ua->argv[i];
139 case 4: /* fileset */
141 bsendmsg(ua, _("FileSet specified twice.\n"));
144 fileset_name = ua->argv[i];
149 bsendmsg(ua, _("Level specified twice.\n"));
152 level_name = ua->argv[i];
155 case 6: /* storage */
158 bsendmsg(ua, _("Storage specified twice.\n"));
161 store_name = ua->argv[i];
166 bsendmsg(ua, _("Pool specified twice.\n"));
169 pool_name = ua->argv[i];
174 bsendmsg(ua, _("Where specified twice.\n"));
180 case 10: /* bootstrap */
182 bsendmsg(ua, _("Bootstrap specified twice.\n"));
185 bootstrap = ua->argv[i];
188 case 11: /* replace */
190 bsendmsg(ua, _("Replace specified twice.\n"));
193 replace = ua->argv[i];
198 bsendmsg(ua, _("When specified twice.\n"));
204 case 13: /* Priority */
206 bsendmsg(ua, _("Priority specified twice.\n"));
209 Priority = atoi(ua->argv[i]);
211 bsendmsg(ua, _("Priority must be positive nonzero setting it to 10.\n"));
219 case 15: /* Verify Job */
220 if (verify_job_name) {
221 bsendmsg(ua, _("Verify Job specified twice.\n"));
224 verify_job_name = ua->argv[i];
228 files = atoi(ua->argv[i]);
235 } /* end strcase compare */
236 } /* end keyword loop */
238 * End of keyword for loop -- if not found, we got a bogus keyword
241 Dmsg1(200, "%s not found\n", ua->argk[i]);
243 * Special case for Job Name, it can be the first
244 * keyword that has no value.
246 if (!job_name && !ua->argv[i]) {
247 job_name = ua->argk[i]; /* use keyword as job name */
248 Dmsg1(200, "Set jobname=%s\n", job_name);
250 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
254 } /* end argc loop */
256 Dmsg0(200, "Done scan.\n");
260 job = (JOB *)GetResWithName(R_JOB, job_name);
262 if (*job_name != 0) {
263 bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
265 job = select_job_resource(ua);
267 Dmsg1(200, "Found job=%s\n", job_name);
270 bsendmsg(ua, _("A job name must be specified.\n"));
271 job = select_job_resource(ua);
278 store = (STORE *)GetResWithName(R_STORAGE, store_name);
280 if (*store_name != 0) {
281 bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
283 store = select_storage_resource(ua);
286 store = job->storage; /* use default */
294 pool = (POOL *)GetResWithName(R_POOL, pool_name);
296 if (*pool_name != 0) {
297 bsendmsg(ua, _("Pool \"%s\" not found.\n"), pool_name);
299 pool = select_pool_resource(ua);
302 pool = job->pool; /* use default */
309 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
311 if (*client_name != 0) {
312 bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
314 client = select_client_resource(ua);
317 client = job->client; /* use default */
324 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
326 bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
327 fileset = select_fileset_resource(ua);
330 fileset = job->fileset; /* use default */
336 if (verify_job_name) {
337 verify_job = (JOB *)GetResWithName(R_JOB, verify_job_name);
339 bsendmsg(ua, _("Verify Job \"%s\" not found.\n"), verify_job_name);
340 verify_job = select_job_resource(ua);
343 verify_job = job->verify_job;
347 * Create JCR to run job. NOTE!!! after this point, free_jcr()
350 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
351 set_jcr_defaults(jcr, job);
354 jcr->client = client;
355 jcr->fileset = fileset;
357 jcr->ExpectedFiles = files;
362 jcr->where = bstrdup(where);
366 jcr->sched_time = str_to_utime(when);
367 if (jcr->sched_time == 0) {
368 bsendmsg(ua, _("Invalid time, using current time.\n"));
369 jcr->sched_time = time(NULL);
374 if (jcr->RestoreBootstrap) {
375 free(jcr->RestoreBootstrap);
377 jcr->RestoreBootstrap = bstrdup(bootstrap);
382 for (i=0; ReplaceOptions[i].name; i++) {
383 if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
384 jcr->replace = ReplaceOptions[i].token;
388 bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
391 } else if (job->replace) {
392 jcr->replace = job->replace;
394 jcr->replace = REPLACE_ALWAYS;
398 jcr->JobPriority = Priority;
402 replace = ReplaceOptions[0].name;
403 for (i=0; ReplaceOptions[i].name; i++) {
404 if (ReplaceOptions[i].token == jcr->replace) {
405 replace = ReplaceOptions[i].name;
409 if (!get_level_from_name(jcr, level_name)) {
410 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
415 jcr->RestoreJobId = atoi(jid);
418 /* Run without prompting? */
419 if (find_arg(ua, _("yes")) > 0) {
424 * Prompt User to see if all run job parameters are correct, and
425 * allow him to modify them.
427 Dmsg1(20, "JobType=%c\n", jcr->JobType);
428 switch (jcr->JobType) {
430 char dt[MAX_TIME_LENGTH];
432 bsendmsg(ua, _("Run %s job\n\
441 jcr->fileset->hdr.name,
442 NPRT(jcr->client->hdr.name),
443 NPRT(jcr->store->hdr.name),
444 bstrutime(dt, sizeof(dt), jcr->sched_time),
446 jcr->JobLevel = L_FULL;
450 if (jcr->JobType == JT_BACKUP) {
451 bsendmsg(ua, _("Run %s job\n\
462 jcr->fileset->hdr.name,
463 level_to_str(jcr->JobLevel),
464 jcr->client->hdr.name,
465 jcr->store->hdr.name,
466 NPRT(jcr->pool->hdr.name),
467 bstrutime(dt, sizeof(dt), jcr->sched_time),
469 } else { /* JT_VERIFY */
471 if (jcr->job->verify_job) {
472 Name = jcr->job->verify_job->hdr.name;
476 bsendmsg(ua, _("Run %s job\n\
488 jcr->fileset->hdr.name,
489 level_to_str(jcr->JobLevel),
490 jcr->client->hdr.name,
491 jcr->store->hdr.name,
492 NPRT(jcr->pool->hdr.name),
494 bstrutime(dt, sizeof(dt), jcr->sched_time),
499 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
501 jcr->RestoreJobId = atoi(jid);
503 if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
506 jcr->RestoreJobId = ua->pint32_val;
509 jcr->JobLevel = L_FULL; /* default level */
510 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
511 if (jcr->RestoreJobId == 0) {
512 bsendmsg(ua, _("Run Restore job\n\
523 NPRT(jcr->RestoreBootstrap),
524 jcr->where?jcr->where:NPRT(job->RestoreWhere),
526 jcr->fileset->hdr.name,
527 jcr->client->hdr.name,
528 jcr->store->hdr.name,
529 bstrutime(dt, sizeof(dt), jcr->sched_time),
532 bsendmsg(ua, _("Run Restore job\n\
544 NPRT(jcr->RestoreBootstrap),
545 jcr->where?jcr->where:NPRT(job->RestoreWhere),
547 jcr->fileset->hdr.name,
548 jcr->client->hdr.name,
549 jcr->store->hdr.name,
550 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1),
551 bstrutime(dt, sizeof(dt), jcr->sched_time),
556 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
561 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
565 * At user request modify parameters of job to be run.
567 if (ua->cmd[0] == 0) {
570 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
573 start_prompt(ua, _("Parameters to modify:\n"));
574 add_prompt(ua, _("Level")); /* 0 */
575 add_prompt(ua, _("Storage")); /* 1 */
576 add_prompt(ua, _("Job")); /* 2 */
577 add_prompt(ua, _("FileSet")); /* 3 */
578 add_prompt(ua, _("Client")); /* 4 */
579 add_prompt(ua, _("When")); /* 5 */
580 add_prompt(ua, _("Priority")); /* 6 */
581 if (jcr->JobType == JT_BACKUP ||
582 jcr->JobType == JT_VERIFY) {
583 add_prompt(ua, _("Pool")); /* 7 */
584 if (jcr->JobType == JT_VERIFY) {
585 add_prompt(ua, _("Verify Job")); /* 8 */
587 } else if (jcr->JobType == JT_RESTORE) {
588 add_prompt(ua, _("Bootstrap")); /* 7 */
589 add_prompt(ua, _("Where")); /* 8 */
590 add_prompt(ua, _("Replace")); /* 9 */
591 add_prompt(ua, _("JobId")); /* 10 */
593 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
596 if (jcr->JobType == JT_BACKUP) {
597 start_prompt(ua, _("Levels:\n"));
598 add_prompt(ua, _("Base"));
599 add_prompt(ua, _("Full"));
600 add_prompt(ua, _("Incremental"));
601 add_prompt(ua, _("Differential"));
602 add_prompt(ua, _("Since"));
603 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
605 jcr->JobLevel = L_BASE;
608 jcr->JobLevel = L_FULL;
611 jcr->JobLevel = L_INCREMENTAL;
614 jcr->JobLevel = L_DIFFERENTIAL;
617 jcr->JobLevel = L_SINCE;
623 } else if (jcr->JobType == JT_VERIFY) {
624 start_prompt(ua, _("Levels:\n"));
625 add_prompt(ua, _("Initialize Catalog"));
626 add_prompt(ua, _("Verify Catalog"));
627 add_prompt(ua, _("Verify Volume to Catalog"));
628 add_prompt(ua, _("Verify Disk to Catalog"));
629 add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
630 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
632 jcr->JobLevel = L_VERIFY_INIT;
635 jcr->JobLevel = L_VERIFY_CATALOG;
638 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
641 jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
644 jcr->JobLevel = L_VERIFY_DATA;
651 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
656 store = select_storage_resource(ua);
664 job = select_job_resource(ua);
667 set_jcr_defaults(jcr, job);
673 fileset = select_fileset_resource(ua);
675 jcr->fileset = fileset;
681 client = select_client_resource(ua);
683 jcr->client = client;
689 if (!get_cmd(ua, _("Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now): "))) {
692 if (ua->cmd[0] == 0) {
693 jcr->sched_time = time(NULL);
695 jcr->sched_time = str_to_utime(ua->cmd);
696 if (jcr->sched_time == 0) {
697 bsendmsg(ua, _("Invalid time, using current time.\n"));
698 jcr->sched_time = time(NULL);
704 if (!get_pint(ua, _("Enter new Priority: "))) {
707 if (ua->pint32_val == 0) {
708 bsendmsg(ua, _("Priority must be a positive integer.\n"));
710 jcr->JobPriority = ua->pint32_val;
714 /* Pool or Bootstrap depending on JobType */
715 if (jcr->JobType == JT_BACKUP ||
716 jcr->JobType == JT_VERIFY) { /* Pool */
717 pool = select_pool_resource(ua);
726 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
729 if (jcr->RestoreBootstrap) {
730 free(jcr->RestoreBootstrap);
731 jcr->RestoreBootstrap = NULL;
733 if (ua->cmd[0] != 0) {
734 jcr->RestoreBootstrap = bstrdup(ua->cmd);
735 fd = fopen(jcr->RestoreBootstrap, "r");
737 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
738 jcr->RestoreBootstrap, strerror(errno));
739 free(jcr->RestoreBootstrap);
740 jcr->RestoreBootstrap = NULL;
748 if (jcr->JobType == JT_VERIFY) {
749 JOB *job = select_job_resource(ua);
751 jcr->job->verify_job = job;
753 jcr->job->verify_job = NULL;
758 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
765 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
768 jcr->where = bstrdup(ua->cmd);
772 start_prompt(ua, _("Replace:\n"));
773 for (i=0; ReplaceOptions[i].name; i++) {
774 add_prompt(ua, ReplaceOptions[i].name);
776 opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
778 jcr->replace = ReplaceOptions[opt].token;
783 jid = NULL; /* force reprompt */
784 jcr->RestoreJobId = 0;
785 if (jcr->RestoreBootstrap) {
786 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
795 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
797 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
799 JobId = run_job(jcr);
800 free_jcr(jcr); /* release jcr */
802 bsendmsg(ua, _("Job failed.\n"));
804 bsendmsg(ua, _("Job started. JobId=%u\n"), JobId);
810 bsendmsg(ua, _("Job not run.\n"));
812 return 0; /* do not run */