]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/verify.c
Retention period updates
[bacula/bacula] / bacula / src / dird / verify.c
1 /*
2  *
3  *   Bacula Director -- verify.c -- responsible for running file verification
4  *
5  *     Kern Sibbald, October MM
6  *
7  *    This routine is run as a separate thread.  There may be more
8  *    work to be done to make it totally reentrant!!!!
9  * 
10  * Current implementation is Catalog verification only (i.e. no
11  *  verification versus tape).
12  *
13  *  Basic tasks done here:
14  *     Open DB
15  *     Open connection with File daemon and pass him commands
16  *       to do the verify.
17  *     When the File daemon sends the attributes, compare them to
18  *       what is in the DB.
19  *
20  */
21
22 /*
23    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
24
25    This program is free software; you can redistribute it and/or
26    modify it under the terms of the GNU General Public License as
27    published by the Free Software Foundation; either version 2 of
28    the License, or (at your option) any later version.
29
30    This program is distributed in the hope that it will be useful,
31    but WITHOUT ANY WARRANTY; without even the implied warranty of
32    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33    General Public License for more details.
34
35    You should have received a copy of the GNU General Public
36    License along with this program; if not, write to the Free
37    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
38    MA 02111-1307, USA.
39
40  */
41
42 #include "bacula.h"
43 #include "dird.h"
44
45 /* Imported Global Variables */
46 extern int debug_level;
47
48 /* Commands sent to File daemon */
49 static char verifycmd[]   = "verify";
50 static char levelcmd[]    = "level = %s%s\n";
51
52 /* Responses received from File daemon */
53 static char OKverify[]   = "2000 OK verify\n";
54 static char OKlevel[]    = "2000 OK level\n";
55
56 /* Forward referenced functions */
57 static void verify_cleanup(JCR *jcr);
58 static void prt_fname(JCR *jcr);
59 static int missing_handler(void *ctx, int num_fields, char **row);
60
61 /* 
62  * Do a verification of the specified files
63  *    
64  *  Returns:  0 on failure
65  *            1 on success
66  */
67 int do_verify(JCR *jcr) 
68 {
69    char *level;
70    BSOCK   *fd;
71    JOB_DBR jr;
72    int last_full_id;
73    CLIENT_DBR cr;
74
75    memset(&cr, 0, sizeof(cr));
76    cr.AutoPrune = jcr->client->AutoPrune;
77    cr.FileRetention = jcr->client->FileRetention;
78    cr.JobRetention = jcr->client->JobRetention;
79    strcpy(cr.Name, jcr->client->hdr.name);
80    if (jcr->client_name) {
81       free(jcr->client_name);
82    }
83    jcr->client_name = bstrdup(jcr->client->hdr.name);
84    if (!db_create_client_record(jcr->db, &cr)) {
85       Jmsg(jcr, M_ERROR, 0, _("Could not create Client record. %s"), 
86          db_strerror(jcr->db));
87       jcr->JobStatus = JS_ErrorTerminated;
88       verify_cleanup(jcr);                    
89       return 0;
90    }
91    jcr->jr.ClientId = cr.ClientId;
92
93    Dmsg1(9, "bdird: created client %s record\n", jcr->client->hdr.name);
94
95    /* If we are doing a verify from the catalog,
96     * we must look up the time and date of the
97     * last full verify.
98     */
99    if (jcr->level == L_VERIFY_CATALOG) {
100       memcpy(&jr, &(jcr->jr), sizeof(jr));
101       if (!db_find_last_full_verify(jcr->db, &jr)) {
102          Jmsg(jcr, M_FATAL, 0, _("Unable to find last full verify. %s"),
103             db_strerror(jcr->db));
104          jcr->JobStatus = JS_ErrorTerminated;
105          verify_cleanup(jcr);
106          return 0;
107       }
108       last_full_id = jr.JobId;
109       Dmsg1(20, "Last full id=%d\n", last_full_id);
110    }
111
112    jcr->jr.JobId = jcr->JobId;
113    jcr->jr.StartTime = jcr->start_time;
114    jcr->jr.Level = jcr->level;
115    if (!db_update_job_start_record(jcr->db, &jcr->jr)) {
116       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
117       jcr->JobStatus = JS_ErrorTerminated;
118       verify_cleanup(jcr);
119       return 0;
120    }
121
122    if (!jcr->fname) {
123       jcr->fname = (char *) get_pool_memory(PM_FNAME);
124    }
125
126    jcr->jr.JobId = last_full_id;      /* save last full id */
127
128    /* Print Job Start message */
129    Jmsg(jcr, M_INFO, 0, _("Start Verify JobId %d Job=%s\n"),
130       jcr->JobId, jcr->Job);
131
132    if (jcr->level == L_VERIFY_CATALOG) {
133       memset(&jr, 0, sizeof(jr));
134       jr.JobId = last_full_id;
135       if (!db_get_job_record(jcr->db, &jr)) {
136          Jmsg(jcr, M_ERROR, 0, _("Could not get job record. %s"), db_strerror(jcr->db));
137          jcr->JobStatus = JS_ErrorTerminated;
138          verify_cleanup(jcr);
139          return 0;
140       }
141       Jmsg(jcr, M_INFO, 0, _("Verifying against Init JobId %d run %s\n"),
142          last_full_id, jr.cStartTime); 
143    }
144
145    /*
146     * OK, now connect to the File daemon
147     *  and ask him for the files.
148     */
149    jcr->sd_auth_key = bstrdup("dummy");    /* dummy Storage daemon key */
150    if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
151       jcr->JobStatus = JS_ErrorTerminated;
152       verify_cleanup(jcr);
153       return 0;
154    }
155
156    fd = jcr->file_bsock;
157
158    Dmsg0(30, ">filed: Send include list\n");
159    if (!send_include_list(jcr)) {
160       jcr->JobStatus = JS_ErrorTerminated;
161       verify_cleanup(jcr);
162       return 0;
163    }
164
165    Dmsg0(30, ">filed: Send exclude list\n");
166    if (!send_exclude_list(jcr)) {
167       jcr->JobStatus = JS_ErrorTerminated;
168       verify_cleanup(jcr);
169       return 0;
170    }
171
172    /* 
173     * Send Level command to File daemon
174     *
175     */
176    switch (jcr->level) {
177       case L_VERIFY_INIT:
178          level = "init";
179          break;
180       case L_VERIFY_CATALOG:
181          level = "catalog";
182          break;
183       case L_VERIFY_VOLUME:
184          level = "volume";
185          break;
186       case L_VERIFY_DATA:
187          level = "data";
188          break;
189       default:
190          Emsg1(M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->level);
191          jcr->JobStatus = JS_ErrorTerminated;
192          verify_cleanup(jcr);
193          return 0;
194    }
195    Dmsg1(20, ">filed: %s", fd->msg);
196    bnet_fsend(fd, levelcmd, level, " ");
197    if (!response(fd, OKlevel, "Level")) {
198       jcr->JobStatus = JS_ErrorTerminated;
199       verify_cleanup(jcr);
200       return 0;
201    }
202
203    /* 
204     * Send verify command to File daemon
205     */
206    bnet_fsend(fd, verifycmd);
207    if (!response(fd, OKverify, "Verify")) {
208       jcr->JobStatus = JS_ErrorTerminated;
209       verify_cleanup(jcr);
210       return 0;
211    }
212
213    /*
214     * Now get data back from File daemon and
215     *  compare it to the catalog or store it in the
216     *  catalog depending on the run type.
217     */
218    /* Compare to catalog */
219    if (jcr->level == L_VERIFY_CATALOG) {
220       Dmsg0(10, "Verify level=catalog\n");
221       get_attributes_and_compare_to_catalog(jcr, last_full_id);
222
223    /* Build catalog */
224    } else if (jcr->level == L_VERIFY_INIT) {
225       Dmsg0(10, "Verify level=init\n");
226       get_attributes_and_put_in_catalog(jcr);
227
228    } else {
229       Emsg1(M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->level);
230       jcr->JobStatus = JS_ErrorTerminated;
231       verify_cleanup(jcr);
232       return 0;
233    }
234
235    verify_cleanup(jcr);
236    return 1;
237 }
238
239 /*
240  * Release resources allocated during backup.
241  *
242  */
243 static void verify_cleanup(JCR *jcr)
244 {
245    char sdt[50], edt[50];
246    char ec1[30];
247    char term_code[100];
248    char *term_msg;
249    int msg_type;
250    int TermCode;
251    int last_full_id;
252
253    Dmsg0(100, "Enter verify_cleanup()\n");
254
255    last_full_id = jcr->jr.JobId;
256
257
258    if (jcr->jr.EndTime == 0) {
259       jcr->jr.EndTime = time(NULL);
260    }
261    jcr->end_time = jcr->jr.EndTime;
262    jcr->jr.JobId = jcr->JobId;
263    jcr->jr.JobStatus = jcr->JobStatus;
264    TermCode = jcr->JobStatus;
265
266    if (!db_update_job_end_record(jcr->db, &jcr->jr)) {
267       Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"), 
268          db_strerror(jcr->db));
269    }
270
271    msg_type = M_INFO;                 /* by default INFO message */
272    switch (TermCode) {
273       case JS_Terminated:
274          term_msg = _("Verify OK");
275          break;
276       case JS_Errored:
277          term_msg = _("*** Verify Error ***"); 
278          msg_type = M_ERROR;          /* Generate error message */
279          break;
280       case JS_Cancelled:
281          term_msg = _("Verify Cancelled");
282          break;
283       case JS_Differences:
284          term_msg = _("Verify Differences");
285          break;
286       default:
287          term_msg = term_code;
288          sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
289          break;
290    }
291    bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
292    bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
293
294    Jmsg(jcr, msg_type, 0, _("%s\n\
295 JobId:                  %d\n\
296 Job:                    %s\n\
297 FileSet:                %s\n\
298 Verify Level:           %s\n\
299 Client:                 %s\n\
300 Start time:             %s\n\
301 End time:               %s\n\
302 Files Examined:         %s\n\
303 Termination:            %s\n"),
304         edt,
305         jcr->jr.JobId,
306         jcr->jr.Job,
307         jcr->fileset->hdr.name,
308         level_to_str(jcr->level),
309         jcr->client->hdr.name,
310         sdt,
311         edt,
312         edit_uint64_with_commas(jcr->jr.JobFiles, ec1),
313         term_msg);
314
315    Dmsg0(100, "Leave verify_cleanup()\n");
316    if (jcr->fname) {
317       free_memory(jcr->fname);
318       jcr->fname = NULL;
319    }
320 }
321
322 /*
323  * This routine is called only during a Verify
324  */
325 int get_attributes_and_compare_to_catalog(JCR *jcr, int last_full_id)
326 {
327    BSOCK   *fd;
328    int n, len;
329    FILE_DBR fdbr;
330    struct stat statf;                 /* file stat */
331    struct stat statc;                 /* catalog stat */
332    int stat = JS_Terminated;
333    char buf[MAXSTRING];
334    char *fname = (char *)get_pool_memory(PM_MESSAGE);
335    int do_MD5 = FALSE;
336
337    memset(&fdbr, 0, sizeof(FILE_DBR));
338    fd = jcr->file_bsock;
339    fdbr.JobId = last_full_id;
340    
341    Dmsg0(20, "bdird: waiting to receive file attributes\n");
342    /*
343     * Get Attributes and MD5 Signature from File daemon
344     */
345    while ((n=bget_msg(fd, 0)) > 0) {
346       long file_index, attr_file_index;
347       int stream;
348       char *attr, *p;
349       char Opts_MD5[MAXSTRING];        /* Verify Opts or MD5 signature */
350
351       fname = (char *)check_pool_memory_size(fname, fd->msglen);
352       jcr->fname = (char *)check_pool_memory_size(jcr->fname, fd->msglen);
353       Dmsg1(50, "Atts+MD5=%s\n", fd->msg);
354       if ((len = sscanf(fd->msg, "%ld %d %100s %s", &file_index, &stream, 
355             Opts_MD5, fname)) != 4) {
356          Jmsg3(jcr, M_FATAL, 0, _("bird<filed: bad attributes, expected 4 fields got %d\n\
357  mslen=%d msg=%s\n"), len, fd->msglen, fd->msg);
358          jcr->JobStatus = JS_ErrorTerminated;
359          return 0;
360       }
361       /*
362        * Got attributes stream, decode it
363        */
364       if (stream == STREAM_UNIX_ATTRIBUTES) {
365          attr_file_index = file_index;    /* remember attribute file_index */
366          len = strlen(fd->msg);
367          attr = &fd->msg[len+1];
368          decode_stat(attr, &statf);  /* decode file stat packet */
369          do_MD5 = FALSE;
370          jcr->fn_printed = FALSE;
371          strip_trailing_junk(fname);
372          strcpy(jcr->fname, fname);  /* move filename into JCR */
373
374          Dmsg2(040, "dird<filed: stream=%d %s\n", stream, jcr->fname);
375          Dmsg1(20, "dird<filed: attr=%s\n", attr);
376
377          /* 
378           * Find equivalent record in the database 
379           */
380          fdbr.FileId = 0;
381          if (!db_get_file_attributes_record(jcr->db, jcr->fname, &fdbr)) {
382             Jmsg(jcr, M_INFO, 0, _("New file: %s\n"), jcr->fname);
383             Dmsg1(020, _("File not in catalog: %s\n"), jcr->fname);
384             stat = JS_Differences;
385             continue;
386          } else {
387             /* 
388              * mark file record as visited by stuffing the
389              * current JobId, which is unique, into the FileIndex
390              */
391             db_mark_file_record(jcr->db, fdbr.FileId, jcr->JobId);
392          }
393
394          Dmsg3(100, "Found %s in catalog. inx=%d Opts=%s\n", jcr->fname, 
395             file_index, Opts_MD5);
396          decode_stat(fdbr.LStat, &statc); /* decode catalog stat */
397          /*
398           * Loop over options supplied by user and verify the
399           * fields he requests.
400           */
401          for (p=Opts_MD5; *p; p++) {
402             switch (*p) {
403             case 'i':                /* compare INODEs */
404                if (statc.st_ino != statf.st_ino) {
405                   prt_fname(jcr);
406                   Jmsg(jcr, M_INFO, 0, _("      st_ino   differ. Cat: %x File: %x\n"), 
407                      statc.st_ino, statf.st_ino);
408                   stat = JS_Differences;
409                }
410                break;
411             case 'p':                /* permissions bits */
412                if (statc.st_mode != statf.st_mode) {
413                   prt_fname(jcr);
414                   Jmsg(jcr, M_INFO, 0, _("      st_mode  differ. Cat: %x File: %x\n"), 
415                      statc.st_mode, statf.st_mode);
416                   stat = JS_Differences;
417                }
418                break;
419             case 'n':                /* number of links */
420                if (statc.st_nlink != statf.st_nlink) {
421                   prt_fname(jcr);
422                   Jmsg(jcr, M_INFO, 0, _("      st_nlink differ. Cat: %d File: %d\n"), 
423                      statc.st_nlink, statf.st_nlink);
424                   stat = JS_Differences;
425                }
426                break;
427             case 'u':                /* user id */
428                if (statc.st_uid != statf.st_uid) {
429                   prt_fname(jcr);
430                   Jmsg(jcr, M_INFO, 0, _("      st_uid   differ. Cat: %d File: %d\n"), 
431                      statc.st_uid, statf.st_uid);
432                   stat = JS_Differences;
433                }
434                break;
435             case 'g':                /* group id */
436                if (statc.st_gid != statf.st_gid) {
437                   prt_fname(jcr);
438                   Jmsg(jcr, M_INFO, 0, _("      st_gid   differ. Cat: %d File: %d\n"), 
439                      statc.st_gid, statf.st_gid);
440                   stat = JS_Differences;
441                }
442                break;
443             case 's':                /* size */
444                if (statc.st_size != statf.st_size) {
445                   prt_fname(jcr);
446                   Jmsg(jcr, M_INFO, 0, _("      st_size  differ. Cat: %d File: %d\n"), 
447                      statc.st_size, statf.st_size);
448                   stat = JS_Differences;
449                }
450                break;
451             case 'a':                /* access time */
452                if (statc.st_atime != statf.st_atime) {
453                   prt_fname(jcr);
454                   Jmsg(jcr, M_INFO, 0, _("      st_atime differs\n"));
455                   stat = JS_Differences;
456                }
457                break;
458             case 'm':
459                if (statc.st_mtime != statf.st_mtime) {
460                   prt_fname(jcr);
461                   Jmsg(jcr, M_INFO, 0, _("      st_mtime differs\n"));
462                   stat = JS_Differences;
463                }
464                break;
465             case 'c':                /* ctime */
466                if (statc.st_ctime != statf.st_ctime) {
467                   prt_fname(jcr);
468                   Jmsg(jcr, M_INFO, 0, _("      st_ctime differs\n"));
469                   stat = JS_Differences;
470                }
471                break;
472             case 'd':                /* file size decrease */
473                if (statc.st_size > statf.st_size) {
474                   prt_fname(jcr);
475                   Jmsg(jcr, M_INFO, 0, _("      st_size  decrease. Cat: %d File: %d\n"), 
476                      statc.st_size, statf.st_size);
477                   stat = JS_Differences;
478                }
479                break;
480             case '5':                /* compare MD5 */
481                Dmsg1(500, "set Do_MD5 for %s\n", jcr->fname);
482                do_MD5 = TRUE;
483                break;
484             case ':':
485             case 'V':
486             default:
487                break;
488             }
489          }
490       /*
491        * Got MD5 Signature from Storage daemon
492        *  It came across in the Opts_MD5 field.
493        */
494       } else if (stream == STREAM_MD5_SIGNATURE) {
495          Dmsg2(100, "stream=MD5 inx=%d fname=%s\n", file_index, jcr->fname);
496          /* 
497           * When ever we get an MD5 signature is MUST have been
498           * preceded by an attributes record, which sets attr_file_index
499           */
500          if (attr_file_index != file_index) {
501             Jmsg2(jcr, M_FATAL, 0, _("MD5 index %d not same as attributes %d\n"),
502                file_index, attr_file_index);
503             jcr->JobStatus = JS_ErrorTerminated;
504             return 0;
505          } 
506          if (do_MD5) {
507             db_escape_string(buf, Opts_MD5, strlen(Opts_MD5));
508             if (strcmp(buf, fdbr.MD5) != 0) {
509                prt_fname(jcr);
510                if (debug_level >= 10) {
511                   Jmsg(jcr, M_INFO, 0, _("      MD5 not same. File=%s Cat=%s\n"), buf, fdbr.MD5);
512                } else {
513                   Jmsg(jcr, M_INFO, 0, _("      MD5 differs.\n"));
514                }
515                stat = JS_Differences;
516             }
517             do_MD5 = FALSE;
518          }
519       }
520       jcr->jr.JobFiles = file_index;
521    } 
522    if (n < 0) {
523       Jmsg2(jcr, M_FATAL, 0, _("bdird<filed: bad attributes from filed n=%d : %s\n"),
524                         n, strerror(errno));
525       jcr->JobStatus = JS_ErrorTerminated;
526       return 0;
527    }
528
529    /* Now find all the files that are missing -- i.e. all files in
530     *  the database where the FileIndex != current JobId
531     */
532    jcr->fn_printed = FALSE;
533    sprintf(buf, 
534 "SELECT Path.Path,Filename.Name FROM File,Path,Filename "
535 "WHERE File.JobId=%d "
536 "AND File.FileIndex!=%d AND File.PathId=Path.PathId "
537 "AND File.FilenameId=Filename.FilenameId", 
538       last_full_id, jcr->JobId);
539    /* missing_handler is called for each file found */
540    db_sql_query(jcr->db, buf, missing_handler, (void *)jcr);
541    if (jcr->fn_printed) {
542       stat = JS_Differences;
543    }
544    jcr->JobStatus = stat;
545    return 1;
546 }
547
548 /*
549  * We are called here for each record that matches the above
550  *  SQL query -- that is for each file contained in the Catalog
551  *  that was not marked earlier. This means that the file in
552  *  question is a missing file (in the Catalog but on on Disk).
553  */
554 static int missing_handler(void *ctx, int num_fields, char **row)
555 {
556    JCR *jcr = (JCR *)ctx;
557
558    if (!jcr->fn_printed) {
559       Jmsg(jcr, M_INFO, 0, "\n");
560       Jmsg(jcr, M_INFO, 0, _("The following files are missing:\n"));
561       jcr->fn_printed = TRUE;
562    }
563    Jmsg(jcr, M_INFO, 0, "      %s%s\n", row[0]?row[0]:"", row[1]?row[1]:"");
564    return 0;
565 }
566
567
568 /* 
569  * Print filename for verify
570  */
571 static void prt_fname(JCR *jcr)
572 {
573    if (!jcr->fn_printed) {
574       Jmsg(jcr, M_INFO, 0, _("File: %s\n"), jcr->fname);
575       jcr->fn_printed = TRUE;
576    }
577 }