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