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