]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bbatch.c
ebl make mysql working with batch mode
[bacula/bacula] / bacula / src / tools / bbatch.c
1 /*
2  *
3  *  Program to test batch mode
4  *
5  *   Eric Bollengier, March 2007
6  *
7  *
8  *   Version $Id$
9  */
10 /*
11    Bacula® - The Network Backup Solution
12
13    Copyright (C) 2001-2006 Free Software Foundation Europe e.V.
14
15    The main author of Bacula is Kern Sibbald, with contributions from
16    many others, a complete list can be found in the file AUTHORS.
17    This program is Free Software; you can redistribute it and/or
18    modify it under the terms of version two of the GNU General Public
19    License as published by the Free Software Foundation plus additions
20    that are listed in the file LICENSE.
21
22    This program is distributed in the hope that it will be useful, but
23    WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public License
28    along with this program; if not, write to the Free Software
29    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30    02110-1301, USA.
31
32    Bacula® is a registered trademark of John Walker.
33    The licensor of Bacula is the Free Software Foundation Europe
34    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
35    Switzerland, email:ftf@fsfeurope.org.
36 */
37
38 /*
39   to create datafile
40  
41   for i in $(seq 10000 99999) ; do
42      j=$((($i % 1000) + 555))
43      echo "$i;/tmp/totabofds$j/fiddddle${j}$i;xxxLSTATxxxx;xxxxxxxMD5xxxxxx"
44   done  > dat1
45  
46   or
47
48   j=0
49   find / | while read a; do
50    j=$(($j+1))
51    echo "$j;$a;xxxLSTATxxxx;xxxxxxxMD5xxxxxx"
52   done > dat1
53  */
54
55 #include "bacula.h"
56 #include "stored/stored.h"
57 #include "findlib/find.h"
58 #include "cats/cats.h"
59  
60 /* Forward referenced functions */
61 static void *do_batch(void *);
62
63
64 /* Local variables */
65 static B_DB *db;
66
67 static const char *db_name = "bacula";
68 static const char *db_user = "bacula";
69 static const char *db_password = "";
70 static const char *db_host = NULL;
71
72 char *datafile=NULL;
73
74 static void usage()
75 {
76    fprintf(stderr, _(
77 PROG_COPYRIGHT
78 "\nVersion: %s (%s)\n\n"
79 "Usage: bbatch [ options ] -w working/dir -f datafile\n"
80 "       -d <nn>           set debug level to nn\n"
81 "       -n <name>         specify the database name (default bacula)\n"
82 "       -u <user>         specify database user name (default bacula)\n"
83 "       -P <password      specify database password (default none)\n"
84 "       -h <host>         specify database host (default NULL)\n"
85 "       -w <working>      specify working directory\n"
86 "       -v                verbose\n"
87 "       -f <file>         specify data file\n"
88 "       -?                print this message\n\n"), 2001, VERSION, BDATE);
89    exit(1);
90 }
91
92 /* number of thread started */
93 int nb=0;
94
95 int main (int argc, char *argv[])
96 {
97    int ch;
98    setlocale(LC_ALL, "");
99    bindtextdomain("bacula", LOCALEDIR);
100    textdomain("bacula");
101    init_stack_dump();
102    
103    char **files = (char **) malloc (10 * sizeof(char *));
104    int i;
105    my_name_is(argc, argv, "bbatch");
106    init_msg(NULL, NULL);
107
108    OSDependentInit();
109
110    while ((ch = getopt(argc, argv, "h:c:d:n:P:Su:vf:w:?")) != -1) {
111       switch (ch) {
112       case 'd':                    /* debug level */
113          debug_level = atoi(optarg);
114          if (debug_level <= 0)
115             debug_level = 1;
116          break;
117
118       case 'h':
119          db_host = optarg;
120          break;
121
122       case 'n':
123          db_name = optarg;
124          break;
125
126       case 'w':
127          working_directory = optarg;
128          break;
129
130       case 'u':
131          db_user = optarg;
132          break;
133
134       case 'P':
135          db_password = optarg;
136          break;
137
138       case 'v':
139          verbose++;
140          break;
141
142       case 'f':
143          if (nb < 10 ) {
144             files[nb++] = optarg;
145          }
146          break;
147
148       case '?':
149       default:
150          usage();
151
152       }
153    }
154    argc -= optind;
155    argv += optind;
156
157    if (argc != 0) {
158       Pmsg0(0, _("Wrong number of arguments: \n"));
159       usage();
160    }
161
162 #ifdef HAVE_BATCH_FILE_INSERT
163    printf("With new Batch mode\n");
164 #else
165    printf("Without new Batch mode\n");
166 #endif
167    i = nb;
168    while (--i >= 0) {
169       pthread_t thid;
170       JCR *bjcr = new_jcr(sizeof(JCR), NULL);
171       bjcr->bsr = NULL;
172       bjcr->VolSessionId = 1;
173       bjcr->VolSessionTime = (uint32_t)time(NULL);
174       bjcr->NumReadVolumes = 0;
175       bjcr->NumWriteVolumes = 0;
176       bjcr->JobId = getpid();
177       bjcr->JobType = JT_CONSOLE;
178       bjcr->JobLevel = L_FULL;
179       bjcr->JobStatus = JS_Running;
180       bjcr->where = bstrdup(files[i]);
181       bjcr->job_name = get_pool_memory(PM_FNAME);
182       pm_strcpy(bjcr->job_name, "Dummy.Job.Name");
183       bjcr->client_name = get_pool_memory(PM_FNAME);
184       pm_strcpy(bjcr->client_name, "Dummy.Client.Name");
185       bstrncpy(bjcr->Job, "bbatch", sizeof(bjcr->Job));
186       bjcr->fileset_name = get_pool_memory(PM_FNAME);
187       pm_strcpy(bjcr->fileset_name, "Dummy.fileset.name");
188       bjcr->fileset_md5 = get_pool_memory(PM_FNAME);
189       pm_strcpy(bjcr->fileset_md5, "Dummy.fileset.md5");
190       
191       if ((db=db_init_database(NULL, db_name, db_user, db_password,
192                                db_host, 0, NULL, 0)) == NULL) {
193          Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
194       }
195       if (!db_open_database(NULL, db)) {
196          Emsg0(M_ERROR_TERM, 0, db_strerror(db));
197       }
198       Dmsg0(200, "Database opened\n");
199       if (verbose) {
200          Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
201       }
202       
203       bjcr->db = db;
204
205       pthread_create(&thid, NULL, do_batch, bjcr);
206    }
207
208    while (nb > 0) {
209       bmicrosleep(1,0);
210    }
211
212    return 0;
213 }
214
215 static void fill_attr(ATTR_DBR *ar, char *data)
216 {
217    char *p;
218    char *b;
219    int index=0;
220    ar->Stream = STREAM_UNIX_ATTRIBUTES;
221    ar->JobId = getpid();
222
223    for(p = b = data; *p; p++) {
224       if (*p == ';') {
225          *p = '\0';
226          switch (index) {
227          case 0:
228             ar->FileIndex = str_to_int64(b);
229             break;
230          case 1:
231             ar->fname = b;
232             break;
233          case 2:
234             ar->attr = b;
235             break;
236          case 3:
237             ar->Digest = b;
238             break;
239          }
240          index++;
241          b = ++p;
242       }
243    }
244 }
245
246 static void *do_batch(void *jcr)
247 {
248    JCR *bjcr = (JCR *)jcr;
249    char data[1024];
250    int lineno = 0;
251    struct ATTR_DBR ar;
252    memset(&ar, 0, sizeof(ar));
253    btime_t begin = get_current_btime();
254    char *datafile = bjcr->where;
255
256    FILE *fd = fopen(datafile, "r");
257    if (!fd) {
258       Emsg1(M_ERROR_TERM, 0, _("Error opening datafile %s\n"), datafile);
259    }
260    while (fgets(data, sizeof(data)-1, fd)) {
261       strip_trailing_newline(data);
262       lineno++;
263       if (verbose && ((lineno % 5000) == 1)) {
264          printf("\r%i", lineno);
265       }
266       fill_attr(&ar, data);
267       if (!db_create_file_attributes_record(bjcr, bjcr->db, &ar)) {
268          Emsg0(M_ERROR_TERM, 0, _("Error while inserting file\n"));
269       }
270    }
271    fclose(fd);
272    db_write_batch_file_records(bjcr);
273    btime_t end = get_current_btime();
274    
275    char ed1[200], ed2[200];
276    printf("\rbegin = %s, end = %s\n", edit_int64(begin, ed1),edit_int64(end, ed2));
277    printf("Insert time = %llims\n", (end - begin) / 10000);
278    printf("Create %u files at %.2f/s\n", lineno, 
279           (lineno / ((float)((end - begin) / 1000000))));
280    nb--;
281    pthread_exit(NULL);
282    return NULL;
283 }