3 * Bacula Director -- Run Command
5 * Kern Sibbald, December MMI
11 Copyright (C) 2001-2004 Kern Sibbald
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
52 int run_cmd(UAContext *ua, const char *cmd)
55 char *job_name, *level_name, *jid, *store_name, *pool_name;
56 char *where, *fileset_name, *client_name, *bootstrap;
58 char *when, *verify_job_name, *catalog_name;
60 int i, j, opt, files = 0;
63 JOB *verify_job = NULL;
65 CLIENT *client = NULL;
66 FILESET *fileset = NULL;
68 static const char *kw[] = { /* command line arguments */
69 N_("job"), /* Used in a switch() */
73 N_("fileset"), /* 4 */
75 N_("storage"), /* 6 */
79 N_("bootstrap"), /* 10 */
80 N_("replace"), /* 11 */
82 N_("priority"), /* 13 */
83 N_("yes"), /* 14 -- if you change this change YES_POS too */
84 N_("verifyjob"), /* 15 */
85 N_("files"), /* 16 number of files to restore */
86 N_("catalog"), /* 17 override catalog */
106 verify_job_name = NULL;
109 for (i=1; i<ua->argc; i++) {
110 Dmsg2(800, "Doing arg %d = %s\n", i, ua->argk[i]);
112 /* Keep looking until we find a good keyword */
113 for (j=0; !kw_ok && kw[j]; j++) {
114 if (strcasecmp(ua->argk[i], _(kw[j])) == 0) {
115 /* Note, yes and run have no value, so do not err */
116 if (!ua->argv[i] && j != YES_POS /*yes*/) {
117 bsendmsg(ua, _("Value missing for keyword %s\n"), ua->argk[i]);
120 Dmsg1(800, "Got keyword=%s\n", kw[j]);
124 bsendmsg(ua, _("Job name specified twice.\n"));
127 job_name = ua->argv[i];
132 bsendmsg(ua, _("JobId specified twice.\n"));
141 bsendmsg(ua, _("Client specified twice.\n"));
144 client_name = ua->argv[i];
147 case 4: /* fileset */
149 bsendmsg(ua, _("FileSet specified twice.\n"));
152 fileset_name = ua->argv[i];
157 bsendmsg(ua, _("Level specified twice.\n"));
160 level_name = ua->argv[i];
163 case 6: /* storage */
166 bsendmsg(ua, _("Storage specified twice.\n"));
169 store_name = ua->argv[i];
174 bsendmsg(ua, _("Pool specified twice.\n"));
177 pool_name = ua->argv[i];
182 bsendmsg(ua, _("Where specified twice.\n"));
188 case 10: /* bootstrap */
190 bsendmsg(ua, _("Bootstrap specified twice.\n"));
193 bootstrap = ua->argv[i];
196 case 11: /* replace */
198 bsendmsg(ua, _("Replace specified twice.\n"));
201 replace = ua->argv[i];
206 bsendmsg(ua, _("When specified twice.\n"));
212 case 13: /* Priority */
214 bsendmsg(ua, _("Priority specified twice.\n"));
217 Priority = atoi(ua->argv[i]);
219 bsendmsg(ua, _("Priority must be positive nonzero setting it to 10.\n"));
227 case 15: /* Verify Job */
228 if (verify_job_name) {
229 bsendmsg(ua, _("Verify Job specified twice.\n"));
232 verify_job_name = ua->argv[i];
236 files = atoi(ua->argv[i]);
240 case 17: /* catalog */
241 catalog_name = ua->argv[i];
248 } /* end strcase compare */
249 } /* end keyword loop */
251 * End of keyword for loop -- if not found, we got a bogus keyword
254 Dmsg1(200, "%s not found\n", ua->argk[i]);
256 * Special case for Job Name, it can be the first
257 * keyword that has no value.
259 if (!job_name && !ua->argv[i]) {
260 job_name = ua->argk[i]; /* use keyword as job name */
261 Dmsg1(200, "Set jobname=%s\n", job_name);
263 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
267 } /* end argc loop */
269 Dmsg0(800, "Done scan.\n");
272 if (catalog_name != NULL) {
273 catalog = (CAT *)GetResWithName(R_CATALOG, catalog_name);
274 if (catalog == NULL) {
275 bsendmsg(ua, _("Catalog \"%s\" not found\n"), catalog_name);
279 Dmsg1(200, "Using catalog=%s\n", catalog_name);
283 job = (JOB *)GetResWithName(R_JOB, job_name);
285 if (*job_name != 0) {
286 bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
288 job = select_job_resource(ua);
290 Dmsg1(200, "Found job=%s\n", job_name);
293 bsendmsg(ua, _("A job name must be specified.\n"));
294 job = select_job_resource(ua);
298 } else if (!acl_access_ok(ua, Job_ACL, job->hdr.name)) {
299 bsendmsg(ua, _("No authorization. Job \"%s\".\n"),
305 store = (STORE *)GetResWithName(R_STORAGE, store_name);
307 if (*store_name != 0) {
308 bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
310 store = select_storage_resource(ua);
313 store = (STORE *)job->storage[0]->first(); /* use default */
317 } else if (!acl_access_ok(ua, Storage_ACL, store->hdr.name)) {
318 bsendmsg(ua, _("No authorization. Storage \"%s\".\n"),
322 Dmsg1(200, "Using storage=%s\n", store->hdr.name);
325 pool = (POOL *)GetResWithName(R_POOL, pool_name);
327 if (*pool_name != 0) {
328 bsendmsg(ua, _("Pool \"%s\" not found.\n"), pool_name);
330 pool = select_pool_resource(ua);
333 pool = job->pool; /* use default */
337 } else if (!acl_access_ok(ua, Pool_ACL, pool->hdr.name)) {
338 bsendmsg(ua, _("No authorization. Pool \"%s\".\n"),
342 Dmsg1(200, "Using pool\n", pool->hdr.name);
345 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
347 if (*client_name != 0) {
348 bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
350 client = select_client_resource(ua);
353 client = job->client; /* use default */
357 } else if (!acl_access_ok(ua, Client_ACL, client->hdr.name)) {
358 bsendmsg(ua, _("No authorization. Client \"%s\".\n"),
362 Dmsg1(200, "Using client=%s\n", client->hdr.name);
365 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
367 bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
368 fileset = select_fileset_resource(ua);
371 fileset = job->fileset; /* use default */
375 } else if (!acl_access_ok(ua, FileSet_ACL, fileset->hdr.name)) {
376 bsendmsg(ua, _("No authorization. FileSet \"%s\".\n"),
381 if (verify_job_name) {
382 verify_job = (JOB *)GetResWithName(R_JOB, verify_job_name);
384 bsendmsg(ua, _("Verify Job \"%s\" not found.\n"), verify_job_name);
385 verify_job = select_job_resource(ua);
388 verify_job = job->verify_job;
392 * Create JCR to run job. NOTE!!! after this point, free_jcr()
395 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
396 set_jcr_defaults(jcr, job);
398 jcr->verify_job = verify_job;
399 set_storage(jcr, store);
400 jcr->client = client;
401 jcr->fileset = fileset;
403 jcr->ExpectedFiles = files;
404 if (catalog != NULL) {
405 jcr->catalog = catalog;
411 jcr->where = bstrdup(where);
415 jcr->sched_time = str_to_utime(when);
416 if (jcr->sched_time == 0) {
417 bsendmsg(ua, _("Invalid time, using current time.\n"));
418 jcr->sched_time = time(NULL);
423 if (jcr->RestoreBootstrap) {
424 free(jcr->RestoreBootstrap);
426 jcr->RestoreBootstrap = bstrdup(bootstrap);
431 for (i=0; ReplaceOptions[i].name; i++) {
432 if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
433 jcr->replace = ReplaceOptions[i].token;
437 bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
440 } else if (job->replace) {
441 jcr->replace = job->replace;
443 jcr->replace = REPLACE_ALWAYS;
447 jcr->JobPriority = Priority;
450 if (find_arg(ua, _("fdcalled")) > 0) {
451 jcr->file_bsock = dup_bsock(ua->UA_sock);
456 replace = ReplaceOptions[0].name;
457 for (i=0; ReplaceOptions[i].name; i++) {
458 if (ReplaceOptions[i].token == jcr->replace) {
459 replace = ReplaceOptions[i].name;
463 if (!get_level_from_name(jcr, level_name)) {
464 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
469 jcr->RestoreJobId = atoi(jid);
472 /* Run without prompting? */
473 if (ua->batch || find_arg(ua, _("yes")) > 0) {
478 * Prompt User to see if all run job parameters are correct, and
479 * allow him to modify them.
481 Dmsg1(20, "JobType=%c\n", jcr->JobType);
482 switch (jcr->JobType) {
484 char dt[MAX_TIME_LENGTH];
486 bsendmsg(ua, _("Run %s job\n\
495 jcr->fileset->hdr.name,
496 NPRT(jcr->client->hdr.name),
497 NPRT(jcr->store->hdr.name),
498 bstrutime(dt, sizeof(dt), jcr->sched_time),
500 jcr->JobLevel = L_FULL;
504 if (jcr->JobType == JT_BACKUP) {
505 bsendmsg(ua, _("Run %s job\n\
516 jcr->fileset->hdr.name,
517 level_to_str(jcr->JobLevel),
518 jcr->client->hdr.name,
519 jcr->store->hdr.name,
520 NPRT(jcr->pool->hdr.name),
521 bstrutime(dt, sizeof(dt), jcr->sched_time),
523 } else { /* JT_VERIFY */
525 if (jcr->verify_job) {
526 Name = jcr->verify_job->hdr.name;
530 bsendmsg(ua, _("Run %s job\n\
542 jcr->fileset->hdr.name,
543 level_to_str(jcr->JobLevel),
544 jcr->client->hdr.name,
545 jcr->store->hdr.name,
546 NPRT(jcr->pool->hdr.name),
548 bstrutime(dt, sizeof(dt), jcr->sched_time),
553 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
555 jcr->RestoreJobId = atoi(jid);
557 if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
560 jcr->RestoreJobId = ua->pint32_val;
563 jcr->JobLevel = L_FULL; /* default level */
564 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
565 if (jcr->RestoreJobId == 0) {
566 bsendmsg(ua, _("Run Restore job\n"
578 NPRT(jcr->RestoreBootstrap),
579 jcr->where?jcr->where:NPRT(job->RestoreWhere),
581 jcr->fileset->hdr.name,
582 jcr->client->hdr.name,
583 jcr->store->hdr.name,
584 bstrutime(dt, sizeof(dt), jcr->sched_time),
585 jcr->catalog->hdr.name,
588 bsendmsg(ua, _("Run Restore job\n"
600 NPRT(jcr->RestoreBootstrap),
601 jcr->where?jcr->where:NPRT(job->RestoreWhere),
603 jcr->client->hdr.name,
604 jcr->store->hdr.name,
605 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1),
606 bstrutime(dt, sizeof(dt), jcr->sched_time),
607 jcr->catalog->hdr.name,
612 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
617 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
621 * At user request modify parameters of job to be run.
623 if (ua->cmd[0] == 0) {
626 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
629 start_prompt(ua, _("Parameters to modify:\n"));
630 add_prompt(ua, _("Level")); /* 0 */
631 add_prompt(ua, _("Storage")); /* 1 */
632 add_prompt(ua, _("Job")); /* 2 */
633 add_prompt(ua, _("FileSet")); /* 3 */
634 add_prompt(ua, _("Client")); /* 4 */
635 add_prompt(ua, _("When")); /* 5 */
636 add_prompt(ua, _("Priority")); /* 6 */
637 if (jcr->JobType == JT_BACKUP ||
638 jcr->JobType == JT_VERIFY) {
639 add_prompt(ua, _("Pool")); /* 7 */
640 if (jcr->JobType == JT_VERIFY) {
641 add_prompt(ua, _("Verify Job")); /* 8 */
643 } else if (jcr->JobType == JT_RESTORE) {
644 add_prompt(ua, _("Bootstrap")); /* 7 */
645 add_prompt(ua, _("Where")); /* 8 */
646 add_prompt(ua, _("Replace")); /* 9 */
647 add_prompt(ua, _("JobId")); /* 10 */
649 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
652 if (jcr->JobType == JT_BACKUP) {
653 start_prompt(ua, _("Levels:\n"));
654 add_prompt(ua, _("Base"));
655 add_prompt(ua, _("Full"));
656 add_prompt(ua, _("Incremental"));
657 add_prompt(ua, _("Differential"));
658 add_prompt(ua, _("Since"));
659 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
661 jcr->JobLevel = L_BASE;
664 jcr->JobLevel = L_FULL;
667 jcr->JobLevel = L_INCREMENTAL;
670 jcr->JobLevel = L_DIFFERENTIAL;
673 jcr->JobLevel = L_SINCE;
679 } else if (jcr->JobType == JT_VERIFY) {
680 start_prompt(ua, _("Levels:\n"));
681 add_prompt(ua, _("Initialize Catalog"));
682 add_prompt(ua, _("Verify Catalog"));
683 add_prompt(ua, _("Verify Volume to Catalog"));
684 add_prompt(ua, _("Verify Disk to Catalog"));
685 add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
686 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
688 jcr->JobLevel = L_VERIFY_INIT;
691 jcr->JobLevel = L_VERIFY_CATALOG;
694 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
697 jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
700 jcr->JobLevel = L_VERIFY_DATA;
707 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
712 store = select_storage_resource(ua);
714 set_storage(jcr, store);
720 job = select_job_resource(ua);
723 set_jcr_defaults(jcr, job);
729 fileset = select_fileset_resource(ua);
731 jcr->fileset = fileset;
737 client = select_client_resource(ua);
739 jcr->client = client;
745 if (!get_cmd(ua, _("Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now): "))) {
748 if (ua->cmd[0] == 0) {
749 jcr->sched_time = time(NULL);
751 jcr->sched_time = str_to_utime(ua->cmd);
752 if (jcr->sched_time == 0) {
753 bsendmsg(ua, _("Invalid time, using current time.\n"));
754 jcr->sched_time = time(NULL);
760 if (!get_pint(ua, _("Enter new Priority: "))) {
763 if (ua->pint32_val == 0) {
764 bsendmsg(ua, _("Priority must be a positive integer.\n"));
766 jcr->JobPriority = ua->pint32_val;
770 /* Pool or Bootstrap depending on JobType */
771 if (jcr->JobType == JT_BACKUP ||
772 jcr->JobType == JT_VERIFY) { /* Pool */
773 pool = select_pool_resource(ua);
782 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
785 if (jcr->RestoreBootstrap) {
786 free(jcr->RestoreBootstrap);
787 jcr->RestoreBootstrap = NULL;
789 if (ua->cmd[0] != 0) {
790 jcr->RestoreBootstrap = bstrdup(ua->cmd);
791 fd = fopen(jcr->RestoreBootstrap, "r");
793 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
794 jcr->RestoreBootstrap, strerror(errno));
795 free(jcr->RestoreBootstrap);
796 jcr->RestoreBootstrap = NULL;
804 if (jcr->JobType == JT_VERIFY) {
805 verify_job = select_job_resource(ua);
807 jcr->verify_job = verify_job;
812 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
819 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
822 jcr->where = bstrdup(ua->cmd);
826 start_prompt(ua, _("Replace:\n"));
827 for (i=0; ReplaceOptions[i].name; i++) {
828 add_prompt(ua, ReplaceOptions[i].name);
830 opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
832 jcr->replace = ReplaceOptions[opt].token;
837 jid = NULL; /* force reprompt */
838 jcr->RestoreJobId = 0;
839 if (jcr->RestoreBootstrap) {
840 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
849 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
851 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
853 JobId = run_job(jcr);
854 free_jcr(jcr); /* release jcr */
856 bsendmsg(ua, _("Job failed.\n"));
858 bsendmsg(ua, _("Job started. JobId=%u\n"), JobId);
864 bsendmsg(ua, _("Job not run.\n"));
866 return 0; /* do not run */