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;
72 N_("yes"), /* 11 -- see below */
91 for (i=1; i<ua->argc; i++) {
93 Dmsg2(200, "Doing arg %d = %s\n", i, ua->argk[i]);
94 for (j=0; !found && kw[j]; j++) {
95 if (strcasecmp(ua->argk[i], _(kw[j])) == 0) {
96 /* Note, yes has no value, so do not err */
97 if (!ua->argv[i] && j != 11 /*yes*/) {
98 bsendmsg(ua, _("Value missing for keyword %s\n"), ua->argk[i]);
101 Dmsg1(200, "Got keyword=%s\n", kw[j]);
105 bsendmsg(ua, _("Job name specified twice.\n"));
108 job_name = ua->argv[i];
113 bsendmsg(ua, _("JobId specified twice.\n"));
121 bsendmsg(ua, _("Client specified twice.\n"));
124 client_name = ua->argv[i];
127 case 3: /* fileset */
129 bsendmsg(ua, _("FileSet specified twice.\n"));
132 fileset_name = ua->argv[i];
137 bsendmsg(ua, _("Level specified twice.\n"));
140 level_name = ua->argv[i];
143 case 5: /* storage */
145 bsendmsg(ua, _("Storage specified twice.\n"));
148 store_name = ua->argv[i];
153 bsendmsg(ua, _("Pool specified twice.\n"));
156 pool_name = ua->argv[i];
161 bsendmsg(ua, _("Where specified twice.\n"));
167 case 8: /* bootstrap */
169 bsendmsg(ua, _("Bootstrap specified twice.\n"));
172 bootstrap = ua->argv[i];
175 case 9: /* replace */
177 bsendmsg(ua, _("Replace specified twice.\n"));
180 replace = ua->argv[i];
185 bsendmsg(ua, _("When specified twice.\n"));
197 } /* end strcase compare */
198 } /* end keyword loop */
200 Dmsg1(200, "%s not found\n", ua->argk[i]);
202 * Special case for Job Name, it can be the first
203 * keyword that has no value.
205 if (!job_name && !ua->argv[i]) {
206 job_name = ua->argk[i]; /* use keyword as job name */
207 Dmsg1(200, "Set jobname=%s\n", job_name);
209 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
213 } /* end argc loop */
215 Dmsg0(200, "Done scan.\n");
221 job = (JOB *)GetResWithName(R_JOB, job_name);
223 bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
224 job = select_job_resource(ua);
226 Dmsg1(200, "Found job=%s\n", job_name);
229 bsendmsg(ua, _("A job name must be specified.\n"));
230 job = select_job_resource(ua);
237 store = (STORE *)GetResWithName(R_STORAGE, store_name);
239 bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
240 store = select_storage_resource(ua);
243 store = job->storage; /* use default */
251 pool = (POOL *)GetResWithName(R_POOL, pool_name);
253 bsendmsg(ua, _("Pool \"%s\" not found.\n"), pool_name);
254 pool = get_pool_resource(ua);
257 pool = job->pool; /* use default */
261 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
263 bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
264 client = select_client_resource(ua);
267 client = job->client; /* use default */
274 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
276 bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
277 fileset = select_fileset_resource(ua);
280 fileset = job->fileset; /* use default */
287 /* Create JCR to run job */
288 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
289 set_jcr_defaults(jcr, job);
292 jcr->client = client;
293 jcr->fileset = fileset;
299 jcr->where = bstrdup(where);
303 jcr->sched_time = str_to_utime(when);
304 if (jcr->sched_time == 0) {
305 bsendmsg(ua, _("Invalid time, using current time.\n"));
306 jcr->sched_time = time(NULL);
311 if (jcr->RestoreBootstrap) {
312 free(jcr->RestoreBootstrap);
314 jcr->RestoreBootstrap = bstrdup(bootstrap);
319 for (i=0; ReplaceOptions[i].name; i++) {
320 if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
321 jcr->replace = ReplaceOptions[i].token;
325 bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
328 } else if (job->replace) {
329 jcr->replace = job->replace;
331 jcr->replace = REPLACE_ALWAYS;
335 replace = ReplaceOptions[0].name;
336 for (i=0; ReplaceOptions[i].name; i++) {
337 if (ReplaceOptions[i].token == jcr->replace) {
338 replace = ReplaceOptions[i].name;
341 Dmsg1(20, "JobType=%c\n", jcr->JobType);
342 switch (jcr->JobType) {
344 char dt[MAX_TIME_LENGTH];
346 bsendmsg(ua, _("Run %s job\n\
354 jcr->fileset->hdr.name,
355 NPRT(jcr->client->hdr.name),
356 NPRT(jcr->store->hdr.name),
357 bstrutime(dt, sizeof(dt), jcr->sched_time));
358 jcr->JobLevel = L_FULL;
363 /* Look up level name and pull code */
365 for (i=0; joblevels[i].level_name; i++) {
366 if (strcasecmp(level_name, _(joblevels[i].level_name)) == 0) {
367 jcr->JobLevel = joblevels[i].level;
373 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
379 bsendmsg(ua, _("Run %s job\n\
387 jcr->JobType==JT_BACKUP?_("Backup"):_("Verify"),
389 jcr->fileset->hdr.name,
390 level_to_str(jcr->JobLevel),
391 jcr->client->hdr.name,
392 jcr->store->hdr.name,
393 NPRT(jcr->pool->hdr.name),
394 bstrutime(dt, sizeof(dt), jcr->sched_time));
397 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
399 jcr->RestoreJobId = atoi(jid);
401 if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
405 jcr->RestoreJobId = ua->pint32_val;
408 jcr->JobLevel = L_FULL; /* default level */
409 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
410 if (jcr->RestoreJobId == 0) {
411 bsendmsg(ua, _("Run Restore job\n\
421 NPRT(jcr->RestoreBootstrap),
422 jcr->where?jcr->where:NPRT(job->RestoreWhere),
424 jcr->fileset->hdr.name,
425 jcr->client->hdr.name,
426 jcr->store->hdr.name,
427 bstrutime(dt, sizeof(dt), jcr->sched_time));
429 bsendmsg(ua, _("Run Restore job\n\
440 NPRT(jcr->RestoreBootstrap),
441 jcr->where?jcr->where:NPRT(job->RestoreWhere),
443 jcr->fileset->hdr.name,
444 jcr->client->hdr.name,
445 jcr->store->hdr.name,
446 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1),
447 bstrutime(dt, sizeof(dt), jcr->sched_time));
451 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
456 /* Run without prompting? */
457 if (find_arg(ua, _("yes")) > 0) {
458 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
460 bsendmsg(ua, _("Run command submitted.\n"));
464 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
466 return 0; /* do not run */
469 * At user request modify parameters of job to be run.
471 if (ua->cmd[0] == 0) {
472 bsendmsg(ua, _("Job not run.\n"));
474 return 0; /* do not run */
476 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
479 start_prompt(ua, _("Parameters to modify:\n"));
480 add_prompt(ua, _("Level")); /* 0 */
481 add_prompt(ua, _("Storage")); /* 1 */
482 add_prompt(ua, _("Job")); /* 2 */
483 add_prompt(ua, _("FileSet")); /* 3 */
484 add_prompt(ua, _("Client")); /* 4 */
485 add_prompt(ua, _("When")); /* 5 */
486 if (jcr->JobType == JT_BACKUP ||
487 jcr->JobType == JT_VERIFY) {
488 add_prompt(ua, _("Pool")); /* 6 */
489 } else if (jcr->JobType == JT_RESTORE) {
490 add_prompt(ua, _("Bootstrap")); /* 6 */
491 add_prompt(ua, _("Where")); /* 7 */
492 add_prompt(ua, _("Replace")); /* 8 */
493 add_prompt(ua, _("JobId")); /* 9 */
495 switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
498 if (jcr->JobType == JT_BACKUP) {
499 start_prompt(ua, _("Levels:\n"));
500 add_prompt(ua, _("Base"));
501 add_prompt(ua, _("Full"));
502 add_prompt(ua, _("Incremental"));
503 add_prompt(ua, _("Differential"));
504 add_prompt(ua, _("Since"));
505 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
507 jcr->JobLevel = L_BASE;
510 jcr->JobLevel = L_FULL;
513 jcr->JobLevel = L_INCREMENTAL;
516 jcr->JobLevel = L_DIFFERENTIAL;
519 jcr->JobLevel = L_SINCE;
525 } else if (jcr->JobType == JT_VERIFY) {
526 start_prompt(ua, _("Levels:\n"));
527 add_prompt(ua, _("Initialize Catalog"));
528 add_prompt(ua, _("Verify Catalog"));
529 add_prompt(ua, _("Verify Volume to Catalog"));
530 add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
531 switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
533 jcr->JobLevel = L_VERIFY_INIT;
536 jcr->JobLevel = L_VERIFY_CATALOG;
539 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
542 jcr->JobLevel = L_VERIFY_DATA;
549 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
554 store = select_storage_resource(ua);
562 job = select_job_resource(ua);
565 set_jcr_defaults(jcr, job);
571 fileset = select_fileset_resource(ua);
573 jcr->fileset = fileset;
579 client = select_client_resource(ua);
581 jcr->client = client;
587 if (!get_cmd(ua, _("Please enter scheduled start time (return for now): "))) {
590 if (ua->cmd[0] == 0) {
591 jcr->sched_time = time(NULL);
593 jcr->sched_time = str_to_utime(ua->cmd);
594 if (jcr->sched_time == 0) {
595 bsendmsg(ua, _("Invalid time, using current time.\n"));
596 jcr->sched_time = time(NULL);
601 if (jcr->JobType == JT_BACKUP ||
602 jcr->JobType == JT_VERIFY) { /* Pool */
603 pool = select_pool_resource(ua);
612 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
615 if (jcr->RestoreBootstrap) {
616 free(jcr->RestoreBootstrap);
617 jcr->RestoreBootstrap = NULL;
619 if (ua->cmd[0] != 0) {
620 jcr->RestoreBootstrap = bstrdup(ua->cmd);
621 fd = fopen(jcr->RestoreBootstrap, "r");
623 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
624 jcr->RestoreBootstrap, strerror(errno));
625 free(jcr->RestoreBootstrap);
626 jcr->RestoreBootstrap = NULL;
634 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
641 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
644 jcr->where = bstrdup(ua->cmd);
648 start_prompt(ua, _("Replace:\n"));
649 for (i=0; ReplaceOptions[i].name; i++) {
650 add_prompt(ua, ReplaceOptions[i].name);
652 opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
654 jcr->replace = ReplaceOptions[opt].token;
659 jid = NULL; /* force reprompt */
660 jcr->RestoreJobId = 0;
661 if (jcr->RestoreBootstrap) {
662 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
668 bsendmsg(ua, _("Job not run.\n"));
670 return 0; /* error do no run Job */
673 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
674 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
676 bsendmsg(ua, _("Run command submitted.\n"));
680 bsendmsg(ua, _("Job not run.\n"));
682 return 0; /* do not run */