]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_run.c
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[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    set_storage(jcr, store);
397    jcr->client = client;
398    jcr->fileset = fileset;
399    jcr->pool = pool;
400    jcr->ExpectedFiles = files;
401    if (catalog != NULL) {
402       jcr->catalog = catalog;
403    }
404    if (where) {
405       if (jcr->where) {
406          free(jcr->where);
407       }
408       jcr->where = bstrdup(where);
409    }
410
411    if (when) {
412       jcr->sched_time = str_to_utime(when);
413       if (jcr->sched_time == 0) {
414          bsendmsg(ua, _("Invalid time, using current time.\n"));
415          jcr->sched_time = time(NULL);
416       }
417    }
418
419    if (bootstrap) {
420       if (jcr->RestoreBootstrap) {
421          free(jcr->RestoreBootstrap);
422       }
423       jcr->RestoreBootstrap = bstrdup(bootstrap);
424    }
425
426    if (replace) {
427       jcr->replace = 0;
428       for (i=0; ReplaceOptions[i].name; i++) {
429          if (strcasecmp(replace, ReplaceOptions[i].name) == 0) {
430             jcr->replace = ReplaceOptions[i].token;
431          }
432       }
433       if (!jcr->replace) {
434          bsendmsg(ua, _("Invalid replace option: %s\n"), replace);
435          goto bail_out;
436       }
437    } else if (job->replace) {
438       jcr->replace = job->replace;
439    } else {
440       jcr->replace = REPLACE_ALWAYS;
441    }
442
443    if (Priority) {
444       jcr->JobPriority = Priority;
445    }
446
447    if (find_arg(ua, _("fdcalled")) > 0) {
448       jcr->file_bsock = dup_bsock(ua->UA_sock);
449       ua->quit = true;
450    }
451
452 try_again:
453    replace = ReplaceOptions[0].name;
454    for (i=0; ReplaceOptions[i].name; i++) {
455       if (ReplaceOptions[i].token == jcr->replace) {
456          replace = ReplaceOptions[i].name;
457       }
458    }
459    if (level_name) {
460       if (!get_level_from_name(jcr, level_name)) {
461          bsendmsg(ua, _("Level %s not valid.\n"), level_name);
462          goto bail_out;
463       }
464    }
465    if (jid) {
466       jcr->RestoreJobId = atoi(jid);
467    }
468
469    /* Run without prompting? */
470    if (ua->batch || find_arg(ua, _("yes")) > 0) {
471       goto start_job;
472    }
473
474    /*
475     * Prompt User to see if all run job parameters are correct, and
476     *   allow him to modify them.
477     */
478    Dmsg1(20, "JobType=%c\n", jcr->JobType);
479    switch (jcr->JobType) {
480       char ec1[30];
481       char dt[MAX_TIME_LENGTH];
482    case JT_ADMIN:
483          bsendmsg(ua, _("Run %s job\n"
484 "JobName:  %s\n"
485 "FileSet:  %s\n"
486 "Client:   %s\n"
487 "Storage:  %s\n"
488 "When:     %s\n"
489 "Priority: %d\n"),
490                  _("Admin"),
491                  job->hdr.name,
492                  jcr->fileset->hdr.name,
493                  NPRT(jcr->client->hdr.name),
494                  NPRT(jcr->store->hdr.name),
495                  bstrutime(dt, sizeof(dt), jcr->sched_time),
496                  jcr->JobPriority);
497       jcr->JobLevel = L_FULL;
498       break;
499    case JT_BACKUP:
500    case JT_VERIFY:
501       if (jcr->JobType == JT_BACKUP) {
502          bsendmsg(ua, _("Run %s job\n"
503 "JobName:  %s\n"
504 "FileSet:  %s\n"
505 "Level:    %s\n"
506 "Client:   %s\n"
507 "Storage:  %s\n"
508 "Pool:     %s\n"
509 "When:     %s\n"
510 "Priority: %d\n"),
511                  _("Backup"),
512                  job->hdr.name,
513                  jcr->fileset->hdr.name,
514                  level_to_str(jcr->JobLevel),
515                  jcr->client->hdr.name,
516                  jcr->store->hdr.name,
517                  NPRT(jcr->pool->hdr.name),
518                  bstrutime(dt, sizeof(dt), jcr->sched_time),
519                  jcr->JobPriority);
520       } else {  /* JT_VERIFY */
521          const char *Name;
522          if (jcr->verify_job) {
523             Name = jcr->verify_job->hdr.name;
524          } else {
525             Name = "";
526          }
527          bsendmsg(ua, _("Run %s job\n"
528 "JobName:     %s\n"
529 "FileSet:     %s\n"
530 "Level:       %s\n"
531 "Client:      %s\n"
532 "Storage:     %s\n"
533 "Pool:        %s\n"
534 "Verify Job:  %s\n"
535 "When:        %s\n"
536 "Priority:    %d\n"),
537               _("Verify"),
538               job->hdr.name,
539               jcr->fileset->hdr.name,
540               level_to_str(jcr->JobLevel),
541               jcr->client->hdr.name,
542               jcr->store->hdr.name,
543               NPRT(jcr->pool->hdr.name),
544               Name,
545               bstrutime(dt, sizeof(dt), jcr->sched_time),
546               jcr->JobPriority);
547       }
548       break;
549    case JT_RESTORE:
550       if (jcr->RestoreJobId == 0 && !jcr->RestoreBootstrap) {
551          if (jid) {
552             jcr->RestoreJobId = atoi(jid);
553          } else {
554             if (!get_pint(ua, _("Please enter a JobId for restore: "))) {
555                goto bail_out;
556             }
557             jcr->RestoreJobId = ua->pint32_val;
558          }
559       }
560       jcr->JobLevel = L_FULL;      /* default level */
561       Dmsg1(20, "JobId to restore=%d\n", jcr->RestoreJobId);
562       if (jcr->RestoreJobId == 0) {
563          bsendmsg(ua, _("Run Restore job\n"
564                         "JobName:    %s\n"
565                         "Bootstrap:  %s\n"
566                         "Where:      %s\n"
567                         "Replace:    %s\n"
568                         "FileSet:    %s\n"
569                         "Client:     %s\n"
570                         "Storage:    %s\n"
571                         "When:       %s\n"
572                         "Catalog:    %s\n"
573                         "Priority:   %d\n"),
574               job->hdr.name,
575               NPRT(jcr->RestoreBootstrap),
576               jcr->where?jcr->where:NPRT(job->RestoreWhere),
577               replace,
578               jcr->fileset->hdr.name,
579               jcr->client->hdr.name,
580               jcr->store->hdr.name,
581               bstrutime(dt, sizeof(dt), jcr->sched_time),
582               jcr->catalog->hdr.name,
583               jcr->JobPriority);
584       } else {
585          bsendmsg(ua, _("Run Restore job\n"
586                        "JobName:    %s\n"
587                        "Bootstrap:  %s\n"
588                        "Where:      %s\n"
589                        "Replace:    %s\n"
590                        "Client:     %s\n"
591                        "Storage:    %s\n"
592                        "JobId:      %s\n"
593                        "When:       %s\n"
594                        "Catalog:    %s\n"
595                        "Priority:   %d\n"),
596               job->hdr.name,
597               NPRT(jcr->RestoreBootstrap),
598               jcr->where?jcr->where:NPRT(job->RestoreWhere),
599               replace,
600               jcr->client->hdr.name,
601               jcr->store->hdr.name,
602               jcr->RestoreJobId==0?"*None*":edit_uint64(jcr->RestoreJobId, ec1),
603               bstrutime(dt, sizeof(dt), jcr->sched_time),
604               jcr->catalog->hdr.name,
605               jcr->JobPriority);
606       }
607       break;
608    default:
609       bsendmsg(ua, _("Unknown Job Type=%d\n"), jcr->JobType);
610       goto bail_out;
611    }
612
613
614    if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
615       goto bail_out;
616    }
617    /*
618     * At user request modify parameters of job to be run.
619     */
620    if (ua->cmd[0] == 0) {
621       goto bail_out;
622    }
623    if (strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
624       FILE *fd;
625
626       start_prompt(ua, _("Parameters to modify:\n"));
627       add_prompt(ua, _("Level"));            /* 0 */
628       add_prompt(ua, _("Storage"));          /* 1 */
629       add_prompt(ua, _("Job"));              /* 2 */
630       add_prompt(ua, _("FileSet"));          /* 3 */
631       add_prompt(ua, _("Client"));           /* 4 */
632       add_prompt(ua, _("When"));             /* 5 */
633       add_prompt(ua, _("Priority"));         /* 6 */
634       if (jcr->JobType == JT_BACKUP ||
635           jcr->JobType == JT_VERIFY) {
636          add_prompt(ua, _("Pool"));          /* 7 */
637          if (jcr->JobType == JT_VERIFY) {
638             add_prompt(ua, _("Verify Job"));  /* 8 */
639          }
640       } else if (jcr->JobType == JT_RESTORE) {
641          add_prompt(ua, _("Bootstrap"));     /* 7 */
642          add_prompt(ua, _("Where"));         /* 8 */
643          add_prompt(ua, _("Replace"));       /* 9 */
644          add_prompt(ua, _("JobId"));         /* 10 */
645       }
646       switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
647       case 0:
648          /* Level */
649          if (jcr->JobType == JT_BACKUP) {
650             start_prompt(ua, _("Levels:\n"));
651             add_prompt(ua, _("Base"));
652             add_prompt(ua, _("Full"));
653             add_prompt(ua, _("Incremental"));
654             add_prompt(ua, _("Differential"));
655             add_prompt(ua, _("Since"));
656             switch (do_prompt(ua, "", _("Select level"), NULL, 0)) {
657             case 0:
658                jcr->JobLevel = L_BASE;
659                break;
660             case 1:
661                jcr->JobLevel = L_FULL;
662                break;
663             case 2:
664                jcr->JobLevel = L_INCREMENTAL;
665                break;
666             case 3:
667                jcr->JobLevel = L_DIFFERENTIAL;
668                break;
669             case 4:
670                jcr->JobLevel = L_SINCE;
671                break;
672             default:
673                break;
674             }
675             goto try_again;
676          } else if (jcr->JobType == JT_VERIFY) {
677             start_prompt(ua, _("Levels:\n"));
678             add_prompt(ua, _("Initialize Catalog"));
679             add_prompt(ua, _("Verify Catalog"));
680             add_prompt(ua, _("Verify Volume to Catalog"));
681             add_prompt(ua, _("Verify Disk to Catalog"));
682             add_prompt(ua, _("Verify Volume Data (not yet implemented)"));
683             switch (do_prompt(ua, "",  _("Select level"), NULL, 0)) {
684             case 0:
685                jcr->JobLevel = L_VERIFY_INIT;
686                break;
687             case 1:
688                jcr->JobLevel = L_VERIFY_CATALOG;
689                break;
690             case 2:
691                jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
692                break;
693             case 3:
694                jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
695                break;
696             case 4:
697                jcr->JobLevel = L_VERIFY_DATA;
698                break;
699             default:
700                break;
701             }
702             goto try_again;
703          } else {
704             bsendmsg(ua, _("Level not appropriate for this Job. Cannot be changed.\n"));
705          }
706          goto try_again;
707       case 1:
708          /* Storage */
709          store = select_storage_resource(ua);
710          if (store) {
711             set_storage(jcr, store);
712             goto try_again;
713          }
714          break;
715       case 2:
716          /* Job */
717          job = select_job_resource(ua);
718          if (job) {
719             jcr->job = job;
720             set_jcr_defaults(jcr, job);
721             goto try_again;
722          }
723          break;
724       case 3:
725          /* FileSet */
726          fileset = select_fileset_resource(ua);
727          if (fileset) {
728             jcr->fileset = fileset;
729             goto try_again;
730          }
731          break;
732       case 4:
733          /* Client */
734          client = select_client_resource(ua);
735          if (client) {
736             jcr->client = client;
737             goto try_again;
738          }
739          break;
740       case 5:
741          /* When */
742          if (!get_cmd(ua, _("Please enter desired start time as YYYY-MM-DD HH:MM:SS (return for now): "))) {
743             break;
744          }
745          if (ua->cmd[0] == 0) {
746             jcr->sched_time = time(NULL);
747          } else {
748             jcr->sched_time = str_to_utime(ua->cmd);
749             if (jcr->sched_time == 0) {
750                bsendmsg(ua, _("Invalid time, using current time.\n"));
751                jcr->sched_time = time(NULL);
752             }
753          }
754          goto try_again;
755       case 6:
756          /* Priority */
757          if (!get_pint(ua, _("Enter new Priority: "))) {
758             break;
759          }
760          if (ua->pint32_val == 0) {
761             bsendmsg(ua, _("Priority must be a positive integer.\n"));
762          } else {
763             jcr->JobPriority = ua->pint32_val;
764          }
765          goto try_again;
766       case 7:
767          /* Pool or Bootstrap depending on JobType */
768          if (jcr->JobType == JT_BACKUP ||
769              jcr->JobType == JT_VERIFY) {      /* Pool */
770             pool = select_pool_resource(ua);
771             if (pool) {
772                jcr->pool = pool;
773                goto try_again;
774             }
775             break;
776          }
777
778          /* Bootstrap */
779          if (!get_cmd(ua, _("Please enter the Bootstrap file name: "))) {
780             break;
781          }
782          if (jcr->RestoreBootstrap) {
783             free(jcr->RestoreBootstrap);
784             jcr->RestoreBootstrap = NULL;
785          }
786          if (ua->cmd[0] != 0) {
787             jcr->RestoreBootstrap = bstrdup(ua->cmd);
788             fd = fopen(jcr->RestoreBootstrap, "r");
789             if (!fd) {
790                bsendmsg(ua, _("Warning cannot open %s: ERR=%s\n"),
791                   jcr->RestoreBootstrap, strerror(errno));
792                free(jcr->RestoreBootstrap);
793                jcr->RestoreBootstrap = NULL;
794             } else {
795                fclose(fd);
796             }
797          }
798          goto try_again;
799       case 8:
800          /* Verify Job */
801          if (jcr->JobType == JT_VERIFY) {
802             verify_job = select_job_resource(ua);
803             if (verify_job) {
804               jcr->verify_job = verify_job;
805             }
806             goto try_again;
807          }
808          /* Where */
809          if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
810             break;
811          }
812          if (jcr->where) {
813             free(jcr->where);
814             jcr->where = NULL;
815          }
816          if (ua->cmd[0] == '/' && ua->cmd[1] == 0) {
817             ua->cmd[0] = 0;
818          }
819          jcr->where = bstrdup(ua->cmd);
820          goto try_again;
821       case 9:
822          /* Replace */
823          start_prompt(ua, _("Replace:\n"));
824          for (i=0; ReplaceOptions[i].name; i++) {
825             add_prompt(ua, ReplaceOptions[i].name);
826          }
827          opt = do_prompt(ua, "", _("Select replace option"), NULL, 0);
828          if (opt >=  0) {
829             jcr->replace = ReplaceOptions[opt].token;
830          }
831          goto try_again;
832       case 10:
833          /* JobId */
834          jid = NULL;                  /* force reprompt */
835          jcr->RestoreJobId = 0;
836          if (jcr->RestoreBootstrap) {
837             bsendmsg(ua, _("You must set the bootstrap file to NULL to be able to specify a JobId.\n"));
838          }
839          goto try_again;
840       default:
841          goto try_again;
842       }
843       goto bail_out;
844    }
845
846    if (strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
847       JobId_t JobId;
848       Dmsg1(200, "Calling run_job job=%x\n", jcr->job);
849 start_job:
850       JobId = run_job(jcr);
851       free_jcr(jcr);                  /* release jcr */
852       if (JobId == 0) {
853          bsendmsg(ua, _("Job failed.\n"));
854       } else {
855          bsendmsg(ua, _("Job started. JobId=%u\n"), JobId);
856       }
857       return JobId;
858    }
859
860 bail_out:
861    bsendmsg(ua, _("Job not run.\n"));
862    free_jcr(jcr);
863    return 0;                       /* do not run */
864 }