]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/mac.c
- Move test for MaxStartDelay as suggested by Peter.
[bacula/bacula] / bacula / src / dird / mac.c
1 /*
2  *
3  *   Bacula Director -- mac.c -- responsible for doing
4  *     migration, archive, and copy jobs.
5  *
6  *     Kern Sibbald, September MMIV
7  *
8  *  Basic tasks done here:
9  *     Open DB and create records for this job.
10  *     Open Message Channel with Storage daemon to tell him a job will be starting.
11  *     Open connection with File daemon and pass him commands
12  *       to do the backup.
13  *     When the File daemon finishes the job, update the DB.
14  *
15  *   Version $Id$
16  */
17
18 /*
19    Copyright (C) 2004-2005 Kern Sibbald
20
21    This program is free software; you can redistribute it and/or
22    modify it under the terms of the GNU General Public License as
23    published by the Free Software Foundation; either version 2 of
24    the License, or (at your option) any later version.
25
26    This program is distributed in the hope that it will be useful,
27    but WITHOUT ANY WARRANTY; without even the implied warranty of
28    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29    General Public License for more details.
30
31    You should have received a copy of the GNU General Public
32    License along with this program; if not, write to the Free
33    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34    MA 02111-1307, USA.
35
36  */
37
38 #include "bacula.h"
39 #include "dird.h"
40 #include "ua.h"
41
42 /* 
43  * Called here before the job is run to do the job
44  *   specific setup.
45  */
46 bool do_mac_init(JCR *jcr)
47 {
48    FILESET_DBR fsr;
49    POOL_DBR pr;
50    JOB_DBR jr;
51    JobId_t input_jobid;
52    char *Name;
53
54    if (!get_or_create_fileset_record(jcr, &fsr)) {
55       return false;
56    }
57    bstrncpy(jcr->FSCreateTime, fsr.cCreateTime, sizeof(jcr->FSCreateTime));
58
59    /*
60     * Find JobId of last job that ran.
61     */
62    memcpy(&jr, &jcr->jr, sizeof(jr));
63    Name = jcr->job->hdr.name;
64    Dmsg1(100, "find last jobid for: %s\n", NPRT(Name));
65    if (!db_find_last_jobid(jcr, jcr->db, Name, &jr)) {
66       Jmsg(jcr, M_FATAL, 0, _(
67            "Unable to find JobId of previous Job for this client.\n"));
68       return false;
69    }
70    input_jobid = jr.JobId;
71    jcr->JobLevel = jr.JobLevel;
72    Dmsg1(100, "Last jobid=%d\n", input_jobid);
73
74    /*
75     * Get the Pool record -- first apply any level defined pools
76     */
77    switch (jcr->JobLevel) {
78    case L_FULL:
79       if (jcr->full_pool) {
80          jcr->pool = jcr->full_pool;
81       }
82       break;
83    case L_INCREMENTAL:
84       if (jcr->inc_pool) {
85          jcr->pool = jcr->inc_pool;
86       }
87       break;
88    case L_DIFFERENTIAL:
89       if (jcr->dif_pool) {
90          jcr->pool = jcr->dif_pool;
91       }
92       break;
93    }
94    memset(&pr, 0, sizeof(pr));
95    bstrncpy(pr.Name, jcr->pool->hdr.name, sizeof(pr.Name));
96
97    while (!db_get_pool_record(jcr, jcr->db, &pr)) { /* get by Name */
98       /* Try to create the pool */
99       if (create_pool(jcr, jcr->db, jcr->pool, POOL_OP_CREATE) < 0) {
100          Jmsg(jcr, M_FATAL, 0, _("Pool %s not in database. %s"), pr.Name,
101             db_strerror(jcr->db));
102          return false;
103       } else {
104          Jmsg(jcr, M_INFO, 0, _("Pool %s created in database.\n"), pr.Name);
105       }
106    }
107    jcr->PoolId = pr.PoolId;               /****FIXME**** this can go away */
108    jcr->jr.PoolId = pr.PoolId;
109    jcr->needs_sd = true;
110    return true;
111 }
112
113 /*
114  * Do a Migration, Archive, or Copy of a previous job
115  *
116  *  Returns:  false on failure
117  *            true  on success
118  */
119 bool do_mac(JCR *jcr)
120 {
121    int stat;
122    const char *Type;
123
124    switch(jcr->JobType) {
125    case JT_MIGRATION:
126       Type = "Migration";
127       break;
128    case JT_ARCHIVE:
129       Type = "Archive";
130       break;
131    case JT_COPY:
132       Type = "Copy";
133       break;
134    default:
135       Type = "Unknown";
136       break;
137    }
138
139
140    /* Print Job Start message */
141    Jmsg(jcr, M_INFO, 0, _("Start %s JobId %u, Job=%s\n"),
142         Type, jcr->JobId, jcr->Job);
143
144    set_jcr_job_status(jcr, JS_Running);
145    Dmsg2(100, "JobId=%d JobLevel=%c\n", jcr->jr.JobId, jcr->jr.JobLevel);
146    if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
147       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
148       return false;
149    }
150
151    /*
152     * Open a message channel connection with the Storage
153     * daemon. This is to let him know that our client
154     * will be contacting him for a backup  session.
155     *
156     */
157    Dmsg0(110, "Open connection with storage daemon\n");
158    set_jcr_job_status(jcr, JS_WaitSD);
159    /*
160     * Start conversation with Storage daemon
161     */
162    if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
163       return false;
164    }
165    /*
166     * Now start a job with the Storage daemon
167     */
168    if (!start_storage_daemon_job(jcr, jcr->storage, SD_APPEND)) {
169       return false;
170    }
171    /*
172     * Now start a Storage daemon message thread
173     */
174    if (!start_storage_daemon_message_thread(jcr)) {
175       return false;
176    }
177    Dmsg0(150, "Storage daemon connection OK\n");
178
179    /* Pickup Job termination data */
180    set_jcr_job_status(jcr, JS_Running);
181
182    /* Note, the SD stores in jcr->JobFiles/ReadBytes/JobBytes/Errors */
183    wait_for_storage_daemon_termination(jcr);
184
185    if (jcr->JobStatus != JS_Terminated) {
186       stat = jcr->JobStatus;
187    } else {
188       stat = jcr->SDJobStatus;
189    }
190    if (stat == JS_Terminated) {
191       mac_cleanup(jcr, stat);
192       return true;
193    }
194    return false;
195 }
196
197
198 /*
199  * Release resources allocated during backup.
200  */
201 void mac_cleanup(JCR *jcr, int TermCode)
202 {
203    char sdt[50], edt[50];
204    char ec1[30], ec2[30], ec3[30], ec4[30], ec5[30], compress[50];
205    char term_code[100], fd_term_msg[100], sd_term_msg[100];
206    const char *term_msg;
207    int msg_type;
208    MEDIA_DBR mr;
209    double kbps, compression;
210    utime_t RunTime;
211    const char *Type;
212
213    switch(jcr->JobType) {
214    case JT_MIGRATION:
215       Type = "Migration";
216       break;
217    case JT_ARCHIVE:
218       Type = "Archive";
219       break;
220    case JT_COPY:
221       Type = "Copy";
222       break;
223    default:
224       Type = "Unknown";
225       break;
226    }
227
228    Dmsg2(100, "Enter mac_cleanup %d %c\n", TermCode, TermCode);
229    dequeue_messages(jcr);             /* display any queued messages */
230    memset(&mr, 0, sizeof(mr));
231    set_jcr_job_status(jcr, TermCode);
232
233    update_job_end_record(jcr);        /* update database */
234
235    if (!db_get_job_record(jcr, jcr->db, &jcr->jr)) {
236       Jmsg(jcr, M_WARNING, 0, _("Error getting job record for stats: %s"),
237          db_strerror(jcr->db));
238       set_jcr_job_status(jcr, JS_ErrorTerminated);
239    }
240
241    bstrncpy(mr.VolumeName, jcr->VolumeName, sizeof(mr.VolumeName));
242    if (!db_get_media_record(jcr, jcr->db, &mr)) {
243       Jmsg(jcr, M_WARNING, 0, _("Error getting Media record for Volume \"%s\": ERR=%s"),
244          mr.VolumeName, db_strerror(jcr->db));
245       set_jcr_job_status(jcr, JS_ErrorTerminated);
246    }
247
248    /* Now update the bootstrap file if any */
249    if (jcr->JobStatus == JS_Terminated && jcr->jr.JobBytes &&
250        jcr->job->WriteBootstrap) {
251       FILE *fd;
252       BPIPE *bpipe = NULL;
253       int got_pipe = 0;
254       char *fname = jcr->job->WriteBootstrap;
255       VOL_PARAMS *VolParams = NULL;
256       int VolCount;
257
258       if (*fname == '|') {
259          fname++;
260          got_pipe = 1;
261          bpipe = open_bpipe(fname, 0, "w");
262          fd = bpipe ? bpipe->wfd : NULL;
263       } else {
264          /* ***FIXME*** handle BASE */
265          fd = fopen(fname, jcr->JobLevel==L_FULL?"w+":"a+");
266       }
267       if (fd) {
268          VolCount = db_get_job_volume_parameters(jcr, jcr->db, jcr->JobId,
269                     &VolParams);
270          if (VolCount == 0) {
271             Jmsg(jcr, M_ERROR, 0, _("Could not get Job Volume Parameters to "
272                  "update Bootstrap file. ERR=%s\n"), db_strerror(jcr->db));
273              if (jcr->SDJobFiles != 0) {
274                 set_jcr_job_status(jcr, JS_ErrorTerminated);
275              }
276
277          }
278          for (int i=0; i < VolCount; i++) {
279             /* Write the record */
280             fprintf(fd, "Volume=\"%s\"\n", VolParams[i].VolumeName);
281             fprintf(fd, "MediaType=\"%s\"\n", VolParams[i].MediaType);
282             fprintf(fd, "VolSessionId=%u\n", jcr->VolSessionId);
283             fprintf(fd, "VolSessionTime=%u\n", jcr->VolSessionTime);
284             fprintf(fd, "VolFile=%u-%u\n", VolParams[i].StartFile,
285                          VolParams[i].EndFile);
286             fprintf(fd, "VolBlock=%u-%u\n", VolParams[i].StartBlock,
287                          VolParams[i].EndBlock);
288             fprintf(fd, "FileIndex=%d-%d\n", VolParams[i].FirstIndex,
289                          VolParams[i].LastIndex);
290          }
291          if (VolParams) {
292             free(VolParams);
293          }
294          if (got_pipe) {
295             close_bpipe(bpipe);
296          } else {
297             fclose(fd);
298          }
299       } else {
300          berrno be;
301          Jmsg(jcr, M_ERROR, 0, _("Could not open WriteBootstrap file:\n"
302               "%s: ERR=%s\n"), fname, be.strerror());
303          set_jcr_job_status(jcr, JS_ErrorTerminated);
304       }
305    }
306
307    msg_type = M_INFO;                 /* by default INFO message */
308    switch (jcr->JobStatus) {
309       case JS_Terminated:
310          if (jcr->Errors || jcr->SDErrors) {
311             term_msg = _("Backup OK -- with warnings");
312          } else {
313             term_msg = _("Backup OK");
314          }
315          break;
316       case JS_FatalError:
317       case JS_ErrorTerminated:
318          term_msg = _("*** Backup Error ***");
319          msg_type = M_ERROR;          /* Generate error message */
320          if (jcr->store_bsock) {
321             bnet_sig(jcr->store_bsock, BNET_TERMINATE);
322             if (jcr->SD_msg_chan) {
323                pthread_cancel(jcr->SD_msg_chan);
324             }
325          }
326          break;
327       case JS_Canceled:
328          term_msg = _("Backup Canceled");
329          if (jcr->store_bsock) {
330             bnet_sig(jcr->store_bsock, BNET_TERMINATE);
331             if (jcr->SD_msg_chan) {
332                pthread_cancel(jcr->SD_msg_chan);
333             }
334          }
335          break;
336       default:
337          term_msg = term_code;
338          sprintf(term_code, _("Inappropriate term code: %c\n"), jcr->JobStatus);
339          break;
340    }
341    bstrftimes(sdt, sizeof(sdt), jcr->jr.StartTime);
342    bstrftimes(edt, sizeof(edt), jcr->jr.EndTime);
343    RunTime = jcr->jr.EndTime - jcr->jr.StartTime;
344    if (RunTime <= 0) {
345       kbps = 0;
346    } else {
347       kbps = (double)jcr->jr.JobBytes / (1000 * RunTime);
348    }
349    if (!db_get_job_volume_names(jcr, jcr->db, jcr->jr.JobId, &jcr->VolumeName)) {
350       /*
351        * Note, if the job has erred, most likely it did not write any
352        *  tape, so suppress this "error" message since in that case
353        *  it is normal.  Or look at it the other way, only for a
354        *  normal exit should we complain about this error.
355        */
356       if (jcr->JobStatus == JS_Terminated && jcr->jr.JobBytes) {
357          Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
358       }
359       jcr->VolumeName[0] = 0;         /* none */
360    }
361
362    if (jcr->ReadBytes == 0) {
363       bstrncpy(compress, "None", sizeof(compress));
364    } else {
365       compression = (double)100 - 100.0 * ((double)jcr->JobBytes / (double)jcr->ReadBytes);
366       if (compression < 0.5) {
367          bstrncpy(compress, "None", sizeof(compress));
368       } else {
369          bsnprintf(compress, sizeof(compress), "%.1f %%", (float)compression);
370       }
371    }
372    jobstatus_to_ascii(jcr->FDJobStatus, fd_term_msg, sizeof(fd_term_msg));
373    jobstatus_to_ascii(jcr->SDJobStatus, sd_term_msg, sizeof(sd_term_msg));
374
375 // bmicrosleep(15, 0);                /* for debugging SIGHUP */
376
377    Jmsg(jcr, msg_type, 0, _("Bacula " VERSION " (" LSMDATE "): %s\n"
378 "  JobId:                  %d\n"
379 "  Job:                    %s\n"
380 "  Backup Level:           %s%s\n"
381 "  Client:                 %s\n"
382 "  FileSet:                \"%s\" %s\n"
383 "  Pool:                   \"%s\"\n"
384 "  Start time:             %s\n"
385 "  End time:               %s\n"
386 "  FD Files Written:       %s\n"
387 "  SD Files Written:       %s\n"
388 "  FD Bytes Written:       %s\n"
389 "  SD Bytes Written:       %s\n"
390 "  Rate:                   %.1f KB/s\n"
391 "  Software Compression:   %s\n"
392 "  Volume name(s):         %s\n"
393 "  Volume Session Id:      %d\n"
394 "  Volume Session Time:    %d\n"
395 "  Last Volume Bytes:      %s\n"
396 "  Non-fatal FD errors:    %d\n"
397 "  SD Errors:              %d\n"
398 "  FD termination status:  %s\n"
399 "  SD termination status:  %s\n"
400 "  Termination:            %s\n\n"),
401         edt,
402         jcr->jr.JobId,
403         jcr->jr.Job,
404         level_to_str(jcr->JobLevel), jcr->since,
405         jcr->client->hdr.name,
406         jcr->fileset->hdr.name, jcr->FSCreateTime,
407         jcr->pool->hdr.name,
408         sdt,
409         edt,
410         edit_uint64_with_commas(jcr->jr.JobFiles, ec1),
411         edit_uint64_with_commas(jcr->SDJobFiles, ec4),
412         edit_uint64_with_commas(jcr->jr.JobBytes, ec2),
413         edit_uint64_with_commas(jcr->SDJobBytes, ec5),
414         (float)kbps,
415         compress,
416         jcr->VolumeName,
417         jcr->VolSessionId,
418         jcr->VolSessionTime,
419         edit_uint64_with_commas(mr.VolBytes, ec3),
420         jcr->Errors,
421         jcr->SDErrors,
422         fd_term_msg,
423         sd_term_msg,
424         term_msg);
425
426    Dmsg0(100, "Leave mac_cleanup()\n");
427 }