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"), /* 15 */
79 N_("files"), /* 16 number of files to restore */
99 verify_job_name = NULL;
101 for (i=1; i<ua->argc; i++) {
102 Dmsg2(200, "Doing arg %d = %s\n", i, ua->argk[i]);
104 /* Keep looking until we find a good keyword */
105 for (j=0; !kw_ok && 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*/) {
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 15: /* Verify Job */
220 if (verify_job_name) {
221 bsendmsg(ua, _("Verify Job specified twice.\n"));
224 verify_job_name = ua->argv[i];
227 case 16: /* files -- ignore for now */
234 } /* end strcase compare */
235 } /* end keyword loop */
237 * End of keyword for loop -- if not found, we got a bogus keyword
240 Dmsg1(200, "%s not found\n", ua->argk[i]);
242 * Special case for Job Name, it can be the first
243 * keyword that has no value.
245 if (!job_name && !ua->argv[i]) {
246 job_name = ua->argk[i]; /* use keyword as job name */
247 Dmsg1(200, "Set jobname=%s\n", job_name);
249 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
253 } /* end argc loop */
255 Dmsg0(200, "Done scan.\n");
259 job = (JOB *)GetResWithName(R_JOB, job_name);
261 if (*job_name != 0) {
262 bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
264 job = select_job_resource(ua);
266 Dmsg1(200, "Found job=%s\n", job_name);
269 bsendmsg(ua, _("A job name must be specified.\n"));
270 job = select_job_resource(ua);
277 store = (STORE *)GetResWithName(R_STORAGE, store_name);
279 if (*store_name != 0) {
280 bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
282 store = select_storage_resource(ua);
285 store = job->storage; /* use default */
293 pool = (POOL *)GetResWithName(R_POOL, pool_name);
295 if (*pool_name != 0) {
296 bsendmsg(ua, _("Pool \"%s\" not found.\n"), pool_name);
298 pool = select_pool_resource(ua);
301 pool = job->pool; /* use default */
308 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
310 if (*client_name != 0) {
311 bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
313 client = select_client_resource(ua);
316 client = job->client; /* use default */
323 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
325 bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
326 fileset = select_fileset_resource(ua);
329 fileset = job->fileset; /* use default */
335 if (verify_job_name) {
336 verify_job = (JOB *)GetResWithName(R_JOB, verify_job_name);
338 bsendmsg(ua, _("Verify Job \"%s\" not found.\n"), verify_job_name);
339 verify_job = select_job_resource(ua);
342 verify_job = job->verify_job;
346 * Create JCR to run job. NOTE!!! after this point, free_jcr()
349 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
350 set_jcr_defaults(jcr, job);
353 jcr->client = client;
354 jcr->fileset = fileset;
360 jcr->where = bstrdup(where);
364 jcr->sched_time = str_to_utime(when);
365 if (jcr->sched_time == 0) {
366 bsendmsg(ua, _("Invalid time, using current time.\n"));
367 jcr->sched_time = time(NULL);
372 if (jcr->RestoreBootstrap) {
373 free(jcr->RestoreBootstrap);
375 jcr->RestoreBootstrap = bstrdup(bootstrap);
380 for (i=0; ReplaceOptions[i].name; i++) {
381 if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
382 jcr->replace = ReplaceOptions[i].token;
386 bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
389 } else if (job->replace) {
390 jcr->replace = job->replace;
392 jcr->replace = REPLACE_ALWAYS;
396 jcr->JobPriority = Priority;
400 replace = ReplaceOptions[0].name;
401 for (i=0; ReplaceOptions[i].name; i++) {
402 if (ReplaceOptions[i].token == jcr->replace) {
403 replace = ReplaceOptions[i].name;
407 /* Look up level name and pull code */
409 for (i=0; joblevels[i].level_name; i++) {
410 if (strcasecmp(level_name, _(joblevels[i].level_name)) == 0) {
411 jcr->JobLevel = joblevels[i].level;
417 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
423 jcr->RestoreJobId = atoi(jid);
426 /* Run without prompting? */
427 if (find_arg(ua, _("yes")) > 0) {
428 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
430 free_jcr(jcr); /* release jcr */
431 bsendmsg(ua, _("Run command submitted.\n"));
436 * Prompt User to see if all run job parameters are correct, and
437 * allow him to modify them.
439 Dmsg1(20, "JobType=%c\n", jcr->JobType);
440 switch (jcr->JobType) {
442 char dt[MAX_TIME_LENGTH];
444 bsendmsg(ua, _("Run %s job\n\
453 jcr->fileset->hdr.name,
454 NPRT(jcr->client->hdr.name),
455 NPRT(jcr->store->hdr.name),
456 bstrutime(dt, sizeof(dt), jcr->sched_time),
458 jcr->JobLevel = L_FULL;
462 if (jcr->JobType == JT_BACKUP) {
463 bsendmsg(ua, _("Run %s job\n\
474 jcr->fileset->hdr.name,
475 level_to_str(jcr->JobLevel),
476 jcr->client->hdr.name,
477 jcr->store->hdr.name,
478 NPRT(jcr->pool->hdr.name),
479 bstrutime(dt, sizeof(dt), jcr->sched_time),
481 } else { /* JT_VERIFY */
483 if (jcr->job->verify_job) {
484 Name = jcr->job->verify_job->hdr.name;
488 bsendmsg(ua, _("Run %s job\n\
500 jcr->fileset->hdr.name,
501 level_to_str(jcr->JobLevel),
502 jcr->client->hdr.name,
503 jcr->store->hdr.name,
504 NPRT(jcr->pool->hdr.name),
506 bstrutime(dt, sizeof(dt), jcr->sched_time),
511 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
513 jcr->RestoreJobId = atoi(jid);
515 if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
518 jcr->RestoreJobId = ua->pint32_val;
521 jcr->JobLevel = L_FULL; /* default level */
522 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
523 if (jcr->RestoreJobId == 0) {
524 bsendmsg(ua, _("Run Restore job\n\
535 NPRT(jcr->RestoreBootstrap),
536 jcr->where?jcr->where:NPRT(job->RestoreWhere),
538 jcr->fileset->hdr.name,
539 jcr->client->hdr.name,
540 jcr->store->hdr.name,
541 bstrutime(dt, sizeof(dt), jcr->sched_time),
544 bsendmsg(ua, _("Run Restore job\n\
556 NPRT(jcr->RestoreBootstrap),
557 jcr->where?jcr->where:NPRT(job->RestoreWhere),
559 jcr->fileset->hdr.name,
560 jcr->client->hdr.name,
561 jcr->store->hdr.name,
562 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1),
563 bstrutime(dt, sizeof(dt), jcr->sched_time),
568 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
573 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
577 * At user request modify parameters of job to be run.
579 if (ua->cmd[0] == 0) {
582 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
585 start_prompt(ua, _("Parameters to modify:\n"));
586 add_prompt(ua, _("Level")); /* 0 */
587 add_prompt(ua, _("Storage")); /* 1 */
588 add_prompt(ua, _("Job")); /* 2 */
589 add_prompt(ua, _("FileSet")); /* 3 */
590 add_prompt(ua, _("Client")); /* 4 */
591 add_prompt(ua, _("When")); /* 5 */
592 add_prompt(ua, _("Priority")); /* 6 */
593 if (jcr->JobType == JT_BACKUP ||
594 jcr->JobType == JT_VERIFY) {
595 add_prompt(ua, _("Pool")); /* 7 */
596 if (jcr->JobType == JT_VERIFY) {
597 add_prompt(ua, _("Verify Job")); /* 8 */
599 } else if (jcr->JobType == JT_RESTORE) {
600 add_prompt(ua, _("Bootstrap")); /* 7 */
601 add_prompt(ua, _("Where")); /* 8 */
602 add_prompt(ua, _("Replace")); /* 9 */
603 add_prompt(ua, _("JobId")); /* 10 */
605 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
608 if (jcr->JobType == JT_BACKUP) {
609 start_prompt(ua, _("Levels:\n"));
610 add_prompt(ua, _("Base"));
611 add_prompt(ua, _("Full"));
612 add_prompt(ua, _("Incremental"));
613 add_prompt(ua, _("Differential"));
614 add_prompt(ua, _("Since"));
615 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
617 jcr->JobLevel = L_BASE;
620 jcr->JobLevel = L_FULL;
623 jcr->JobLevel = L_INCREMENTAL;
626 jcr->JobLevel = L_DIFFERENTIAL;
629 jcr->JobLevel = L_SINCE;
635 } else if (jcr->JobType == JT_VERIFY) {
636 start_prompt(ua, _("Levels:\n"));
637 add_prompt(ua, _("Initialize Catalog"));
638 add_prompt(ua, _("Verify Catalog"));
639 add_prompt(ua, _("Verify Volume to Catalog"));
640 add_prompt(ua, _("Verify Disk to Catalog"));
641 add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
642 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
644 jcr->JobLevel = L_VERIFY_INIT;
647 jcr->JobLevel = L_VERIFY_CATALOG;
650 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
653 jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
656 jcr->JobLevel = L_VERIFY_DATA;
663 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
668 store = select_storage_resource(ua);
676 job = select_job_resource(ua);
679 set_jcr_defaults(jcr, job);
685 fileset = select_fileset_resource(ua);
687 jcr->fileset = fileset;
693 client = select_client_resource(ua);
695 jcr->client = client;
701 if (!get_cmd(ua, _("Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now): "))) {
704 if (ua->cmd[0] == 0) {
705 jcr->sched_time = time(NULL);
707 jcr->sched_time = str_to_utime(ua->cmd);
708 if (jcr->sched_time == 0) {
709 bsendmsg(ua, _("Invalid time, using current time.\n"));
710 jcr->sched_time = time(NULL);
716 if (!get_pint(ua, _("Enter new Priority: "))) {
719 if (ua->pint32_val == 0) {
720 bsendmsg(ua, _("Priority must be a positive integer.\n"));
722 jcr->JobPriority = ua->pint32_val;
726 /* Pool or Bootstrap depending on JobType */
727 if (jcr->JobType == JT_BACKUP ||
728 jcr->JobType == JT_VERIFY) { /* Pool */
729 pool = select_pool_resource(ua);
738 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
741 if (jcr->RestoreBootstrap) {
742 free(jcr->RestoreBootstrap);
743 jcr->RestoreBootstrap = NULL;
745 if (ua->cmd[0] != 0) {
746 jcr->RestoreBootstrap = bstrdup(ua->cmd);
747 fd = fopen(jcr->RestoreBootstrap, "r");
749 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
750 jcr->RestoreBootstrap, strerror(errno));
751 free(jcr->RestoreBootstrap);
752 jcr->RestoreBootstrap = NULL;
760 if (jcr->JobType == JT_VERIFY) {
761 JOB *job = select_job_resource(ua);
763 jcr->job->verify_job = job;
765 jcr->job->verify_job = NULL;
770 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
777 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
780 jcr->where = bstrdup(ua->cmd);
784 start_prompt(ua, _("Replace:\n"));
785 for (i=0; ReplaceOptions[i].name; i++) {
786 add_prompt(ua, ReplaceOptions[i].name);
788 opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
790 jcr->replace = ReplaceOptions[opt].token;
795 jid = NULL; /* force reprompt */
796 jcr->RestoreJobId = 0;
797 if (jcr->RestoreBootstrap) {
798 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
807 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
808 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
810 free_jcr(jcr); /* release jcr */
811 bsendmsg(ua, _("Run command submitted.\n"));
816 bsendmsg(ua, _("Job not run.\n"));
818 return 0; /* do not run */