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