3 * Bacula Director -- Run Command
5 * Kern Sibbald, December MMI
11 Copyright (C) 2001-2004 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_kw ReplaceOptions[];
39 * For Backup and Verify Jobs
40 * run [job=]<job-name> level=<level-name>
43 * run <job-name> jobid=nn
46 int run_cmd(UAContext *ua, const char *cmd)
49 char *job_name, *level_name, *jid, *store_name, *pool_name;
50 char *where, *fileset_name, *client_name, *bootstrap;
52 char *when, *verify_job_name, *catalog_name;
54 int i, j, opt, files = 0;
57 JOB *verify_job = NULL;
59 CLIENT *client = NULL;
60 FILESET *fileset = NULL;
62 static const 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"), /* 15 */
79 N_("files"), /* 16 number of files to restore */
80 N_("catalog"), /* 17 override catalog */
100 verify_job_name = NULL;
103 for (i=1; i<ua->argc; i++) {
104 Dmsg2(200, "Doing arg %d = %s\n", i, ua->argk[i]);
106 /* Keep looking until we find a good keyword */
107 for (j=0; !kw_ok && kw[j]; j++) {
108 if (strcasecmp(ua->argk[i], _(kw[j])) == 0) {
109 /* Note, yes and run have no value, so do not err */
110 if (!ua->argv[i] && j != YES_POS /*yes*/) {
111 bsendmsg(ua, _("Value missing for keyword %s\n"), ua->argk[i]);
114 Dmsg1(200, "Got keyword=%s\n", kw[j]);
118 bsendmsg(ua, _("Job name specified twice.\n"));
121 job_name = ua->argv[i];
126 bsendmsg(ua, _("JobId specified twice.\n"));
135 bsendmsg(ua, _("Client specified twice.\n"));
138 client_name = ua->argv[i];
141 case 4: /* fileset */
143 bsendmsg(ua, _("FileSet specified twice.\n"));
146 fileset_name = ua->argv[i];
151 bsendmsg(ua, _("Level specified twice.\n"));
154 level_name = ua->argv[i];
157 case 6: /* storage */
160 bsendmsg(ua, _("Storage specified twice.\n"));
163 store_name = ua->argv[i];
168 bsendmsg(ua, _("Pool specified twice.\n"));
171 pool_name = ua->argv[i];
176 bsendmsg(ua, _("Where specified twice.\n"));
182 case 10: /* bootstrap */
184 bsendmsg(ua, _("Bootstrap specified twice.\n"));
187 bootstrap = ua->argv[i];
190 case 11: /* replace */
192 bsendmsg(ua, _("Replace specified twice.\n"));
195 replace = ua->argv[i];
200 bsendmsg(ua, _("When specified twice.\n"));
206 case 13: /* Priority */
208 bsendmsg(ua, _("Priority specified twice.\n"));
211 Priority = atoi(ua->argv[i]);
213 bsendmsg(ua, _("Priority must be positive nonzero setting it to 10.\n"));
221 case 15: /* Verify Job */
222 if (verify_job_name) {
223 bsendmsg(ua, _("Verify Job specified twice.\n"));
226 verify_job_name = ua->argv[i];
230 files = atoi(ua->argv[i]);
234 case 17: /* catalog */
235 catalog_name = ua->argv[i];
242 } /* end strcase compare */
243 } /* end keyword loop */
245 * End of keyword for loop -- if not found, we got a bogus keyword
248 Dmsg1(200, "%s not found\n", ua->argk[i]);
250 * Special case for Job Name, it can be the first
251 * keyword that has no value.
253 if (!job_name && !ua->argv[i]) {
254 job_name = ua->argk[i]; /* use keyword as job name */
255 Dmsg1(200, "Set jobname=%s\n", job_name);
257 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
261 } /* end argc loop */
263 Dmsg0(200, "Done scan.\n");
266 if (catalog_name != NULL) {
267 catalog = (CAT *)GetResWithName(R_CATALOG, catalog_name);
268 if (catalog == NULL) {
269 bsendmsg(ua, _("Catalog \"%s\" not found\n"), catalog_name);
276 job = (JOB *)GetResWithName(R_JOB, job_name);
278 if (*job_name != 0) {
279 bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
281 job = select_job_resource(ua);
283 Dmsg1(200, "Found job=%s\n", job_name);
286 bsendmsg(ua, _("A job name must be specified.\n"));
287 job = select_job_resource(ua);
291 } else if (!acl_access_ok(ua, Job_ACL, job->hdr.name)) {
292 bsendmsg(ua, _("No authorization. Job \"%s\".\n"),
298 store = (STORE *)GetResWithName(R_STORAGE, store_name);
300 if (*store_name != 0) {
301 bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
303 store = select_storage_resource(ua);
306 store = (STORE *)job->storage[0]->first(); /* use default */
310 } else if (!acl_access_ok(ua, Storage_ACL, store->hdr.name)) {
311 bsendmsg(ua, _("No authorization. Storage \"%s\".\n"),
317 pool = (POOL *)GetResWithName(R_POOL, pool_name);
319 if (*pool_name != 0) {
320 bsendmsg(ua, _("Pool \"%s\" not found.\n"), pool_name);
322 pool = select_pool_resource(ua);
325 pool = job->pool; /* use default */
329 } else if (!acl_access_ok(ua, Pool_ACL, store->hdr.name)) {
330 bsendmsg(ua, _("No authorization. Pool \"%s\".\n"),
336 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
338 if (*client_name != 0) {
339 bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
341 client = select_client_resource(ua);
344 client = job->client; /* use default */
348 } else if (!acl_access_ok(ua, Client_ACL, store->hdr.name)) {
349 bsendmsg(ua, _("No authorization. Client \"%s\".\n"),
355 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
357 bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
358 fileset = select_fileset_resource(ua);
361 fileset = job->fileset; /* use default */
365 } else if (!acl_access_ok(ua, FileSet_ACL, store->hdr.name)) {
366 bsendmsg(ua, _("No authorization. FileSet \"%s\".\n"),
371 if (verify_job_name) {
372 verify_job = (JOB *)GetResWithName(R_JOB, verify_job_name);
374 bsendmsg(ua, _("Verify Job \"%s\" not found.\n"), verify_job_name);
375 verify_job = select_job_resource(ua);
378 verify_job = job->verify_job;
382 * Create JCR to run job. NOTE!!! after this point, free_jcr()
385 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
386 set_jcr_defaults(jcr, job);
388 jcr->verify_job = verify_job;
390 jcr->client = client;
391 jcr->fileset = fileset;
393 jcr->ExpectedFiles = files;
394 if (catalog != NULL) {
395 jcr->catalog = catalog;
401 jcr->where = bstrdup(where);
405 jcr->sched_time = str_to_utime(when);
406 if (jcr->sched_time == 0) {
407 bsendmsg(ua, _("Invalid time, using current time.\n"));
408 jcr->sched_time = time(NULL);
413 if (jcr->RestoreBootstrap) {
414 free(jcr->RestoreBootstrap);
416 jcr->RestoreBootstrap = bstrdup(bootstrap);
421 for (i=0; ReplaceOptions[i].name; i++) {
422 if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
423 jcr->replace = ReplaceOptions[i].token;
427 bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
430 } else if (job->replace) {
431 jcr->replace = job->replace;
433 jcr->replace = REPLACE_ALWAYS;
437 jcr->JobPriority = Priority;
440 if (find_arg(ua, _("fdcalled")) > 0) {
441 jcr->file_bsock = dup_bsock(ua->UA_sock);
446 replace = ReplaceOptions[0].name;
447 for (i=0; ReplaceOptions[i].name; i++) {
448 if (ReplaceOptions[i].token == jcr->replace) {
449 replace = ReplaceOptions[i].name;
453 if (!get_level_from_name(jcr, level_name)) {
454 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
459 jcr->RestoreJobId = atoi(jid);
462 /* Run without prompting? */
463 if (find_arg(ua, _("yes")) > 0) {
468 * Prompt User to see if all run job parameters are correct, and
469 * allow him to modify them.
471 Dmsg1(20, "JobType=%c\n", jcr->JobType);
472 switch (jcr->JobType) {
474 char dt[MAX_TIME_LENGTH];
476 bsendmsg(ua, _("Run %s job\n\
485 jcr->fileset->hdr.name,
486 NPRT(jcr->client->hdr.name),
487 NPRT(jcr->store->hdr.name),
488 bstrutime(dt, sizeof(dt), jcr->sched_time),
490 jcr->JobLevel = L_FULL;
494 if (jcr->JobType == JT_BACKUP) {
495 bsendmsg(ua, _("Run %s job\n\
506 jcr->fileset->hdr.name,
507 level_to_str(jcr->JobLevel),
508 jcr->client->hdr.name,
509 jcr->store->hdr.name,
510 NPRT(jcr->pool->hdr.name),
511 bstrutime(dt, sizeof(dt), jcr->sched_time),
513 } else { /* JT_VERIFY */
515 if (jcr->verify_job) {
516 Name = jcr->verify_job->hdr.name;
520 bsendmsg(ua, _("Run %s job\n\
532 jcr->fileset->hdr.name,
533 level_to_str(jcr->JobLevel),
534 jcr->client->hdr.name,
535 jcr->store->hdr.name,
536 NPRT(jcr->pool->hdr.name),
538 bstrutime(dt, sizeof(dt), jcr->sched_time),
543 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
545 jcr->RestoreJobId = atoi(jid);
547 if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
550 jcr->RestoreJobId = ua->pint32_val;
553 jcr->JobLevel = L_FULL; /* default level */
554 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
555 if (jcr->RestoreJobId == 0) {
556 bsendmsg(ua, _("Run Restore job\n"
568 NPRT(jcr->RestoreBootstrap),
569 jcr->where?jcr->where:NPRT(job->RestoreWhere),
571 jcr->fileset->hdr.name,
572 jcr->client->hdr.name,
573 jcr->store->hdr.name,
574 bstrutime(dt, sizeof(dt), jcr->sched_time),
575 jcr->catalog->hdr.name,
578 bsendmsg(ua, _("Run Restore job\n"
590 NPRT(jcr->RestoreBootstrap),
591 jcr->where?jcr->where:NPRT(job->RestoreWhere),
593 jcr->client->hdr.name,
594 jcr->store->hdr.name,
595 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1),
596 bstrutime(dt, sizeof(dt), jcr->sched_time),
597 jcr->catalog->hdr.name,
602 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
607 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
611 * At user request modify parameters of job to be run.
613 if (ua->cmd[0] == 0) {
616 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
619 start_prompt(ua, _("Parameters to modify:\n"));
620 add_prompt(ua, _("Level")); /* 0 */
621 add_prompt(ua, _("Storage")); /* 1 */
622 add_prompt(ua, _("Job")); /* 2 */
623 add_prompt(ua, _("FileSet")); /* 3 */
624 add_prompt(ua, _("Client")); /* 4 */
625 add_prompt(ua, _("When")); /* 5 */
626 add_prompt(ua, _("Priority")); /* 6 */
627 if (jcr->JobType == JT_BACKUP ||
628 jcr->JobType == JT_VERIFY) {
629 add_prompt(ua, _("Pool")); /* 7 */
630 if (jcr->JobType == JT_VERIFY) {
631 add_prompt(ua, _("Verify Job")); /* 8 */
633 } else if (jcr->JobType == JT_RESTORE) {
634 add_prompt(ua, _("Bootstrap")); /* 7 */
635 add_prompt(ua, _("Where")); /* 8 */
636 add_prompt(ua, _("Replace")); /* 9 */
637 add_prompt(ua, _("JobId")); /* 10 */
639 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
642 if (jcr->JobType == JT_BACKUP) {
643 start_prompt(ua, _("Levels:\n"));
644 add_prompt(ua, _("Base"));
645 add_prompt(ua, _("Full"));
646 add_prompt(ua, _("Incremental"));
647 add_prompt(ua, _("Differential"));
648 add_prompt(ua, _("Since"));
649 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
651 jcr->JobLevel = L_BASE;
654 jcr->JobLevel = L_FULL;
657 jcr->JobLevel = L_INCREMENTAL;
660 jcr->JobLevel = L_DIFFERENTIAL;
663 jcr->JobLevel = L_SINCE;
669 } else if (jcr->JobType == JT_VERIFY) {
670 start_prompt(ua, _("Levels:\n"));
671 add_prompt(ua, _("Initialize Catalog"));
672 add_prompt(ua, _("Verify Catalog"));
673 add_prompt(ua, _("Verify Volume to Catalog"));
674 add_prompt(ua, _("Verify Disk to Catalog"));
675 add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
676 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
678 jcr->JobLevel = L_VERIFY_INIT;
681 jcr->JobLevel = L_VERIFY_CATALOG;
684 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
687 jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
690 jcr->JobLevel = L_VERIFY_DATA;
697 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
702 store = select_storage_resource(ua);
710 job = select_job_resource(ua);
713 set_jcr_defaults(jcr, job);
719 fileset = select_fileset_resource(ua);
721 jcr->fileset = fileset;
727 client = select_client_resource(ua);
729 jcr->client = client;
735 if (!get_cmd(ua, _("Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now): "))) {
738 if (ua->cmd[0] == 0) {
739 jcr->sched_time = time(NULL);
741 jcr->sched_time = str_to_utime(ua->cmd);
742 if (jcr->sched_time == 0) {
743 bsendmsg(ua, _("Invalid time, using current time.\n"));
744 jcr->sched_time = time(NULL);
750 if (!get_pint(ua, _("Enter new Priority: "))) {
753 if (ua->pint32_val == 0) {
754 bsendmsg(ua, _("Priority must be a positive integer.\n"));
756 jcr->JobPriority = ua->pint32_val;
760 /* Pool or Bootstrap depending on JobType */
761 if (jcr->JobType == JT_BACKUP ||
762 jcr->JobType == JT_VERIFY) { /* Pool */
763 pool = select_pool_resource(ua);
772 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
775 if (jcr->RestoreBootstrap) {
776 free(jcr->RestoreBootstrap);
777 jcr->RestoreBootstrap = NULL;
779 if (ua->cmd[0] != 0) {
780 jcr->RestoreBootstrap = bstrdup(ua->cmd);
781 fd = fopen(jcr->RestoreBootstrap, "r");
783 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
784 jcr->RestoreBootstrap, strerror(errno));
785 free(jcr->RestoreBootstrap);
786 jcr->RestoreBootstrap = NULL;
794 if (jcr->JobType == JT_VERIFY) {
795 verify_job = select_job_resource(ua);
797 jcr->verify_job = verify_job;
802 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
809 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
812 jcr->where = bstrdup(ua->cmd);
816 start_prompt(ua, _("Replace:\n"));
817 for (i=0; ReplaceOptions[i].name; i++) {
818 add_prompt(ua, ReplaceOptions[i].name);
820 opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
822 jcr->replace = ReplaceOptions[opt].token;
827 jid = NULL; /* force reprompt */
828 jcr->RestoreJobId = 0;
829 if (jcr->RestoreBootstrap) {
830 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
839 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
841 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
843 JobId = run_job(jcr);
844 free_jcr(jcr); /* release jcr */
846 bsendmsg(ua, _("Job failed.\n"));
848 bsendmsg(ua, _("Job started. JobId=%u\n"), JobId);
854 bsendmsg(ua, _("Job not run.\n"));
856 return 0; /* do not run */