]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/job.c
btape test and fill improvements + first cut Qmsg
[bacula/bacula] / bacula / src / dird / job.c
1 /*
2  *
3  *   Bacula Director Job processing routines
4  *
5  *     Kern Sibbald, October MM
6  *
7  *    Version $Id$
8  */
9 /*
10    Copyright (C) 2000-2004 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "dird.h"
31
32 /* Forward referenced subroutines */
33 static void *job_thread(void *arg);
34 static void job_monitor_watchdog(watchdog_t *self);
35 static void job_monitor_destructor(watchdog_t *self);
36 static bool job_check_maxwaittime(JCR *control_jcr, JCR *jcr);
37 static bool job_check_maxruntime(JCR *control_jcr, JCR *jcr);
38
39 /* Exported subroutines */
40
41 /* Imported subroutines */
42 extern void term_scheduler();
43 extern void term_ua_server();
44 extern int do_backup(JCR *jcr);
45 extern int do_admin(JCR *jcr);
46 extern int do_restore(JCR *jcr);
47 extern int do_verify(JCR *jcr);
48
49 /* Imported variables */
50 extern time_t watchdog_time;
51
52 jobq_t  job_queue;
53
54 void init_job_server(int max_workers)
55 {
56    int stat;
57    watchdog_t *wd;
58    
59    if ((stat = jobq_init(&job_queue, max_workers, job_thread)) != 0) {
60       Emsg1(M_ABORT, 0, _("Could not init job queue: ERR=%s\n"), strerror(stat));
61    }
62    if ((wd = new_watchdog()) == NULL) {
63       Emsg0(M_ABORT, 0, _("Could not init job monitor watchdogs\n"));
64    }
65    wd->callback = job_monitor_watchdog;
66    wd->destructor = job_monitor_destructor;
67    wd->one_shot = false;
68    wd->interval = 60;
69    wd->data = create_control_jcr("*JobMonitor*", JT_SYSTEM);
70    register_watchdog(wd);
71    
72    return;
73 }
74
75
76 /*
77  * Run a job -- typically called by the scheduler, but may also
78  *              be called by the UA (Console program).
79  *
80  */
81 void run_job(JCR *jcr)
82 {
83    int stat, errstat;
84
85    P(jcr->mutex);
86    sm_check(__FILE__, __LINE__, True);
87    init_msg(jcr, jcr->messages);
88    create_unique_job_name(jcr, jcr->job->hdr.name);
89    set_jcr_job_status(jcr, JS_Created);
90    jcr->jr.SchedTime = jcr->sched_time;
91    jcr->jr.StartTime = jcr->start_time;
92    jcr->jr.EndTime = 0;               /* perhaps rescheduled, clear it */
93    jcr->jr.Type = jcr->JobType;
94    jcr->jr.Level = jcr->JobLevel;
95    jcr->jr.JobStatus = jcr->JobStatus;
96    bstrncpy(jcr->jr.Name, jcr->job->hdr.name, sizeof(jcr->jr.Name));
97    bstrncpy(jcr->jr.Job, jcr->Job, sizeof(jcr->jr.Job));
98
99    /* Initialize termination condition variable */
100    if ((errstat = pthread_cond_init(&jcr->term_wait, NULL)) != 0) {
101       Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
102       goto bail_out;
103    }
104
105    /*
106     * Open database
107     */
108    Dmsg0(50, "Open database\n");
109    jcr->db=db_init_database(jcr, jcr->catalog->db_name, jcr->catalog->db_user,
110                             jcr->catalog->db_password, jcr->catalog->db_address,
111                             jcr->catalog->db_port, jcr->catalog->db_socket);
112    if (!jcr->db || !db_open_database(jcr, jcr->db)) {
113       Jmsg(jcr, M_FATAL, 0, _("Could not open database \"%s\".\n"),
114                  jcr->catalog->db_name);
115       if (jcr->db) {
116          Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
117       }
118       goto bail_out;
119    }
120    Dmsg0(50, "DB opened\n");
121
122    /*
123     * Create Job record  
124     */
125    jcr->jr.JobStatus = jcr->JobStatus;
126    if (!db_create_job_record(jcr, jcr->db, &jcr->jr)) {
127       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
128       goto bail_out;
129    }
130    jcr->JobId = jcr->jr.JobId;
131
132    Dmsg4(50, "Created job record JobId=%d Name=%s Type=%c Level=%c\n", 
133        jcr->JobId, jcr->Job, jcr->jr.Type, jcr->jr.Level);
134    Dmsg0(200, "Add jrc to work queue\n");
135
136    /* Queue the job to be run */
137    if ((stat = jobq_add(&job_queue, jcr)) != 0) {
138       Jmsg(jcr, M_FATAL, 0, _("Could not add job queue: ERR=%s\n"), strerror(stat));
139       goto bail_out;
140    }
141    Dmsg0(100, "Done run_job()\n");
142
143    V(jcr->mutex);
144    return;
145
146 bail_out:
147    set_jcr_job_status(jcr, JS_ErrorTerminated);
148    V(jcr->mutex);
149    return;
150
151 }
152
153
154 /* 
155  * This is the engine called by jobq.c:jobq_add() when we were pulled                
156  *  from the work queue.
157  *  At this point, we are running in our own thread and all
158  *    necessary resources are allocated -- see jobq.c
159  */
160 static void *job_thread(void *arg)
161 {
162    JCR *jcr = (JCR *)arg;
163
164    pthread_detach(pthread_self());
165    sm_check(__FILE__, __LINE__, True);
166
167    for ( ;; ) {
168
169       Dmsg0(200, "=====Start Job=========\n");
170       jcr->start_time = time(NULL);      /* set the real start time */
171       set_jcr_job_status(jcr, JS_Running);
172
173       if (job_canceled(jcr)) {
174          update_job_end_record(jcr);
175       } else if (jcr->job->MaxStartDelay != 0 && jcr->job->MaxStartDelay <
176           (utime_t)(jcr->start_time - jcr->sched_time)) {
177          Jmsg(jcr, M_FATAL, 0, _("Job canceled because max start delay time exceeded.\n"));
178          set_jcr_job_status(jcr, JS_Canceled);
179          update_job_end_record(jcr);
180       } else {
181
182          /* Run Job */
183          if (jcr->job->RunBeforeJob) {
184             POOLMEM *before = get_pool_memory(PM_FNAME);
185             int status;
186             BPIPE *bpipe;
187             char line[MAXSTRING];
188             
189             before = edit_job_codes(jcr, before, jcr->job->RunBeforeJob, "");
190             bpipe = open_bpipe(before, 0, "r");
191             free_pool_memory(before);
192             while (fgets(line, sizeof(line), bpipe->rfd)) {
193                Jmsg(jcr, M_INFO, 0, _("RunBefore: %s"), line);
194             }
195             status = close_bpipe(bpipe);
196             if (status != 0) {
197                Jmsg(jcr, M_FATAL, 0, _("RunBeforeJob returned non-zero status=%d\n"),
198                   status);
199                set_jcr_job_status(jcr, JS_FatalError);
200                update_job_end_record(jcr);
201                goto bail_out;
202             }
203          }
204          switch (jcr->JobType) {
205          case JT_BACKUP:
206             do_backup(jcr);
207             if (jcr->JobStatus == JS_Terminated) {
208                do_autoprune(jcr);
209             }
210             break;
211          case JT_VERIFY:
212             do_verify(jcr);
213             if (jcr->JobStatus == JS_Terminated) {
214                do_autoprune(jcr);
215             }
216             break;
217          case JT_RESTORE:
218             do_restore(jcr);
219             if (jcr->JobStatus == JS_Terminated) {
220                do_autoprune(jcr);
221             }
222             break;
223          case JT_ADMIN:
224             do_admin(jcr);
225             if (jcr->JobStatus == JS_Terminated) {
226                do_autoprune(jcr);
227             }
228             break;
229          default:
230             Pmsg1(0, "Unimplemented job type: %d\n", jcr->JobType);
231             break;
232          }
233          if ((jcr->job->RunAfterJob && jcr->JobStatus == JS_Terminated) ||
234              (jcr->job->RunAfterFailedJob && jcr->JobStatus != JS_Terminated)) {
235             POOLMEM *after = get_pool_memory(PM_FNAME);
236             int status;
237             BPIPE *bpipe;
238             char line[MAXSTRING];
239             
240             if (jcr->JobStatus == JS_Terminated) {
241                after = edit_job_codes(jcr, after, jcr->job->RunAfterJob, "");
242             } else {
243                after = edit_job_codes(jcr, after, jcr->job->RunAfterFailedJob, "");
244             }
245             bpipe = open_bpipe(after, 0, "r");
246             free_pool_memory(after);
247             while (fgets(line, sizeof(line), bpipe->rfd)) {
248                Jmsg(jcr, M_INFO, 0, _("RunAfter: %s"), line);
249             }
250             status = close_bpipe(bpipe);
251             /*
252              * Note, if we get an error here, do not mark the
253              *  job in error, simply report the error condition.   
254              */
255             if (status != 0) {
256                if (jcr->JobStatus == JS_Terminated) {
257                   Jmsg(jcr, M_ERROR, 0, _("RunAfterJob returned non-zero status=%d\n"),
258                        status);
259                } else {
260                   Jmsg(jcr, M_FATAL, 0, _("RunAfterFailedJob returned non-zero status=%d\n"),
261                        status);
262                }
263             }
264          }
265          /* Send off any queued messages */
266          if (jcr->msg_queue->size() > 0) {
267             dequeue_messages(jcr);
268          }
269       }
270 bail_out:
271       break;
272    }
273
274    Dmsg0(50, "======== End Job ==========\n");
275    sm_check(__FILE__, __LINE__, True);
276    return NULL;
277 }
278
279
280 /*
281  * Cancel a job -- typically called by the UA (Console program), but may also
282  *              be called by the job watchdog.
283  * 
284  *  Returns: 1 if cancel appears to be successful
285  *           0 on failure. Message sent to ua->jcr.
286  */
287 int cancel_job(UAContext *ua, JCR *jcr)
288 {
289    BSOCK *sd, *fd;
290
291    switch (jcr->JobStatus) {
292    case JS_Created:
293    case JS_WaitJobRes:
294    case JS_WaitClientRes:
295    case JS_WaitStoreRes:
296    case JS_WaitPriority:
297    case JS_WaitMaxJobs:
298    case JS_WaitStartTime:
299       set_jcr_job_status(jcr, JS_Canceled);
300       bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
301               jcr->JobId, jcr->Job);
302       jobq_remove(&job_queue, jcr); /* attempt to remove it from queue */
303       return 1;
304          
305    default:
306       set_jcr_job_status(jcr, JS_Canceled);
307
308       /* Cancel File daemon */
309       if (jcr->file_bsock) {
310          ua->jcr->client = jcr->client;
311          if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
312             bsendmsg(ua, _("Failed to connect to File daemon.\n"));
313             return 0;
314          }
315          Dmsg0(200, "Connected to file daemon\n");
316          fd = ua->jcr->file_bsock;
317          bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
318          while (bnet_recv(fd) >= 0) {
319             bsendmsg(ua, "%s", fd->msg);
320          }
321          bnet_sig(fd, BNET_TERMINATE);
322          bnet_close(fd);
323          ua->jcr->file_bsock = NULL;
324       }
325
326       /* Cancel Storage daemon */
327       if (jcr->store_bsock) {
328          ua->jcr->store = jcr->store;
329          if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
330             bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
331             return 0;
332          }
333          Dmsg0(200, "Connected to storage daemon\n");
334          sd = ua->jcr->store_bsock;
335          bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
336          while (bnet_recv(sd) >= 0) {
337             bsendmsg(ua, "%s", sd->msg);
338          }
339          bnet_sig(sd, BNET_TERMINATE);
340          bnet_close(sd);
341          ua->jcr->store_bsock = NULL;
342       }
343    }
344
345    return 1;
346 }
347
348
349 static void job_monitor_destructor(watchdog_t *self)
350 {
351    JCR *control_jcr = (JCR *) self->data;
352
353    free_jcr(control_jcr);
354 }
355
356 static void job_monitor_watchdog(watchdog_t *self)
357 {
358    JCR *control_jcr, *jcr;
359
360    control_jcr = (JCR *)self->data;
361
362    Dmsg1(400, "job_monitor_watchdog %p called\n", self);
363
364    lock_jcr_chain();
365
366    foreach_jcr(jcr) {
367       bool cancel;
368
369       if (jcr->JobId == 0) {
370          Dmsg2(400, "Skipping JCR %p (%s) with JobId 0\n",
371                jcr, jcr->Job);
372          /* Keep reference counts correct */
373          free_locked_jcr(jcr);
374          continue;
375       }
376
377       /* check MaxWaitTime */
378       cancel = job_check_maxwaittime(control_jcr, jcr);
379
380       /* check MaxRunTime */
381       cancel |= job_check_maxruntime(control_jcr, jcr);
382
383       if (cancel) {
384          Dmsg3(200, "Cancelling JCR %p jobid %d (%s)\n",
385                jcr, jcr->JobId, jcr->Job);
386
387          UAContext *ua = new_ua_context(jcr);
388          ua->jcr = control_jcr;
389          cancel_job(ua, jcr);
390          free_ua_context(ua);
391
392          Dmsg1(200, "Have cancelled JCR %p\n", jcr);
393       }
394
395       /* Keep reference counts correct */
396       free_locked_jcr(jcr);
397    }
398    unlock_jcr_chain();
399 }
400
401 /*
402  * Check if the maxwaittime has expired and it is possible
403  *  to cancel the job.
404  */
405 static bool job_check_maxwaittime(JCR *control_jcr, JCR *jcr)
406 {
407    bool cancel = false;
408
409    if (jcr->job->MaxWaitTime == 0) {
410       return false;
411    }
412    if ((watchdog_time - jcr->start_time) < jcr->job->MaxWaitTime) {
413       Dmsg3(200, "Job %p (%s) with MaxWaitTime %d not expired\n",
414             jcr, jcr->Job, jcr->job->MaxWaitTime);
415       return false;
416    }
417    Dmsg3(200, "Job %d (%s): MaxWaitTime of %d seconds exceeded, "
418          "checking status\n",
419          jcr->JobId, jcr->Job, jcr->job->MaxWaitTime);
420    switch (jcr->JobStatus) {
421    case JS_Created:
422    case JS_Blocked:
423    case JS_WaitFD:
424    case JS_WaitSD:
425    case JS_WaitStoreRes:
426    case JS_WaitClientRes:
427    case JS_WaitJobRes:
428    case JS_WaitPriority:
429    case JS_WaitMaxJobs:
430    case JS_WaitStartTime:
431       cancel = true;
432       Dmsg0(200, "JCR blocked in #1\n");
433       break;
434    case JS_Running:
435       Dmsg0(200, "JCR running, checking SD status\n");
436       switch (jcr->SDJobStatus) {
437       case JS_WaitMount:
438       case JS_WaitMedia:
439       case JS_WaitFD:
440          cancel = true;
441          Dmsg0(200, "JCR blocked in #2\n");
442          break;
443       default:
444          Dmsg0(200, "JCR not blocked in #2\n");
445          break;
446       }
447       break;
448    case JS_Terminated:
449    case JS_ErrorTerminated:
450    case JS_Canceled:
451    case JS_FatalError:
452       Dmsg0(200, "JCR already dead in #3\n");
453       break;
454    default:
455       Jmsg1(jcr, M_ERROR, 0, _("Unhandled job status code %d\n"),
456             jcr->JobStatus);
457    }
458    Dmsg3(200, "MaxWaitTime result: %scancel JCR %p (%s)\n",
459          cancel ? "" : "do not ", jcr, jcr->job);
460
461    return cancel;
462 }
463
464 /*
465  * Check if maxruntime has expired and if the job can be
466  *   canceled.
467  */
468 static bool job_check_maxruntime(JCR *control_jcr, JCR *jcr)
469 {
470    bool cancel = false;
471
472    if (jcr->job->MaxRunTime == 0) {
473       return false;
474    }
475    if ((watchdog_time - jcr->start_time) < jcr->job->MaxRunTime) {
476       Dmsg3(200, "Job %p (%s) with MaxRunTime %d not expired\n",
477             jcr, jcr->Job, jcr->job->MaxRunTime);
478       return false;
479    }
480
481    switch (jcr->JobStatus) {
482    case JS_Created:
483    case JS_Running:
484    case JS_Blocked:
485    case JS_WaitFD:
486    case JS_WaitSD:
487    case JS_WaitStoreRes:
488    case JS_WaitClientRes:
489    case JS_WaitJobRes:
490    case JS_WaitPriority:
491    case JS_WaitMaxJobs:
492    case JS_WaitStartTime:
493    case JS_Differences:
494       cancel = true;
495       break;
496    case JS_Terminated:
497    case JS_ErrorTerminated:
498    case JS_Canceled:
499    case JS_FatalError:
500       cancel = false;
501       break;
502    default:
503       Jmsg1(jcr, M_ERROR, 0, _("Unhandled job status code %d\n"),
504             jcr->JobStatus);
505    }
506
507    Dmsg3(200, "MaxRunTime result: %scancel JCR %p (%s)\n",
508          cancel ? "" : "do not ", jcr, jcr->job);
509
510    return cancel;
511 }
512
513
514 /*
515  * Get or create a Client record for this Job
516  */
517 int get_or_create_client_record(JCR *jcr)
518 {
519    CLIENT_DBR cr;
520
521    memset(&cr, 0, sizeof(cr));
522    bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
523    cr.AutoPrune = jcr->client->AutoPrune;
524    cr.FileRetention = jcr->client->FileRetention;
525    cr.JobRetention = jcr->client->JobRetention;
526    if (!jcr->client_name) {
527       jcr->client_name = get_pool_memory(PM_NAME);
528    }
529    pm_strcpy(&jcr->client_name, jcr->client->hdr.name);
530    if (!db_create_client_record(jcr, jcr->db, &cr)) {
531       Jmsg(jcr, M_FATAL, 0, _("Could not create Client record. ERR=%s\n"), 
532          db_strerror(jcr->db));
533       return 0;
534    }
535    jcr->jr.ClientId = cr.ClientId;
536    if (cr.Uname[0]) {
537       if (!jcr->client_uname) {
538          jcr->client_uname = get_pool_memory(PM_NAME);
539       }
540       pm_strcpy(&jcr->client_uname, cr.Uname);
541    }
542    Dmsg2(100, "Created Client %s record %d\n", jcr->client->hdr.name, 
543       jcr->jr.ClientId);
544    return 1;
545 }
546
547
548 /*
549  * Write status and such in DB
550  */
551 void update_job_end_record(JCR *jcr)
552 {
553    if (jcr->jr.EndTime == 0) {
554       jcr->jr.EndTime = time(NULL);
555    }
556    jcr->end_time = jcr->jr.EndTime;
557    jcr->jr.JobId = jcr->JobId;
558    jcr->jr.JobStatus = jcr->JobStatus;
559    jcr->jr.JobFiles = jcr->JobFiles;
560    jcr->jr.JobBytes = jcr->JobBytes;
561    jcr->jr.VolSessionId = jcr->VolSessionId;
562    jcr->jr.VolSessionTime = jcr->VolSessionTime;
563    if (!db_update_job_end_record(jcr, jcr->db, &jcr->jr)) {
564       Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"), 
565          db_strerror(jcr->db));
566    }
567 }
568
569 /*
570  * Takes base_name and appends (unique) current
571  *   date and time to form unique job name.
572  *
573  *  Returns: unique job name in jcr->Job
574  *    date/time in jcr->start_time
575  */
576 void create_unique_job_name(JCR *jcr, char *base_name)
577 {
578    /* Job start mutex */
579    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
580    static time_t last_start_time = 0;
581    time_t now;
582    struct tm tm;
583    char dt[MAX_TIME_LENGTH];
584    char name[MAX_NAME_LENGTH];
585    char *p;
586
587    /* Guarantee unique start time -- maximum one per second, and
588     * thus unique Job Name 
589     */
590    P(mutex);                          /* lock creation of jobs */
591    now = time(NULL);
592    while (now == last_start_time) {
593       bmicrosleep(0, 500000);
594       now = time(NULL);
595    }
596    last_start_time = now;
597    V(mutex);                          /* allow creation of jobs */
598    jcr->start_time = now;
599    /* Form Unique JobName */
600    localtime_r(&now, &tm);
601    /* Use only characters that are permitted in Windows filenames */
602    strftime(dt, sizeof(dt), "%Y-%m-%d_%H.%M.%S", &tm); 
603    bstrncpy(name, base_name, sizeof(name));
604    name[sizeof(name)-22] = 0;          /* truncate if too long */
605    bsnprintf(jcr->Job, sizeof(jcr->Job), "%s.%s", name, dt); /* add date & time */
606    /* Convert spaces into underscores */
607    for (p=jcr->Job; *p; p++) {
608       if (*p == ' ') {
609          *p = '_';
610       }
611    }
612 }
613
614 /*
615  * Free the Job Control Record if no one is still using it.
616  *  Called from main free_jcr() routine in src/lib/jcr.c so
617  *  that we can do our Director specific cleanup of the jcr.
618  */
619 void dird_free_jcr(JCR *jcr)
620 {
621    Dmsg0(200, "Start dird free_jcr\n");
622
623    if (jcr->sd_auth_key) {
624       free(jcr->sd_auth_key);
625       jcr->sd_auth_key = NULL;
626    }
627    if (jcr->where) {
628       free(jcr->where);
629       jcr->where = NULL;
630    }
631    if (jcr->file_bsock) {
632       Dmsg0(200, "Close File bsock\n");
633       bnet_close(jcr->file_bsock);
634       jcr->file_bsock = NULL;
635    }
636    if (jcr->store_bsock) {
637       Dmsg0(200, "Close Store bsock\n");
638       bnet_close(jcr->store_bsock);
639       jcr->store_bsock = NULL;
640    }
641    if (jcr->fname) {  
642       Dmsg0(200, "Free JCR fname\n");
643       free_pool_memory(jcr->fname);
644       jcr->fname = NULL;
645    }
646    if (jcr->stime) {
647       Dmsg0(200, "Free JCR stime\n");
648       free_pool_memory(jcr->stime);
649       jcr->stime = NULL;
650    }
651    if (jcr->RestoreBootstrap) {
652       free(jcr->RestoreBootstrap);
653       jcr->RestoreBootstrap = NULL;
654    }
655    if (jcr->client_uname) {
656       free_pool_memory(jcr->client_uname);
657       jcr->client_uname = NULL;
658    }
659    pthread_cond_destroy(&jcr->term_wait);
660    Dmsg0(200, "End dird free_jcr\n");
661 }
662
663 /*
664  * Set some defaults in the JCR necessary to
665  * run. These items are pulled from the job
666  * definition as defaults, but can be overridden
667  * later either by the Run record in the Schedule resource,
668  * or by the Console program.
669  */
670 void set_jcr_defaults(JCR *jcr, JOB *job)
671 {
672    jcr->job = job;
673    jcr->JobType = job->JobType;
674    switch (jcr->JobType) {
675    case JT_ADMIN:
676    case JT_RESTORE:
677       jcr->JobLevel = L_NONE;
678       break;
679    default:
680       jcr->JobLevel = job->level;
681       break;
682    }
683    jcr->JobPriority = job->Priority;
684    jcr->store = job->storage;
685    jcr->client = job->client;
686    if (!jcr->client_name) {
687       jcr->client_name = get_pool_memory(PM_NAME);
688    }
689    pm_strcpy(&jcr->client_name, jcr->client->hdr.name);
690    jcr->pool = job->pool;
691    jcr->full_pool = job->full_pool;
692    jcr->inc_pool = job->inc_pool;
693    jcr->dif_pool = job->dif_pool;
694    jcr->catalog = job->client->catalog;
695    jcr->fileset = job->fileset;
696    jcr->messages = job->messages; 
697    if (jcr->RestoreBootstrap) {
698       free(jcr->RestoreBootstrap);
699       jcr->RestoreBootstrap = NULL;
700    }
701    /* This can be overridden by Console program */
702    if (job->RestoreBootstrap) {
703       jcr->RestoreBootstrap = bstrdup(job->RestoreBootstrap);
704    }
705    /* If no default level given, set one */
706    if (jcr->JobLevel == 0) {
707       switch (jcr->JobType) {
708       case JT_VERIFY:
709          jcr->JobLevel = L_VERIFY_CATALOG;
710          break;
711       case JT_BACKUP:
712          jcr->JobLevel = L_INCREMENTAL;
713          break;
714       case JT_RESTORE:
715       case JT_ADMIN:
716          jcr->JobLevel = L_NONE;
717          break;
718       default:
719          break;
720       }
721    }
722 }