]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_run.c
9a95b87cc886d0b1993f608272d8ee7ad1b72ca5
[bacula/bacula] / bacula / src / dird / ua_run.c
1 /*
2  *
3  *   Bacula Director -- Run Command
4  *
5  *     Kern Sibbald, December MMI
6  *
7  *   Version $Id$
8  */
9
10 /*
11    Copyright (C) 2001-2004 Kern Sibbald
12
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.
17
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.
22
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,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "dird.h"
32
33 /* Imported subroutines */
34
35 /* Imported variables */
36 extern struct s_kw ReplaceOptions[];
37
38 /*
39  * For Backup and Verify Jobs
40  *     run [job=]<job-name> level=<level-name>
41  *
42  * For Restore Jobs
43  *     run <job-name> jobid=nn
44  *
45  *  Returns: 0 on error
46  *           JobId if OK
47  *
48  */
49 int run_cmd(UAContext *ua, const char *cmd)
50 {
51    JCR *jcr;
52    char *job_name, *level_name, *jid, *store_name, *pool_name;
53    char *where, *fileset_name, *client_name, *bootstrap;
54    const char *replace;
55    char *when, *verify_job_name, *catalog_name;
56    int Priority = 0;
57    int i, j, opt, files = 0;
58    bool kw_ok;
59    JOB *job = NULL;
60    JOB *verify_job = NULL;
61    STORE *store = NULL;
62    CLIENT *client = NULL;
63    FILESET *fileset = NULL;
64    POOL *pool = NULL;
65    static const char *kw[] = {        /* command line arguments */
66       N_("job"),                      /*  Used in a switch() */
67       N_("jobid"),                    /* 1 */
68       N_("client"),                   /* 2 */
69       N_("fd"), 
70       N_("fileset"),                  /* 4 */
71       N_("level"),                    /* 5 */
72       N_("storage"),                  /* 6 */
73       N_("sd"),                       /* 7 */
74       N_("pool"),                     /* 8 */
75       N_("where"),                    /* 9 */
76       N_("bootstrap"),                /* 10 */
77       N_("replace"),                  /* 11 */
78       N_("when"),                     /* 12 */
79       N_("priority"),                 /* 13 */
80       N_("yes"),          /* 14 -- if you change this change YES_POS too */
81       N_("verifyjob"),                /* 15 */
82       N_("files"),                    /* 16 number of files to restore */
83        N_("catalog"),                 /* 17 override catalog */
84       NULL};
85
86 #define YES_POS 14
87
88    if (!open_db(ua)) {
89       return 1;
90    }
91
92    job_name = NULL;
93    level_name = NULL;
94    jid = NULL;
95    store_name = NULL;
96    pool_name = NULL;
97    where = NULL;
98    when = NULL;
99    client_name = NULL;
100    fileset_name = NULL;
101    bootstrap = NULL;
102    replace = NULL;
103    verify_job_name = NULL;
104    catalog_name = NULL;
105
106    for (i=1; i<ua->argc; i++) {
107       Dmsg2(800, "Doing arg %d = %s\n", i, ua->argk[i]);
108       kw_ok = false;
109       /* Keep looking until we find a good keyword */
110       for (j=0; !kw_ok && kw[j]; j++) {
111          if (strcasecmp(ua->argk[i], _(kw[j])) == 0) {
112             /* Note, yes and run have no value, so do not err */
113             if (!ua->argv[i] && j != YES_POS /*yes*/) {  
114                bsendmsg(ua, _("Value missing for keyword %s\n"), ua->argk[i]);
115                return 1;
116             }
117             Dmsg1(800, "Got keyword=%s\n", kw[j]);
118             switch (j) {
119             case 0: /* job */
120                if (job_name) {
121                   bsendmsg(ua, _("Job name specified twice.\n"));
122                   return 0;
123                }
124                job_name = ua->argv[i];
125                kw_ok = true;
126                break;
127             case 1: /* JobId */
128                if (jid) {
129                   bsendmsg(ua, _("JobId specified twice.\n"));
130                   return 0;
131                }
132                jid = ua->argv[i];
133                kw_ok = true;
134                break;
135             case 2: /* client */
136             case 3: /* fd */
137                if (client_name) {
138                   bsendmsg(ua, _("Client specified twice.\n"));
139                   return 0;
140                }
141                client_name = ua->argv[i];
142                kw_ok = true;
143                break;
144             case 4: /* fileset */
145                if (fileset_name) {
146                   bsendmsg(ua, _("FileSet specified twice.\n"));
147                   return 0;
148                }
149                fileset_name = ua->argv[i];
150                kw_ok = true;
151                break;
152             case 5: /* level */
153                if (level_name) {
154                   bsendmsg(ua, _("Level specified twice.\n"));
155                   return 0;
156                }
157                level_name = ua->argv[i];
158                kw_ok = true;
159                break;
160             case 6: /* storage */
161             case 7: /* sd */
162                if (store_name) {
163                   bsendmsg(ua, _("Storage specified twice.\n"));
164                   return 0;
165                }
166                store_name = ua->argv[i];
167                kw_ok = true;
168                break;
169             case 8: /* pool */
170                if (pool_name) {
171                   bsendmsg(ua, _("Pool specified twice.\n"));
172                   return 0;
173                }
174                pool_name = ua->argv[i];
175                kw_ok = true;
176                break;
177             case 9: /* where */
178                if (where) {
179                   bsendmsg(ua, _("Where specified twice.\n"));
180                   return 0;
181                }
182                where = ua->argv[i];
183                kw_ok = true;
184                break;
185             case 10: /* bootstrap */
186                if (bootstrap) {
187                   bsendmsg(ua, _("Bootstrap specified twice.\n"));
188                   return 0;
189                }
190                bootstrap = ua->argv[i];
191                kw_ok = true;
192                break;
193             case 11: /* replace */
194                if (replace) {
195                   bsendmsg(ua, _("Replace specified twice.\n"));
196                   return 0;
197                }
198                replace = ua->argv[i];
199                kw_ok = true;
200                break;
201             case 12: /* When */
202                if (when) {
203                   bsendmsg(ua, _("When specified twice.\n"));
204                   return 0;
205                }
206                when = ua->argv[i];
207                kw_ok = true;
208                break;
209             case 13:  /* Priority */
210                if (Priority) {
211                   bsendmsg(ua, _("Priority specified twice.\n"));
212                   return 0;
213                }
214                Priority = atoi(ua->argv[i]);
215                if (Priority <= 0) {
216                   bsendmsg(ua, _("Priority must be positive nonzero setting it to 10.\n"));
217                   Priority = 10;
218                }
219                kw_ok = true;
220                break;
221             case 14: /* yes */
222                kw_ok = true;
223                break;
224             case 15: /* Verify Job */
225                if (verify_job_name) {
226                   bsendmsg(ua, _("Verify Job specified twice.\n"));
227                   return 0;
228                }
229                verify_job_name = ua->argv[i];
230                kw_ok = true;
231                break;
232             case 16: /* files */
233                files = atoi(ua->argv[i]);
234                kw_ok = true;
235                break;
236
237             case 17: /* catalog */
238                catalog_name = ua->argv[i];
239                kw_ok = true;
240                break;
241
242             default:
243                break;
244             }
245          } /* end strcase compare */
246       } /* end keyword loop */
247       /*
248        * End of keyword for loop -- if not found, we got a bogus keyword
249        */
250       if (!kw_ok) {
251          Dmsg1(200, "%s not found\n", ua->argk[i]);
252          /*
253           * Special case for Job Name, it can be the first
254           * keyword that has no value.
255           */
256          if (!job_name && !ua->argv[i]) {
257             job_name = ua->argk[i];   /* use keyword as job name */
258             Dmsg1(200, "Set jobname=%s\n", job_name);
259          } else {
260             bsendmsg(ua, _("Invalid keyword: %s\n"), ua->argk[i]);
261             return 0;
262          }
263       }
264    } /* end argc loop */
265              
266    Dmsg0(800, "Done scan.\n");
267
268    CAT *catalog = NULL;
269    if (catalog_name != NULL) {
270        catalog = (CAT *)GetResWithName(R_CATALOG, catalog_name);
271        if (catalog == NULL) {
272             bsendmsg(ua, _("Catalog \"%s\" not found\n"), catalog_name);
273            return 0;
274        }
275    }
276    Dmsg1(200, "Using catalog=%s\n", catalog_name);
277
278    if (job_name) {
279       /* Find Job */
280       job = (JOB *)GetResWithName(R_JOB, job_name);
281       if (!job) {
282          if (*job_name != 0) {
283             bsendmsg(ua, _("Job \"%s\" not found\n"), job_name);
284          }
285          job = select_job_resource(ua);
286       } else {
287          Dmsg1(200, "Found job=%s\n", job_name);
288       }
289    } else {
290       bsendmsg(ua, _("A job name must be specified.\n"));
291       job = select_job_resource(ua);
292    }
293    if (!job) {
294       return 0;
295    } else if (!acl_access_ok(ua, Job_ACL, job->hdr.name)) {
296       bsendmsg(ua, _("No authorization. Job \"%s\".\n"),
297          job->hdr.name);
298       return 0;
299    }
300
301    if (store_name) {
302       store = (STORE *)GetResWithName(R_STORAGE, store_name);
303       if (!store) {
304          if (*store_name != 0) {
305             bsendmsg(ua, _("Storage \"%s\" not found.\n"), store_name);
306          }
307          store = select_storage_resource(ua);
308       }
309    } else {
310       store = (STORE *)job->storage[0]->first();           /* use default */
311    }
312    if (!store) {
313       return 1;
314    } else if (!acl_access_ok(ua, Storage_ACL, store->hdr.name)) {
315       bsendmsg(ua, _("No authorization. Storage \"%s\".\n"),
316                store->hdr.name);
317       return 0;
318    }
319    Dmsg1(200, "Using storage=%s\n", store->hdr.name);
320
321    if (pool_name) {
322       pool = (POOL *)GetResWithName(R_POOL, pool_name);
323       if (!pool) {
324          if (*pool_name != 0) {
325             bsendmsg(ua, _("Pool \"%s\" not found.\n"), pool_name);
326          }
327          pool = select_pool_resource(ua);
328       }
329    } else {
330       pool = job->pool;             /* use default */
331    }
332    if (!pool) {
333       return 0;
334    } else if (!acl_access_ok(ua, Pool_ACL, store->hdr.name)) {
335       bsendmsg(ua, _("No authorization. Pool \"%s\".\n"),
336                pool->hdr.name);
337       return 0;
338    }
339    Dmsg1(200, "Using pool\n", pool->hdr.name);
340
341    if (client_name) {
342       client = (CLIENT *)GetResWithName(R_CLIENT, client_name);
343       if (!client) {
344          if (*client_name != 0) {
345             bsendmsg(ua, _("Client \"%s\" not found.\n"), client_name);
346          }
347          client = select_client_resource(ua);
348       }
349    } else {
350       client = job->client;           /* use default */
351    }
352    if (!client) {
353       return 0;
354    } else if (!acl_access_ok(ua, Client_ACL, store->hdr.name)) {
355       bsendmsg(ua, _("No authorization. Client \"%s\".\n"),
356                client->hdr.name);
357       return 0;
358    }
359    Dmsg1(200, "Using client=%s\n", client->hdr.name);
360
361    if (fileset_name) {
362       fileset = (FILESET *)GetResWithName(R_FILESET, fileset_name);
363       if (!fileset) {
364          bsendmsg(ua, _("FileSet \"%s\" not found.\n"), fileset_name);
365          fileset = select_fileset_resource(ua);
366       }
367    } else {
368       fileset = job->fileset;           /* use default */
369    }
370    if (!fileset) {
371       return 0;
372    } else if (!acl_access_ok(ua, FileSet_ACL, store->hdr.name)) {
373       bsendmsg(ua, _("No authorization. FileSet \"%s\".\n"),
374                fileset->hdr.name);
375       return 0;
376    }
377
378    if (verify_job_name) {
379       verify_job = (JOB *)GetResWithName(R_JOB, verify_job_name);
380       if (!verify_job) {
381          bsendmsg(ua, _("Verify Job \"%s\" not found.\n"), verify_job_name);
382          verify_job = select_job_resource(ua);
383       }
384    } else {
385       verify_job = job->verify_job;
386    }
387
388    /*
389     * Create JCR to run job.  NOTE!!! after this point, free_jcr()
390     *  before returning.
391     */
392    jcr = new_jcr(sizeof(JCR), dird_free_jcr);
393    set_jcr_defaults(jcr, job);
394
395    jcr->verify_job = verify_job;
396    jcr->store = store;
397    /* If specific name given, zap all other stores */
398    if (store_name) {
399       for (i=0; i < MAX_STORE; i++) {
400          jcr->storage[i] = NULL;
401       }
402    }
403    jcr->client = client;
404    jcr->fileset = fileset;
405    jcr->pool = pool;
406    jcr->ExpectedFiles = files;
407    if (catalog != NULL) {
408       jcr->catalog = catalog;
409    }
410    if (where) {
411       if (jcr->where) {
412          free(jcr->where);
413       }
414       jcr->where = bstrdup(where);
415    }
416
417    if (when) {
418       jcr->sched_time = str_to_utime(when);
419       if (jcr->sched_time == 0) {
420          bsendmsg(ua, _("Invalid time, using current time.\n"));
421          jcr->sched_time = time(NULL);
422       }
423    }
424          
425    if (bootstrap) {
426       if (jcr->RestoreBootstrap) {
427          free(jcr->RestoreBootstrap);
428       }
429       jcr->RestoreBootstrap = bstrdup(bootstrap);
430    }
431
432    if (replace) {
433       jcr->replace = 0;
434       for (i=0; ReplaceOptions[i].name; i++) {
435          if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
436             jcr->replace = ReplaceOptions[i].token;
437          }
438       }
439       if (!jcr->replace) {
440          bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
441          goto bail_out;
442       }
443    } else if (job->replace) {
444       jcr->replace = job->replace;
445    } else {
446       jcr->replace = REPLACE_ALWAYS;
447    }
448
449    if (Priority) {
450       jcr->JobPriority = Priority;
451    }
452       
453    if (find_arg(ua, _("fdcalled")) > 0) {
454       jcr->file_bsock = dup_bsock(ua->UA_sock);
455       ua->quit = true;
456    }
457
458 try_again:
459    replace = ReplaceOptions[0].name;
460    for (i=0; ReplaceOptions[i].name; i++) {
461       if (ReplaceOptions[i].token == jcr->replace) {
462          replace = ReplaceOptions[i].name;
463       }
464    }
465    if (level_name) {
466       if (!get_level_from_name(jcr, level_name)) {
467          bsendmsg(ua, _("Level %s not valid.\n"), level_name);
468          goto bail_out;
469       }
470    }
471    if (jid) {
472       jcr->RestoreJobId = atoi(jid);
473    }
474
475    /* Run without prompting? */
476    if (ua->batch || find_arg(ua, _("yes")) > 0) {
477       goto start_job;
478    }
479
480    /*  
481     * Prompt User to see if all run job parameters are correct, and
482     *   allow him to modify them.
483     */
484    Dmsg1(20, "JobType=%c\n", jcr->JobType);
485    switch (jcr->JobType) {
486       char ec1[30];
487       char dt[MAX_TIME_LENGTH];
488    case JT_ADMIN:
489          bsendmsg(ua, _("Run %s job\n\
490 JobName:  %s\n\
491 FileSet:  %s\n\
492 Client:   %s\n\
493 Storage:  %s\n\
494 When:     %s\n\
495 Priority: %d\n"),
496                  _("Admin"),
497                  job->hdr.name,
498                  jcr->fileset->hdr.name,
499                  NPRT(jcr->client->hdr.name),
500                  NPRT(jcr->store->hdr.name), 
501                  bstrutime(dt, sizeof(dt), jcr->sched_time), 
502                  jcr->JobPriority);
503       jcr->JobLevel = L_FULL;
504       break;
505    case JT_BACKUP:
506    case JT_VERIFY:
507       if (jcr->JobType == JT_BACKUP) {
508          bsendmsg(ua, _("Run %s job\n\
509 JobName:  %s\n\
510 FileSet:  %s\n\
511 Level:    %s\n\
512 Client:   %s\n\
513 Storage:  %s\n\
514 Pool:     %s\n\
515 When:     %s\n\
516 Priority: %d\n"),
517                  _("Backup"),
518                  job->hdr.name,
519                  jcr->fileset->hdr.name,
520                  level_to_str(jcr->JobLevel),
521                  jcr->client->hdr.name,
522                  jcr->store->hdr.name,
523                  NPRT(jcr->pool->hdr.name), 
524                  bstrutime(dt, sizeof(dt), jcr->sched_time),
525                  jcr->JobPriority);
526       } else {  /* JT_VERIFY */
527          const char *Name;
528          if (jcr->verify_job) {
529             Name = jcr->verify_job->hdr.name;
530          } else {
531             Name = "";
532          }
533          bsendmsg(ua, _("Run %s job\n\
534 JobName:     %s\n\
535 FileSet:     %s\n\
536 Level:       %s\n\
537 Client:      %s\n\
538 Storage:     %s\n\
539 Pool:        %s\n\
540 Verify Job:  %s\n\
541 When:        %s\n\
542 Priority:    %d\n"),
543               _("Verify"),
544               job->hdr.name,
545               jcr->fileset->hdr.name,
546               level_to_str(jcr->JobLevel),
547               jcr->client->hdr.name,
548               jcr->store->hdr.name,
549               NPRT(jcr->pool->hdr.name), 
550               Name,            
551               bstrutime(dt, sizeof(dt), jcr->sched_time),
552               jcr->JobPriority);
553       }
554       break;
555    case JT_RESTORE:
556       if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
557          if (jid) {
558             jcr->RestoreJobId = atoi(jid);
559          } else {
560             if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
561                goto bail_out;
562             }  
563             jcr->RestoreJobId = ua->pint32_val;
564          }
565       }
566       jcr->JobLevel = L_FULL;      /* default level */
567       Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
568       if (jcr->RestoreJobId == 0) {
569          bsendmsg(ua, _("Run Restore job\n"
570                         "JobName:    %s\n"
571                         "Bootstrap:  %s\n"
572                         "Where:      %s\n"
573                         "Replace:    %s\n"
574                         "FileSet:    %s\n"
575                         "Client:     %s\n"
576                         "Storage:    %s\n"
577                         "When:       %s\n"
578                         "Catalog:    %s\n"
579                         "Priority:   %d\n"),
580               job->hdr.name,
581               NPRT(jcr->RestoreBootstrap),
582               jcr->where?jcr->where:NPRT(job->RestoreWhere),
583               replace,
584               jcr->fileset->hdr.name,
585               jcr->client->hdr.name,
586               jcr->store->hdr.name, 
587               bstrutime(dt, sizeof(dt), jcr->sched_time),
588               jcr->catalog->hdr.name,
589               jcr->JobPriority);
590       } else {
591          bsendmsg(ua, _("Run Restore job\n"
592                        "JobName:    %s\n"
593                        "Bootstrap:  %s\n"
594                        "Where:      %s\n"
595                        "Replace:    %s\n"
596                        "Client:     %s\n"
597                        "Storage:    %s\n"
598                        "JobId:      %s\n"
599                        "When:       %s\n"
600                        "Catalog:    %s\n"
601                        "Priority:   %d\n"),
602               job->hdr.name,
603               NPRT(jcr->RestoreBootstrap),
604               jcr->where?jcr->where:NPRT(job->RestoreWhere),
605               replace,
606               jcr->client->hdr.name,
607               jcr->store->hdr.name, 
608               jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1), 
609               bstrutime(dt, sizeof(dt), jcr->sched_time),
610               jcr->catalog->hdr.name,
611               jcr->JobPriority);
612       }
613       break;
614    default:
615       bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
616       goto bail_out;
617    }
618
619
620    if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
621       goto bail_out;
622    }
623    /*
624     * At user request modify parameters of job to be run.
625     */
626    if (ua->cmd[0] == 0) {
627       goto bail_out;
628    }
629    if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
630       FILE *fd;
631
632       start_prompt(ua, _("Parameters to modify:\n"));
633       add_prompt(ua, _("Level"));            /* 0 */
634       add_prompt(ua, _("Storage"));          /* 1 */
635       add_prompt(ua, _("Job"));              /* 2 */
636       add_prompt(ua, _("FileSet"));          /* 3 */
637       add_prompt(ua, _("Client"));           /* 4 */
638       add_prompt(ua, _("When"));             /* 5 */
639       add_prompt(ua, _("Priority"));         /* 6 */
640       if (jcr->JobType == JT_BACKUP ||
641           jcr->JobType == JT_VERIFY) {
642          add_prompt(ua, _("Pool"));          /* 7 */
643          if (jcr->JobType == JT_VERIFY) {
644             add_prompt(ua, _("Verify Job"));  /* 8 */
645          }
646       } else if (jcr->JobType == JT_RESTORE) {
647          add_prompt(ua, _("Bootstrap"));     /* 7 */
648          add_prompt(ua, _("Where"));         /* 8 */
649          add_prompt(ua, _("Replace"));       /* 9 */
650          add_prompt(ua, _("JobId"));         /* 10 */
651       }
652       switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
653       case 0:
654          /* Level */
655          if (jcr->JobType == JT_BACKUP) {
656             start_prompt(ua, _("Levels:\n"));
657             add_prompt(ua, _("Base"));
658             add_prompt(ua, _("Full"));
659             add_prompt(ua, _("Incremental"));
660             add_prompt(ua, _("Differential"));
661             add_prompt(ua, _("Since"));
662             switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
663             case 0:
664                jcr->JobLevel = L_BASE;
665                break;
666             case 1:
667                jcr->JobLevel = L_FULL;
668                break;
669             case 2:
670                jcr->JobLevel = L_INCREMENTAL;
671                break;
672             case 3:
673                jcr->JobLevel = L_DIFFERENTIAL;
674                break;
675             case 4:
676                jcr->JobLevel = L_SINCE;
677                break;
678             default:
679                break;
680             }
681             goto try_again;
682          } else if (jcr->JobType == JT_VERIFY) {
683             start_prompt(ua, _("Levels:\n"));
684             add_prompt(ua, _("Initialize Catalog"));
685             add_prompt(ua, _("Verify Catalog"));
686             add_prompt(ua, _("Verify Volume to Catalog"));
687             add_prompt(ua, _("Verify Disk to Catalog"));
688             add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
689             switch (do_prompt(ua, "",  _("Select level"), NULL, 0)) {
690             case 0:
691                jcr->JobLevel = L_VERIFY_INIT;
692                break;
693             case 1:
694                jcr->JobLevel = L_VERIFY_CATALOG;
695                break;
696             case 2:
697                jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
698                break;
699             case 3:
700                jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
701                break;
702             case 4:
703                jcr->JobLevel = L_VERIFY_DATA;
704                break;
705             default:
706                break;
707             }
708             goto try_again;
709          } else {
710             bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
711          }
712          goto try_again;
713       case 1:
714          /* Storage */
715          store = select_storage_resource(ua);
716          if (store) {
717             jcr->store = store;
718             goto try_again;
719          }
720          break;
721       case 2:
722          /* Job */
723          job = select_job_resource(ua);
724          if (job) {
725             jcr->job = job;
726             set_jcr_defaults(jcr, job);
727             goto try_again;
728          }
729          break;
730       case 3:
731          /* FileSet */
732          fileset = select_fileset_resource(ua);
733          if (fileset) {
734             jcr->fileset = fileset;
735             goto try_again;
736          }      
737          break;
738       case 4:
739          /* Client */
740          client = select_client_resource(ua);
741          if (client) {
742             jcr->client = client;
743             goto try_again;
744          }
745          break;
746       case 5:
747          /* When */
748          if (!get_cmd(ua, _("Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now): "))) {
749             break;
750          }
751          if (ua->cmd[0] == 0) {
752             jcr->sched_time = time(NULL);
753          } else {
754             jcr->sched_time = str_to_utime(ua->cmd);
755             if (jcr->sched_time == 0) {
756                bsendmsg(ua, _("Invalid time, using current time.\n"));
757                jcr->sched_time = time(NULL);
758             }
759          }
760          goto try_again;
761       case 6:
762          /* Priority */
763          if (!get_pint(ua, _("Enter new Priority: "))) {
764             break;
765          }
766          if (ua->pint32_val == 0) {
767             bsendmsg(ua, _("Priority must be a positive integer.\n"));
768          } else {
769             jcr->JobPriority = ua->pint32_val;
770          }
771          goto try_again;
772       case 7:
773          /* Pool or Bootstrap depending on JobType */
774          if (jcr->JobType == JT_BACKUP ||
775              jcr->JobType == JT_VERIFY) {      /* Pool */
776             pool = select_pool_resource(ua);
777             if (pool) {
778                jcr->pool = pool;
779                goto try_again;
780             }
781             break;
782          }
783
784          /* Bootstrap */
785          if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
786             break;
787          }
788          if (jcr->RestoreBootstrap) {
789             free(jcr->RestoreBootstrap);
790             jcr->RestoreBootstrap = NULL;
791          }
792          if (ua->cmd[0] != 0) {
793             jcr->RestoreBootstrap = bstrdup(ua->cmd);
794             fd = fopen(jcr->RestoreBootstrap, "r");
795             if (!fd) {
796                bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
797                   jcr->RestoreBootstrap, strerror(errno));
798                free(jcr->RestoreBootstrap);
799                jcr->RestoreBootstrap = NULL;
800             } else {
801                fclose(fd);
802             }
803          }
804          goto try_again;
805       case 8:
806          /* Verify Job */
807          if (jcr->JobType == JT_VERIFY) {
808             verify_job = select_job_resource(ua);
809             if (verify_job) {
810               jcr->verify_job = verify_job;
811             }
812             goto try_again;
813          }
814          /* Where */
815          if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
816             break;
817          }
818          if (jcr->where) {
819             free(jcr->where);
820             jcr->where = NULL;
821          }
822          if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
823             ua->cmd[0] = 0;
824          }
825          jcr->where = bstrdup(ua->cmd);
826          goto try_again;
827       case 9:
828          /* Replace */
829          start_prompt(ua, _("Replace:\n"));
830          for (i=0; ReplaceOptions[i].name; i++) {
831             add_prompt(ua, ReplaceOptions[i].name);
832          }
833          opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
834          if (opt >=  0) {
835             jcr->replace = ReplaceOptions[opt].token;
836          }
837          goto try_again;
838       case 10:
839          /* JobId */
840          jid = NULL;                  /* force reprompt */
841          jcr->RestoreJobId = 0;
842          if (jcr->RestoreBootstrap) {
843             bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
844          }
845          goto try_again;
846       default: 
847          goto try_again;
848       }
849       goto bail_out;
850    }
851
852    if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
853       JobId_t JobId;
854       Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
855 start_job:
856       JobId = run_job(jcr);
857       free_jcr(jcr);                  /* release jcr */
858       if (JobId == 0) {
859          bsendmsg(ua, _("Job failed.\n"));
860       } else {
861          bsendmsg(ua, _("Job started. JobId=%u\n"), JobId);
862       }
863       return JobId;
864    }
865
866 bail_out:
867    bsendmsg(ua, _("Job not run.\n"));
868    free_jcr(jcr);
869    return 0;                       /* do not run */
870 }