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 runcmd(UAContext *ua, char *cmd)
50 char *job_name, *level_name, *jid, *store_name, *pool_name;
51 char *where, *fileset_name, *client_name, *bootstrap, *replace;
57 CLIENT *client = NULL;
58 FILESET *fileset = NULL;
60 static char *kw[] = { /* command line arguments */
73 N_("yes"), /* 12 -- if you change this change YES_POS too */
94 for (i=1; i<ua->argc; i++) {
96 Dmsg2(200, "Doing arg %d = %s\n", i, ua->argk[i]);
97 for (j=0; !found && kw[j]; j++) {
98 if (strcasecmp(ua->argk[i], _(kw[j])) == 0) {
99 /* Note, yes has no value, so do not err */
100 if (!ua->argv[i] && j != YES_POS /*yes*/) {
101 bsendmsg(ua, _("Value missing for keyword %s\n"), ua->argk[i]);
104 Dmsg1(200, "Got keyword=%s\n", kw[j]);
108 bsendmsg(ua, _("Job name specified twice.\n"));
111 job_name = ua->argv[i];
116 bsendmsg(ua, _("JobId specified twice.\n"));
124 bsendmsg(ua, _("Client specified twice.\n"));
127 client_name = ua->argv[i];
130 case 3: /* fileset */
132 bsendmsg(ua, _("FileSet specified twice.\n"));
135 fileset_name = ua->argv[i];
140 bsendmsg(ua, _("Level specified twice.\n"));
143 level_name = ua->argv[i];
146 case 5: /* storage */
148 bsendmsg(ua, _("Storage specified twice.\n"));
151 store_name = ua->argv[i];
156 bsendmsg(ua, _("Pool specified twice.\n"));
159 pool_name = ua->argv[i];
164 bsendmsg(ua, _("Where specified twice.\n"));
170 case 8: /* bootstrap */
172 bsendmsg(ua, _("Bootstrap specified twice.\n"));
175 bootstrap = ua->argv[i];
178 case 9: /* replace */
180 bsendmsg(ua, _("Replace specified twice.\n"));
183 replace = ua->argv[i];
188 bsendmsg(ua, _("When specified twice.\n"));
194 case 11: /* Priority */
196 bsendmsg(ua, _("Priority specified twice.\n"));
199 Priority = atoi(ua->argv[i]);
201 bsendmsg(ua, _("Priority must be positive nonzero setting it to 10.\n"));
211 } /* end strcase compare */
212 } /* end keyword loop */
214 Dmsg1(200, "%s not found\n", ua->argk[i]);
216 * Special case for Job Name, it can be the first
217 * keyword that has no value.
219 if (!job_name && !ua->argv[i]) {
220 job_name = ua->argk[i]; /* use keyword as job name */
221 Dmsg1(200, "Set jobname=%s\n", job_name);
223 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
227 } /* end argc loop */
229 Dmsg0(200, "Done scan.\n");
235 job = (JOB *)GetResWithName(R_JOB, job_name);
237 bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
238 job = select_job_resource(ua);
240 Dmsg1(200, "Found job=%s\n", job_name);
243 bsendmsg(ua, _("A job name must be specified.\n"));
244 job = select_job_resource(ua);
251 store = (STORE *)GetResWithName(R_STORAGE, store_name);
253 bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
254 store = select_storage_resource(ua);
257 store = job->storage; /* use default */
265 pool = (POOL *)GetResWithName(R_POOL, pool_name);
267 bsendmsg(ua, _("Pool \"%s\" not found.\n"), pool_name);
268 pool = get_pool_resource(ua);
271 pool = job->pool; /* use default */
275 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
277 bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
278 client = select_client_resource(ua);
281 client = job->client; /* use default */
288 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
290 bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
291 fileset = select_fileset_resource(ua);
294 fileset = job->fileset; /* use default */
301 /* Create JCR to run job */
302 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
303 set_jcr_defaults(jcr, job);
306 jcr->client = client;
307 jcr->fileset = fileset;
313 jcr->where = bstrdup(where);
317 jcr->sched_time = str_to_utime(when);
318 if (jcr->sched_time == 0) {
319 bsendmsg(ua, _("Invalid time, using current time.\n"));
320 jcr->sched_time = time(NULL);
325 if (jcr->RestoreBootstrap) {
326 free(jcr->RestoreBootstrap);
328 jcr->RestoreBootstrap = bstrdup(bootstrap);
333 for (i=0; ReplaceOptions[i].name; i++) {
334 if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
335 jcr->replace = ReplaceOptions[i].token;
339 bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
342 } else if (job->replace) {
343 jcr->replace = job->replace;
345 jcr->replace = REPLACE_ALWAYS;
349 jcr->JobPriority = Priority;
353 replace = ReplaceOptions[0].name;
354 for (i=0; ReplaceOptions[i].name; i++) {
355 if (ReplaceOptions[i].token == jcr->replace) {
356 replace = ReplaceOptions[i].name;
359 Dmsg1(20, "JobType=%c\n", jcr->JobType);
360 switch (jcr->JobType) {
362 char dt[MAX_TIME_LENGTH];
364 bsendmsg(ua, _("Run %s job\n\
373 jcr->fileset->hdr.name,
374 NPRT(jcr->client->hdr.name),
375 NPRT(jcr->store->hdr.name),
376 bstrutime(dt, sizeof(dt), jcr->sched_time),
378 jcr->JobLevel = L_FULL;
383 /* Look up level name and pull code */
385 for (i=0; joblevels[i].level_name; i++) {
386 if (strcasecmp(level_name, _(joblevels[i].level_name)) == 0) {
387 jcr->JobLevel = joblevels[i].level;
393 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
399 bsendmsg(ua, _("Run %s job\n\
408 jcr->JobType==JT_BACKUP?_("Backup"):_("Verify"),
410 jcr->fileset->hdr.name,
411 level_to_str(jcr->JobLevel),
412 jcr->client->hdr.name,
413 jcr->store->hdr.name,
414 NPRT(jcr->pool->hdr.name),
415 bstrutime(dt, sizeof(dt), jcr->sched_time),
419 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
421 jcr->RestoreJobId = atoi(jid);
423 if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
427 jcr->RestoreJobId = ua->pint32_val;
430 jcr->JobLevel = L_FULL; /* default level */
431 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
432 if (jcr->RestoreJobId == 0) {
433 bsendmsg(ua, _("Run Restore job\n\
444 NPRT(jcr->RestoreBootstrap),
445 jcr->where?jcr->where:NPRT(job->RestoreWhere),
447 jcr->fileset->hdr.name,
448 jcr->client->hdr.name,
449 jcr->store->hdr.name,
450 bstrutime(dt, sizeof(dt), jcr->sched_time),
453 bsendmsg(ua, _("Run Restore job\n\
465 NPRT(jcr->RestoreBootstrap),
466 jcr->where?jcr->where:NPRT(job->RestoreWhere),
468 jcr->fileset->hdr.name,
469 jcr->client->hdr.name,
470 jcr->store->hdr.name,
471 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1),
472 bstrutime(dt, sizeof(dt), jcr->sched_time),
477 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
482 /* Run without prompting? */
483 if (find_arg(ua, _("yes")) > 0) {
484 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
486 bsendmsg(ua, _("Run command submitted.\n"));
490 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
492 return 0; /* do not run */
495 * At user request modify parameters of job to be run.
497 if (ua->cmd[0] == 0) {
498 bsendmsg(ua, _("Job not run.\n"));
500 return 0; /* do not run */
502 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
505 start_prompt(ua, _("Parameters to modify:\n"));
506 add_prompt(ua, _("Level")); /* 0 */
507 add_prompt(ua, _("Storage")); /* 1 */
508 add_prompt(ua, _("Job")); /* 2 */
509 add_prompt(ua, _("FileSet")); /* 3 */
510 add_prompt(ua, _("Client")); /* 4 */
511 add_prompt(ua, _("When")); /* 5 */
512 add_prompt(ua, _("Priority")); /* 6 */
513 if (jcr->JobType == JT_BACKUP ||
514 jcr->JobType == JT_VERIFY) {
515 add_prompt(ua, _("Pool")); /* 7 */
516 } else if (jcr->JobType == JT_RESTORE) {
517 add_prompt(ua, _("Bootstrap")); /* 7 */
518 add_prompt(ua, _("Where")); /* 8 */
519 add_prompt(ua, _("Replace")); /* 9 */
520 add_prompt(ua, _("JobId")); /* 10 */
522 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
525 if (jcr->JobType == JT_BACKUP) {
526 start_prompt(ua, _("Levels:\n"));
527 add_prompt(ua, _("Base"));
528 add_prompt(ua, _("Full"));
529 add_prompt(ua, _("Incremental"));
530 add_prompt(ua, _("Differential"));
531 add_prompt(ua, _("Since"));
532 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
534 jcr->JobLevel = L_BASE;
537 jcr->JobLevel = L_FULL;
540 jcr->JobLevel = L_INCREMENTAL;
543 jcr->JobLevel = L_DIFFERENTIAL;
546 jcr->JobLevel = L_SINCE;
552 } else if (jcr->JobType == JT_VERIFY) {
553 start_prompt(ua, _("Levels:\n"));
554 add_prompt(ua, _("Initialize Catalog"));
555 add_prompt(ua, _("Verify Catalog"));
556 add_prompt(ua, _("Verify Volume to Catalog"));
557 add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
558 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
560 jcr->JobLevel = L_VERIFY_INIT;
563 jcr->JobLevel = L_VERIFY_CATALOG;
566 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
569 jcr->JobLevel = L_VERIFY_DATA;
576 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
581 store = select_storage_resource(ua);
589 job = select_job_resource(ua);
592 set_jcr_defaults(jcr, job);
598 fileset = select_fileset_resource(ua);
600 jcr->fileset = fileset;
606 client = select_client_resource(ua);
608 jcr->client = client;
614 if (!get_cmd(ua, _("Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now): "))) {
617 if (ua->cmd[0] == 0) {
618 jcr->sched_time = time(NULL);
620 jcr->sched_time = str_to_utime(ua->cmd);
621 if (jcr->sched_time == 0) {
622 bsendmsg(ua, _("Invalid time, using current time.\n"));
623 jcr->sched_time = time(NULL);
629 if (!get_pint(ua, _("Enter new Priority: "))) {
632 if (ua->pint32_val == 0) {
633 bsendmsg(ua, _("Priority must be a positive integer.\n"));
635 jcr->JobPriority = ua->pint32_val;
639 if (jcr->JobType == JT_BACKUP ||
640 jcr->JobType == JT_VERIFY) { /* Pool */
641 pool = select_pool_resource(ua);
650 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
653 if (jcr->RestoreBootstrap) {
654 free(jcr->RestoreBootstrap);
655 jcr->RestoreBootstrap = NULL;
657 if (ua->cmd[0] != 0) {
658 jcr->RestoreBootstrap = bstrdup(ua->cmd);
659 fd = fopen(jcr->RestoreBootstrap, "r");
661 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
662 jcr->RestoreBootstrap, strerror(errno));
663 free(jcr->RestoreBootstrap);
664 jcr->RestoreBootstrap = NULL;
672 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
679 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
682 jcr->where = bstrdup(ua->cmd);
686 start_prompt(ua, _("Replace:\n"));
687 for (i=0; ReplaceOptions[i].name; i++) {
688 add_prompt(ua, ReplaceOptions[i].name);
690 opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
692 jcr->replace = ReplaceOptions[opt].token;
697 jid = NULL; /* force reprompt */
698 jcr->RestoreJobId = 0;
699 if (jcr->RestoreBootstrap) {
700 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
706 bsendmsg(ua, _("Job not run.\n"));
708 return 0; /* error do no run Job */
711 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
712 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
714 bsendmsg(ua, _("Run command submitted.\n"));
718 bsendmsg(ua, _("Job not run.\n"));
720 return 0; /* do not run */