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
49 int run_cmd(UAContext *ua, const char *cmd)
52 char *job_name, *level_name, *jid, *store_name, *pool_name;
53 char *where, *fileset_name, *client_name, *bootstrap;
55 char *when, *verify_job_name, *catalog_name;
57 int i, j, opt, files = 0;
60 JOB *verify_job = NULL;
62 CLIENT *client = NULL;
63 FILESET *fileset = NULL;
65 static const char *kw[] = { /* command line arguments */
66 N_("job"), /* Used in a switch() */
70 N_("fileset"), /* 4 */
72 N_("storage"), /* 6 */
76 N_("bootstrap"), /* 10 */
77 N_("replace"), /* 11 */
79 N_("priority"), /* 13 */
80 N_("yes"), /* 14 -- if you change this change YES_POS too */
81 N_("verifyjob"), /* 15 */
82 N_("files"), /* 16 number of files to restore */
83 N_("catalog"), /* 17 override catalog */
103 verify_job_name = NULL;
106 for (i=1; i<ua->argc; i++) {
107 Dmsg2(800, "Doing arg %d = %s\n", i, ua->argk[i]);
109 /* Keep looking until we find a good keyword */
110 for (j=0; !kw_ok && kw[j]; j++) {
111 if (strcasecmp(ua->argk[i], _(kw[j])) == 0) {
112 /* Note, yes and run have no value, so do not err */
113 if (!ua->argv[i] && j != YES_POS /*yes*/) {
114 bsendmsg(ua, _("Value missing for keyword %s\n"), ua->argk[i]);
117 Dmsg1(800, "Got keyword=%s\n", kw[j]);
121 bsendmsg(ua, _("Job name specified twice.\n"));
124 job_name = ua->argv[i];
129 bsendmsg(ua, _("JobId specified twice.\n"));
138 bsendmsg(ua, _("Client specified twice.\n"));
141 client_name = ua->argv[i];
144 case 4: /* fileset */
146 bsendmsg(ua, _("FileSet specified twice.\n"));
149 fileset_name = ua->argv[i];
154 bsendmsg(ua, _("Level specified twice.\n"));
157 level_name = ua->argv[i];
160 case 6: /* storage */
163 bsendmsg(ua, _("Storage specified twice.\n"));
166 store_name = ua->argv[i];
171 bsendmsg(ua, _("Pool specified twice.\n"));
174 pool_name = ua->argv[i];
179 bsendmsg(ua, _("Where specified twice.\n"));
185 case 10: /* bootstrap */
187 bsendmsg(ua, _("Bootstrap specified twice.\n"));
190 bootstrap = ua->argv[i];
193 case 11: /* replace */
195 bsendmsg(ua, _("Replace specified twice.\n"));
198 replace = ua->argv[i];
203 bsendmsg(ua, _("When specified twice.\n"));
209 case 13: /* Priority */
211 bsendmsg(ua, _("Priority specified twice.\n"));
214 Priority = atoi(ua->argv[i]);
216 bsendmsg(ua, _("Priority must be positive nonzero setting it to 10.\n"));
224 case 15: /* Verify Job */
225 if (verify_job_name) {
226 bsendmsg(ua, _("Verify Job specified twice.\n"));
229 verify_job_name = ua->argv[i];
233 files = atoi(ua->argv[i]);
237 case 17: /* catalog */
238 catalog_name = ua->argv[i];
245 } /* end strcase compare */
246 } /* end keyword loop */
248 * End of keyword for loop -- if not found, we got a bogus keyword
251 Dmsg1(200, "%s not found\n", ua->argk[i]);
253 * Special case for Job Name, it can be the first
254 * keyword that has no value.
256 if (!job_name && !ua->argv[i]) {
257 job_name = ua->argk[i]; /* use keyword as job name */
258 Dmsg1(200, "Set jobname=%s\n", job_name);
260 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
264 } /* end argc loop */
266 Dmsg0(800, "Done scan.\n");
269 if (catalog_name != NULL) {
270 catalog = (CAT *)GetResWithName(R_CATALOG, catalog_name);
271 if (catalog == NULL) {
272 bsendmsg(ua, _("Catalog \"%s\" not found\n"), catalog_name);
276 Dmsg1(200, "Using catalog=%s\n", catalog_name);
280 job = (JOB *)GetResWithName(R_JOB, job_name);
282 if (*job_name != 0) {
283 bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
285 job = select_job_resource(ua);
287 Dmsg1(200, "Found job=%s\n", job_name);
290 bsendmsg(ua, _("A job name must be specified.\n"));
291 job = select_job_resource(ua);
295 } else if (!acl_access_ok(ua, Job_ACL, job->hdr.name)) {
296 bsendmsg(ua, _("No authorization. Job \"%s\".\n"),
302 store = (STORE *)GetResWithName(R_STORAGE, store_name);
304 if (*store_name != 0) {
305 bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
307 store = select_storage_resource(ua);
310 store = (STORE *)job->storage[0]->first(); /* use default */
314 } else if (!acl_access_ok(ua, Storage_ACL, store->hdr.name)) {
315 bsendmsg(ua, _("No authorization. Storage \"%s\".\n"),
319 Dmsg1(200, "Using storage=%s\n", store->hdr.name);
322 pool = (POOL *)GetResWithName(R_POOL, pool_name);
324 if (*pool_name != 0) {
325 bsendmsg(ua, _("Pool \"%s\" not found.\n"), pool_name);
327 pool = select_pool_resource(ua);
330 pool = job->pool; /* use default */
334 } else if (!acl_access_ok(ua, Pool_ACL, store->hdr.name)) {
335 bsendmsg(ua, _("No authorization. Pool \"%s\".\n"),
339 Dmsg1(200, "Using pool\n", pool->hdr.name);
342 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
344 if (*client_name != 0) {
345 bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
347 client = select_client_resource(ua);
350 client = job->client; /* use default */
354 } else if (!acl_access_ok(ua, Client_ACL, store->hdr.name)) {
355 bsendmsg(ua, _("No authorization. Client \"%s\".\n"),
359 Dmsg1(200, "Using client=%s\n", client->hdr.name);
362 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
364 bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
365 fileset = select_fileset_resource(ua);
368 fileset = job->fileset; /* use default */
372 } else if (!acl_access_ok(ua, FileSet_ACL, store->hdr.name)) {
373 bsendmsg(ua, _("No authorization. FileSet \"%s\".\n"),
378 if (verify_job_name) {
379 verify_job = (JOB *)GetResWithName(R_JOB, verify_job_name);
381 bsendmsg(ua, _("Verify Job \"%s\" not found.\n"), verify_job_name);
382 verify_job = select_job_resource(ua);
385 verify_job = job->verify_job;
389 * Create JCR to run job. NOTE!!! after this point, free_jcr()
392 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
393 set_jcr_defaults(jcr, job);
395 jcr->verify_job = verify_job;
396 set_storage(jcr, store);
397 jcr->client = client;
398 jcr->fileset = fileset;
400 jcr->ExpectedFiles = files;
401 if (catalog != NULL) {
402 jcr->catalog = catalog;
408 jcr->where = bstrdup(where);
412 jcr->sched_time = str_to_utime(when);
413 if (jcr->sched_time == 0) {
414 bsendmsg(ua, _("Invalid time, using current time.\n"));
415 jcr->sched_time = time(NULL);
420 if (jcr->RestoreBootstrap) {
421 free(jcr->RestoreBootstrap);
423 jcr->RestoreBootstrap = bstrdup(bootstrap);
428 for (i=0; ReplaceOptions[i].name; i++) {
429 if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
430 jcr->replace = ReplaceOptions[i].token;
434 bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
437 } else if (job->replace) {
438 jcr->replace = job->replace;
440 jcr->replace = REPLACE_ALWAYS;
444 jcr->JobPriority = Priority;
447 if (find_arg(ua, _("fdcalled")) > 0) {
448 jcr->file_bsock = dup_bsock(ua->UA_sock);
453 replace = ReplaceOptions[0].name;
454 for (i=0; ReplaceOptions[i].name; i++) {
455 if (ReplaceOptions[i].token == jcr->replace) {
456 replace = ReplaceOptions[i].name;
460 if (!get_level_from_name(jcr, level_name)) {
461 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
466 jcr->RestoreJobId = atoi(jid);
469 /* Run without prompting? */
470 if (ua->batch || find_arg(ua, _("yes")) > 0) {
475 * Prompt User to see if all run job parameters are correct, and
476 * allow him to modify them.
478 Dmsg1(20, "JobType=%c\n", jcr->JobType);
479 switch (jcr->JobType) {
481 char dt[MAX_TIME_LENGTH];
483 bsendmsg(ua, _("Run %s job\n\
492 jcr->fileset->hdr.name,
493 NPRT(jcr->client->hdr.name),
494 NPRT(jcr->store->hdr.name),
495 bstrutime(dt, sizeof(dt), jcr->sched_time),
497 jcr->JobLevel = L_FULL;
501 if (jcr->JobType == JT_BACKUP) {
502 bsendmsg(ua, _("Run %s job\n\
513 jcr->fileset->hdr.name,
514 level_to_str(jcr->JobLevel),
515 jcr->client->hdr.name,
516 jcr->store->hdr.name,
517 NPRT(jcr->pool->hdr.name),
518 bstrutime(dt, sizeof(dt), jcr->sched_time),
520 } else { /* JT_VERIFY */
522 if (jcr->verify_job) {
523 Name = jcr->verify_job->hdr.name;
527 bsendmsg(ua, _("Run %s job\n\
539 jcr->fileset->hdr.name,
540 level_to_str(jcr->JobLevel),
541 jcr->client->hdr.name,
542 jcr->store->hdr.name,
543 NPRT(jcr->pool->hdr.name),
545 bstrutime(dt, sizeof(dt), jcr->sched_time),
550 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
552 jcr->RestoreJobId = atoi(jid);
554 if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
557 jcr->RestoreJobId = ua->pint32_val;
560 jcr->JobLevel = L_FULL; /* default level */
561 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
562 if (jcr->RestoreJobId == 0) {
563 bsendmsg(ua, _("Run Restore job\n"
575 NPRT(jcr->RestoreBootstrap),
576 jcr->where?jcr->where:NPRT(job->RestoreWhere),
578 jcr->fileset->hdr.name,
579 jcr->client->hdr.name,
580 jcr->store->hdr.name,
581 bstrutime(dt, sizeof(dt), jcr->sched_time),
582 jcr->catalog->hdr.name,
585 bsendmsg(ua, _("Run Restore job\n"
597 NPRT(jcr->RestoreBootstrap),
598 jcr->where?jcr->where:NPRT(job->RestoreWhere),
600 jcr->client->hdr.name,
601 jcr->store->hdr.name,
602 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1),
603 bstrutime(dt, sizeof(dt), jcr->sched_time),
604 jcr->catalog->hdr.name,
609 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
614 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
618 * At user request modify parameters of job to be run.
620 if (ua->cmd[0] == 0) {
623 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
626 start_prompt(ua, _("Parameters to modify:\n"));
627 add_prompt(ua, _("Level")); /* 0 */
628 add_prompt(ua, _("Storage")); /* 1 */
629 add_prompt(ua, _("Job")); /* 2 */
630 add_prompt(ua, _("FileSet")); /* 3 */
631 add_prompt(ua, _("Client")); /* 4 */
632 add_prompt(ua, _("When")); /* 5 */
633 add_prompt(ua, _("Priority")); /* 6 */
634 if (jcr->JobType == JT_BACKUP ||
635 jcr->JobType == JT_VERIFY) {
636 add_prompt(ua, _("Pool")); /* 7 */
637 if (jcr->JobType == JT_VERIFY) {
638 add_prompt(ua, _("Verify Job")); /* 8 */
640 } else if (jcr->JobType == JT_RESTORE) {
641 add_prompt(ua, _("Bootstrap")); /* 7 */
642 add_prompt(ua, _("Where")); /* 8 */
643 add_prompt(ua, _("Replace")); /* 9 */
644 add_prompt(ua, _("JobId")); /* 10 */
646 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
649 if (jcr->JobType == JT_BACKUP) {
650 start_prompt(ua, _("Levels:\n"));
651 add_prompt(ua, _("Base"));
652 add_prompt(ua, _("Full"));
653 add_prompt(ua, _("Incremental"));
654 add_prompt(ua, _("Differential"));
655 add_prompt(ua, _("Since"));
656 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
658 jcr->JobLevel = L_BASE;
661 jcr->JobLevel = L_FULL;
664 jcr->JobLevel = L_INCREMENTAL;
667 jcr->JobLevel = L_DIFFERENTIAL;
670 jcr->JobLevel = L_SINCE;
676 } else if (jcr->JobType == JT_VERIFY) {
677 start_prompt(ua, _("Levels:\n"));
678 add_prompt(ua, _("Initialize Catalog"));
679 add_prompt(ua, _("Verify Catalog"));
680 add_prompt(ua, _("Verify Volume to Catalog"));
681 add_prompt(ua, _("Verify Disk to Catalog"));
682 add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
683 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
685 jcr->JobLevel = L_VERIFY_INIT;
688 jcr->JobLevel = L_VERIFY_CATALOG;
691 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
694 jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
697 jcr->JobLevel = L_VERIFY_DATA;
704 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
709 store = select_storage_resource(ua);
711 set_storage(jcr, store);
717 job = select_job_resource(ua);
720 set_jcr_defaults(jcr, job);
726 fileset = select_fileset_resource(ua);
728 jcr->fileset = fileset;
734 client = select_client_resource(ua);
736 jcr->client = client;
742 if (!get_cmd(ua, _("Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now): "))) {
745 if (ua->cmd[0] == 0) {
746 jcr->sched_time = time(NULL);
748 jcr->sched_time = str_to_utime(ua->cmd);
749 if (jcr->sched_time == 0) {
750 bsendmsg(ua, _("Invalid time, using current time.\n"));
751 jcr->sched_time = time(NULL);
757 if (!get_pint(ua, _("Enter new Priority: "))) {
760 if (ua->pint32_val == 0) {
761 bsendmsg(ua, _("Priority must be a positive integer.\n"));
763 jcr->JobPriority = ua->pint32_val;
767 /* Pool or Bootstrap depending on JobType */
768 if (jcr->JobType == JT_BACKUP ||
769 jcr->JobType == JT_VERIFY) { /* Pool */
770 pool = select_pool_resource(ua);
779 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
782 if (jcr->RestoreBootstrap) {
783 free(jcr->RestoreBootstrap);
784 jcr->RestoreBootstrap = NULL;
786 if (ua->cmd[0] != 0) {
787 jcr->RestoreBootstrap = bstrdup(ua->cmd);
788 fd = fopen(jcr->RestoreBootstrap, "r");
790 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
791 jcr->RestoreBootstrap, strerror(errno));
792 free(jcr->RestoreBootstrap);
793 jcr->RestoreBootstrap = NULL;
801 if (jcr->JobType == JT_VERIFY) {
802 verify_job = select_job_resource(ua);
804 jcr->verify_job = verify_job;
809 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
816 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
819 jcr->where = bstrdup(ua->cmd);
823 start_prompt(ua, _("Replace:\n"));
824 for (i=0; ReplaceOptions[i].name; i++) {
825 add_prompt(ua, ReplaceOptions[i].name);
827 opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
829 jcr->replace = ReplaceOptions[opt].token;
834 jid = NULL; /* force reprompt */
835 jcr->RestoreJobId = 0;
836 if (jcr->RestoreBootstrap) {
837 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
846 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
848 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
850 JobId = run_job(jcr);
851 free_jcr(jcr); /* release jcr */
853 bsendmsg(ua, _("Job failed.\n"));
855 bsendmsg(ua, _("Job started. JobId=%u\n"), JobId);
861 bsendmsg(ua, _("Job not run.\n"));
863 return 0; /* do not run */