3 * Program to test batch mode
5 * Eric Bollengier, March 2007
11 Bacula® - The Network Backup Solution
13 Copyright (C) 2001-2014 Free Software Foundation Europe e.V.
15 The main author of Bacula is Kern Sibbald, with contributions from many
16 others, a complete list can be found in the file AUTHORS.
18 You may use this file and others of this release according to the
19 license defined in the LICENSE file, which includes the Affero General
20 Public License, v3.0 ("AGPLv3") and some additional permissions and
21 terms pursuant to its AGPLv3 Section 7.
23 Bacula® is a registered trademark of Kern Sibbald.
29 for i in $(seq 10000 99999) ; do
30 j=$((($i % 1000) + 555))
31 echo "$i;/tmp/totabofds$j/fiddddle${j}$i;xxxLSTATxxxx;xxxxxxxMD5xxxxxx"
37 find / | while read a; do
39 echo "$j;$a;xxxLSTATxxxx;xxxxxxxMD5xxxxxx"
44 #include "stored/stored.h"
45 #include "findlib/find.h"
46 #include "cats/cats.h"
47 #include "cats/sql_glue.h"
49 /* Forward referenced functions */
50 static void *do_batch(void *);
56 static const char *db_name = "bacula";
57 static const char *db_user = "bacula";
58 static const char *db_password = "";
59 static const char *db_host = NULL;
61 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
67 "\nVersion: %s (%s)\n"
68 "Example : bbatch -w /path/to/workdir -h localhost -f dat1 -f dat -f datx\n"
69 " will start 3 thread and load dat1, dat and datx in your catalog\n"
70 "See bbatch.c to generate datafile\n\n"
71 "Usage: bbatch [ options ] -w working/dir -f datafile\n"
72 " -b with batch mode\n"
73 " -B without batch mode\n"
74 " -d <nn> set debug level to <nn>\n"
75 " -dt print timestamp in debug output\n"
76 " -n <name> specify the database name (default bacula)\n"
77 " -u <user> specify database user name (default bacula)\n"
78 " -P <password specify database password (default none)\n"
79 " -h <host> specify database host (default NULL)\n"
80 " -w <working> specify working directory\n"
81 " -r <jobids> call restore code with given jobids\n"
83 " -f <file> specify data file\n"
84 " -? print this message\n\n"), 2001, VERSION, BDATE);
88 /* number of thread started */
91 static int list_handler(void *ctx, int num_fields, char **row)
93 uint64_t *a = (uint64_t*) ctx;
98 int main (int argc, char *argv[])
101 bool disable_batch = false;
102 char *restore_list=NULL;
103 setlocale(LC_ALL, "");
104 bindtextdomain("bacula", LOCALEDIR);
105 textdomain("bacula");
109 char **files = (char **) malloc (10 * sizeof(char *));
111 my_name_is(argc, argv, "bbatch");
112 init_msg(NULL, NULL);
116 while ((ch = getopt(argc, argv, "bBh:c:d:n:P:Su:vf:w:r:?")) != -1) {
119 restore_list=bstrdup(optarg);
122 disable_batch = true;
125 disable_batch = false;
127 case 'd': /* debug level */
128 if (*optarg == 't') {
129 dbg_timestamp = true;
131 debug_level = atoi(optarg);
132 if (debug_level <= 0) {
147 working_directory = optarg;
155 db_password = optarg;
164 files[nb++] = optarg;
178 Pmsg0(0, _("Wrong number of arguments: \n"));
185 /* To use the -r option, the catalog should already contains records */
187 if ((db = db_init_database(NULL, NULL, db_name, db_user, db_password,
188 db_host, 0, NULL, false, disable_batch)) == NULL) {
189 Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
191 if (!db_open_database(NULL, db)) {
192 Emsg0(M_ERROR_TERM, 0, db_strerror(db));
195 start = get_current_btime();
196 db_get_file_list(NULL, db, restore_list, false, false, list_handler, &nb_file);
197 end = get_current_btime();
199 Pmsg3(0, _("Computing file list for jobid=%s files=%lld secs=%d\n"),
200 restore_list, nb_file, (uint32_t)btime_to_unix(end-start));
207 printf("Without new Batch mode\n");
209 printf("With new Batch mode\n");
215 JCR *bjcr = new_jcr(sizeof(JCR), NULL);
217 bjcr->VolSessionId = 1;
218 bjcr->VolSessionTime = (uint32_t)time(NULL);
219 bjcr->NumReadVolumes = 0;
220 bjcr->NumWriteVolumes = 0;
221 bjcr->JobId = getpid();
222 bjcr->setJobType(JT_CONSOLE);
223 bjcr->setJobLevel(L_FULL);
224 bjcr->JobStatus = JS_Running;
225 bjcr->where = bstrdup(files[i]);
226 bjcr->job_name = get_pool_memory(PM_FNAME);
227 pm_strcpy(bjcr->job_name, "Dummy.Job.Name");
228 bjcr->client_name = get_pool_memory(PM_FNAME);
229 pm_strcpy(bjcr->client_name, "Dummy.Client.Name");
230 bstrncpy(bjcr->Job, "bbatch", sizeof(bjcr->Job));
231 bjcr->fileset_name = get_pool_memory(PM_FNAME);
232 pm_strcpy(bjcr->fileset_name, "Dummy.fileset.name");
233 bjcr->fileset_md5 = get_pool_memory(PM_FNAME);
234 pm_strcpy(bjcr->fileset_md5, "Dummy.fileset.md5");
236 if ((db = db_init_database(NULL, NULL, db_name, db_user, db_password,
237 db_host, 0, NULL, false, false)) == NULL) {
238 Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
240 if (!db_open_database(NULL, db)) {
241 Emsg0(M_ERROR_TERM, 0, db_strerror(db));
243 Dmsg0(200, "Database opened\n");
245 Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
250 pthread_create(&thid, NULL, do_batch, bjcr);
260 static void fill_attr(ATTR_DBR *ar, char *data)
265 ar->Stream = STREAM_UNIX_ATTRIBUTES;
266 ar->JobId = getpid();
268 for(p = b = data; *p; p++) {
273 ar->FileIndex = str_to_int64(b);
291 static void *do_batch(void *jcr)
293 JCR *bjcr = (JCR *)jcr;
297 memset(&ar, 0, sizeof(ar));
298 btime_t begin = get_current_btime();
299 char *datafile = bjcr->where;
301 FILE *fd = fopen(datafile, "r");
303 Emsg1(M_ERROR_TERM, 0, _("Error opening datafile %s\n"), datafile);
305 while (fgets(data, sizeof(data)-1, fd)) {
306 strip_trailing_newline(data);
308 if (verbose && ((lineno % 5000) == 1)) {
309 printf("\r%i", lineno);
311 fill_attr(&ar, data);
312 if (!db_create_attributes_record(bjcr, bjcr->db, &ar)) {
313 Emsg0(M_ERROR_TERM, 0, _("Error while inserting file\n"));
317 db_write_batch_file_records(bjcr);
318 btime_t end = get_current_btime();
321 char ed1[200], ed2[200];
322 printf("\rbegin = %s, end = %s\n", edit_int64(begin, ed1),edit_int64(end, ed2));
323 printf("Insert time = %sms\n", edit_int64((end - begin) / 10000, ed1));
324 printf("Create %u files at %.2f/s\n", lineno,
325 (lineno / ((float)((end - begin) / 1000000))));