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 */
34 extern void run_job(JCR *jcr);
36 /* Imported variables */
37 extern struct s_jl joblevels[];
38 extern struct s_kw ReplaceOptions[];
41 * For Backup and Verify Jobs
42 * run [job=]<job-name> level=<level-name>
45 * run <job-name> jobid=nn
48 int runcmd(UAContext *ua, char *cmd)
51 char *job_name, *level_name, *jid, *store_name, *pool_name;
52 char *where, *fileset_name, *client_name, *bootstrap, *replace;
57 CLIENT *client = NULL;
58 FILESET *fileset = NULL;
90 for (i=1; i<ua->argc; i++) {
92 Dmsg2(200, "Doing arg %d = %s\n", i, ua->argk[i]);
93 for (j=0; !found && kw[j]; j++) {
94 if (strcasecmp(ua->argk[i], _(kw[j])) == 0) {
96 bsendmsg(ua, _("Value missing for keyword %s\n"), ua->argk[i]);
99 Dmsg1(200, "Got keyword=%s\n", kw[j]);
103 bsendmsg(ua, _("Job name specified twice.\n"));
106 job_name = ua->argv[i];
111 bsendmsg(ua, _("JobId specified twice.\n"));
119 bsendmsg(ua, _("Client specified twice.\n"));
122 client_name = ua->argv[i];
125 case 3: /* fileset */
127 bsendmsg(ua, _("FileSet specified twice.\n"));
130 fileset_name = ua->argv[i];
135 bsendmsg(ua, _("Level specified twice.\n"));
138 level_name = ua->argv[i];
141 case 5: /* storage */
143 bsendmsg(ua, _("Storage specified twice.\n"));
146 store_name = ua->argv[i];
151 bsendmsg(ua, _("Pool specified twice.\n"));
154 pool_name = ua->argv[i];
159 bsendmsg(ua, _("Where specified twice.\n"));
165 case 8: /* bootstrap */
167 bsendmsg(ua, _("Bootstrap specified twice.\n"));
170 bootstrap = ua->argv[i];
173 case 9: /* replace */
175 bsendmsg(ua, _("Replace specified twice.\n"));
178 replace = ua->argv[i];
183 bsendmsg(ua, _("When specified twice.\n"));
192 } /* end strcase compare */
193 } /* end keyword loop */
195 Dmsg1(200, "%s not found\n", ua->argk[i]);
197 * Special case for Job Name, it can be the first
198 * keyword that has no value.
200 if (!job_name && !ua->argv[i]) {
201 job_name = ua->argk[i]; /* use keyword as job name */
202 Dmsg1(200, "Set jobname=%s\n", job_name);
204 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
208 } /* end argc loop */
210 Dmsg0(200, "Done scan.\n");
216 job = (JOB *)GetResWithName(R_JOB, job_name);
218 bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
219 job = select_job_resource(ua);
221 Dmsg1(200, "Found job=%s\n", job_name);
224 bsendmsg(ua, _("A job name must be specified.\n"));
225 job = select_job_resource(ua);
232 store = (STORE *)GetResWithName(R_STORAGE, store_name);
234 bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
235 store = select_storage_resource(ua);
238 store = job->storage; /* use default */
246 pool = (POOL *)GetResWithName(R_POOL, pool_name);
248 bsendmsg(ua, _("Pool \"%s\" not found.\n"), pool_name);
249 pool = get_pool_resource(ua);
252 pool = job->pool; /* use default */
256 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
258 bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
259 client = select_client_resource(ua);
262 client = job->client; /* use default */
269 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
271 bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
272 fileset = select_fileset_resource(ua);
275 fileset = job->fileset; /* use default */
282 /* Create JCR to run job */
283 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
284 set_jcr_defaults(jcr, job);
287 jcr->client = client;
288 jcr->fileset = fileset;
294 jcr->where = bstrdup(where);
298 jcr->sched_time = str_to_utime(when);
299 if (jcr->sched_time == 0) {
300 bsendmsg(ua, _("Invalid time, using current time.\n"));
301 jcr->sched_time = time(NULL);
306 if (jcr->RestoreBootstrap) {
307 free(jcr->RestoreBootstrap);
309 jcr->RestoreBootstrap = bstrdup(bootstrap);
314 for (i=0; ReplaceOptions[i].name; i++) {
315 if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
316 jcr->replace = ReplaceOptions[i].token;
320 bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
323 } else if (job->replace) {
324 jcr->replace = job->replace;
326 jcr->replace = REPLACE_ALWAYS;
330 replace = ReplaceOptions[0].name;
331 for (i=0; ReplaceOptions[i].name; i++) {
332 if (ReplaceOptions[i].token == jcr->replace) {
333 replace = ReplaceOptions[i].name;
336 Dmsg1(20, "JobType=%c\n", jcr->JobType);
337 switch (jcr->JobType) {
339 char dt[MAX_TIME_LENGTH];
341 bsendmsg(ua, _("Run %s job\n\
349 jcr->fileset->hdr.name,
350 NPRT(jcr->client->hdr.name),
351 NPRT(jcr->store->hdr.name),
352 bstrutime(dt, sizeof(dt), jcr->sched_time));
353 jcr->JobLevel = L_FULL;
358 /* Look up level name and pull code */
360 for (i=0; joblevels[i].level_name; i++) {
361 if (strcasecmp(level_name, _(joblevels[i].level_name)) == 0) {
362 jcr->JobLevel = joblevels[i].level;
368 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
374 bsendmsg(ua, _("Run %s job\n\
382 jcr->JobType==JT_BACKUP?_("Backup"):_("Verify"),
384 jcr->fileset->hdr.name,
385 level_to_str(jcr->JobLevel),
386 jcr->client->hdr.name,
387 jcr->store->hdr.name,
388 NPRT(jcr->pool->hdr.name),
389 bstrutime(dt, sizeof(dt), jcr->sched_time));
392 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
394 jcr->RestoreJobId = atoi(jid);
396 if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
400 jcr->RestoreJobId = ua->pint32_val;
403 jcr->JobLevel = L_FULL; /* default level */
404 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
405 if (jcr->RestoreJobId == 0) {
406 bsendmsg(ua, _("Run Restore job\n\
416 NPRT(jcr->RestoreBootstrap),
417 jcr->where?jcr->where:NPRT(job->RestoreWhere),
419 jcr->fileset->hdr.name,
420 jcr->client->hdr.name,
421 jcr->store->hdr.name,
422 bstrutime(dt, sizeof(dt), jcr->sched_time));
424 bsendmsg(ua, _("Run Restore job\n\
435 NPRT(jcr->RestoreBootstrap),
436 jcr->where?jcr->where:NPRT(job->RestoreWhere),
438 jcr->fileset->hdr.name,
439 jcr->client->hdr.name,
440 jcr->store->hdr.name,
441 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1),
442 bstrutime(dt, sizeof(dt), jcr->sched_time));
446 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
450 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
452 return 0; /* do not run */
455 * At user request modify parameters of job to be run.
457 if (ua->cmd[0] == 0) {
458 bsendmsg(ua, _("Job not run.\n"));
460 return 0; /* do not run */
462 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
465 start_prompt(ua, _("Parameters to modify:\n"));
466 add_prompt(ua, _("Level")); /* 0 */
467 add_prompt(ua, _("Storage")); /* 1 */
468 add_prompt(ua, _("Job")); /* 2 */
469 add_prompt(ua, _("FileSet")); /* 3 */
470 add_prompt(ua, _("Client")); /* 4 */
471 add_prompt(ua, _("When")); /* 5 */
472 if (jcr->JobType == JT_BACKUP ||
473 jcr->JobType == JT_VERIFY) {
474 add_prompt(ua, _("Pool")); /* 6 */
475 } else if (jcr->JobType == JT_RESTORE) {
476 add_prompt(ua, _("Bootstrap")); /* 6 */
477 add_prompt(ua, _("Where")); /* 7 */
478 add_prompt(ua, _("Replace")); /* 8 */
479 add_prompt(ua, _("JobId")); /* 9 */
481 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
484 if (jcr->JobType == JT_BACKUP) {
485 start_prompt(ua, _("Levels:\n"));
486 add_prompt(ua, _("Base"));
487 add_prompt(ua, _("Full"));
488 add_prompt(ua, _("Incremental"));
489 add_prompt(ua, _("Differential"));
490 add_prompt(ua, _("Since"));
491 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
493 jcr->JobLevel = L_BASE;
496 jcr->JobLevel = L_FULL;
499 jcr->JobLevel = L_INCREMENTAL;
502 jcr->JobLevel = L_DIFFERENTIAL;
505 jcr->JobLevel = L_SINCE;
511 } else if (jcr->JobType == JT_VERIFY) {
512 start_prompt(ua, _("Levels:\n"));
513 add_prompt(ua, _("Initialize Catalog"));
514 add_prompt(ua, _("Verify Catalog"));
515 add_prompt(ua, _("Verify Volume to Catalog"));
516 add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
517 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
519 jcr->JobLevel = L_VERIFY_INIT;
522 jcr->JobLevel = L_VERIFY_CATALOG;
525 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
528 jcr->JobLevel = L_VERIFY_DATA;
535 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
540 store = select_storage_resource(ua);
548 job = select_job_resource(ua);
551 set_jcr_defaults(jcr, job);
557 fileset = select_fileset_resource(ua);
559 jcr->fileset = fileset;
565 client = select_client_resource(ua);
567 jcr->client = client;
573 if (!get_cmd(ua, _("Please enter scheduled start time (return for now): "))) {
576 if (ua->cmd[0] == 0) {
577 jcr->sched_time = time(NULL);
579 jcr->sched_time = str_to_utime(ua->cmd);
580 if (jcr->sched_time == 0) {
581 bsendmsg(ua, _("Invalid time, using current time.\n"));
582 jcr->sched_time = time(NULL);
587 if (jcr->JobType == JT_BACKUP ||
588 jcr->JobType == JT_VERIFY) { /* Pool */
589 pool = select_pool_resource(ua);
598 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
601 if (jcr->RestoreBootstrap) {
602 free(jcr->RestoreBootstrap);
603 jcr->RestoreBootstrap = NULL;
605 if (ua->cmd[0] != 0) {
606 jcr->RestoreBootstrap = bstrdup(ua->cmd);
607 fd = fopen(jcr->RestoreBootstrap, "r");
609 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
610 jcr->RestoreBootstrap, strerror(errno));
611 free(jcr->RestoreBootstrap);
612 jcr->RestoreBootstrap = NULL;
620 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
627 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
630 jcr->where = bstrdup(ua->cmd);
634 start_prompt(ua, _("Replace:\n"));
635 for (i=0; ReplaceOptions[i].name; i++) {
636 add_prompt(ua, ReplaceOptions[i].name);
638 opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
640 jcr->replace = ReplaceOptions[opt].token;
645 jid = NULL; /* force reprompt */
646 jcr->RestoreJobId = 0;
647 if (jcr->RestoreBootstrap) {
648 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
654 bsendmsg(ua, _("Job not run.\n"));
656 return 0; /* error do no run Job */
658 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
659 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
661 bsendmsg(ua, _("Run command submitted.\n"));
665 bsendmsg(ua, _("Job not run.\n"));
667 return 0; /* do not run */