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 */
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"), /* 16 */
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*/) {
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"));
131 bsendmsg(ua, _("Client specified twice.\n"));
134 client_name = ua->argv[i];
137 case 4: /* fileset */
139 bsendmsg(ua, _("FileSet specified twice.\n"));
142 fileset_name = ua->argv[i];
147 bsendmsg(ua, _("Level specified twice.\n"));
150 level_name = ua->argv[i];
153 case 6: /* storage */
156 bsendmsg(ua, _("Storage specified twice.\n"));
159 store_name = ua->argv[i];
164 bsendmsg(ua, _("Pool specified twice.\n"));
167 pool_name = ua->argv[i];
172 bsendmsg(ua, _("Where specified twice.\n"));
178 case 10: /* bootstrap */
180 bsendmsg(ua, _("Bootstrap specified twice.\n"));
183 bootstrap = ua->argv[i];
186 case 11: /* replace */
188 bsendmsg(ua, _("Replace specified twice.\n"));
191 replace = ua->argv[i];
196 bsendmsg(ua, _("When specified twice.\n"));
202 case 13: /* Priority */
204 bsendmsg(ua, _("Priority specified twice.\n"));
207 Priority = atoi(ua->argv[i]);
209 bsendmsg(ua, _("Priority must be positive nonzero setting it to 10.\n"));
216 case 15: /* Verify Job */
217 if (verify_job_name) {
218 bsendmsg(ua, _("Verify Job specified twice.\n"));
221 verify_job_name = ua->argv[i];
228 } /* end strcase compare */
229 } /* end keyword loop */
231 Dmsg1(200, "%s not found\n", ua->argk[i]);
233 * Special case for Job Name, it can be the first
234 * keyword that has no value.
236 if (!job_name && !ua->argv[i]) {
237 job_name = ua->argk[i]; /* use keyword as job name */
238 Dmsg1(200, "Set jobname=%s\n", job_name);
240 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
244 } /* end argc loop */
246 Dmsg0(200, "Done scan.\n");
250 job = (JOB *)GetResWithName(R_JOB, job_name);
252 bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
253 job = select_job_resource(ua);
255 Dmsg1(200, "Found job=%s\n", job_name);
258 bsendmsg(ua, _("A job name must be specified.\n"));
259 job = select_job_resource(ua);
266 store = (STORE *)GetResWithName(R_STORAGE, store_name);
268 bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
269 store = select_storage_resource(ua);
272 store = job->storage; /* use default */
280 pool = (POOL *)GetResWithName(R_POOL, pool_name);
282 bsendmsg(ua, _("Pool \"%s\" not found.\n"), pool_name);
283 pool = get_pool_resource(ua);
286 pool = job->pool; /* use default */
290 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
292 bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
293 client = select_client_resource(ua);
296 client = job->client; /* use default */
303 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
305 bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
306 fileset = select_fileset_resource(ua);
309 fileset = job->fileset; /* use default */
315 if (verify_job_name) {
316 verify_job = (JOB *)GetResWithName(R_JOB, verify_job_name);
318 bsendmsg(ua, _("Verify Job \"%s\" not found.\n"), verify_job_name);
319 verify_job = select_job_resource(ua);
322 verify_job = job->verify_job;
326 * Create JCR to run job. NOTE!!! after this point, free_jcr()
329 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
330 set_jcr_defaults(jcr, job);
333 jcr->client = client;
334 jcr->fileset = fileset;
340 jcr->where = bstrdup(where);
344 jcr->sched_time = str_to_utime(when);
345 if (jcr->sched_time == 0) {
346 bsendmsg(ua, _("Invalid time, using current time.\n"));
347 jcr->sched_time = time(NULL);
352 if (jcr->RestoreBootstrap) {
353 free(jcr->RestoreBootstrap);
355 jcr->RestoreBootstrap = bstrdup(bootstrap);
360 for (i=0; ReplaceOptions[i].name; i++) {
361 if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
362 jcr->replace = ReplaceOptions[i].token;
366 bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
369 } else if (job->replace) {
370 jcr->replace = job->replace;
372 jcr->replace = REPLACE_ALWAYS;
376 jcr->JobPriority = Priority;
380 replace = ReplaceOptions[0].name;
381 for (i=0; ReplaceOptions[i].name; i++) {
382 if (ReplaceOptions[i].token == jcr->replace) {
383 replace = ReplaceOptions[i].name;
387 /* Look up level name and pull code */
389 for (i=0; joblevels[i].level_name; i++) {
390 if (strcasecmp(level_name, _(joblevels[i].level_name)) == 0) {
391 jcr->JobLevel = joblevels[i].level;
397 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
403 jcr->RestoreJobId = atoi(jid);
406 /* Run without prompting? */
407 if (find_arg(ua, _("yes")) > 0) {
408 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
410 free_jcr(jcr); /* release jcr */
411 bsendmsg(ua, _("Run command submitted.\n"));
416 * Prompt User to see if all run job parameters are correct, and
417 * allow him to modify them.
419 Dmsg1(20, "JobType=%c\n", jcr->JobType);
420 switch (jcr->JobType) {
422 char dt[MAX_TIME_LENGTH];
424 bsendmsg(ua, _("Run %s job\n\
433 jcr->fileset->hdr.name,
434 NPRT(jcr->client->hdr.name),
435 NPRT(jcr->store->hdr.name),
436 bstrutime(dt, sizeof(dt), jcr->sched_time),
438 jcr->JobLevel = L_FULL;
442 if (jcr->JobType == JT_BACKUP) {
443 bsendmsg(ua, _("Run %s job\n\
454 jcr->fileset->hdr.name,
455 level_to_str(jcr->JobLevel),
456 jcr->client->hdr.name,
457 jcr->store->hdr.name,
458 NPRT(jcr->pool->hdr.name),
459 bstrutime(dt, sizeof(dt), jcr->sched_time),
461 } else { /* JT_VERIFY */
463 if (jcr->job->verify_job) {
464 Name = jcr->job->verify_job->hdr.name;
468 bsendmsg(ua, _("Run %s job\n\
480 jcr->fileset->hdr.name,
481 level_to_str(jcr->JobLevel),
482 jcr->client->hdr.name,
483 jcr->store->hdr.name,
484 NPRT(jcr->pool->hdr.name),
486 bstrutime(dt, sizeof(dt), jcr->sched_time),
491 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
493 jcr->RestoreJobId = atoi(jid);
495 if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
498 jcr->RestoreJobId = ua->pint32_val;
501 jcr->JobLevel = L_FULL; /* default level */
502 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
503 if (jcr->RestoreJobId == 0) {
504 bsendmsg(ua, _("Run Restore job\n\
515 NPRT(jcr->RestoreBootstrap),
516 jcr->where?jcr->where:NPRT(job->RestoreWhere),
518 jcr->fileset->hdr.name,
519 jcr->client->hdr.name,
520 jcr->store->hdr.name,
521 bstrutime(dt, sizeof(dt), jcr->sched_time),
524 bsendmsg(ua, _("Run Restore job\n\
536 NPRT(jcr->RestoreBootstrap),
537 jcr->where?jcr->where:NPRT(job->RestoreWhere),
539 jcr->fileset->hdr.name,
540 jcr->client->hdr.name,
541 jcr->store->hdr.name,
542 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1),
543 bstrutime(dt, sizeof(dt), jcr->sched_time),
548 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
553 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
557 * At user request modify parameters of job to be run.
559 if (ua->cmd[0] == 0) {
562 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
565 start_prompt(ua, _("Parameters to modify:\n"));
566 add_prompt(ua, _("Level")); /* 0 */
567 add_prompt(ua, _("Storage")); /* 1 */
568 add_prompt(ua, _("Job")); /* 2 */
569 add_prompt(ua, _("FileSet")); /* 3 */
570 add_prompt(ua, _("Client")); /* 4 */
571 add_prompt(ua, _("When")); /* 5 */
572 add_prompt(ua, _("Priority")); /* 6 */
573 if (jcr->JobType == JT_BACKUP ||
574 jcr->JobType == JT_VERIFY) {
575 add_prompt(ua, _("Pool")); /* 7 */
576 if (jcr->JobType == JT_VERIFY) {
577 add_prompt(ua, _("Verify Job")); /* 8 */
579 } else if (jcr->JobType == JT_RESTORE) {
580 add_prompt(ua, _("Bootstrap")); /* 7 */
581 add_prompt(ua, _("Where")); /* 8 */
582 add_prompt(ua, _("Replace")); /* 9 */
583 add_prompt(ua, _("JobId")); /* 10 */
585 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
588 if (jcr->JobType == JT_BACKUP) {
589 start_prompt(ua, _("Levels:\n"));
590 add_prompt(ua, _("Base"));
591 add_prompt(ua, _("Full"));
592 add_prompt(ua, _("Incremental"));
593 add_prompt(ua, _("Differential"));
594 add_prompt(ua, _("Since"));
595 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
597 jcr->JobLevel = L_BASE;
600 jcr->JobLevel = L_FULL;
603 jcr->JobLevel = L_INCREMENTAL;
606 jcr->JobLevel = L_DIFFERENTIAL;
609 jcr->JobLevel = L_SINCE;
615 } else if (jcr->JobType == JT_VERIFY) {
616 start_prompt(ua, _("Levels:\n"));
617 add_prompt(ua, _("Initialize Catalog"));
618 add_prompt(ua, _("Verify Catalog"));
619 add_prompt(ua, _("Verify Volume to Catalog"));
620 add_prompt(ua, _("Verify Disk to Catalog"));
621 add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
622 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
624 jcr->JobLevel = L_VERIFY_INIT;
627 jcr->JobLevel = L_VERIFY_CATALOG;
630 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
633 jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
636 jcr->JobLevel = L_VERIFY_DATA;
643 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
648 store = select_storage_resource(ua);
656 job = select_job_resource(ua);
659 set_jcr_defaults(jcr, job);
665 fileset = select_fileset_resource(ua);
667 jcr->fileset = fileset;
673 client = select_client_resource(ua);
675 jcr->client = client;
681 if (!get_cmd(ua, _("Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now): "))) {
684 if (ua->cmd[0] == 0) {
685 jcr->sched_time = time(NULL);
687 jcr->sched_time = str_to_utime(ua->cmd);
688 if (jcr->sched_time == 0) {
689 bsendmsg(ua, _("Invalid time, using current time.\n"));
690 jcr->sched_time = time(NULL);
696 if (!get_pint(ua, _("Enter new Priority: "))) {
699 if (ua->pint32_val == 0) {
700 bsendmsg(ua, _("Priority must be a positive integer.\n"));
702 jcr->JobPriority = ua->pint32_val;
706 /* Pool or Bootstrap depending on JobType */
707 if (jcr->JobType == JT_BACKUP ||
708 jcr->JobType == JT_VERIFY) { /* Pool */
709 pool = select_pool_resource(ua);
718 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
721 if (jcr->RestoreBootstrap) {
722 free(jcr->RestoreBootstrap);
723 jcr->RestoreBootstrap = NULL;
725 if (ua->cmd[0] != 0) {
726 jcr->RestoreBootstrap = bstrdup(ua->cmd);
727 fd = fopen(jcr->RestoreBootstrap, "r");
729 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
730 jcr->RestoreBootstrap, strerror(errno));
731 free(jcr->RestoreBootstrap);
732 jcr->RestoreBootstrap = NULL;
740 if (jcr->JobType == JT_VERIFY) {
741 JOB *job = select_job_resource(ua);
743 jcr->job->verify_job = job;
745 jcr->job->verify_job = NULL;
750 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
757 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
760 jcr->where = bstrdup(ua->cmd);
764 start_prompt(ua, _("Replace:\n"));
765 for (i=0; ReplaceOptions[i].name; i++) {
766 add_prompt(ua, ReplaceOptions[i].name);
768 opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
770 jcr->replace = ReplaceOptions[opt].token;
775 jid = NULL; /* force reprompt */
776 jcr->RestoreJobId = 0;
777 if (jcr->RestoreBootstrap) {
778 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
787 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
788 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
790 free_jcr(jcr); /* release jcr */
791 bsendmsg(ua, _("Run command submitted.\n"));
796 bsendmsg(ua, _("Job not run.\n"));
798 return 0; /* do not run */