]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bbatch.c
Patch to add MySQL ssl access
[bacula/bacula] / bacula / src / tools / bbatch.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2001-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21  *
22  *  Program to test batch mode
23  *
24  *   Eric Bollengier, March 2007
25  *
26  */
27
28 /*
29   to create datafile
30  
31   for i in $(seq 10000 99999) ; do
32      j=$((($i % 1000) + 555))
33      echo "$i;/tmp/totabofds$j/fiddddle${j}$i;xxxLSTATxxxx;xxxxxxxMD5xxxxxx"
34   done  > dat1
35  
36   or
37
38   j=0
39   find / | while read a; do
40    j=$(($j+1))
41    echo "$j;$a;xxxLSTATxxxx;xxxxxxxMD5xxxxxx"
42   done > dat1
43  */
44
45 #include "bacula.h"
46 #include "stored/stored.h"
47 #include "findlib/find.h"
48 #include "cats/cats.h"
49  
50 /* Forward referenced functions */
51 static void *do_batch(void *);
52
53
54 /* Local variables */
55 static BDB *db;
56
57 static const char *db_name = "bacula";
58 static const char *db_user = "bacula";
59 static const char *db_password = "";
60 static const char *db_host = NULL;
61 static const char *db_ssl_key= NULL;
62 static const char *db_ssl_cert= NULL;
63 static const char *db_ssl_ca= NULL;
64 static const char *db_ssl_capath= NULL;
65 static const char *db_ssl_cipher= NULL;
66
67 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
68
69 static void usage()
70 {
71    fprintf(stderr, _(
72 PROG_COPYRIGHT
73 "\n%sVersion: %s (%s)\n"
74 "Example : bbatch -w /path/to/workdir -h localhost -f dat1 -f dat -f datx\n"
75 " will start 3 thread and load dat1, dat and datx in your catalog\n"
76 "See bbatch.c to generate datafile\n\n"
77 "Usage: bbatch [ options ] -w working/dir -f datafile\n"
78 "       -b                with batch mode\n"
79 "       -B                without batch mode\n"
80 "       -d <nn>           set debug level to <nn>\n"
81 "       -dt               print timestamp in debug output\n"
82 "       -n <name>         specify the database name (default bacula)\n"
83 "       -u <user>         specify database user name (default bacula)\n"
84 "       -P <password      specify database password (default none)\n"
85 "       -h <host>         specify database host (default NULL)\n"
86 "       -k <sslkey>       path name to the key file (default NULL)\n"
87 "       -e <sslcert>      path name to the certificate file (default NULL)\n"
88 "       -a <sslca>        path name to the CA certificate file (default NULL)\n"
89 "       -w <working>      specify working directory\n"
90 "       -r <jobids>       call restore code with given jobids\n"
91 "       -v                verbose\n"
92 "       -f <file>         specify data file\n"
93 "       -?                print this message\n\n"), 2001, "", VERSION, BDATE);
94    exit(1);
95 }
96
97 /* number of thread started */
98 int nb=0;
99
100 static int list_handler(void *ctx, int num_fields, char **row)
101 {
102    uint64_t *a = (uint64_t*) ctx;
103    (*a)++;
104    return 0;
105 }
106
107 int main (int argc, char *argv[])
108 {
109    int ch;
110    bool disable_batch = false;
111    char *restore_list=NULL;
112    setlocale(LC_ALL, "");
113    bindtextdomain("bacula", LOCALEDIR);
114    textdomain("bacula");
115    init_stack_dump();
116    lmgr_init_thread();
117    
118    char **files = (char **) malloc (10 * sizeof(char *));
119    int i;
120    my_name_is(argc, argv, "bbatch");
121    init_msg(NULL, NULL);
122
123    OSDependentInit();
124
125    while ((ch = getopt(argc, argv, "bBh:k:e:a:c:d:n:P:Su:vf:w:r:?")) != -1) {
126       switch (ch) {
127       case 'r':
128          restore_list=bstrdup(optarg);
129          break;
130       case 'B':
131          disable_batch = true;
132          break;
133       case 'b':
134          disable_batch = false;
135          break;
136       case 'd':                    /* debug level */
137          if (*optarg == 't') {
138             dbg_timestamp = true;
139          } else {
140             debug_level = atoi(optarg);
141             if (debug_level <= 0) {
142                debug_level = 1;
143             }
144          }
145          break;
146
147       case 'h':
148          db_host = optarg;
149          break;
150
151       case 'k':
152          db_ssl_key = optarg;
153          break;
154
155       case 'e':
156          db_ssl_cert = optarg;
157          break;
158
159       case 'a':
160          db_ssl_ca = optarg;
161          break;
162
163       case 'n':
164          db_name = optarg;
165          break;
166
167       case 'w':
168          working_directory = optarg;
169          break;
170
171       case 'u':
172          db_user = optarg;
173          break;
174
175       case 'P':
176          db_password = optarg;
177          break;
178
179       case 'v':
180          verbose++;
181          break;
182
183       case 'f':
184          if (nb < 10 ) {
185             files[nb++] = optarg;
186          }
187          break;
188
189       case '?':
190       default:
191          usage();
192
193       }
194    }
195    argc -= optind;
196    argv += optind;
197
198    if (argc != 0) {
199       Pmsg0(0, _("Wrong number of arguments: \n"));
200       usage();
201    }
202
203    if (restore_list) {
204       uint64_t nb_file=0;
205       btime_t start, end;
206       /* To use the -r option, the catalog should already contains records */
207       
208       if ((db = db_init_database(NULL, NULL, db_name, db_user, db_password,
209                                  db_host, 0, NULL, db_ssl_key, db_ssl_cert,
210                                  db_ssl_ca, db_ssl_capath, db_ssl_cipher,
211                                  false, disable_batch)) == NULL) {
212          Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
213       }
214       if (!db_open_database(NULL, db)) {
215          Emsg0(M_ERROR_TERM, 0, db_strerror(db));
216       }
217
218       start = get_current_btime();
219       db_get_file_list(NULL, db, restore_list, false, false, list_handler, &nb_file);
220       end = get_current_btime();
221
222       Pmsg3(0, _("Computing file list for jobid=%s files=%lld secs=%d\n"), 
223             restore_list, nb_file, (uint32_t)btime_to_unix(end-start));
224       
225       free(restore_list);
226       return 0;
227    }
228
229    if (disable_batch) {
230       printf("Without new Batch mode\n");
231    } else {
232       printf("With new Batch mode\n");
233    }
234
235    i = nb;
236    while (--i >= 0) {
237       pthread_t thid;
238       JCR *bjcr = new_jcr(sizeof(JCR), NULL);
239       bjcr->bsr = NULL;
240       bjcr->VolSessionId = 1;
241       bjcr->VolSessionTime = (uint32_t)time(NULL);
242       bjcr->NumReadVolumes = 0;
243       bjcr->NumWriteVolumes = 0;
244       bjcr->JobId = getpid();
245       bjcr->setJobType(JT_CONSOLE);
246       bjcr->setJobLevel(L_FULL);
247       bjcr->JobStatus = JS_Running;
248       bjcr->where = bstrdup(files[i]);
249       bjcr->job_name = get_pool_memory(PM_FNAME);
250       pm_strcpy(bjcr->job_name, "Dummy.Job.Name");
251       bjcr->client_name = get_pool_memory(PM_FNAME);
252       pm_strcpy(bjcr->client_name, "Dummy.Client.Name");
253       bstrncpy(bjcr->Job, "bbatch", sizeof(bjcr->Job));
254       bjcr->fileset_name = get_pool_memory(PM_FNAME);
255       pm_strcpy(bjcr->fileset_name, "Dummy.fileset.name");
256       bjcr->fileset_md5 = get_pool_memory(PM_FNAME);
257       pm_strcpy(bjcr->fileset_md5, "Dummy.fileset.md5");
258       
259       if ((db = db_init_database(NULL, NULL, db_name, db_user, db_password,
260                                  db_host, 0, NULL, db_ssl_key, db_ssl_cert,
261                                  db_ssl_ca, db_ssl_capath, db_ssl_cipher,
262                                  false, false)) == NULL) {
263          Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
264       }
265       if (!db_open_database(NULL, db)) {
266          Emsg0(M_ERROR_TERM, 0, db_strerror(db));
267       }
268       Dmsg0(200, "Database opened\n");
269       if (verbose) {
270          Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
271       }
272       
273       bjcr->db = db;
274
275       pthread_create(&thid, NULL, do_batch, bjcr);
276    }
277
278    while (nb > 0) {
279       bmicrosleep(1,0);
280    }
281
282    return 0;
283 }
284
285 static void fill_attr(ATTR_DBR *ar, char *data)
286 {
287    char *p;
288    char *b;
289    int index=0;
290    ar->Stream = STREAM_UNIX_ATTRIBUTES;
291    ar->JobId = getpid();
292
293    for(p = b = data; *p; p++) {
294       if (*p == ';') {
295          *p = '\0';
296          switch (index) {
297          case 0:
298             ar->FileIndex = str_to_int64(b);
299             break;
300          case 1:
301             ar->fname = b;
302             break;
303          case 2:
304             ar->attr = b;
305             break;
306          case 3:
307             ar->Digest = b;
308             break;
309          }
310          index++;
311          b = ++p;
312       }
313    }
314 }
315
316 static void *do_batch(void *jcr)
317 {
318    JCR *bjcr = (JCR *)jcr;
319    char data[1024];
320    int lineno = 0;
321    struct ATTR_DBR ar;
322    memset(&ar, 0, sizeof(ar));
323    btime_t begin = get_current_btime();
324    char *datafile = bjcr->where;
325
326    FILE *fd = fopen(datafile, "r");
327    if (!fd) {
328       Emsg1(M_ERROR_TERM, 0, _("Error opening datafile %s\n"), datafile);
329    }
330    while (fgets(data, sizeof(data)-1, fd)) {
331       strip_trailing_newline(data);
332       lineno++;
333       if (verbose && ((lineno % 5000) == 1)) {
334          printf("\r%i", lineno);
335       }
336       fill_attr(&ar, data);
337       if (!db_create_attributes_record(bjcr, bjcr->db, &ar)) {
338          Emsg0(M_ERROR_TERM, 0, _("Error while inserting file\n"));
339       }
340    }
341    fclose(fd);
342    db_write_batch_file_records(bjcr);
343    btime_t end = get_current_btime();
344    
345    P(mutex);
346    char ed1[200], ed2[200];
347    printf("\rbegin = %s, end = %s\n", edit_int64(begin, ed1),edit_int64(end, ed2));
348    printf("Insert time = %sms\n", edit_int64((end - begin) / 10000, ed1));
349    printf("Create %u files at %.2f/s\n", lineno, 
350           (lineno / ((float)((end - begin) / 1000000))));
351    nb--;
352    V(mutex);
353    pthread_exit(NULL);
354    return NULL;
355 }