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 */
34 extern void run_job(JCR *jcr);
36 /* Imported variables */
37 extern struct s_jl joblevels[];
38 extern struct s_kw ReplaceOptions[];
41 * For Backup and Verify Jobs
42 * run [job=]<job-name> level=<level-name>
45 * run <job-name> jobid=nn
48 int runcmd(UAContext *ua, char *cmd)
51 char *job_name, *level_name, *jid, *store_name, *pool_name;
52 char *where, *fileset_name, *client_name, *bootstrap, *replace;
58 CLIENT *client = NULL;
59 FILESET *fileset = NULL;
61 static char *kw[] = { /* command line arguments */
74 N_("yes"), /* 12 -- see below */
93 for (i=1; i<ua->argc; i++) {
95 Dmsg2(200, "Doing arg %d = %s\n", i, ua->argk[i]);
96 for (j=0; !found && kw[j]; j++) {
97 if (strcasecmp(ua->argk[i], _(kw[j])) == 0) {
98 /* Note, yes has no value, so do not err */
99 if (!ua->argv[i] && j != 11 /*yes*/) {
100 bsendmsg(ua, _("Value missing for keyword %s\n"), ua->argk[i]);
103 Dmsg1(200, "Got keyword=%s\n", kw[j]);
107 bsendmsg(ua, _("Job name specified twice.\n"));
110 job_name = ua->argv[i];
115 bsendmsg(ua, _("JobId specified twice.\n"));
123 bsendmsg(ua, _("Client specified twice.\n"));
126 client_name = ua->argv[i];
129 case 3: /* fileset */
131 bsendmsg(ua, _("FileSet specified twice.\n"));
134 fileset_name = ua->argv[i];
139 bsendmsg(ua, _("Level specified twice.\n"));
142 level_name = ua->argv[i];
145 case 5: /* storage */
147 bsendmsg(ua, _("Storage specified twice.\n"));
150 store_name = ua->argv[i];
155 bsendmsg(ua, _("Pool specified twice.\n"));
158 pool_name = ua->argv[i];
163 bsendmsg(ua, _("Where specified twice.\n"));
169 case 8: /* bootstrap */
171 bsendmsg(ua, _("Bootstrap specified twice.\n"));
174 bootstrap = ua->argv[i];
177 case 9: /* replace */
179 bsendmsg(ua, _("Replace specified twice.\n"));
182 replace = ua->argv[i];
187 bsendmsg(ua, _("When specified twice.\n"));
193 case 11: /* Priority */
195 bsendmsg(ua, _("Priority specified twice.\n"));
198 Priority = atoi(ua->argv[i]);
200 bsendmsg(ua, _("Priority must be positive nonzero setting it to 10.\n"));
210 } /* end strcase compare */
211 } /* end keyword loop */
213 Dmsg1(200, "%s not found\n", ua->argk[i]);
215 * Special case for Job Name, it can be the first
216 * keyword that has no value.
218 if (!job_name && !ua->argv[i]) {
219 job_name = ua->argk[i]; /* use keyword as job name */
220 Dmsg1(200, "Set jobname=%s\n", job_name);
222 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
226 } /* end argc loop */
228 Dmsg0(200, "Done scan.\n");
234 job = (JOB *)GetResWithName(R_JOB, job_name);
236 bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
237 job = select_job_resource(ua);
239 Dmsg1(200, "Found job=%s\n", job_name);
242 bsendmsg(ua, _("A job name must be specified.\n"));
243 job = select_job_resource(ua);
250 store = (STORE *)GetResWithName(R_STORAGE, store_name);
252 bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
253 store = select_storage_resource(ua);
256 store = job->storage; /* use default */
264 pool = (POOL *)GetResWithName(R_POOL, pool_name);
266 bsendmsg(ua, _("Pool \"%s\" not found.\n"), pool_name);
267 pool = get_pool_resource(ua);
270 pool = job->pool; /* use default */
274 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
276 bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
277 client = select_client_resource(ua);
280 client = job->client; /* use default */
287 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
289 bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
290 fileset = select_fileset_resource(ua);
293 fileset = job->fileset; /* use default */
300 /* Create JCR to run job */
301 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
302 set_jcr_defaults(jcr, job);
305 jcr->client = client;
306 jcr->fileset = fileset;
312 jcr->where = bstrdup(where);
316 jcr->sched_time = str_to_utime(when);
317 if (jcr->sched_time == 0) {
318 bsendmsg(ua, _("Invalid time, using current time.\n"));
319 jcr->sched_time = time(NULL);
324 if (jcr->RestoreBootstrap) {
325 free(jcr->RestoreBootstrap);
327 jcr->RestoreBootstrap = bstrdup(bootstrap);
332 for (i=0; ReplaceOptions[i].name; i++) {
333 if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
334 jcr->replace = ReplaceOptions[i].token;
338 bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
341 } else if (job->replace) {
342 jcr->replace = job->replace;
344 jcr->replace = REPLACE_ALWAYS;
348 jcr->JobPriority = Priority;
352 replace = ReplaceOptions[0].name;
353 for (i=0; ReplaceOptions[i].name; i++) {
354 if (ReplaceOptions[i].token == jcr->replace) {
355 replace = ReplaceOptions[i].name;
358 Dmsg1(20, "JobType=%c\n", jcr->JobType);
359 switch (jcr->JobType) {
361 char dt[MAX_TIME_LENGTH];
363 bsendmsg(ua, _("Run %s job\n\
372 jcr->fileset->hdr.name,
373 NPRT(jcr->client->hdr.name),
374 NPRT(jcr->store->hdr.name),
375 bstrutime(dt, sizeof(dt), jcr->sched_time),
377 jcr->JobLevel = L_FULL;
382 /* Look up level name and pull code */
384 for (i=0; joblevels[i].level_name; i++) {
385 if (strcasecmp(level_name, _(joblevels[i].level_name)) == 0) {
386 jcr->JobLevel = joblevels[i].level;
392 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
398 bsendmsg(ua, _("Run %s job\n\
407 jcr->JobType==JT_BACKUP?_("Backup"):_("Verify"),
409 jcr->fileset->hdr.name,
410 level_to_str(jcr->JobLevel),
411 jcr->client->hdr.name,
412 jcr->store->hdr.name,
413 NPRT(jcr->pool->hdr.name),
414 bstrutime(dt, sizeof(dt), jcr->sched_time),
418 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
420 jcr->RestoreJobId = atoi(jid);
422 if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
426 jcr->RestoreJobId = ua->pint32_val;
429 jcr->JobLevel = L_FULL; /* default level */
430 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
431 if (jcr->RestoreJobId == 0) {
432 bsendmsg(ua, _("Run Restore job\n\
443 NPRT(jcr->RestoreBootstrap),
444 jcr->where?jcr->where:NPRT(job->RestoreWhere),
446 jcr->fileset->hdr.name,
447 jcr->client->hdr.name,
448 jcr->store->hdr.name,
449 bstrutime(dt, sizeof(dt), jcr->sched_time),
452 bsendmsg(ua, _("Run Restore job\n\
464 NPRT(jcr->RestoreBootstrap),
465 jcr->where?jcr->where:NPRT(job->RestoreWhere),
467 jcr->fileset->hdr.name,
468 jcr->client->hdr.name,
469 jcr->store->hdr.name,
470 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1),
471 bstrutime(dt, sizeof(dt), jcr->sched_time),
476 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
481 /* Run without prompting? */
482 if (find_arg(ua, _("yes")) > 0) {
483 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
485 bsendmsg(ua, _("Run command submitted.\n"));
489 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
491 return 0; /* do not run */
494 * At user request modify parameters of job to be run.
496 if (ua->cmd[0] == 0) {
497 bsendmsg(ua, _("Job not run.\n"));
499 return 0; /* do not run */
501 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
504 start_prompt(ua, _("Parameters to modify:\n"));
505 add_prompt(ua, _("Level")); /* 0 */
506 add_prompt(ua, _("Storage")); /* 1 */
507 add_prompt(ua, _("Job")); /* 2 */
508 add_prompt(ua, _("FileSet")); /* 3 */
509 add_prompt(ua, _("Client")); /* 4 */
510 add_prompt(ua, _("When")); /* 5 */
511 add_prompt(ua, _("Priority")); /* 6 */
512 if (jcr->JobType == JT_BACKUP ||
513 jcr->JobType == JT_VERIFY) {
514 add_prompt(ua, _("Pool")); /* 7 */
515 } else if (jcr->JobType == JT_RESTORE) {
516 add_prompt(ua, _("Bootstrap")); /* 7 */
517 add_prompt(ua, _("Where")); /* 8 */
518 add_prompt(ua, _("Replace")); /* 9 */
519 add_prompt(ua, _("JobId")); /* 10 */
521 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
524 if (jcr->JobType == JT_BACKUP) {
525 start_prompt(ua, _("Levels:\n"));
526 add_prompt(ua, _("Base"));
527 add_prompt(ua, _("Full"));
528 add_prompt(ua, _("Incremental"));
529 add_prompt(ua, _("Differential"));
530 add_prompt(ua, _("Since"));
531 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
533 jcr->JobLevel = L_BASE;
536 jcr->JobLevel = L_FULL;
539 jcr->JobLevel = L_INCREMENTAL;
542 jcr->JobLevel = L_DIFFERENTIAL;
545 jcr->JobLevel = L_SINCE;
551 } else if (jcr->JobType == JT_VERIFY) {
552 start_prompt(ua, _("Levels:\n"));
553 add_prompt(ua, _("Initialize Catalog"));
554 add_prompt(ua, _("Verify Catalog"));
555 add_prompt(ua, _("Verify Volume to Catalog"));
556 add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
557 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
559 jcr->JobLevel = L_VERIFY_INIT;
562 jcr->JobLevel = L_VERIFY_CATALOG;
565 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
568 jcr->JobLevel = L_VERIFY_DATA;
575 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
580 store = select_storage_resource(ua);
588 job = select_job_resource(ua);
591 set_jcr_defaults(jcr, job);
597 fileset = select_fileset_resource(ua);
599 jcr->fileset = fileset;
605 client = select_client_resource(ua);
607 jcr->client = client;
613 if (!get_cmd(ua, _("Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now): "))) {
616 if (ua->cmd[0] == 0) {
617 jcr->sched_time = time(NULL);
619 jcr->sched_time = str_to_utime(ua->cmd);
620 if (jcr->sched_time == 0) {
621 bsendmsg(ua, _("Invalid time, using current time.\n"));
622 jcr->sched_time = time(NULL);
628 if (!get_pint(ua, _("Enter new Priority: "))) {
631 if (ua->pint32_val == 0) {
632 bsendmsg(ua, _("Priority must be a positive integer.\n"));
634 jcr->JobPriority = ua->pint32_val;
638 if (jcr->JobType == JT_BACKUP ||
639 jcr->JobType == JT_VERIFY) { /* Pool */
640 pool = select_pool_resource(ua);
649 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
652 if (jcr->RestoreBootstrap) {
653 free(jcr->RestoreBootstrap);
654 jcr->RestoreBootstrap = NULL;
656 if (ua->cmd[0] != 0) {
657 jcr->RestoreBootstrap = bstrdup(ua->cmd);
658 fd = fopen(jcr->RestoreBootstrap, "r");
660 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
661 jcr->RestoreBootstrap, strerror(errno));
662 free(jcr->RestoreBootstrap);
663 jcr->RestoreBootstrap = NULL;
671 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
678 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
681 jcr->where = bstrdup(ua->cmd);
685 start_prompt(ua, _("Replace:\n"));
686 for (i=0; ReplaceOptions[i].name; i++) {
687 add_prompt(ua, ReplaceOptions[i].name);
689 opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
691 jcr->replace = ReplaceOptions[opt].token;
696 jid = NULL; /* force reprompt */
697 jcr->RestoreJobId = 0;
698 if (jcr->RestoreBootstrap) {
699 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
705 bsendmsg(ua, _("Job not run.\n"));
707 return 0; /* error do no run Job */
710 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
711 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
713 bsendmsg(ua, _("Run command submitted.\n"));
717 bsendmsg(ua, _("Job not run.\n"));
719 return 0; /* do not run */