]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bbatch.c
ebl add a new bbatch tool
[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
53  */
54
55 #include "bacula.h"
56 #include "stored.h"
57 #include "findlib/find.h"
58 #include "cats/cats.h"
59  
60 /* Forward referenced functions */
61 static void do_batch();
62
63
64 /* Local variables */
65 static B_DB *db;
66
67 static time_t lasttime = 0;
68
69 static const char *db_name = "bacula";
70 static const char *db_user = "bacula";
71 static const char *db_password = "";
72 static const char *db_host = NULL;
73 static bool list_records = false;
74 static int ignored_msgs = 0;
75
76 static JCR *bjcr;
77
78 char *datafile=NULL;
79
80 static void usage()
81 {
82    fprintf(stderr, _(
83 PROG_COPYRIGHT
84 "\nVersion: %s (%s)\n\n"
85 "Usage: bbatch [ options ] -w working/dir -f datafile\n"
86 "       -d <nn>           set debug level to nn\n"
87 "       -n <name>         specify the database name (default bacula)\n"
88 "       -u <user>         specify database user name (default bacula)\n"
89 "       -P <password      specify database password (default none)\n"
90 "       -h <host>         specify database host (default NULL)\n"
91 "       -w <working>      specify working directory\n"
92 "       -v                verbose\n"
93 "       -f <file>         specify data file\n"
94 "       -?                print this message\n\n"), 2001, VERSION, BDATE);
95    exit(1);
96 }
97
98 int main (int argc, char *argv[])
99 {
100    int ch;
101    struct stat stat_buf;
102    setlocale(LC_ALL, "");
103    bindtextdomain("bacula", LOCALEDIR);
104    textdomain("bacula");
105    init_stack_dump();
106
107    my_name_is(argc, argv, "bbatch");
108    init_msg(NULL, NULL);
109
110    OSDependentInit();
111
112    while ((ch = getopt(argc, argv, "c:d:n:P:Su:vf:w:?")) != -1) {
113       switch (ch) {
114       case 'd':                    /* debug level */
115          debug_level = atoi(optarg);
116          if (debug_level <= 0)
117             debug_level = 1;
118          break;
119
120       case 'h':
121          db_host = optarg;
122          break;
123
124       case 'n':
125          db_name = optarg;
126          break;
127
128       case 'w':
129          working_directory = optarg;
130          break;
131
132       case 'u':
133          db_user = optarg;
134          break;
135
136       case 'P':
137          db_password = optarg;
138          break;
139
140       case 'v':
141          verbose++;
142          break;
143
144       case 'f':
145          datafile = optarg;
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    if (!datafile) {
163       Pmsg0(0, _("You must specified a data file\n"));
164       usage();
165    }
166
167 #ifdef HAVE_BATCH_FILE_INSERT
168    printf("With new Batch mode\n");
169 #else
170    printf("Without new Batch mode\n");
171 #endif
172
173    bjcr = new_jcr(sizeof(JCR), NULL);
174    bjcr->bsr = NULL;
175    bjcr->VolSessionId = 1;
176    bjcr->VolSessionTime = (uint32_t)time(NULL);
177    bjcr->NumReadVolumes = 0;
178    bjcr->NumWriteVolumes = 0;
179    bjcr->JobId = getpid();
180    bjcr->JobType = JT_CONSOLE;
181    bjcr->JobLevel = L_FULL;
182    bjcr->JobStatus = JS_Running;
183    bjcr->where = bstrdup("");
184    bjcr->job_name = get_pool_memory(PM_FNAME);
185    pm_strcpy(bjcr->job_name, "Dummy.Job.Name");
186    bjcr->client_name = get_pool_memory(PM_FNAME);
187    pm_strcpy(bjcr->client_name, "Dummy.Client.Name");
188    bstrncpy(bjcr->Job, "bbatch", sizeof(bjcr->Job));
189    bjcr->fileset_name = get_pool_memory(PM_FNAME);
190    pm_strcpy(bjcr->fileset_name, "Dummy.fileset.name");
191    bjcr->fileset_md5 = get_pool_memory(PM_FNAME);
192    pm_strcpy(bjcr->fileset_md5, "Dummy.fileset.md5");
193
194    if ((db=db_init_database(NULL, db_name, db_user, db_password,
195         db_host, 0, NULL, 0)) == NULL) {
196       Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
197    }
198    if (!db_open_database(NULL, db)) {
199       Emsg0(M_ERROR_TERM, 0, db_strerror(db));
200    }
201    Dmsg0(200, "Database opened\n");
202    if (verbose) {
203       Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
204    }
205
206    bjcr->db = db;
207
208    do_batch();
209
210    return 0;
211 }
212
213 static void fill_attr(ATTR_DBR *ar, char *data)
214 {
215    char *p;
216    char *b;
217    int index=0;
218    ar->Stream = STREAM_UNIX_ATTRIBUTES;
219    ar->JobId = getpid();
220
221    for(p = b = data; *p; p++) {
222       if (*p == ';') {
223          *p = '\0';
224          switch (index) {
225          case 0:
226             ar->FileIndex = str_to_int64(b);
227             break;
228          case 1:
229             ar->fname = b;
230             break;
231          case 2:
232             ar->attr = b;
233             break;
234          case 3:
235             ar->Digest = b;
236             break;
237          }
238          index++;
239          p++;
240       }
241    }
242 }
243
244 static void do_batch()
245 {
246    char data[1024];
247    int lineno = 0;
248    struct ATTR_DBR ar;
249    memset(&ar, 0, sizeof(ar));
250    btime_t begin = get_current_btime();
251
252    FILE *fd = fopen(datafile, "r");
253    if (!fd) {
254       Emsg1(M_ERROR_TERM, 0, _("Error opening datafile %s\n"), datafile);
255    }
256    while (fgets(data, sizeof(data)-1, fd)) {
257       strip_trailing_newline(data);
258       lineno++;
259       if (verbose && ((lineno % 5000) == 1)) {
260          printf("\r%i", lineno);
261       }
262       fill_attr(&ar, data);
263       if (!db_create_file_attributes_record(bjcr, bjcr->db, &ar)) {
264          Emsg0(M_ERROR_TERM, 0, _("Error while inserting file\n"));
265       }
266    }
267    fclose(fd);
268    db_write_batch_file_records(bjcr);
269    btime_t end = get_current_btime();
270    
271    char ed1[200], ed2[200];
272    printf("\rbegin = %s, end = %s\n", edit_int64(begin, ed1),edit_int64(end, ed2));
273    printf("Insert time = %ims\n", (end - begin) / 10000);
274    printf("Create %i files at %.2f/s\n", lineno, 
275           (lineno / ((float)((end - begin) / 1000000))));
276 }