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,
34 /* Imported subroutines */
35 extern void run_job(JCR *jcr);
37 /* Imported variables */
38 extern struct s_jl joblevels[];
39 extern struct s_kw ReplaceOptions[];
42 * For Backup and Verify Jobs
43 * run [job=]<job-name> level=<level-name>
46 * run <job-name> jobid=nn
49 int runcmd(UAContext *ua, char *cmd)
52 char *job_name, *level_name, *jid, *store_name;
53 char *where, *fileset_name, *client_name, *bootstrap, *replace;
57 CLIENT *client = NULL;
58 FILESET *fileset = NULL;
85 for (i=1; i<ua->argc; i++) {
87 Dmsg2(200, "Doing arg %d = %s\n", i, ua->argk[i]);
88 for (j=0; !found && kw[j]; j++) {
89 if (strcasecmp(ua->argk[i], _(kw[j])) == 0) {
91 bsendmsg(ua, _("Value missing for keyword %s\n"), ua->argk[i]);
94 Dmsg1(200, "Got keyword=%s\n", kw[j]);
98 bsendmsg(ua, _("Job name specified twice.\n"));
101 job_name = ua->argv[i];
106 bsendmsg(ua, _("JobId specified twice.\n"));
114 bsendmsg(ua, _("Client specified twice.\n"));
117 client_name = ua->argv[i];
120 case 3: /* fileset */
122 bsendmsg(ua, _("FileSet specified twice.\n"));
125 fileset_name = ua->argv[i];
130 bsendmsg(ua, _("Level specified twice.\n"));
133 level_name = ua->argv[i];
136 case 5: /* storage */
138 bsendmsg(ua, _("Storage specified twice.\n"));
141 store_name = ua->argv[i];
146 bsendmsg(ua, _("Where specified twice.\n"));
152 case 7: /* bootstrap */
154 bsendmsg(ua, _("Bootstrap specified twice.\n"));
157 bootstrap = ua->argv[i];
160 case 8: /* replace */
162 bsendmsg(ua, _("Replace specified twice.\n"));
165 replace = ua->argv[i];
171 } /* end strcase compare */
172 } /* end keyword loop */
174 Dmsg1(200, "%s not found\n", ua->argk[i]);
176 * Special case for Job Name, it can be the first
177 * keyword that has no value.
179 if (!job_name && !ua->argv[i]) {
180 job_name = ua->argk[i]; /* use keyword as job name */
181 Dmsg1(200, "Set jobname=%s\n", job_name);
183 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
187 } /* end argc loop */
189 Dmsg0(200, "Done scan.\n");
195 job = (JOB *)GetResWithName(R_JOB, job_name);
197 bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
198 job = select_job_resource(ua);
200 Dmsg1(200, "Found job=%s\n", job_name);
203 bsendmsg(ua, _("A job name must be specified.\n"));
204 job = select_job_resource(ua);
211 store = (STORE *)GetResWithName(R_STORAGE, store_name);
213 bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
214 store = select_storage_resource(ua);
217 store = job->storage; /* use default */
224 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
226 bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
227 client = select_client_resource(ua);
230 client = job->client; /* use default */
237 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
239 bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
240 fileset = select_fileset_resource(ua);
243 fileset = job->fileset; /* use default */
250 /* Create JCR to run job */
251 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
252 set_jcr_defaults(jcr, job);
255 jcr->client = client;
256 jcr->fileset = fileset;
258 if (jcr->RestoreWhere) {
259 free(jcr->RestoreWhere);
261 jcr->RestoreWhere = bstrdup(where);
264 if (jcr->RestoreBootstrap) {
265 free(jcr->RestoreBootstrap);
267 jcr->RestoreBootstrap = bstrdup(bootstrap);
272 for (i=0; ReplaceOptions[i].name; i++) {
273 if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
274 jcr->replace = ReplaceOptions[i].token;
278 bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
281 } else if (job->replace) {
282 jcr->replace = job->replace;
284 jcr->replace = REPLACE_ALWAYS;
288 replace = ReplaceOptions[0].name;
289 for (i=0; ReplaceOptions[i].name; i++) {
290 if (ReplaceOptions[i].token == jcr->replace) {
291 replace = ReplaceOptions[i].name;
294 Dmsg1(20, "JobType=%c\n", jcr->JobType);
295 switch (jcr->JobType) {
298 bsendmsg(ua, _("Run %s job\n\
305 jcr->fileset->hdr.name,
306 NPRT(jcr->client->hdr.name),
307 NPRT(jcr->store->hdr.name));
313 /* Look up level name and pull code */
315 for (i=0; joblevels[i].level_name; i++) {
316 if (strcasecmp(level_name, _(joblevels[i].level_name)) == 0) {
317 jcr->JobLevel = joblevels[i].level;
323 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
329 bsendmsg(ua, _("Run %s job\n\
335 jcr->JobType==JT_BACKUP?_("Backup"):_("Verify"),
337 jcr->fileset->hdr.name,
338 level_to_str(jcr->JobLevel),
339 jcr->client->hdr.name,
340 jcr->store->hdr.name);
343 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
345 jcr->RestoreJobId = atoi(jid);
347 if (!get_cmd(ua, _("Please enter a JobId for restore: "))) {
351 jcr->RestoreJobId = atoi(ua->cmd);
354 jcr->JobLevel = 'F'; /* default level */
355 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
356 bsendmsg(ua, _("Run Restore job\n\
366 NPRT(jcr->RestoreBootstrap),
367 jcr->RestoreWhere?jcr->RestoreWhere:NPRT(job->RestoreWhere),
369 jcr->fileset->hdr.name,
370 jcr->client->hdr.name,
371 jcr->store->hdr.name,
372 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1));
375 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
379 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
381 return 0; /* do not run */
384 * At user request modify parameters of job to be run.
386 if (strlen(ua->cmd) == 0) {
387 bsendmsg(ua, _("Job not run.\n"));
389 return 0; /* do not run */
391 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
394 start_prompt(ua, _("Parameters to modify:\n"));
395 add_prompt(ua, _("Level")); /* 0 */
396 add_prompt(ua, _("Storage")); /* 1 */
397 add_prompt(ua, _("Job")); /* 2 */
398 add_prompt(ua, _("FileSet")); /* 3 */
399 add_prompt(ua, _("Client")); /* 4 */
400 if (jcr->JobType == JT_RESTORE) {
401 add_prompt(ua, _("Bootstrap")); /* 5 */
402 add_prompt(ua, _("Where")); /* 6 */
403 add_prompt(ua, _("Replace")); /* 7 */
404 add_prompt(ua, _("JobId")); /* 8 */
406 switch (do_prompt(ua, _("Select parameter to modify"), NULL, 0)) {
409 if (jcr->JobType == JT_BACKUP) {
410 start_prompt(ua, _("Levels:\n"));
411 add_prompt(ua, _("Full"));
412 add_prompt(ua, _("Incremental"));
413 add_prompt(ua, _("Differential"));
414 add_prompt(ua, _("Since"));
415 switch (do_prompt(ua, _("Select level"), NULL, 0)) {
417 jcr->JobLevel = L_FULL;
420 jcr->JobLevel = L_INCREMENTAL;
423 jcr->JobLevel = L_DIFFERENTIAL;
426 jcr->JobLevel = L_SINCE;
432 } else if (jcr->JobType == JT_VERIFY) {
433 start_prompt(ua, _("Levels:\n"));
434 add_prompt(ua, _("Initialize Catalog"));
435 add_prompt(ua, _("Verify Catalog"));
436 add_prompt(ua, _("Verify Volume"));
437 add_prompt(ua, _("Verify Volume Data"));
438 switch (do_prompt(ua, _("Select level"), NULL, 0)) {
440 jcr->JobLevel = L_VERIFY_INIT;
443 jcr->JobLevel = L_VERIFY_CATALOG;
446 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
449 jcr->JobLevel = L_VERIFY_DATA;
456 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
460 store = select_storage_resource(ua);
468 job = select_job_resource(ua);
471 set_jcr_defaults(jcr, job);
477 fileset = select_fileset_resource(ua);
479 jcr->fileset = fileset;
484 client = select_client_resource(ua);
486 jcr->client = client;
492 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
495 if (jcr->RestoreBootstrap) {
496 free(jcr->RestoreBootstrap);
497 jcr->RestoreBootstrap = NULL;
499 if (ua->cmd[0] != 0) {
500 jcr->RestoreBootstrap = bstrdup(ua->cmd);
501 fd = fopen(jcr->RestoreBootstrap, "r");
503 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
504 jcr->RestoreBootstrap, strerror(errno));
505 free(jcr->RestoreBootstrap);
506 jcr->RestoreBootstrap = NULL;
514 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
517 if (jcr->RestoreWhere) {
518 free(jcr->RestoreWhere);
519 jcr->RestoreWhere = NULL;
521 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
524 jcr->RestoreWhere = bstrdup(ua->cmd);
528 start_prompt(ua, _("Replace:\n"));
529 for (i=0; ReplaceOptions[i].name; i++) {
530 add_prompt(ua, ReplaceOptions[i].name);
532 opt = do_prompt(ua, _("Select replace option"), NULL, 0);
534 jcr->replace = ReplaceOptions[opt].token;
539 jid = NULL; /* force reprompt */
540 jcr->RestoreJobId = 0;
541 if (jcr->RestoreBootstrap) {
542 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
548 bsendmsg(ua, _("Job not run.\n"));
550 return 0; /* error do no run Job */
552 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
553 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
558 bsendmsg(ua, _("Job not run.\n"));
560 return 0; /* do not run */