3 * Bacula Director -- Run Command
5 * Kern Sibbald, December MMI
11 Copyright (C) 2001, 2002 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[];
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;
52 char *where, *fileset_name, *client_name, *bootstrap;
56 CLIENT *client = NULL;
57 FILESET *fileset = NULL;
82 for (i=1; i<ua->argc; i++) {
84 Dmsg2(200, "Doing arg %d = %s\n", i, ua->argk[i]);
85 for (j=0; !found && kw[j]; j++) {
86 if (strcasecmp(ua->argk[i], _(kw[j])) == 0) {
88 bsendmsg(ua, _("Value missing for keyword %s\n"), ua->argk[i]);
91 Dmsg1(200, "Got keyword=%s\n", kw[j]);
95 bsendmsg(ua, _("Job name specified twice.\n"));
98 job_name = ua->argv[i];
103 bsendmsg(ua, _("JobId specified twice.\n"));
111 bsendmsg(ua, _("Client specified twice.\n"));
114 client_name = ua->argv[i];
117 case 3: /* fileset */
119 bsendmsg(ua, _("FileSet specified twice.\n"));
122 fileset_name = ua->argv[i];
127 bsendmsg(ua, _("Level specified twice.\n"));
130 level_name = ua->argv[i];
133 case 5: /* storage */
135 bsendmsg(ua, _("Storage specified twice.\n"));
138 store_name = ua->argv[i];
143 bsendmsg(ua, _("Where specified twice.\n"));
149 case 7: /* bootstrap */
151 bsendmsg(ua, _("Bootstrap specified twice.\n"));
154 bootstrap = ua->argv[i];
160 } /* end strcase compare */
161 } /* end keyword loop */
163 Dmsg1(200, "%s not found\n", ua->argk[i]);
165 * Special case for Job Name, it can be the first
166 * keyword that has no value.
168 if (!job_name && !ua->argv[i]) {
169 job_name = ua->argk[i]; /* use keyword as job name */
170 Dmsg1(200, "Set jobname=%s\n", job_name);
172 bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
176 } /* end argc loop */
178 Dmsg0(200, "Done scan.\n");
184 job = (JOB *)GetResWithName(R_JOB, job_name);
186 bsendmsg(ua, _("Job %s: not found\n"), job_name);
187 job = select_job_resource(ua);
189 Dmsg1(200, "Found job=%s\n", job_name);
192 bsendmsg(ua, _("A job name must be specified.\n"));
193 job = select_job_resource(ua);
200 store = (STORE *)GetResWithName(R_STORAGE, store_name);
202 bsendmsg(ua, _("Storage %s not found.\n"), store_name);
203 store = select_storage_resource(ua);
206 store = job->storage; /* use default */
213 client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
215 bsendmsg(ua, _("Client %s not found.\n"), client_name);
216 client = select_client_resource(ua);
219 client = job->client; /* use default */
226 fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
228 bsendmsg(ua, _("FileSet %s not found.\n"), fileset_name);
229 fileset = select_fileset_resource(ua);
232 fileset = job->fileset; /* use default */
239 /* Create JCR to run job */
240 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
241 set_jcr_defaults(jcr, job);
244 jcr->client = client;
245 jcr->fileset = fileset;
247 if (jcr->RestoreWhere) {
248 free(jcr->RestoreWhere);
250 jcr->RestoreWhere = bstrdup(where);
253 if (jcr->RestoreBootstrap) {
254 free(jcr->RestoreBootstrap);
256 jcr->RestoreBootstrap = bstrdup(bootstrap);
263 Dmsg1(20, "JobType=%c\n", jcr->JobType);
264 switch (jcr->JobType) {
267 bsendmsg(ua, _("Run %s job\n\
274 jcr->fileset->hdr.name,
275 NPRT(jcr->client->hdr.name),
276 NPRT(jcr->store->hdr.name));
282 /* Look up level name and pull code */
284 for (i=0; joblevels[i].level_name; i++) {
285 if (strcasecmp(level_name, _(joblevels[i].level_name)) == 0) {
286 jcr->JobLevel = joblevels[i].level;
292 bsendmsg(ua, _("Level %s not valid.\n"), level_name);
298 bsendmsg(ua, _("Run %s job\n\
304 jcr->JobType==JT_BACKUP?_("Backup"):_("Verify"),
306 jcr->fileset->hdr.name,
307 level_to_str(jcr->JobLevel),
308 jcr->client->hdr.name,
309 jcr->store->hdr.name);
312 if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
314 jcr->RestoreJobId = atoi(jid);
316 if (!get_cmd(ua, _("Please enter a JobId for restore: "))) {
320 jcr->RestoreJobId = atoi(ua->cmd);
323 jcr->JobLevel = 'F'; /* default level */
324 Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
325 bsendmsg(ua, _("Run Restore job\n\
334 NPRT(jcr->RestoreBootstrap),
335 jcr->RestoreWhere?jcr->RestoreWhere:NPRT(job->RestoreWhere),
336 jcr->fileset->hdr.name,
337 jcr->client->hdr.name,
338 jcr->store->hdr.name,
339 jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1));
342 bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
346 if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
348 return 0; /* do not run */
351 * At user request modify parameters of job to be run.
353 if (strlen(ua->cmd) == 0) {
354 bsendmsg(ua, _("Job not run.\n"));
356 return 0; /* do not run */
358 if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
361 start_prompt(ua, _("Parameters to modify:\n"));
362 add_prompt(ua, _("Level")); /* 0 */
363 add_prompt(ua, _("Storage")); /* 1 */
364 add_prompt(ua, _("Job")); /* 2 */
365 add_prompt(ua, _("FileSet")); /* 3 */
366 add_prompt(ua, _("Client")); /* 4 */
367 if (jcr->JobType == JT_RESTORE) {
368 add_prompt(ua, _("Bootstrap")); /* 5 */
369 add_prompt(ua, _("Where")); /* 6 */
370 add_prompt(ua, _("JobId")); /* 7 */
372 switch (do_prompt(ua, _("Select parameter to modify"), NULL)) {
375 if (jcr->JobType == JT_BACKUP) {
376 start_prompt(ua, _("Levels:\n"));
377 add_prompt(ua, _("Full"));
378 add_prompt(ua, _("Incremental"));
379 add_prompt(ua, _("Differential"));
380 add_prompt(ua, _("Level"));
381 add_prompt(ua, _("Since"));
382 switch (do_prompt(ua, _("Select level"), NULL)) {
384 jcr->JobLevel = L_FULL;
387 jcr->JobLevel = L_INCREMENTAL;
390 jcr->JobLevel = L_DIFFERENTIAL;
393 jcr->JobLevel = L_LEVEL;
396 jcr->JobLevel = L_SINCE;
402 } else if (jcr->JobType == JT_VERIFY) {
403 start_prompt(ua, _("Levels:\n"));
404 add_prompt(ua, _("Initialize Catalog"));
405 add_prompt(ua, _("Verify Catalog"));
406 add_prompt(ua, _("Verify Volume"));
407 add_prompt(ua, _("Verify Volume Data"));
408 switch (do_prompt(ua, _("Select level"), NULL)) {
410 jcr->JobLevel = L_VERIFY_INIT;
413 jcr->JobLevel = L_VERIFY_CATALOG;
416 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
419 jcr->JobLevel = L_VERIFY_DATA;
426 bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
430 store = select_storage_resource(ua);
438 job = select_job_resource(ua);
441 set_jcr_defaults(jcr, job);
447 fileset = select_fileset_resource(ua);
449 jcr->fileset = fileset;
454 client = select_client_resource(ua);
456 jcr->client = client;
462 if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
465 if (jcr->RestoreBootstrap) {
466 free(jcr->RestoreBootstrap);
467 jcr->RestoreBootstrap = NULL;
469 if (ua->cmd[0] != 0) {
470 jcr->RestoreBootstrap = bstrdup(ua->cmd);
471 fd = fopen(jcr->RestoreBootstrap, "r");
473 bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
474 jcr->RestoreBootstrap, strerror(errno));
475 free(jcr->RestoreBootstrap);
476 jcr->RestoreBootstrap = NULL;
484 if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
487 if (jcr->RestoreWhere) {
488 free(jcr->RestoreWhere);
489 jcr->RestoreWhere = NULL;
491 if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
494 jcr->RestoreWhere = bstrdup(ua->cmd);
498 jid = NULL; /* force reprompt */
499 jcr->RestoreJobId = 0;
500 if (jcr->RestoreBootstrap) {
501 bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
507 bsendmsg(ua, _("Job not run.\n"));
509 return 0; /* error do no run Job */
511 if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
512 Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
517 bsendmsg(ua, _("Job not run.\n"));
519 return 0; /* do not run */