]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/backup.c
Initial revision
[bacula/bacula] / bacula / src / dird / backup.c
1 /*
2  *
3  *   Bacula Director -- backup.c -- responsible for doing backup jobs
4  *
5  *     Kern Sibbald, March MM
6  *
7  *    This routine is called as a thread. It may not yet be totally
8  *      thread reentrant!!!
9  *
10  *  Basic tasks done here:
11  *     Open DB and create records for this job.
12  *     Open Message Channel with Storage daemon to tell him a job will be starting.
13  *     Open connection with File daemon and pass him commands
14  *       to do the backup.
15  *     When the File daemon finishes the job, update the DB.
16  *
17  */
18
19 /*
20    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
21
22    This program is free software; you can redistribute it and/or
23    modify it under the terms of the GNU General Public License as
24    published by the Free Software Foundation; either version 2 of
25    the License, or (at your option) any later version.
26
27    This program is distributed in the hope that it will be useful,
28    but WITHOUT ANY WARRANTY; without even the implied warranty of
29    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30    General Public License for more details.
31
32    You should have received a copy of the GNU General Public
33    License along with this program; if not, write to the Free
34    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35    MA 02111-1307, USA.
36
37  */
38
39 #include "bacula.h"
40 #include "dird.h"
41
42 /* Commands sent to File daemon */
43 static char backupcmd[] = "backup\n";
44 static char storaddr[]  = "storage address=%s port=%d\n";
45 static char levelcmd[]  = "level = %s%s\n";
46
47 /* Responses received from File daemon */
48 static char OKbackup[] = "2000 OK backup\n";
49 static char OKstore[]  = "2000 OK storage\n";
50 static char OKlevel[]  = "2000 OK level\n";
51
52 /* Forward referenced functions */
53 static void backup_cleanup(JCR *jcr, int TermCode, char *since);
54 static int wait_for_job_termination(JCR *jcr);               
55
56 /* External functions */
57
58 /* 
59  * Do a backup of the specified FileSet
60  *    
61  *  Returns:  0 on failure
62  *            1 on success
63  */
64 int do_backup(JCR *jcr) 
65 {
66    char since[MAXSTRING];
67    int stat;
68    BSOCK   *fd;
69    POOL_DBR pr;
70 #ifdef needed
71    MEDIA_DBR mr;
72 #endif
73    CLIENT_DBR cr;
74    FILESET_DBR fsr;
75
76    since[0] = 0;
77    /*
78     * Get or Create client record
79     */
80    memset(&cr, 0, sizeof(cr));
81    strcpy(cr.Name, jcr->client->hdr.name);
82    if (jcr->client_name) {
83       free(jcr->client_name);
84    }
85    jcr->client_name = bstrdup(jcr->client->hdr.name);
86    if (!db_create_client_record(jcr->db, &cr)) {
87       Jmsg(jcr, M_ERROR, 0, _("Could not create Client record. %s"), 
88          db_strerror(jcr->db));
89       backup_cleanup(jcr, JS_ErrorTerminated, since);
90       return 0;
91    }
92    jcr->jr.ClientId = cr.ClientId;
93    Dmsg2(9, "Created Client %s record %d\n", jcr->client->hdr.name, 
94       jcr->jr.ClientId);
95
96    /*
97     * Get or Create FileSet record
98     */
99    memset(&fsr, 0, sizeof(fsr));
100    strcpy(fsr.FileSet, jcr->fileset->hdr.name);
101    if (jcr->fileset->have_MD5) {
102       struct MD5Context md5c;
103       unsigned char signature[16];
104       memcpy(&md5c, &jcr->fileset->md5c, sizeof(md5c));
105       MD5Final(signature, &md5c);
106       bin_to_base64(fsr.MD5, (char *)signature, 16); /* encode 16 bytes */
107    } else {
108       Jmsg(jcr, M_WARNING, 0, _("FileSet MD5 signature not found.\n"));
109    }
110    if (!db_create_fileset_record(jcr->db, &fsr)) {
111       Jmsg(jcr, M_ERROR, 0, _("Could not create FileSet record. %s"), 
112          db_strerror(jcr->db));
113       backup_cleanup(jcr, JS_ErrorTerminated, since);
114       return 0;
115    }   
116    jcr->jr.FileSetId = fsr.FileSetId;
117    Dmsg2(9, "Created FileSet %s record %d\n", jcr->fileset->hdr.name, 
118       jcr->jr.FileSetId);
119
120
121    /* Look up the last
122     * FULL backup job to get the time/date for a 
123     * differential or incremental save.
124     */
125    jcr->stime = (char *) get_pool_memory(PM_MESSAGE);
126    jcr->stime[0] = 0;
127    since[0] = 0;
128    switch (jcr->level) {
129       case L_DIFFERENTIAL:
130       case L_INCREMENTAL:
131          /* Look up start time of last job */
132          jcr->jr.JobId = 0;
133          if (!db_find_job_start_time(jcr->db, &jcr->jr, jcr->stime)) {
134             Jmsg(jcr, M_INFO, 0, _("Last FULL backup time not found. Doing FULL backup.\n"));
135             jcr->level = L_FULL;
136             jcr->jr.Level = L_FULL;
137          } else {
138             strcpy(since, ", since=");
139             strcat(since, jcr->stime);
140          }
141          Dmsg1(15, "Last start time = %s\n", jcr->stime);
142          break;
143    }
144
145    jcr->jr.JobId = jcr->JobId;
146    jcr->jr.StartTime = jcr->start_time;
147    if (!db_update_job_start_record(jcr->db, &jcr->jr)) {
148       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
149       backup_cleanup(jcr, JS_ErrorTerminated, since);
150       return 0;
151    }
152
153    jcr->fname = (char *) get_pool_memory(PM_FNAME);
154
155    /* Print Job Start message */
156    Jmsg(jcr, M_INFO, 0, _("Start Backup JobId %d, Job=%s\n"),
157         jcr->JobId, jcr->Job);
158
159    /* 
160     * Get the Pool record  
161     */
162    memset(&pr, 0, sizeof(pr));
163    strcpy(pr.Name, jcr->pool->hdr.name);
164    while (!db_get_pool_record(jcr->db, &pr)) { /* get by Name */
165       /* Try to create the pool */
166       if (create_pool(jcr->db, jcr->pool) < 0) {
167          Jmsg(jcr, M_FATAL, 0, _("Pool %s not in database. %s"), pr.Name, 
168             db_strerror(jcr->db));
169          backup_cleanup(jcr, JS_ErrorTerminated, since);
170          return 0;
171       } else {
172          Jmsg(jcr, M_INFO, 0, _("Pool %s created in database.\n"), pr.Name);
173       }
174    }
175    jcr->PoolId = pr.PoolId;               /****FIXME**** this can go away */
176    jcr->jr.PoolId = pr.PoolId;
177
178 #ifdef needed
179    /* NOTE, THIS IS NOW DONE BY THE STORAGE DAEMON
180     *
181     * Find at least one Volume associated with this Pool
182     *  It must be marked Append, and be of the correct Media Type
183     *  for the storage type.
184     */
185    memset(&mr, 0, sizeof(mr));
186    mr.PoolId = pr.PoolId;
187    strcpy(mr.VolStatus, "Append");
188    strcpy(mr.MediaType, jcr->store->media_type);
189    if (!db_find_next_volume(jcr->db, 1, &mr)) {
190       if (!newVolume(jcr)) {
191          Jmsg(jcr, M_FATAL, 0, _("No writable %s media in Pool %s.\n\
192       Please use the Console program to add available Volumes.\n"), mr.MediaType, pr.Name);
193          backup_cleanup(jcr, JS_ErrorTerminated, since);
194          return 0;
195       }
196    }
197 #endif
198
199    /*
200     * Open a message channel connection with the Storage
201     * daemon. This is to let him know that our client
202     * will be contacting him for a backup  session.
203     *
204     */
205    Dmsg0(10, "Open connection with storage daemon\n");
206    jcr->JobStatus = JS_Blocked;
207    /*
208     * Start conversation with Storage daemon  
209     */
210    if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
211       backup_cleanup(jcr, JS_ErrorTerminated, since);
212       return 0;
213    }
214    /*
215     * Now start a job with the Storage daemon
216     */
217    if (!start_storage_daemon_job(jcr)) {
218       backup_cleanup(jcr, JS_ErrorTerminated, since);
219       return 0;
220    }
221    /*
222     * Now start a Storage daemon message thread
223     */
224    if (!start_storage_daemon_message_thread(jcr)) {
225       backup_cleanup(jcr, JS_ErrorTerminated, since);
226       return 0;
227    }
228
229    Dmsg0(50, "Storage daemon connection OK\n");
230
231    if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
232       backup_cleanup(jcr, JS_ErrorTerminated, since);
233       return 0;
234    }
235
236    jcr->JobStatus = JS_Running;
237    fd = jcr->file_bsock;
238
239    if (!send_include_list(jcr)) {
240       backup_cleanup(jcr, JS_ErrorTerminated, since);
241       return 0;
242    }
243
244    if (!send_exclude_list(jcr)) {
245       backup_cleanup(jcr, JS_ErrorTerminated, since);
246       return 0;
247    }
248
249    /* 
250     * send Storage daemon address to the File daemon
251     */
252    if (jcr->store->SDDport == 0) {
253       jcr->store->SDDport = jcr->store->SDport;
254    }
255    bnet_fsend(fd, storaddr, jcr->store->address, jcr->store->SDDport);
256    if (!response(fd, OKstore, "Storage")) {
257       backup_cleanup(jcr, JS_ErrorTerminated, since);
258       return 0;
259    }
260
261    /* 
262     * Send Level command to File daemon
263     */
264    switch (jcr->level) {
265       case L_FULL:
266          bnet_fsend(fd, levelcmd, "full", " ");
267          break;
268       case L_DIFFERENTIAL:
269       case L_INCREMENTAL:
270          bnet_fsend(fd, levelcmd, "since ", jcr->stime);
271          free_pool_memory(jcr->stime);
272          jcr->stime = NULL;
273          break;
274       case L_SINCE:
275       default:
276          Emsg1(M_FATAL, 0, _("Unimplemented backup level %d\n"), jcr->level);
277          backup_cleanup(jcr, JS_ErrorTerminated, since);
278          return 0;
279    }
280    Dmsg1(20, ">filed: %s", fd->msg);
281    if (!response(fd, OKlevel, "Level")) {
282       backup_cleanup(jcr, JS_ErrorTerminated, since);
283       return 0;
284    }
285
286    /* Send backup command */
287    bnet_fsend(fd, backupcmd);
288    if (!response(fd, OKbackup, "backup")) {
289       backup_cleanup(jcr, JS_ErrorTerminated, since);
290       return 0;
291    }
292
293    /* Pickup Job termination data */        
294    stat = wait_for_job_termination(jcr);
295    backup_cleanup(jcr, stat, since);
296    return 1;
297 }
298
299 /*
300  *  NOTE! This is no longer really needed as the Storage
301  *        daemon now passes this information directly
302  *        back to us.   
303  */
304 static int wait_for_job_termination(JCR *jcr)
305 {
306    int32_t n = 0;
307    BSOCK *fd = jcr->file_bsock;
308
309    jcr->JobStatus = JS_WaitFD;
310    /* Wait for Client to terminate */
311    while ((n = bget_msg(fd, 0)) > 0 && !job_cancelled(jcr)) {
312       /* get and discard Client output */
313    }
314    bnet_sig(fd, BNET_TERMINATE);      /* tell Client we are terminating */
315    if (n < 0) {
316       Jmsg(jcr, M_FATAL, 0, _("<filed: network error during BACKUP command. ERR=%s\n"),
317           bnet_strerror(fd));
318    }
319
320    /* Now wait for Storage daemon to terminate our message thread */
321    P(jcr->mutex);
322    jcr->JobStatus = JS_WaitSD;
323    while (!jcr->msg_thread_done && !job_cancelled(jcr)) {
324       struct timeval tv;
325       struct timezone tz;
326       struct timespec timeout;
327
328       gettimeofday(&tv, &tz);
329       timeout.tv_nsec = 0;
330       timeout.tv_sec = tv.tv_sec + 10; /* wait 10 seconds */
331       Dmsg0(300, "I'm waiting for message thread termination.\n");
332       pthread_cond_timedwait(&jcr->term_wait, &jcr->mutex, &timeout);
333    }
334    V(jcr->mutex);
335    if (n < 0) {                                     
336       return JS_ErrorTerminated;
337    }
338    return jcr->SDJobStatus;
339 }
340
341 /*
342  * Release resources allocated during backup.
343  */
344 static void backup_cleanup(JCR *jcr, int TermCode, char *since)
345 {
346    char sdt[50], edt[50];
347    char ec1[30], ec2[30], ec3[30];
348    char term_code[100];
349    char *term_msg;
350    int msg_type;
351    MEDIA_DBR mr;
352
353    memset(&mr, 0, sizeof(mr));
354    Dmsg0(100, "Enter backup_cleanup()\n");
355    if (jcr->jr.EndTime == 0) {
356       jcr->jr.EndTime = time(NULL);
357    }
358    jcr->end_time = jcr->jr.EndTime;
359    jcr->jr.JobId = jcr->JobId;
360    jcr->jr.JobStatus = jcr->JobStatus = TermCode;
361    jcr->jr.JobFiles = jcr->JobFiles;
362    jcr->jr.JobBytes = jcr->JobBytes;
363    jcr->jr.VolSessionId = jcr->VolSessionId;
364    jcr->jr.VolSessionTime = jcr->VolSessionTime;
365    if (!db_update_job_end_record(jcr->db, &jcr->jr)) {
366       Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"), 
367          db_strerror(jcr->db));
368    }
369    if (!db_get_job_record(jcr->db, &jcr->jr)) {
370       Jmsg(jcr, M_WARNING, 0, _("Error getting job record for stats: %s"), 
371          db_strerror(jcr->db));
372    }
373
374    strcpy(mr.VolumeName, jcr->VolumeName);
375    if (!db_get_media_record(jcr->db, &mr)) {
376       Jmsg(jcr, M_WARNING, 0, _("Error getting Media record for stats: %s"), 
377          db_strerror(jcr->db));
378    }
379
380       
381    msg_type = M_INFO;                 /* by default INFO message */
382    switch (TermCode) {
383       case JS_Terminated:
384          term_msg = _("Backup OK");
385          break;
386       case JS_Errored:
387          term_msg = _("*** Backup Error ***"); 
388          msg_type = M_ERROR;          /* Generate error message */
389          if (jcr->store_bsock) {
390             bnet_sig(jcr->store_bsock, BNET_TERMINATE);
391             pthread_cancel(jcr->SD_msg_chan);
392          }
393          break;
394       case JS_Cancelled:
395          term_msg = _("Backup Cancelled");
396          if (jcr->store_bsock) {
397             bnet_sig(jcr->store_bsock, BNET_TERMINATE);
398             pthread_cancel(jcr->SD_msg_chan);
399          }
400          break;
401       default:
402          term_msg = term_code;
403          sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
404          break;
405    }
406    bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
407    bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
408    if (!db_get_job_volume_names(jcr->db, jcr->jr.JobId, jcr->VolumeName)) {
409       jcr->VolumeName[0] = 0;         /* none */
410    }
411
412    Jmsg(jcr, msg_type, 0, _("%s\n\
413 JobId:                  %d\n\
414 Job:                    %s\n\
415 FileSet:                %s\n\
416 Backup Level:           %s%s\n\
417 Client:                 %s\n\
418 Start time:             %s\n\
419 End time:               %s\n\
420 Bytes Written:          %s\n\
421 Files Written:          %s\n\
422 Volume names(s):        %s\n\
423 Volume Session Id:      %d\n\
424 Volume Session Time:    %d\n\
425 Volume Bytes:           %s\n\
426 Termination:            %s\n"),
427         edt,
428         jcr->jr.JobId,
429         jcr->jr.Job,
430         jcr->fileset->hdr.name,
431         level_to_str(jcr->level), since,
432         jcr->client->hdr.name,
433         sdt,
434         edt,
435         edit_uint_with_commas(jcr->jr.JobBytes, ec1),
436         edit_uint_with_commas(jcr->jr.JobFiles, ec2),
437         jcr->VolumeName,
438         jcr->VolSessionId,
439         jcr->VolSessionTime,
440         edit_uint_with_commas(mr.VolBytes, ec3),
441         term_msg);
442
443    Dmsg0(100, "Leave backup_cleanup()\n");
444 }