3 * Bacula Director -- Run Command
5 * Kern Sibbald, December MMI
11 Copyright (C) 2001-2003 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_jl joblevels[];
37 extern struct s_kw ReplaceOptions[];
40 * For Backup and Verify Jobs
41 * run [job=]<job-name> level=<level-name>
44 * run <job-name> jobid=nn
47 int run_cmd(UAContext *ua, char *cmd)
50 char *job_name, *level_name, *jid, *store_name, *pool_name;
51 char *where, *fileset_name, *client_name, *bootstrap, *replace;
52 char *when, *verify_job_name;
57 JOB *verify_job = NULL;
59 CLIENT *client = NULL;
60 FILESET *fileset = NULL;
62 static char *kw[] = { /* command line arguments */
75 N_("yes"), /* 12 -- if you change this change YES_POS too */
76 N_("run"), /* 13 -- if you change this change RUN_POS too */
98 verify_job_name = NULL;
100 for (i=1; i<ua->argc; i++) {
102 Dmsg2(200, "Doing arg %d = %s\n", i, ua->argk[i]);
103 for (j=0; !found && kw[j]; j++) {
104 if (strcasecmp(ua->argk[i], _(kw[j])) == 0) {
105 /* Note, yes and run have no value, so do not err */
106 if (!ua->argv[i] && (j != YES_POS /*yes*/ && j != RUN_POS)) {
107 bsendmsg(ua, _("Value missing for keyword %s\n"), ua->argk[i]);
110 Dmsg1(200, "Got keyword=%s\n", kw[j]);
114 bsendmsg(ua, _("Job name specified twice.\n"));
117 job_name = ua->argv[i];
122 bsendmsg(ua, _("JobId specified twice.\n"));
130 bsendmsg(ua, _("Client specified twice.\n"));
133 client_name = ua->argv[i];
136 case 3: /* fileset */
138 bsendmsg(ua, _("FileSet specified twice.\n"));
141 fileset_name = ua->argv[i];
146 bsendmsg(ua, _("Level specified twice.\n"));
149 level_name = ua->argv[i];
152 case 5: /* storage */
154 bsendmsg(ua, _("Storage specified twice.\n"));
157 store_name = ua->argv[i];
162 bsendmsg(ua, _("Pool specified twice.\n"));
165 pool_name = ua->argv[i];
170 bsendmsg(ua, _("Where specified twice.\n"));
176 case 8: /* bootstrap */
178 bsendmsg(ua, _("Bootstrap specified twice.\n"));
181 bootstrap = ua->argv[i];
184 case 9: /* replace */
186 bsendmsg(ua, _("Replace specified twice.\n"));
189 replace = ua->argv[i];
194 bsendmsg(ua, _("When specified twice.\n"));
200 case 11: /* Priority */
202 bsendmsg(ua, _("Priority specified twice.\n"));
205 Priority = atoi(ua->argv[i]);
207 bsendmsg(ua, _("Priority must be positive nonzero setting it to 10.\n"));
215 case 14: /* Verify Job */
216 if (verify_job_name) {
217 bsendmsg(ua, _("Verify Job specified twice.\n"));
220 verify_job_name = ua->argv[i];
227 } /* end strcase compare */
228 } /* end keyword loop */
230 Dmsg1(200, "%s not found\n", ua->argk[i]);
232 * Special case for Job Name, it can be the first
233 * keyword that has no value.
235 if (!job_name && !ua->argv[i]) {
236 job_name = ua->argk[i]; /* use keyword as job name */
237 Dmsg1(200, "Set jobname=%s\n", job_name);
239 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
243 } /* end argc loop */
245 Dmsg0(200, "Done scan.\n");
251 job = (JOB *)GetResWithName(R_JOB, job_name);
253 bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
254 job = select_job_resource(ua);
256 Dmsg1(200, "Found job=%s\n", job_name);
259 bsendmsg(ua, _("A job name must be specified.\n"));
260 job = select_job_resource(ua);
267 store = (STORE *)GetResWithName(R_STORAGE, store_name);
269 bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
270 store = select_storage_resource(ua);
273 store = job->storage; /* use default */
281 pool = (POOL *)GetResWithName(R_POOL, pool_name);
283 bsendmsg(ua, _("Pool \"%s\" not found.\n"), pool_name);
284 pool = get_pool_resource(ua);
287 pool = job->pool; /* use default */
291 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
293 bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
294 client = select_client_resource(ua);
297 client = job->client; /* use default */
304 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
306 bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
307 fileset = select_fileset_resource(ua);
310 fileset = job->fileset; /* use default */
316 if (verify_job_name) {
317 verify_job = (JOB *)GetResWithName(R_JOB, verify_job_name);
319 bsendmsg(ua, _("Verify Job \"%s\" not found.\n"), verify_job_name);
320 verify_job = select_job_resource(ua);
323 verify_job = job->verify_job;
326 /* Create JCR to run job */
327 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
328 set_jcr_defaults(jcr, job);
331 jcr->client = client;
332 jcr->fileset = fileset;
338 jcr->where = bstrdup(where);
342 jcr->sched_time = str_to_utime(when);
343 if (jcr->sched_time == 0) {
344 bsendmsg(ua, _("Invalid time, using current time.\n"));
345 jcr->sched_time = time(NULL);
350 if (jcr->RestoreBootstrap) {
351 free(jcr->RestoreBootstrap);
353 jcr->RestoreBootstrap = bstrdup(bootstrap);
358 for (i=0; ReplaceOptions[i].name; i++) {
359 if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
360 jcr->replace = ReplaceOptions[i].token;
364 bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
367 } else if (job->replace) {
368 jcr->replace = job->replace;
370 jcr->replace = REPLACE_ALWAYS;
374 jcr->JobPriority = Priority;
378 replace = ReplaceOptions[0].name;
379 for (i=0; ReplaceOptions[i].name; i++) {
380 if (ReplaceOptions[i].token == jcr->replace) {
381 replace = ReplaceOptions[i].name;
384 Dmsg1(20, "JobType=%c\n", jcr->JobType);
385 switch (jcr->JobType) {
387 char dt[MAX_TIME_LENGTH];
389 bsendmsg(ua, _("Run %s job\n\
398 jcr->fileset->hdr.name,
399 NPRT(jcr->client->hdr.name),
400 NPRT(jcr->store->hdr.name),
401 bstrutime(dt, sizeof(dt), jcr->sched_time),
403 jcr->JobLevel = L_FULL;
408 /* Look up level name and pull code */
410 for (i=0; joblevels[i].level_name; i++) {
411 if (strcasecmp(level_name, _(joblevels[i].level_name)) == 0) {
412 jcr->JobLevel = joblevels[i].level;
418 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
424 if (jcr->JobType == JT_BACKUP) {
425 bsendmsg(ua, _("Run %s job\n\
436 jcr->fileset->hdr.name,
437 level_to_str(jcr->JobLevel),
438 jcr->client->hdr.name,
439 jcr->store->hdr.name,
440 NPRT(jcr->pool->hdr.name),
441 bstrutime(dt, sizeof(dt), jcr->sched_time),
443 } else { /* JT_VERIFY */
445 if (jcr->job->verify_job) {
446 Name = jcr->job->verify_job->hdr.name;
450 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),
468 bstrutime(dt, sizeof(dt), jcr->sched_time),
473 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
475 jcr->RestoreJobId = atoi(jid);
477 if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
481 jcr->RestoreJobId = ua->pint32_val;
484 jcr->JobLevel = L_FULL; /* default level */
485 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
486 if (jcr->RestoreJobId == 0) {
487 bsendmsg(ua, _("Run Restore job\n\
498 NPRT(jcr->RestoreBootstrap),
499 jcr->where?jcr->where:NPRT(job->RestoreWhere),
501 jcr->fileset->hdr.name,
502 jcr->client->hdr.name,
503 jcr->store->hdr.name,
504 bstrutime(dt, sizeof(dt), jcr->sched_time),
507 bsendmsg(ua, _("Run Restore job\n\
519 NPRT(jcr->RestoreBootstrap),
520 jcr->where?jcr->where:NPRT(job->RestoreWhere),
522 jcr->fileset->hdr.name,
523 jcr->client->hdr.name,
524 jcr->store->hdr.name,
525 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1),
526 bstrutime(dt, sizeof(dt), jcr->sched_time),
531 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
536 /* Run without prompting? */
537 if (find_arg(ua, _("yes")) > 0) {
538 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
540 bsendmsg(ua, _("Run command submitted.\n"));
544 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
546 return 0; /* do not run */
549 * At user request modify parameters of job to be run.
551 if (ua->cmd[0] == 0) {
552 bsendmsg(ua, _("Job not run.\n"));
554 return 0; /* do not run */
556 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
559 start_prompt(ua, _("Parameters to modify:\n"));
560 add_prompt(ua, _("Level")); /* 0 */
561 add_prompt(ua, _("Storage")); /* 1 */
562 add_prompt(ua, _("Job")); /* 2 */
563 add_prompt(ua, _("FileSet")); /* 3 */
564 add_prompt(ua, _("Client")); /* 4 */
565 add_prompt(ua, _("When")); /* 5 */
566 add_prompt(ua, _("Priority")); /* 6 */
567 if (jcr->JobType == JT_BACKUP ||
568 jcr->JobType == JT_VERIFY) {
569 add_prompt(ua, _("Pool")); /* 7 */
570 } else if (jcr->JobType == JT_RESTORE) {
571 add_prompt(ua, _("Bootstrap")); /* 7 */
572 add_prompt(ua, _("Where")); /* 8 */
573 add_prompt(ua, _("Replace")); /* 9 */
574 add_prompt(ua, _("JobId")); /* 10 */
576 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
579 if (jcr->JobType == JT_BACKUP) {
580 start_prompt(ua, _("Levels:\n"));
581 add_prompt(ua, _("Base"));
582 add_prompt(ua, _("Full"));
583 add_prompt(ua, _("Incremental"));
584 add_prompt(ua, _("Differential"));
585 add_prompt(ua, _("Since"));
586 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
588 jcr->JobLevel = L_BASE;
591 jcr->JobLevel = L_FULL;
594 jcr->JobLevel = L_INCREMENTAL;
597 jcr->JobLevel = L_DIFFERENTIAL;
600 jcr->JobLevel = L_SINCE;
606 } else if (jcr->JobType == JT_VERIFY) {
607 start_prompt(ua, _("Levels:\n"));
608 add_prompt(ua, _("Initialize Catalog"));
609 add_prompt(ua, _("Verify Catalog"));
610 add_prompt(ua, _("Verify Volume to Catalog"));
611 add_prompt(ua, _("Verify Disk to Catalog"));
612 add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
613 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
615 jcr->JobLevel = L_VERIFY_INIT;
618 jcr->JobLevel = L_VERIFY_CATALOG;
621 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
624 jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
627 jcr->JobLevel = L_VERIFY_DATA;
634 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
639 store = select_storage_resource(ua);
647 job = select_job_resource(ua);
650 set_jcr_defaults(jcr, job);
656 fileset = select_fileset_resource(ua);
658 jcr->fileset = fileset;
664 client = select_client_resource(ua);
666 jcr->client = client;
672 if (!get_cmd(ua, _("Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now): "))) {
675 if (ua->cmd[0] == 0) {
676 jcr->sched_time = time(NULL);
678 jcr->sched_time = str_to_utime(ua->cmd);
679 if (jcr->sched_time == 0) {
680 bsendmsg(ua, _("Invalid time, using current time.\n"));
681 jcr->sched_time = time(NULL);
687 if (!get_pint(ua, _("Enter new Priority: "))) {
690 if (ua->pint32_val == 0) {
691 bsendmsg(ua, _("Priority must be a positive integer.\n"));
693 jcr->JobPriority = ua->pint32_val;
697 /* Pool or Bootstrap depending on JobType */
698 if (jcr->JobType == JT_BACKUP ||
699 jcr->JobType == JT_VERIFY) { /* Pool */
700 pool = select_pool_resource(ua);
709 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
712 if (jcr->RestoreBootstrap) {
713 free(jcr->RestoreBootstrap);
714 jcr->RestoreBootstrap = NULL;
716 if (ua->cmd[0] != 0) {
717 jcr->RestoreBootstrap = bstrdup(ua->cmd);
718 fd = fopen(jcr->RestoreBootstrap, "r");
720 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
721 jcr->RestoreBootstrap, strerror(errno));
722 free(jcr->RestoreBootstrap);
723 jcr->RestoreBootstrap = NULL;
731 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
738 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
741 jcr->where = bstrdup(ua->cmd);
745 start_prompt(ua, _("Replace:\n"));
746 for (i=0; ReplaceOptions[i].name; i++) {
747 add_prompt(ua, ReplaceOptions[i].name);
749 opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
751 jcr->replace = ReplaceOptions[opt].token;
756 jid = NULL; /* force reprompt */
757 jcr->RestoreJobId = 0;
758 if (jcr->RestoreBootstrap) {
759 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
765 bsendmsg(ua, _("Job not run.\n"));
767 return 0; /* error do no run Job */
770 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
771 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
773 bsendmsg(ua, _("Run command submitted.\n"));
777 bsendmsg(ua, _("Job not run.\n"));
779 return 0; /* do not run */