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_("run"), /* 15 -- if you change this change RUN_POS too */
79 N_("verifyjob"), /* 16 */
100 verify_job_name = NULL;
102 for (i=1; i<ua->argc; i++) {
104 Dmsg2(200, "Doing arg %d = %s\n", i, ua->argk[i]);
105 for (j=0; !found && kw[j]; j++) {
106 if (strcasecmp(ua->argk[i], _(kw[j])) == 0) {
107 /* Note, yes and run have no value, so do not err */
108 if (!ua->argv[i] && (j != YES_POS /*yes*/ && j != RUN_POS)) {
109 bsendmsg(ua, _("Value missing for keyword %s\n"), ua->argk[i]);
112 Dmsg1(200, "Got keyword=%s\n", kw[j]);
116 bsendmsg(ua, _("Job name specified twice.\n"));
119 job_name = ua->argv[i];
124 bsendmsg(ua, _("JobId specified twice.\n"));
133 bsendmsg(ua, _("Client specified twice.\n"));
136 client_name = ua->argv[i];
139 case 4: /* fileset */
141 bsendmsg(ua, _("FileSet specified twice.\n"));
144 fileset_name = ua->argv[i];
149 bsendmsg(ua, _("Level specified twice.\n"));
152 level_name = ua->argv[i];
155 case 6: /* storage */
158 bsendmsg(ua, _("Storage specified twice.\n"));
161 store_name = ua->argv[i];
166 bsendmsg(ua, _("Pool specified twice.\n"));
169 pool_name = ua->argv[i];
174 bsendmsg(ua, _("Where specified twice.\n"));
180 case 10: /* bootstrap */
182 bsendmsg(ua, _("Bootstrap specified twice.\n"));
185 bootstrap = ua->argv[i];
188 case 11: /* replace */
190 bsendmsg(ua, _("Replace specified twice.\n"));
193 replace = ua->argv[i];
198 bsendmsg(ua, _("When specified twice.\n"));
204 case 13: /* Priority */
206 bsendmsg(ua, _("Priority specified twice.\n"));
209 Priority = atoi(ua->argv[i]);
211 bsendmsg(ua, _("Priority must be positive nonzero setting it to 10.\n"));
219 case 16: /* Verify Job */
220 if (verify_job_name) {
221 bsendmsg(ua, _("Verify Job specified twice.\n"));
224 verify_job_name = ua->argv[i];
231 } /* end strcase compare */
232 } /* end keyword loop */
234 Dmsg1(200, "%s not found\n", ua->argk[i]);
236 * Special case for Job Name, it can be the first
237 * keyword that has no value.
239 if (!job_name && !ua->argv[i]) {
240 job_name = ua->argk[i]; /* use keyword as job name */
241 Dmsg1(200, "Set jobname=%s\n", job_name);
243 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
247 } /* end argc loop */
249 Dmsg0(200, "Done scan.\n");
253 job = (JOB *)GetResWithName(R_JOB, job_name);
255 bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
256 job = select_job_resource(ua);
258 Dmsg1(200, "Found job=%s\n", job_name);
261 bsendmsg(ua, _("A job name must be specified.\n"));
262 job = select_job_resource(ua);
269 store = (STORE *)GetResWithName(R_STORAGE, store_name);
271 bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
272 store = select_storage_resource(ua);
275 store = job->storage; /* use default */
283 pool = (POOL *)GetResWithName(R_POOL, pool_name);
285 bsendmsg(ua, _("Pool \"%s\" not found.\n"), pool_name);
286 pool = get_pool_resource(ua);
289 pool = job->pool; /* use default */
293 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
295 bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
296 client = select_client_resource(ua);
299 client = job->client; /* use default */
306 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
308 bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
309 fileset = select_fileset_resource(ua);
312 fileset = job->fileset; /* use default */
318 if (verify_job_name) {
319 verify_job = (JOB *)GetResWithName(R_JOB, verify_job_name);
321 bsendmsg(ua, _("Verify Job \"%s\" not found.\n"), verify_job_name);
322 verify_job = select_job_resource(ua);
325 verify_job = job->verify_job;
329 * Create JCR to run job. NOTE!!! after this point, free_jcr()
332 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
333 set_jcr_defaults(jcr, job);
336 jcr->client = client;
337 jcr->fileset = fileset;
343 jcr->where = bstrdup(where);
347 jcr->sched_time = str_to_utime(when);
348 if (jcr->sched_time == 0) {
349 bsendmsg(ua, _("Invalid time, using current time.\n"));
350 jcr->sched_time = time(NULL);
355 if (jcr->RestoreBootstrap) {
356 free(jcr->RestoreBootstrap);
358 jcr->RestoreBootstrap = bstrdup(bootstrap);
363 for (i=0; ReplaceOptions[i].name; i++) {
364 if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
365 jcr->replace = ReplaceOptions[i].token;
369 bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
372 } else if (job->replace) {
373 jcr->replace = job->replace;
375 jcr->replace = REPLACE_ALWAYS;
379 jcr->JobPriority = Priority;
383 replace = ReplaceOptions[0].name;
384 for (i=0; ReplaceOptions[i].name; i++) {
385 if (ReplaceOptions[i].token == jcr->replace) {
386 replace = ReplaceOptions[i].name;
389 Dmsg1(20, "JobType=%c\n", jcr->JobType);
390 switch (jcr->JobType) {
392 char dt[MAX_TIME_LENGTH];
394 bsendmsg(ua, _("Run %s job\n\
403 jcr->fileset->hdr.name,
404 NPRT(jcr->client->hdr.name),
405 NPRT(jcr->store->hdr.name),
406 bstrutime(dt, sizeof(dt), jcr->sched_time),
408 jcr->JobLevel = L_FULL;
413 /* Look up level name and pull code */
415 for (i=0; joblevels[i].level_name; i++) {
416 if (strcasecmp(level_name, _(joblevels[i].level_name)) == 0) {
417 jcr->JobLevel = joblevels[i].level;
423 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
428 if (jcr->JobType == JT_BACKUP) {
429 bsendmsg(ua, _("Run %s job\n\
440 jcr->fileset->hdr.name,
441 level_to_str(jcr->JobLevel),
442 jcr->client->hdr.name,
443 jcr->store->hdr.name,
444 NPRT(jcr->pool->hdr.name),
445 bstrutime(dt, sizeof(dt), jcr->sched_time),
447 } else { /* JT_VERIFY */
449 if (jcr->job->verify_job) {
450 Name = jcr->job->verify_job->hdr.name;
454 bsendmsg(ua, _("Run %s job\n\
466 jcr->fileset->hdr.name,
467 level_to_str(jcr->JobLevel),
468 jcr->client->hdr.name,
469 jcr->store->hdr.name,
470 NPRT(jcr->pool->hdr.name),
472 bstrutime(dt, sizeof(dt), jcr->sched_time),
477 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
479 jcr->RestoreJobId = atoi(jid);
481 if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
484 jcr->RestoreJobId = ua->pint32_val;
487 jcr->JobLevel = L_FULL; /* default level */
488 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
489 if (jcr->RestoreJobId == 0) {
490 bsendmsg(ua, _("Run Restore job\n\
501 NPRT(jcr->RestoreBootstrap),
502 jcr->where?jcr->where:NPRT(job->RestoreWhere),
504 jcr->fileset->hdr.name,
505 jcr->client->hdr.name,
506 jcr->store->hdr.name,
507 bstrutime(dt, sizeof(dt), jcr->sched_time),
510 bsendmsg(ua, _("Run Restore job\n\
522 NPRT(jcr->RestoreBootstrap),
523 jcr->where?jcr->where:NPRT(job->RestoreWhere),
525 jcr->fileset->hdr.name,
526 jcr->client->hdr.name,
527 jcr->store->hdr.name,
528 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1),
529 bstrutime(dt, sizeof(dt), jcr->sched_time),
534 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
538 /* Run without prompting? */
539 if (find_arg(ua, _("yes")) > 0) {
540 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
542 free_jcr(jcr); /* release jcr */
543 bsendmsg(ua, _("Run command submitted.\n"));
547 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
551 * At user request modify parameters of job to be run.
553 if (ua->cmd[0] == 0) {
556 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
559 start_prompt(ua, _("Parameters to modify:\n"));
560 add_prompt(ua, _("Level")); /* 0 */
561 add_prompt(ua, _("Storage")); /* 1 */
562 add_prompt(ua, _("Job")); /* 2 */
563 add_prompt(ua, _("FileSet")); /* 3 */
564 add_prompt(ua, _("Client")); /* 4 */
565 add_prompt(ua, _("When")); /* 5 */
566 add_prompt(ua, _("Priority")); /* 6 */
567 if (jcr->JobType == JT_BACKUP ||
568 jcr->JobType == JT_VERIFY) {
569 add_prompt(ua, _("Pool")); /* 7 */
570 if (jcr->JobType == JT_VERIFY) {
571 add_prompt(ua, _("Verify Job")); /* 8 */
573 } else if (jcr->JobType == JT_RESTORE) {
574 add_prompt(ua, _("Bootstrap")); /* 7 */
575 add_prompt(ua, _("Where")); /* 8 */
576 add_prompt(ua, _("Replace")); /* 9 */
577 add_prompt(ua, _("JobId")); /* 10 */
579 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
582 if (jcr->JobType == JT_BACKUP) {
583 start_prompt(ua, _("Levels:\n"));
584 add_prompt(ua, _("Base"));
585 add_prompt(ua, _("Full"));
586 add_prompt(ua, _("Incremental"));
587 add_prompt(ua, _("Differential"));
588 add_prompt(ua, _("Since"));
589 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
591 jcr->JobLevel = L_BASE;
594 jcr->JobLevel = L_FULL;
597 jcr->JobLevel = L_INCREMENTAL;
600 jcr->JobLevel = L_DIFFERENTIAL;
603 jcr->JobLevel = L_SINCE;
609 } else if (jcr->JobType == JT_VERIFY) {
610 start_prompt(ua, _("Levels:\n"));
611 add_prompt(ua, _("Initialize Catalog"));
612 add_prompt(ua, _("Verify Catalog"));
613 add_prompt(ua, _("Verify Volume to Catalog"));
614 add_prompt(ua, _("Verify Disk to Catalog"));
615 add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
616 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
618 jcr->JobLevel = L_VERIFY_INIT;
621 jcr->JobLevel = L_VERIFY_CATALOG;
624 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
627 jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
630 jcr->JobLevel = L_VERIFY_DATA;
637 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
642 store = select_storage_resource(ua);
650 job = select_job_resource(ua);
653 set_jcr_defaults(jcr, job);
659 fileset = select_fileset_resource(ua);
661 jcr->fileset = fileset;
667 client = select_client_resource(ua);
669 jcr->client = client;
675 if (!get_cmd(ua, _("Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now): "))) {
678 if (ua->cmd[0] == 0) {
679 jcr->sched_time = time(NULL);
681 jcr->sched_time = str_to_utime(ua->cmd);
682 if (jcr->sched_time == 0) {
683 bsendmsg(ua, _("Invalid time, using current time.\n"));
684 jcr->sched_time = time(NULL);
690 if (!get_pint(ua, _("Enter new Priority: "))) {
693 if (ua->pint32_val == 0) {
694 bsendmsg(ua, _("Priority must be a positive integer.\n"));
696 jcr->JobPriority = ua->pint32_val;
700 /* Pool or Bootstrap depending on JobType */
701 if (jcr->JobType == JT_BACKUP ||
702 jcr->JobType == JT_VERIFY) { /* Pool */
703 pool = select_pool_resource(ua);
712 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
715 if (jcr->RestoreBootstrap) {
716 free(jcr->RestoreBootstrap);
717 jcr->RestoreBootstrap = NULL;
719 if (ua->cmd[0] != 0) {
720 jcr->RestoreBootstrap = bstrdup(ua->cmd);
721 fd = fopen(jcr->RestoreBootstrap, "r");
723 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
724 jcr->RestoreBootstrap, strerror(errno));
725 free(jcr->RestoreBootstrap);
726 jcr->RestoreBootstrap = NULL;
734 if (jcr->JobType == JT_VERIFY) {
735 JOB *job = select_job_resource(ua);
737 jcr->job->verify_job = job;
739 jcr->job->verify_job = NULL;
744 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
751 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
754 jcr->where = bstrdup(ua->cmd);
758 start_prompt(ua, _("Replace:\n"));
759 for (i=0; ReplaceOptions[i].name; i++) {
760 add_prompt(ua, ReplaceOptions[i].name);
762 opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
764 jcr->replace = ReplaceOptions[opt].token;
769 jid = NULL; /* force reprompt */
770 jcr->RestoreJobId = 0;
771 if (jcr->RestoreBootstrap) {
772 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
781 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
782 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
784 free_jcr(jcr); /* release jcr */
785 bsendmsg(ua, _("Run command submitted.\n"));
790 bsendmsg(ua, _("Job not run.\n"));
792 return 0; /* do not run */