]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/job.c
3987beee4d4a3cee86e3fbc72b35e88c40cb9c8a
[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-2003 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 int acquire_resource_locks(JCR *jcr);
35 #ifdef USE_SEMAPHORE
36 static void backoff_resource_locks(JCR *jcr, int count);
37 static void release_resource_locks(JCR *jcr);
38 #endif
39
40 /* Exported subroutines */
41
42
43 /* Imported subroutines */
44 extern void term_scheduler();
45 extern void term_ua_server();
46 extern int do_backup(JCR *jcr);
47 extern int do_admin(JCR *jcr);
48 extern int do_restore(JCR *jcr);
49 extern int do_verify(JCR *jcr);
50
51 #ifdef USE_SEMAPHORE
52 static semlock_t job_lock;
53 static pthread_mutex_t mutex;
54 static pthread_cond_t  resource_wait;
55 static int waiting = 0;               /* count of waiting threads */
56 #else
57 #ifdef JOB_QUEUE  
58 jobq_t  job_queue;
59 #endif
60 #endif
61
62 void init_job_server(int max_workers)
63 {
64    int stat;
65 #ifdef USE_SEMAPHORE
66    if ((stat = sem_init(&job_lock, max_workers)) != 0) {
67       Emsg1(M_ABORT, 0, _("Could not init job lock: ERR=%s\n"), strerror(stat));
68    }
69    if ((stat = pthread_mutex_init(&mutex, NULL)) != 0) {
70       Emsg1(M_ABORT, 0, _("Could not init resource mutex: ERR=%s\n"), strerror(stat));
71    }
72    if ((stat = pthread_cond_init(&resource_wait, NULL)) != 0) {
73       Emsg1(M_ABORT, 0, _("Could not init resource wait: ERR=%s\n"), strerror(stat));
74    }
75
76 #else
77 #ifdef JOB_QUEUE
78    if ((stat = jobq_init(&job_queue, max_workers, job_thread)) != 0) {
79       Emsg1(M_ABORT, 0, _("Could not init job queue: ERR=%s\n"), strerror(stat));
80    }
81 #endif
82 #endif
83    return;
84 }
85
86 /*
87  * Run a job -- typically called by the scheduler, but may also
88  *              be called by the UA (Console program).
89  *
90  */
91 void run_job(JCR *jcr)
92 {
93    int stat, errstat;
94 #ifdef USE_SEMAPHORE
95    pthread_t tid;
96 #endif
97
98    sm_check(__FILE__, __LINE__, True);
99    init_msg(jcr, jcr->messages);
100    create_unique_job_name(jcr, jcr->job->hdr.name);
101    set_jcr_job_status(jcr, JS_Created);
102    jcr->jr.SchedTime = jcr->sched_time;
103    jcr->jr.StartTime = jcr->start_time;
104    jcr->jr.Type = jcr->JobType;
105    jcr->jr.Level = jcr->JobLevel;
106    jcr->jr.JobStatus = jcr->JobStatus;
107    bstrncpy(jcr->jr.Name, jcr->job->hdr.name, sizeof(jcr->jr.Name));
108    bstrncpy(jcr->jr.Job, jcr->Job, sizeof(jcr->jr.Job));
109
110    /* Initialize termination condition variable */
111    if ((errstat = pthread_cond_init(&jcr->term_wait, NULL)) != 0) {
112       Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
113       set_jcr_job_status(jcr, JS_ErrorTerminated);
114       free_jcr(jcr);
115       return;
116    }
117
118    /*
119     * Open database
120     */
121    Dmsg0(50, "Open database\n");
122    jcr->db=db_init_database(jcr, jcr->catalog->db_name, jcr->catalog->db_user,
123                             jcr->catalog->db_password, jcr->catalog->db_address,
124                             jcr->catalog->db_port, jcr->catalog->db_socket);
125    if (!jcr->db || !db_open_database(jcr, jcr->db)) {
126       Jmsg(jcr, M_FATAL, 0, _("Could not open database \"%s\".\n"),
127                  jcr->catalog->db_name);
128       if (jcr->db) {
129          Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
130       }
131       set_jcr_job_status(jcr, JS_ErrorTerminated);
132       free_jcr(jcr);
133       return;
134    }
135    Dmsg0(50, "DB opened\n");
136
137    /*
138     * Create Job record  
139     */
140    jcr->jr.JobStatus = jcr->JobStatus;
141    if (!db_create_job_record(jcr, jcr->db, &jcr->jr)) {
142       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
143       set_jcr_job_status(jcr, JS_ErrorTerminated);
144       free_jcr(jcr);
145       return;
146    }
147    jcr->JobId = jcr->jr.JobId;
148    ASSERT(jcr->jr.JobId > 0);
149
150    Dmsg4(50, "Created job record JobId=%d Name=%s Type=%c Level=%c\n", 
151        jcr->JobId, jcr->Job, jcr->jr.Type, jcr->jr.Level);
152    Dmsg0(200, "Add jrc to work queue\n");
153
154 #ifdef USE_SEMAPHORE
155   if ((stat = pthread_create(&tid, NULL, job_thread, (void *)jcr)) != 0) {
156       Emsg1(M_ABORT, 0, _("Unable to create job thread: ERR=%s\n"), strerror(stat));
157    }
158 #else
159 #ifdef JOB_QUEUE
160    /* Queue the job to be run */
161    if ((stat = jobq_add(&job_queue, jcr)) != 0) {
162       Emsg1(M_ABORT, 0, _("Could not add job queue: ERR=%s\n"), strerror(stat));
163    }
164 #endif
165 #endif
166    Dmsg0(100, "Done run_job()\n");
167 }
168
169 /* 
170  * This is the engine called by workq_add() when we were pulled                
171  *  from the work queue.
172  *  At this point, we are running in our own thread 
173  */
174 static void *job_thread(void *arg)
175 {
176    JCR *jcr = (JCR *)arg;
177
178    pthread_detach(pthread_self());
179    sm_check(__FILE__, __LINE__, True);
180
181    for ( ;; ) {
182       if (!acquire_resource_locks(jcr)) {
183          set_jcr_job_status(jcr, JS_Canceled);
184       }
185
186       Dmsg0(200, "=====Start Job=========\n");
187       jcr->start_time = time(NULL);      /* set the real start time */
188       set_jcr_job_status(jcr, JS_Running);
189
190       if (job_canceled(jcr)) {
191          update_job_end_record(jcr);
192       } else if (jcr->job->MaxStartDelay != 0 && jcr->job->MaxStartDelay <
193           (utime_t)(jcr->start_time - jcr->sched_time)) {
194          Jmsg(jcr, M_FATAL, 0, _("Job canceled because max start delay time exceeded.\n"));
195          set_jcr_job_status(jcr, JS_Canceled);
196          update_job_end_record(jcr);
197       } else {
198
199          /* Run Job */
200          if (jcr->job->RunBeforeJob) {
201             POOLMEM *before = get_pool_memory(PM_FNAME);
202             int status;
203             BPIPE *bpipe;
204             char line[MAXSTRING];
205             
206             before = edit_job_codes(jcr, before, jcr->job->RunBeforeJob, "");
207             bpipe = open_bpipe(before, 0, "r");
208             free_pool_memory(before);
209             while (fgets(line, sizeof(line), bpipe->rfd)) {
210                Jmsg(jcr, M_INFO, 0, _("RunBefore: %s"), line);
211             }
212             status = close_bpipe(bpipe);
213             if (status != 0) {
214                Jmsg(jcr, M_FATAL, 0, _("RunBeforeJob returned non-zero status=%d\n"),
215                   status);
216                set_jcr_job_status(jcr, JS_FatalError);
217                update_job_end_record(jcr);
218                goto bail_out;
219             }
220          }
221          switch (jcr->JobType) {
222          case JT_BACKUP:
223             do_backup(jcr);
224             if (jcr->JobStatus == JS_Terminated) {
225                do_autoprune(jcr);
226             }
227             break;
228          case JT_VERIFY:
229             do_verify(jcr);
230             if (jcr->JobStatus == JS_Terminated) {
231                do_autoprune(jcr);
232             }
233             break;
234          case JT_RESTORE:
235             do_restore(jcr);
236             if (jcr->JobStatus == JS_Terminated) {
237                do_autoprune(jcr);
238             }
239             break;
240          case JT_ADMIN:
241             do_admin(jcr);
242             if (jcr->JobStatus == JS_Terminated) {
243                do_autoprune(jcr);
244             }
245             break;
246          default:
247             Pmsg1(0, "Unimplemented job type: %d\n", jcr->JobType);
248             break;
249          }
250          if (jcr->job->RunAfterJob) {
251             POOLMEM *after = get_pool_memory(PM_FNAME);
252             int status;
253             BPIPE *bpipe;
254             char line[MAXSTRING];
255             
256             after = edit_job_codes(jcr, after, jcr->job->RunAfterJob, "");
257             bpipe = open_bpipe(after, 0, "r");
258             free_pool_memory(after);
259             while (fgets(line, sizeof(line), bpipe->rfd)) {
260                Jmsg(jcr, M_INFO, 0, _("RunAfter: %s"), line);
261             }
262             status = close_bpipe(bpipe);
263             if (status != 0) {
264                Jmsg(jcr, M_FATAL, 0, _("RunAfterJob returned non-zero status=%d\n"),
265                   status);
266                set_jcr_job_status(jcr, JS_FatalError);
267                update_job_end_record(jcr);
268             }
269          }
270       }
271 bail_out:
272 #ifndef JOB_QUEUE
273       release_resource_locks(jcr);
274       if (jcr->job->RescheduleOnError && 
275           jcr->JobStatus != JS_Terminated &&
276           jcr->JobStatus != JS_Canceled && 
277           jcr->job->RescheduleTimes > 0 && 
278           jcr->reschedule_count < jcr->job->RescheduleTimes) {
279
280           /*
281            * Reschedule this job by cleaning it up, but
282            *  reuse the same JobId if possible.
283            */
284          jcr->reschedule_count++;
285          jcr->sched_time = time(NULL) + jcr->job->RescheduleInterval;
286          Dmsg2(100, "Rescheduled Job %s to re-run in %d seconds.\n", jcr->Job,
287             (int)jcr->job->RescheduleInterval);
288          jcr->JobStatus = JS_Created; /* force new status */
289          dird_free_jcr(jcr);          /* partial cleanup old stuff */
290          if (jcr->JobBytes == 0) {
291             continue;                    /* reschedule the job */
292          }
293          /* 
294           * Something was actually backed up, so we cannot reuse
295           *   the old JobId or there will be database record
296           *   conflicts.  We now create a new job, copying the
297           *   appropriate fields.
298           */
299          JCR *njcr = new_jcr(sizeof(JCR), dird_free_jcr);
300          set_jcr_defaults(njcr, jcr->job);
301          njcr->reschedule_count = jcr->reschedule_count;
302          njcr->JobLevel = jcr->JobLevel;
303          njcr->JobStatus = jcr->JobStatus;
304          njcr->pool = jcr->pool;
305          njcr->store = jcr->store;
306          njcr->messages = jcr->messages;
307          run_job(njcr);
308       }
309 #endif
310       break;
311    }
312
313 #ifndef JOB_QUEUE
314    if (jcr->db) {
315       Dmsg0(200, "Close DB\n");
316       db_close_database(jcr, jcr->db);
317       jcr->db = NULL;
318    }
319    free_jcr(jcr);
320 #endif
321    Dmsg0(50, "======== End Job ==========\n");
322    sm_check(__FILE__, __LINE__, True);
323    return NULL;
324 }
325
326 /*
327  * Acquire the resources needed. These locks limit the
328  *  number of jobs by each resource. We have limits on
329  *  Jobs, Clients, Storage, and total jobs.
330  */
331 static int acquire_resource_locks(JCR *jcr)
332 {
333 #ifndef JOB_QUEUE
334    time_t now = time(NULL);
335    time_t wtime = jcr->sched_time - now;
336
337    /* Wait until scheduled time arrives */
338    if (wtime > 0 && verbose) {
339       Jmsg(jcr, M_INFO, 0, _("Job %s waiting %d seconds for scheduled start time.\n"), 
340          jcr->Job, wtime);
341       set_jcr_job_status(jcr, JS_WaitStartTime);
342    }
343    /* Check every 30 seconds if canceled */ 
344    while (wtime > 0) {
345       Dmsg2(100, "Waiting on sched time, jobid=%d secs=%d\n", jcr->JobId, wtime);
346       if (wtime > 30) {
347          wtime = 30;
348       }
349       bmicrosleep(wtime, 0);
350       if (job_canceled(jcr)) {
351          return 0;
352       }
353       wtime = jcr->sched_time - time(NULL);
354    }
355 #endif
356
357
358 #ifdef USE_SEMAPHORE
359    int stat;
360
361    /* Initialize semaphores */
362    if (jcr->store->sem.valid != SEMLOCK_VALID) {
363       if ((stat = sem_init(&jcr->store->sem, jcr->store->MaxConcurrentJobs)) != 0) {
364          Emsg1(M_ABORT, 0, _("Could not init Storage semaphore: ERR=%s\n"), strerror(stat));
365       }
366    }
367    if (jcr->client->sem.valid != SEMLOCK_VALID) {
368       if ((stat = sem_init(&jcr->client->sem, jcr->client->MaxConcurrentJobs)) != 0) {
369          Emsg1(M_ABORT, 0, _("Could not init Client semaphore: ERR=%s\n"), strerror(stat));
370       }
371    }
372    if (jcr->job->sem.valid != SEMLOCK_VALID) {
373       if ((stat = sem_init(&jcr->job->sem, jcr->job->MaxConcurrentJobs)) != 0) {
374          Emsg1(M_ABORT, 0, _("Could not init Job semaphore: ERR=%s\n"), strerror(stat));
375       }
376    }
377
378    for ( ;; ) {
379       /* Acquire semaphore */
380       set_jcr_job_status(jcr, JS_WaitJobRes);
381       if ((stat = sem_lock(&jcr->job->sem)) != 0) {
382          Emsg1(M_ABORT, 0, _("Could not acquire Job max jobs lock: ERR=%s\n"), strerror(stat));
383       }
384       set_jcr_job_status(jcr, JS_WaitClientRes);
385       if ((stat = sem_trylock(&jcr->client->sem)) != 0) {
386          if (stat == EBUSY) {
387             backoff_resource_locks(jcr, 1);
388             goto wait;
389          } else {
390             Emsg1(M_ABORT, 0, _("Could not acquire Client max jobs lock: ERR=%s\n"), strerror(stat));
391          }
392       }
393       set_jcr_job_status(jcr, JS_WaitStoreRes);
394       if ((stat = sem_trylock(&jcr->store->sem)) != 0) {
395          if (stat == EBUSY) {
396             backoff_resource_locks(jcr, 2);
397             goto wait;
398          } else {
399             Emsg1(M_ABORT, 0, _("Could not acquire Storage max jobs lock: ERR=%s\n"), strerror(stat));
400          }
401       }
402       set_jcr_job_status(jcr, JS_WaitMaxJobs);
403       if ((stat = sem_trylock(&job_lock)) != 0) {
404          if (stat == EBUSY) {
405             backoff_resource_locks(jcr, 3);
406             goto wait;
407          } else {
408             Emsg1(M_ABORT, 0, _("Could not acquire max jobs lock: ERR=%s\n"), strerror(stat));
409          }
410       }
411       break;
412
413 wait:
414       if (job_canceled(jcr)) {
415          return 0;
416       }
417       P(mutex);
418       /*
419        * Wait for a resource to be released either by backoff or
420        *  by a job terminating.
421        */
422       waiting++;
423       pthread_cond_wait(&resource_wait, &mutex);
424       waiting--;
425       V(mutex);
426       /* Try again */
427    }
428    jcr->acquired_resource_locks = true;
429 #endif
430    return 1;
431 }
432
433 #ifdef USE_SEMAPHORE
434 /*
435  * We could not get all the resource locks because 
436  *  too many jobs are running, so release any locks
437  *  we did acquire, giving others a chance to use them
438  *  while we wait.
439  */
440 static void backoff_resource_locks(JCR *jcr, int count)
441 {
442    P(mutex);
443    switch (count) {
444    case 3:
445       sem_unlock(&jcr->store->sem);
446       /* Fall through wanted */
447    case 2:
448       sem_unlock(&jcr->client->sem);
449       /* Fall through wanted */
450    case 1:
451       sem_unlock(&jcr->job->sem);
452       break;
453    }
454    /*
455     * Since we released a lock, if there are any threads
456     *  waiting, wake them up so that they can try again.
457     */
458    if (waiting > 0) {
459       pthread_cond_broadcast(&resource_wait);
460    }
461    V(mutex);
462 }
463 #endif
464
465 /*
466  * This is called at the end of the job to release
467  *   any resource limits on the number of jobs. If
468  *   there are any other jobs waiting, we wake them
469  *   up so that they can try again.
470  */
471 #ifdef USE_SEMAPHORE
472 static void release_resource_locks(JCR *jcr)
473 {
474    if (!jcr->acquired_resource_locks) {
475       return;                         /* Job canceled, no locks acquired */
476    }
477    P(mutex);
478    sem_unlock(&jcr->store->sem);
479    sem_unlock(&jcr->client->sem);
480    sem_unlock(&jcr->job->sem);
481    sem_unlock(&job_lock);
482    if (waiting > 0) {
483       pthread_cond_broadcast(&resource_wait);
484    }
485    jcr->acquired_resource_locks = false;
486    V(mutex);
487 }
488 #endif
489
490 /*
491  * Get or create a Client record for this Job
492  */
493 int get_or_create_client_record(JCR *jcr)
494 {
495    CLIENT_DBR cr;
496
497    memset(&cr, 0, sizeof(cr));
498    bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
499    cr.AutoPrune = jcr->client->AutoPrune;
500    cr.FileRetention = jcr->client->FileRetention;
501    cr.JobRetention = jcr->client->JobRetention;
502    if (!jcr->client_name) {
503       jcr->client_name = get_pool_memory(PM_NAME);
504    }
505    pm_strcpy(&jcr->client_name, jcr->client->hdr.name);
506    if (!db_create_client_record(jcr, jcr->db, &cr)) {
507       Jmsg(jcr, M_FATAL, 0, _("Could not create Client record. ERR=%s\n"), 
508          db_strerror(jcr->db));
509       return 0;
510    }
511    jcr->jr.ClientId = cr.ClientId;
512    if (cr.Uname[0]) {
513       if (!jcr->client_uname) {
514          jcr->client_uname = get_pool_memory(PM_NAME);
515       }
516       pm_strcpy(&jcr->client_uname, cr.Uname);
517    }
518    Dmsg2(100, "Created Client %s record %d\n", jcr->client->hdr.name, 
519       jcr->jr.ClientId);
520    return 1;
521 }
522
523
524 /*
525  * Write status and such in DB
526  */
527 void update_job_end_record(JCR *jcr)
528 {
529    if (jcr->jr.EndTime == 0) {
530       jcr->jr.EndTime = time(NULL);
531    }
532    jcr->end_time = jcr->jr.EndTime;
533    jcr->jr.JobId = jcr->JobId;
534    jcr->jr.JobStatus = jcr->JobStatus;
535    jcr->jr.JobFiles = jcr->JobFiles;
536    jcr->jr.JobBytes = jcr->JobBytes;
537    jcr->jr.VolSessionId = jcr->VolSessionId;
538    jcr->jr.VolSessionTime = jcr->VolSessionTime;
539    if (!db_update_job_end_record(jcr, jcr->db, &jcr->jr)) {
540       Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"), 
541          db_strerror(jcr->db));
542    }
543 }
544
545 /*
546  * Takes base_name and appends (unique) current
547  *   date and time to form unique job name.
548  *
549  *  Returns: unique job name in jcr->Job
550  *    date/time in jcr->start_time
551  */
552 void create_unique_job_name(JCR *jcr, char *base_name)
553 {
554    /* Job start mutex */
555    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
556    static time_t last_start_time = 0;
557    time_t now;
558    struct tm tm;
559    char dt[MAX_TIME_LENGTH];
560    char name[MAX_NAME_LENGTH];
561    char *p;
562
563    /* Guarantee unique start time -- maximum one per second, and
564     * thus unique Job Name 
565     */
566    P(mutex);                          /* lock creation of jobs */
567    now = time(NULL);
568    while (now == last_start_time) {
569       bmicrosleep(0, 500000);
570       now = time(NULL);
571    }
572    last_start_time = now;
573    V(mutex);                          /* allow creation of jobs */
574    jcr->start_time = now;
575    /* Form Unique JobName */
576    localtime_r(&now, &tm);
577    /* Use only characters that are permitted in Windows filenames */
578    strftime(dt, sizeof(dt), "%Y-%m-%d_%H.%M.%S", &tm); 
579    bstrncpy(name, base_name, sizeof(name));
580    name[sizeof(name)-22] = 0;          /* truncate if too long */
581    bsnprintf(jcr->Job, sizeof(jcr->Job), "%s.%s", name, dt); /* add date & time */
582    /* Convert spaces into underscores */
583    for (p=jcr->Job; *p; p++) {
584       if (*p == ' ') {
585          *p = '_';
586       }
587    }
588 }
589
590 /*
591  * Free the Job Control Record if no one is still using it.
592  *  Called from main free_jcr() routine in src/lib/jcr.c so
593  *  that we can do our Director specific cleanup of the jcr.
594  */
595 void dird_free_jcr(JCR *jcr)
596 {
597    Dmsg0(200, "Start dird free_jcr\n");
598
599    if (jcr->sd_auth_key) {
600       free(jcr->sd_auth_key);
601       jcr->sd_auth_key = NULL;
602    }
603    if (jcr->where) {
604       free(jcr->where);
605       jcr->where = NULL;
606    }
607    if (jcr->file_bsock) {
608       Dmsg0(200, "Close File bsock\n");
609       bnet_close(jcr->file_bsock);
610       jcr->file_bsock = NULL;
611    }
612    if (jcr->store_bsock) {
613       Dmsg0(200, "Close Store bsock\n");
614       bnet_close(jcr->store_bsock);
615       jcr->store_bsock = NULL;
616    }
617    if (jcr->fname) {  
618       Dmsg0(200, "Free JCR fname\n");
619       free_pool_memory(jcr->fname);
620       jcr->fname = NULL;
621    }
622    if (jcr->stime) {
623       Dmsg0(200, "Free JCR stime\n");
624       free_pool_memory(jcr->stime);
625       jcr->stime = NULL;
626    }
627    if (jcr->RestoreBootstrap) {
628       free(jcr->RestoreBootstrap);
629       jcr->RestoreBootstrap = NULL;
630    }
631    if (jcr->client_uname) {
632       free_pool_memory(jcr->client_uname);
633       jcr->client_uname = NULL;
634    }
635    Dmsg0(200, "End dird free_jcr\n");
636 }
637
638 /*
639  * Set some defaults in the JCR necessary to
640  * run. These items are pulled from the job
641  * definition as defaults, but can be overridden
642  * later either by the Run record in the Schedule resource,
643  * or by the Console program.
644  */
645 void set_jcr_defaults(JCR *jcr, JOB *job)
646 {
647    jcr->job = job;
648    jcr->JobType = job->JobType;
649    jcr->JobLevel = job->level;
650    jcr->JobPriority = job->Priority;
651    jcr->store = job->storage;
652    jcr->client = job->client;
653    if (!jcr->client_name) {
654       jcr->client_name = get_pool_memory(PM_NAME);
655    }
656    pm_strcpy(&jcr->client_name, jcr->client->hdr.name);
657    jcr->pool = job->pool;
658    jcr->catalog = job->client->catalog;
659    jcr->fileset = job->fileset;
660    jcr->messages = job->messages; 
661    if (jcr->RestoreBootstrap) {
662       free(jcr->RestoreBootstrap);
663    }
664    /* This can be overridden by Console program */
665    if (job->RestoreBootstrap) {
666       jcr->RestoreBootstrap = bstrdup(job->RestoreBootstrap);
667    }
668    /* If no default level given, set one */
669    if (jcr->JobLevel == 0) {
670       switch (jcr->JobType) {
671       case JT_VERIFY:
672          jcr->JobLevel = L_VERIFY_CATALOG;
673          break;
674       case JT_BACKUP:
675          jcr->JobLevel = L_INCREMENTAL;
676          break;
677       case JT_RESTORE:
678       case JT_ADMIN:
679          jcr->JobLevel = L_FULL;
680          break;
681       default:
682          break;
683       }
684    }
685 }