]> git.sur5r.net Git - bacula/docs/blob - docs/manuals/en/concepts/newfeatures.tex
Update
[bacula/docs] / docs / manuals / en / concepts / newfeatures.tex
1 %%
2 %%
3
4 \chapter{New Features}
5 \label{NewFeaturesChapter}
6 \index[general]{New Features}
7
8 This chapter presents the new features added to the development 2.5.x
9 versions to be released as Bacula version 3.0.0 near the end of 2008.
10
11 \section{Accurate}
12 \index[general]{Accurate Backup}
13
14 As with most other backup programs, Bacula decides what files to backup for
15 Incremental and Differental backup by comparing the change (st\_ctime) and
16 modification (st\_mtime) times of the file to the time the last backup
17 completed.  If one of those two times is different than from last backup time,
18 then the file will be backed up.  This does not, however, permit tracking what
19 files have been deleted and will miss any file with an old time that may have
20 been restored or moved on the client filesystem.
21
22 \subsection{Accurate = \lt{}yes|no\gt{}}
23 If the {\bf Accurate = \lt{}yes|no\gt{}} directive is enabled (default no) in
24 the Job resource, the job will be run as an Accurate Job. For a {\bf Full}
25 backup, there is no difference, but for {\bf Differential} and {\bf
26   Incremental} backups, the Director will send a list of all previous files
27 backed up, and the File daemon will use that list to determine if any new files
28 have been added or or moved and if any files have been deleted. This allows
29 Bacula to make an accurate backup of your system to that point in time so that
30 if you do a restore, it will restore your system exactly.  One note of caution
31 about using Accurate backup is that it requires more resources (CPU and memory)
32 on both the Director and the Client machines to create the list of previous
33 files backed up, to send that list to the File daemon, for the File daemon to
34 keep the list (possibly very big) in memory, and for the File daemon to do
35 comparisons between every file in the FileSet and the list.
36
37
38 \section{Copy Jobs}
39 \index[general]{Copy Jobs}
40 A new {\bf Copy} job type has been implemented. It is essentially
41 identical to the existing Migration feature with the exception that
42 the Job that is copied is left unchanged.  This essentially creates
43 two identical copies of the same backup.  The Copy Job runs without
44 using the File daemon by copying the data from the old backup Volume to
45 a different Volume in a different Pool. See the Migration documentation
46 for additional details.
47
48 \section{Virtual Backup (Vbackup)}
49 \index[general]{Virtual Backup}
50 \index[general]{Vbackup}
51
52 Bacula's virtual backup feature is often called Synthetic Backup or
53 Consolidation in other backup products.  It permits you to consolidate
54 the previous Full backup plus the most recent Differential backup and any
55 subsequent Incremental backups into a new Full backup. This is accomplished
56 without contacting the client by reading the previous backup data and 
57 writing it to a volume in a different pool.  
58
59 In some respects the Vbackup feature works similar to a Migration job, in
60 that Bacula normally reads the data from the pool specified in the 
61 Job resource, and writes it to the {\bf Next Pool} specified in the 
62 Job resource.  The input Storage resource and the Output Storage resource
63 must be different.
64
65 The Vbackup is enabled on a Job by Job in the Job resource by specifying
66 a level of {\bf VirtualFull}.
67
68 A typical Job resource definition might look like the following:
69
70 \begin{verbatim}
71 Job {
72   Name = "MyBackup"
73   Type = Backup
74   Client=localhost-fd
75   FileSet = "Full Set"
76   Storage = File
77   Messages = Standard
78   Pool = Default
79   SpoolData = yes
80 }
81
82 # Default pool definition
83 Pool {
84   Name = Default
85   Pool Type = Backup
86   Recycle = yes            # Automatically recycle Volumes
87   AutoPrune = yes          # Prune expired volumes
88   Volume Retention = 365d  # one year
89   NextPool = Full
90   Storage = File
91 }
92
93 Pool {
94   Name = Full
95   Pool Type = Backup
96   Recycle = yes            # Automatically recycle Volumes
97   AutoPrune = yes          # Prune expired volumes
98   Volume Retention = 365d  # one year
99   Storage = DiskChanger
100 }
101
102 # Definition of file storage device
103 Storage {
104   Name = File
105   Address = localhost
106   Password = "xxx"
107   Device = FileStorage
108   Media Type = File
109   Maximum Concurrent Jobs = 5
110 }
111
112 # Definition of DDS Virtual tape disk storage device
113 Storage {
114   Name = DiskChanger
115   Address = localhost  # N.B. Use a fully qualified name here
116   Password = "yyy"
117   Device = DiskChanger
118   Media Type = DiskChangerMedia
119   Maximum Concurrent Jobs = 4
120   Autochanger = yes
121 }
122 \end{verbatim}
123
124 Then in bconsole or via a Run schedule, you would run the job as:
125
126 \begin{verbatim}
127 run job=MyBackup level=Full
128 run job=MyBackup level=Incremental
129 run job=MyBackup level=Differential
130 run job=MyBackup level=Incremental
131 run job=MyBackup level=Incremental
132 \end{verbatim}
133
134 So providing there were changes between each of those jobs, you would end up
135 with a Full backup, a Differential, which includes the first Incremental
136 backup, then two Incremental backups.  All the above jobs would be written to
137 the {\bf Default} pool.
138
139 To consolidate those backups into a new Full backup, you would run the
140 following:
141
142 \begin{verbatim}
143 run job=MyBackup level=VirtualFull
144 \end{verbatim}
145
146 And it would produce a new Full backup without using the client, and the output
147 would be written to the {\bf Full} Pool which uses the Diskchanger Storage.
148
149 \section{Duplicate Job Control}
150 \index[general]{Duplicate Jobs}
151 The new version of Bacula provides four new directives that
152 give additional control over what Bacula does if duplicate jobs 
153 are started.  A duplicate job in the sense we use it here means
154 a second or subsequent job with the same name starts.  This
155 happens most frequently when the first job runs longer than expected because no 
156 tapes are available.
157
158 The four directives each take as an argument a {\bf yes} or {\bf no} value and
159 are specified in the Job resource.
160
161 They are:
162
163 \subsection{Allow Duplicate Jobs = \lt{}yes|no\gt{}}
164   If this directive is enabled duplicate jobs will be run.  If
165   the directive is set to {\bf no} (default) then only one job of a given name
166   may run at one time, and the action that Bacula takes to ensure only
167   one job runs is determined by the other directives (see below).
168
169 \subsection{Allow Higher Duplicates = \lt{}yes|no\gt{}}
170   If this directive is set to {\bf yes} (default) the job with a higher
171   priority (lower priority number) will be permitted to run.  If the
172   priorities of the two jobs are the same, the outcome is determined by
173   other directives (see below).
174
175 \subsection{Cancel Queued Duplicates = \lt{}yes|no\gt{}}
176   If this directive is set to {\bf yes} (default) any job that is
177   already queued to run but not yet running will be canceled.
178
179 \subsection{Cancel Running Duplicates = \lt{}yes|no\gt{}}
180   If this directive is set to {\bf yes} any job that is already running
181   will be canceled.  The default is {\bf no}.
182
183
184 \section{TLS Authentication}
185 \index[general]{TLS Authentication}
186 In Bacula version 2.5.x and later, in addition to the normal Bacula
187 CRAM-MD5 authentication that is used to authenticate each Bacula
188 connection, you can specify that you want TLS Authentication as well,
189 which will provide more secure authentication.
190
191 This new feature uses Bacula's existing TLS code (normally used for
192 communications encryption) to do authentication.  To use it, you must
193 specify all the TLS directives normally used to enable communications
194 encryption (TLS Enable, TLS Verify Peer, TLS Certificate, ...) and
195 a new directive:
196
197 \subsection{TLS Authenticate = yes}
198 \begin{verbatim}
199 TLS Authenticate = yes
200 \end{verbatim}
201
202 in the main daemon configuration resource (Director for the Director,
203 Client for the File daemon, and Storage for the Storage daemon).
204
205 When {\bf TLS Authenticate} is enabled, after doing the CRAM-MD5
206 authentication, Bacula will do the normal TLS authentication, then TLS 
207 encryption will be turned off.
208
209 If you want to encrypt communications data, do not turn on {\bf TLS
210 Authenticate}.
211
212 \section{bextract non-portable Win32 data}
213 \index[general]{bextract handles Win32 non-portable data}
214 {\bf bextract} has been enhanced to be able to restore
215 non-portable Win32 data to any OS.  Previous versions were 
216 unable to restore non-portable Win32 data to machines that
217 did not have the Win32 BackupRead and BackupWrite API calls.
218
219 \section{State File updated at Job Termination}
220 \index[general]{State File}
221 In previous versions of Bacula, the state file, which provides a
222 summary of previous jobs run in the {\bf status} command output was
223 updated only when Bacula terminated, thus if the daemon crashed, the
224 state file might not contain all the run data.  This version of
225 the Bacula daemons updates the state file on each job termination.
226
227 \section{MaxFullInterval = \lt{}time-interval\gt{}}
228 \index[general]{MaxFullInterval}
229 The new Job resource directive {\bf Max Full Interval = \lt{}time-interval\gt{}}
230 can be used to specify the maximum time interval between {\bf Full} backup
231 jobs. When a job starts, if the time since the last Full backup is
232 greater than the specified interval, and the job would normally be an
233 {\bf Incremental} or {\bf Differential}, it will be automatically
234 upgraded to a {\bf Full} backup.
235
236 \section{MaxDiffInterval = \lt{}time-interval\gt{}}
237 \index[general]{MaxDiffInterval}
238 The new Job resource directive {\bf Max Diff Interval = \lt{}time-interval\gt{}}
239 can be used to specify the maximum time interval between {\bf Differential} backup
240 jobs. When a job starts, if the time since the last Differential backup is
241 greater than the specified interval, and the job would normally be an
242 {\bf Incremental}, it will be automatically
243 upgraded to a {\bf Differential} backup.
244
245 \section{Honor No Dump Flag = \lt{}yes|no\gt{}}
246 \index[general]{MaxDiffInterval}
247 On FreeBSD systems, each file has a {\bf no dump flag} that can be set
248 by the user, and when it is set it is an indication to backup programs
249 to not backup that particular file.  This version of Bacula contains a
250 new Options directive within a FileSet resource, which instructs Bacula to
251 obey this flag.  The new directive is:
252
253 \begin{verbatim}
254   Honor No Dump Flag = yes|no
255 \end{verbatim}
256
257 The default value is {\bf no}.
258
259
260 \section{Ignore Dir = \lt{}filename-string\gt{}}
261 \index[general]{IgnoreDir}
262 The {\bf Ignore Dir = \lt{}filename\gt{}} is a new directive that can be added to the Include
263 section of the FileSet resource.  If the specified
264 filename is found on the Client in any directory to be backed up, 
265 the whole directory will be ignored (not backed up).
266 For example:
267
268 \begin{verbatim}
269   # List of files to be backed up
270   FileSet {
271     Name = "MyFileSet"
272     Include {
273       Options {
274         signature = MD5
275       }
276       File = /home
277       IgnoreDir = .excludeme
278     }
279   }
280 \end{verbatim}
281
282 But in /home, there may be hundreds of directories of users and some
283 people want to indicate that they don't want to have certain
284 directories backed up. For example, with the above FileSet, if
285 the user or sysadmin creates a file named {\bf .excludeme} in 
286 specific directories, such as
287
288 \begin{verbatim}
289    /home/user/www/cache/.excludeme
290    /home/user/temp/.excludeme
291 \end{verbatim}
292
293 then Bacula will not backup the two directories named:
294
295 \begin{verbatim}
296    /home/user/www/cache
297    /home/user/temp
298 \end{verbatim}
299
300 NOTE: subdirectories will not be backed up.  That is, the directive
301 applies to the two directories in question and any children (be they
302 files, directories, etc).
303
304
305
306 \section{Bacula Plugins}
307 \index[general]{Plugin}
308 Support for shared object plugins has been implemented in the Linux
309 (and Unix) File daemon. The API will be documented separately in
310 the Developer's Guide or in a new document.  For the moment, there is
311 a single plugin named {\bf bpipe} that allows an external program to
312 get control to backup and restore a file.
313
314 Plugins are also planned (partially implemented) in the Director and the
315 Storage daemon.  The code is also implemented to work on Win32 machines, 
316 but it has not yet been tested.
317
318 \subsection{Plugin Directory}
319 Each daemon (DIR, FD, SD) has a new {\bf Plugin Directory} directive that may
320 be added to the daemon definition resource. The directory takes a quoted 
321 string argument, which is the name of the directory in which the daemon can
322 find the Bacula plugins. If this directive is not specified, Bacula will not
323 load any plugins. Since each plugin has a distinctive name, all the daemons
324 can share the same plugin directory. 
325
326
327
328 \subsection{Plugin Options}
329 The {\bf Plugin Options} directive takes a quoted string
330 arguement (after the equal sign) and may be specified in the
331 Job resource.  The options specified will be passed to the plugin
332 when it is run.  The value defined in the Job resource can be modified
333 by the user when he runs a Job via the {\bf bconsole} command line 
334 prompts.
335
336 Note: this directive may be specified, but it is not yet passed to
337 the plugin (i.e. not fully implemented).
338
339 \subsection{Plugin Options ACL}
340 The {\bf Plugin Options ACL} directive may be specified in the
341 Director's Console resource. It functions as all the other ACL commands
342 do by permitting users running restricted consoles to specify a 
343 {\bf Plugin Options} that overrides the one specified in the Job
344 definition. Without this directive restricted consoles may not modify
345 the Plugin Options.
346
347 \subsection{Plugin = \lt{}plugin-command-string\gt{}}
348 The {\bf Plugin} directive is specified in the Include section of
349 a FileSet resource where you put your {\bf File = xxx} directives.
350 For example:
351
352 \begin{verbatim}
353   FileSet {
354     Name = "MyFileSet"
355     Include {
356       Options {
357         signature = MD5
358       }
359       File = /home
360       Plugin = "bpipe:..."
361     }
362   }
363 \end{verbatim}
364
365 In the above example, when the File daemon is processing the directives
366 in the Include section, it will first backup all the files in {\bf /home}
367 then it will load the plugin named {\bf bpipe} (actually bpipe-dir.so) from
368 the Plugin Directory.  The syntax and semantics of the Plugin directive
369 require the first part of the string up to the colon (:) to be the name
370 of the plugin. Everything after the first colon is ignored by the File daemon but
371 is passed to the plugin. Thus the plugin writer may define the meaning of the
372 rest of the string as he wishes.
373
374 Please see the next section for information about the {\bf bpipe} Bacula
375 plugin.
376
377 \section{The bpipe Plugin}
378 The {\bf bpipe} plugin is provided in the directory src/plugins/fd/bpipe-fd.c of
379 the Bacula source distribution. When the plugin is compiled and linking into
380 the resulting dynamic shared object (DSO), it will have the name {\bf bpipe-fd.so}.
381
382 The purpose of the plugin is to provide an interface to any system program for
383 backup and restore. As specified above the {\bf bpipe} plugin is specified in
384 the Include section of your Job's FileSet resource.  The full syntax of the
385 plugin directive as interpreted by the {\bf bpipe} plugin (each plugin is free
386 to specify the sytax as it wishes) is:
387
388 \begin{verbatim}
389   Plugin = "<field1>:<field2>:<field3>:<field4>"
390 \end{verbatim}
391
392 where
393 \begin{description}
394 \item {\bf field1} is the name of the plugin with the trailing {\bf -fd.so}
395 stripped off, so in this case, we would put {\bf bpipe} in this field.
396
397 \item {\bf field2} specifies the namespace, which for {\bf bpipe} is the
398 pseudo path and filename under which the backup will be saved. This pseudo
399 path and filename will be seen by the user in the restore file tree.
400 For example, if the value is {\bf /MYSQL/regress.sql}, the data
401 backed up by the plugin will be put under that "pseudo" path and filename.
402 You must be careful to choose a naming convention that is unique to avoid
403 a conflict with a path and filename that actually exists on your system.
404
405 \item {\bf field3} for the {\bf bpipe} plugin 
406 specifies the "reader" program that is called by the plugin during
407 backup to read the data. {\bf bpipe} will call this program by doing a
408 {\bf popen} on it.
409
410 \item {\bf field4} for the {\bf bpipe} plugin
411 specifies the "writer" program that is called by the plugin during
412 restore to write the data back to the filesystem.  
413 \end{description}
414
415 Putting it all together, the full plugin directive line might look
416 like the following:
417
418 \begin{verbatim}
419 Plugin = "bpipe:/MYSQL/regress.sql:mysqldump -f 
420           --opt --databases bacula:mysql"
421 \end{verbatim}
422
423 The directive has been split into two lines, but within the {\bf bacula-dir.conf} file
424 would be written on a single line.
425
426 This causes the File daemon to call the {\bf bpipe} plugin, which will write
427 its data into the "pseudo" file {\bf /MYSQL/regress.sql} by calling the 
428 program {\bf mysqldump -f --opt --database bacula} to read the data during
429 backup. The mysqldump command outputs all the data for the database named
430 {\bf bacula}, which will be read by the plugin and stored in the backup.
431 During restore, the data that was backed up will be sent to the program
432 specified in the last field, which in this case is {\bf mysql}.  When
433 {\bf mysql} is called, it will read the data sent to it by the plugn
434 then write it back to the same database from which it came ({\bf bacula}
435 in this case).
436
437 The {\bf bpipe} plugin is a generic pipe program, that simply transmits 
438 the data from a specified program to Bacula for backup, and then from Bacula to 
439 a specified program for restore.
440
441 By using different command lines to {\bf bpipe},
442 you can backup any kind of data (ASCII or binary) depending
443 on the program called.
444
445 \section{Microsoft Exchange Server 2003/2007 Plugin}
446
447 \subsection{Concepts}
448
449 Microsoft Exchange organises its storage into Storage Groups with
450 Databases inside them. A default installation of Exchange will have a
451 single Storage Group called 'First Storage Group', with two Databases
452 inside it, "Mailbox Store (SERVER NAME)" and 
453 "Public Folder Store (SERVER NAME)", 
454 which hold user email and public folders respectively.
455
456 In the default configuration, Exchange logs everything that happens to
457 log files, such that if you have a backup, and all the log files since,
458 you can restore to the present time. Each Storage Group has its own set
459 of log files and operates independently of any other Storage Groups. At
460 the Storage Group level, the logging can be turned off by enabling a
461 function called "Enable circular logging". At this time the Exchange
462 plugin will not function if this option is enabled.
463
464 The plugin allows backing up of entire storage groups, and the restoring
465 of entire storage groups or individual databases. Backing up and
466 restoring at the individual mailbox or email item is not supported but
467 can be simulated by use of the Recovery Storage Group (see below).
468
469 \subsection{Installing}
470
471 The Exchange plugin requires a DLL that is shipped with Microsoft
472 Exchanger Server called {\bf esebcli2.dll}. Assuming Exchange is installed
473 correctly the Exchange plugin should find this automatically and run
474 without any additional installation.
475
476 If the DLL can not be found automatically it will need to be copied into
477 the Bacula installation
478 directory (eg C:\verb+\+Program Files\verb+\+Bacula\verb+\+bin). The Exchange API DLL is
479 named esebcli2.dll and is found in C:\verb+\+Program Files\verb+\+Exchsrvr\verb+\+bin on a
480 default Exchange installation.
481
482 \subsection{Backup up}
483
484 To back up an Exchange server the Fileset definition must contain at
485 least 'Plugin = "exchange:/@EXCHANGE/Microsoft Information Store"' for
486 the backup to work correctly. The 'exchange:' bit tells Bacula to look
487 for the exchange plugin, the '@EXCHANGE' bit makes sure all the backed
488 up files are prefixed with something that isn't going to share a name
489 with something outside the plugin, and the 'Microsoft Information Store'
490 bit is required also. It is also possible to add the name of a storage
491 group to the 'Plugin =' line, eg 'Plugin =
492 "exchange:/@EXCHANGE/Microsoft Information Store/First Storage Group"'
493 if you want only a single storage group backed up.
494
495 Additionally, you can suffix the 'Plugin =' directive with
496 ':notrunconfull' which will tell the plugin not to truncate the Exchange
497 database at the end of a full backup.
498
499 An Incremental or Differential backup will backup only the database logs
500 for each Storage Group by inspecting the 'modified date' on each
501 physical log file. Because of the way the Exchange API works, the last
502 logfile backed up on each backup will always be backed up by the next
503 Incremental or Differential backup too. This adds 5MB to each
504 Incremental or Differential backup size but otherwise does not cause any
505 problems.
506
507 \subsection{Restoring}
508
509 The restore operation is much the same as a normal Bacula restore, with
510 the following provisos:
511
512 \begin{itemize}
513 \item  The 'Where' restore option must not be specified
514 \item Each Database directory must be marked as a whole. You cannot just
515      select (say) the .edb file and not the others.
516 \item If a Storage Group is restored, the directory of the Storage Group
517      must be marked too.
518 \item  It is possible to restore only a subset of the available log files,
519      but they {\bf must} be contiguous. Exchange will fail to restore correctly
520      if a log file is missing from the sequence of log files
521 \item Each database to be restored must be dismounted and marked as 'Can be
522     overwritten by restore'
523 \item If an entire Storage Group is to be restored (eg all databases and
524    logs in the Storage Group), then it is best to manually delete the
525    database files from the server (eg C:\verb+\+Program Files\verb+\+Exchsrvr\verb+\+mdbdata\verb+\+*)
526    as Exchange can get confused by stray log files lying around.
527 \end{itemize}
528
529 \subsection{Restoring to the Recovery Storage Group}
530
531 The concept of the Recovery Storage Group is well documented by
532 Microsoft 
533 \elink{http://support.microsoft.com/kb/824126}{http://support.microsoft.com/kb/824126}, 
534 but to briefly summarise...
535
536 Microsoft Exchange allows the creation of an additional Storage Group
537 called the Recovery Storage Group, which is used to restore an older
538 copy of a database (eg before a mailbox was deleted) into without
539 messing with the current live data. This is required as the Standard and
540 Small Business Server versions of Exchange can not ordinarily have more
541 than one Storage Group.
542
543 To create the Recovery Storage Group, drill down to the Server in
544 Exchange System Manager, right click, and select 
545 {\bf "New -> Recovery Storage Group..."}. Accept or change the file locations and click OK. On
546 the Recovery Storage Group, right click and select 
547 {\bf "Add Database to Recover..."} and select the database you will be restoring.
548
549 In Bacula, select the Database and the log files, making sure to mark
550 the Storage Group directory itself too. Once you have selected the files
551 to back up, use the RegexWhere clause to remove the prefix of
552 "/@EXCHANGE/Microsoft Information Store/\lt{}storage group name\gt{}/" and
553 replace it with "/@EXCHANGE/Microsoft Information Store/Recovery Storage Group/". 
554 Then run the restore.
555
556 \subsection{Caveats}
557
558 This plugin is still being developed, so you should consider it
559 currently in BETA test, and thus use in a production environment
560 should be done only after very careful testing.
561
562 The "Enable Circular Logging" option cannot be enabled or the plugin
563 will fail.
564
565 Exchange insists that a successful Full backup must have taken place if
566 an Incremental or Differential backup is desired, and the plugin will
567 fail if this is not the case. If a restore is done, Exchange will
568 require that a Full backup be done before an Incremental or Differential
569 backup is done.
570
571 The plugin will most likely not work well if another backup application
572 (eg NTBACKUP) is backing up the Exchange database, especially if the
573 other backup application is truncating the log files.
574
575 The Exchange plugin has not been tested with the {\bf Accurate} option, so
576 we recommend either carefully testing or that you avoid this option for
577 the current time.
578
579 The Exchange plugin is not called during processing the bconsole {\bf estimate} command,
580 and so anything that would be backed up by the plugin will not be added
581 to the estimate total that is displayed.
582
583
584 \section{libdbi Framework}
585 As a general guideline, Bacula has support for a few catalog database drivers
586 (MySQL, PostgreSQL, SQLite)
587 coded natively by the Bacula team.  With the libdbi implementation, which is a
588 Bacula driver that uses libdbi to access the catalog, we have an open field to
589 use many different kinds database engines following the needs of users.
590
591 The according to libdbi (http://libdbi.sourceforge.net/) project: libdbi
592 implements a database-independent abstraction layer in C, similar to the
593 DBI/DBD layer in Perl. Writing one generic set of code, programmers can
594 leverage the power of multiple databases and multiple simultaneous database
595 connections by using this framework.
596
597 Currently the libdbi driver in Bacula project only supports the same drivers
598 natively coded in Bacula.  However the libdbi project has support for many
599 others database engines. You can view the list at
600 http://libdbi-drivers.sourceforge.net/. In the future all those drivers can be
601 supported by Bacula, however, they must be tested properly by the Bacula team.
602
603 Some of benefits of using libdbi are:
604 \begin{itemize}
605 \item The possibility to use proprietary databases engines in which your
606   proprietary licenses prevent the Bacula team from developing the driver.
607  \item The possibility to use the drivers written for the libdbi project.
608  \item The possibility to use other database engines without recompiling Bacula
609    to use them.  Just change one line in bacula-dir.conf
610  \item Abstract Database access, this is, unique point to code and profiling
611    catalog database access.
612  \end{itemize}
613  
614  The following drivers have been tested:
615  \begin{itemize}
616  \item PostgreSQL, with and without batch insert
617  \item Mysql, with and without batch insert
618  \item SQLite
619  \item SQLite3
620  \end{itemize}
621
622  In the future, we will test and approve to use others databases engines
623  (proprietary or not) like DB2, Oracle, Microsoft SQL.
624
625  To compile Bacula to support libdbi we need to configure the code with the
626  --with-dbi and --with-dbi-driver=[database] ./configure options, where
627  [database] is the database engine to be used with Bacula (of course we can
628  change the driver in file bacula-dir.conf, see below).  We must configure the
629  access port of the database engine with the option --with-db-port, because the
630  libdbi framework doesn't know the default access port of each database.
631
632 The next phase is checking (or configuring) the bacula-dir.conf, example:
633 \begin{verbatim}
634 Catalog {
635   Name = MyCatalog
636   dbdriver = dbi:mysql; dbaddress = 127.0.0.1; dbport = 3306
637   dbname = regress; user = regress; password = ""
638 }
639 \end{verbatim}
640
641 The parameter {\bf dbdriver} indicates that we will use the driver dbi with a
642 mysql database.  Currently the drivers supported by Bacula are: postgresql,
643 mysql, sqlite, sqlite3; these are the names that may be added to string "dbi:".
644
645 The following limitations apply when Bacula is set to use the libdbi framework:
646  - Not tested on the Win32 platform
647  - A little performance is lost if comparing with native database driver. 
648    The reason is bound with the database driver provided by libdbi and the 
649    simple fact that one more layer of code was added.
650
651 It is important to remember, when compiling Bacula with libdbi, the
652 following packages are needed:
653  \begin{itemize}
654   \item libdbi version 1.0.0, http://libdbi.sourceforge.net/
655   \item libdbi-drivers 1.0.0, http://libdbi-drivers.sourceforge.net/
656  \end{itemize}
657  
658  You can download them and compile them on your system or install the packages
659  from your OS distribution.
660
661
662 \section{Display Autochanger Content}
663 \index[general]{StatusSlots}
664
665 The {\bf status slots storage=\lt{}storage-name\gt{}} command displays autochanger content.
666
667 \footnotesize
668 \begin{verbatim}
669  Slot |  Volume Name  |  Status  |  Media Type       |   Pool     |
670 ------+---------------+----------+-------------------+------------|
671     1 |         00001 |   Append |  DiskChangerMedia |    Default |
672     2 |         00002 |   Append |  DiskChangerMedia |    Default |
673     3*|         00003 |   Append |  DiskChangerMedia |    Scratch |
674     4 |               |          |                   |            |
675 \end{verbatim}
676 \normalsize
677
678 If you an asterisk ({\bf *}) appears after the slot number, you must run an
679 {\bf update slots} command to synchronize autochanger content with your
680 catalog.
681
682 \section{Miscellaneous}
683 \index[general]{Misc New Features}
684
685 \subsection{Allow Mixed Priority = \lt{}yes|no\gt{}}
686    This directive is only implemented in version 2.5 and later.  When
687    set to {\bf yes} (default {\bf no}), this job may run even if lower
688    priority jobs are already running.  This means a high priority job
689    will not have to wait for other jobs to finish before starting.
690    The scheduler will only mix priorities when all running jobs have
691    this set to true.
692
693    Note that only higher priority jobs will start early.  Suppose the
694    director will allow two concurrent jobs, and that two jobs with
695    priority 10 are running, with two more in the queue.  If a job with
696    priority 5 is added to the queue, it will be run as soon as one of
697    the running jobs finishes.  However, new priority 10 jobs will not
698    be run until the priority 5 job has finished.
699
700 \subsection{Bootstrap File Directive -- FileRegex}
701   {\bf FileRegex} is a new command that can be added to the bootstrap
702   (.bsr) file.  The value is a regular expression.  When specified, only
703   matching filenames will be restored.
704
705   During a restore, if all File records are pruned from the catalog
706   for a Job, normally Bacula can restore only all files saved. That
707   is there is no way using the catalog to select individual files.
708   With this new command, Bacula will ask if you want to specify a Regex
709   expression for extracting only a part of the full backup.
710
711
712 \subsection{Virtual Tape Emulation}
713 We now have a Virtual Tape emulator that allows us to run though 99.9\% of
714 the tape code but actually reading and writing to a disk file. Used with the
715 \textbf{disk-changer} script, you can now emulate an autochanger with 10 drives
716 and 700 slots. This feature is most useful in testing.  It is enabled
717 by using {\bf Device Type = vtape} in the Storage daemon's Device
718 directive. This feature is only implemented on Linux machines.
719
720 \subsection{Bat Enhancements}
721 Bat (the Bacula Administration Tool) GUI program has been significantly
722 enhanced and stabilized. In particular, there are new table based status 
723 commands; it can now be easily localized using Qt4 Linguist.
724
725 The Bat communications protocol has been significantly enhanced to improve
726 GUI handling.
727
728 \subsection{RunScript Enhancements}
729 The {\bf RunScript} resource has been enhanced to permit multiple
730 commands per RunScript.  Simply specify multiple {\bf Command} directives
731 in your RunScript.
732
733 \begin{verbatim}
734 Job {
735   Name = aJob
736   RunScript {
737     Command = "/bin/echo test"
738     Command = "/bin/echo an other test"
739     Command = "/bin/echo 3 commands in the same runscript"
740     RunsWhen = Before
741   }
742  ...
743 }
744 \end{verbatim}
745
746 A new Client RunScript {\bf RunsWhen} keyword of {\bf AfterVSS} has been implemented, which
747 runs the command after the Volume Shadow Copy has been made.
748
749 Console commands can be specified within a RunScript by using:
750 {\bf Console = \lt{}command\gt{}}, however, this command has not been 
751 carefully tested and debugged and is known to easily crash the Director.
752 We would appreciate feedback.  Due to the recursive nature of this command, we
753 may remove it before the final release.
754
755 \subsection{Status Enhancements}
756 The bconsole {\bf status dir} output has been enhanced to indicate
757 Storage daemon job spooling and despooling activity.
758
759 \subsection{Connect Timeout}
760 The default connect timeout to the File
761 daemon has been set to 3 minutes. Previously it was 30 minutes.
762
763 \subsection{ftruncate for NFS Volumes}
764 If you write to a Volume mounted by NFS (say on a local file server),
765 in previous Bacula versions, when the Volume was recycled, it was not
766 properly truncated because NFS does not implement ftruncate (file 
767 truncate). This is now corrected in the new version because we have
768 written code (actually a kind user) that deletes and recreates the Volume,
769 thus accomplishing the same thing as a truncate.
770
771 \subsection{Support for Ubuntu}
772 The new version of Bacula now recognizes the Ubuntu (and Kubuntu)
773 version of Linux, and thus now provides correct autostart routines.
774 Since Ubuntu officially supports Bacula, you can also obtain any
775 recent release of Bacula from the Ubuntu repositories.
776
777 \subsection{Recycle Pool = \lt{}pool-name\gt{}}
778 The new \textbf{RecyclePool} directive defines to which pool the Volume will
779 be placed (moved) when it is recycled. Without this directive, a Volume will
780 remain in the same pool when it is recycled. With this directive, it can be
781 moved automatically to any existing pool during a recycle. This directive is
782 probably most useful when defined in the Scratch pool, so that volumes will
783 be recycled back into the Scratch pool.
784
785 \subsection{FD Version}
786 The File daemon to Director protocol now includes a version 
787 number, which although there is no visible change for users, 
788 will help us in future versions automatically determine
789 if a File daemon is not compatible.
790
791 \subsection{Max Run Sched Time = \lt{}time-period-in-seconds\gt{}}
792 The time specifies the maximum allowed time that a job may run, counted from
793 when the job was scheduled. This can be useful to prevent jobs from running
794 during working hours. We can see it like \texttt{Max Start Delay + Max Run
795   Time}.
796
797 \subsection{Max Wait Time = \lt{}time-period-in-seconds\gt{}}
798
799 Previous \textbf{MaxWaitTime} directives aren't working as expected, instead
800 of checking the maximum allowed time that a job may block for a resource,
801 those directives worked like \textbf{MaxRunTime}. Some users are reporting to
802 use \textbf{Incr/Diff/Full Max Wait Time} to control the maximum run time of
803 their job depending on the level. Now, they have to use
804 \textbf{Incr/Diff/Full Max Run Time}.  \textbf{Incr/Diff/Full Max Wait Time}
805 directives are now deprecated.
806
807 \subsection{Incremental|Differential Max Wait Time = \lt{}time-period-in-seconds\gt{}} 
808 Theses directives have been deprecated in favor of
809 \texttt{Incremental|Differential Max Run Time}.
810
811 \subsection{Max Run Time directives}
812 Using \textbf{Full/Diff/Incr Max Run Time}, it's now possible to specify the
813 maximum allowed time that a job can run depending on the level.
814
815 \addcontentsline{lof}{figure}{Job time control directives}
816 \includegraphics{\idir different_time.eps}
817
818 \subsection{Statistics Enhancements}
819 If you (or probably your boss) want to have statistics on your backups to
820 provide some \textit{Service Level Agreement} indicators, you could use a few
821 SQL queries on the Job table to report how many:
822
823 \begin{itemize}
824 \item jobs have run
825 \item jobs have been successful
826 \item files have been backed up
827 \item ...
828 \end{itemize}
829
830 However, these statistics are accurate only if your job retention is greater
831 than your statistics period. Ie, if jobs are purged from the catalog, you won't
832 be able to use them. 
833
834 Now, you can use the \textbf{update stats [days=num]} console command to fill
835 the JobStat table with new Job records. If you want to be sure to take in
836 account only \textbf{good jobs}, ie if one of your important job has failed but
837 you have fixed the problem and restarted it on time, you probably want to
838 delete the first \textit{bad} job record and keep only the successful one. For
839 that simply let your staff do the job, and update JobStat table after two or
840 three days depending on your organization using the \textbf{[days=num]} option.
841
842 These statistics records aren't used for restoring, but mainly for
843 capacity planning, billings, etc.
844
845 The Bweb interface provides a statistics module that can use this feature. You
846 can also use tools like Talend or extract information by yourself.
847
848 The {\textbf Statistics Retention = \lt{}time\gt{}} director directive defines
849 the length of time that Bacula will keep statistics job records in the Catalog
850 database after the Job End time. (In \texttt{JobStat} table) When this time
851 period expires, and if user runs \texttt{prune stats} command, Bacula will
852 prune (remove) Job records that are older than the specified period.
853
854 You can use the following Job resource in your nightly \textbf{BackupCatalog}
855 job to maintain statistics.
856 \begin{verbatim}
857 Job {
858   Name = BackupCatalog
859   ...
860   RunScript {
861     Console = "update stats days=3"
862     Console = "prune stats yes"
863     RunsWhen = After
864     RunsOnClient = no
865   }
866 }
867 \end{verbatim}
868
869 \subsection{SpoolSize = \lt{}size-specification-in-bytes\gt{}}
870 A new job directive permits to specify the spool size per job. This is used
871 in advanced job tunning. {\bf SpoolSize={\it bytes}}
872
873
874 \section{Building Bacula Plugins}
875 There is currently one sample program {\bf example-plugin-fd.c} and
876 one working plugin {\bf bpipe-fd.c} that can be found in the Bacula
877 {\bf src/plugins/fd} directory.  Both are built with the following:
878
879 \begin{verbatim}
880  cd <bacula-source>
881  ./configure <your-options>
882  make
883  ...
884  cd src/plugins/fd
885  make
886  make test
887 \end{verbatim}
888
889 After building Bacula and changing into the src/plugins/fd directory,
890 the {\bf make} command will build the {\bf bpipe-fd.so} plugin, which 
891 is a very useful and working program.
892
893 The {\bf make test} command will build the {\bf example-plugin-fd.so}
894 plugin and a binary named {\bf main}, which is build from the source
895 code located in {\bf src/filed/fd\_plugins.c}. 
896
897 If you execute {\bf ./main}, it will load and run the example-plugin-fd
898 plugin simulating a small number of the calling sequences that Bacula uses
899 in calling a real plugin.  This allows you to do initial testing of 
900 your plugin prior to trying it with Bacula.
901
902 You can get a good idea of how to write your own plugin by first 
903 studying the example-plugin-fd, and actually running it.  Then
904 it can also be instructive to read the bpipe-fd.c code as it is 
905 a real plugin, which is still rather simple and small.
906
907 When actually writing your own plugin, you may use the example-plugin-fd.c
908 code as a template for your code.
909
910
911 %%
912 %%
913
914 \chapter{Bacula FD Plugin API}
915 To write a Bacula plugin, you create a dynamic shared object
916 program (or dll on Win32) with a particular name and two 
917 exported entry points, place it in the {\bf Plugins Directory}, which is defined in the
918 {\bf bacula-fd.conf} file in the {\bf Client} resource, and when the FD
919 starts, it will load all the plugins that end with {\bf -fd.so} (or {\bf -fd.dll}
920 on Win32) found in that directory.
921
922 \section{Normal vs Command Plugins}
923 In general, there are two ways that plugins are called. The first way, 
924 is when a particular event is detected in Bacula, it will transfer control
925 to each plugin that is loaded in turn informing the plugin of the event. 
926 This is very similar to how a {\bf RunScript} works, and the events are very similar.
927 Once the plugin gets control, it can interact with Bacula by getting and
928 setting Bacula variables.  In this way, it behaves much like a RunScript.
929 Currently very few Bacula variables are defined, but they will be implemented
930 as the need arrises, and it is very extensible.
931
932 We plan to have plugins register to receive events that they normally would
933 not receive, such as an event for each file examined for backup or restore.
934 This feature is not yet implemented.
935
936 The second type of plugin, which is more useful and fully implemented
937 in the current version is what we call a command plugin.  As with all
938 plugins, it gets notified of important events as noted above (details described below),
939 but in addition, this kind of plugin can accept a command line, which
940 is a:
941
942 \begin{verbatim}
943    Plugin = <command-string>
944 \end{verbatim}
945
946 directive that is placed in the Include section of a FileSet and is very
947 similar to the "File = " directive.  When this Plugin directive is encountered 
948 by Bacula during backup, it passes the "command" part of the Plugin directive
949 only to the plugin that is explicitly named in the first field of that command string.
950 This allows that plugin to backup any file or files on the system that it wants. It can
951 even create "virtual files" in the catalog that contain data to be restored but do
952 not necessarily correspond to actual files on the filesystem.
953
954 The important features of the command plugin entry points are:
955 \begin{itemize}
956  \item It is triggered by a "Plugin =" directive in the FileSet
957  \item Only a single plugin is called that is named on the "Plugin =" directive.
958  \item The full command string after the "Plugin =" is passed to the plugin
959     so that it can be told what to backup/restore.
960 \end{itemize}
961
962
963 \section{Loading Plugins}
964 Once the File daemon loads the plugins, it asks the OS for the
965 two entry points (loadPlugin and unloadPlugin) then calls the
966 {\bf loadPlugin} entry point (see below).
967
968 Bacula passes information to the plugin through this call and it gets
969 back information that it needs to use the plugin.  Later, Bacula
970  will call particular functions that are defined by the
971 {\bf loadPlugin} interface.  
972
973 When Bacula is finished with the plugin 
974 (when Bacula is going to exit), it will call the {\bf unloadPlugin}
975 entry point.
976
977 The two entry points are:
978
979 \begin{verbatim}
980 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
981
982 and
983
984 bRC unloadPlugin()
985 \end{verbatim}
986
987 both these external entry points to the shared object are defined as C entry points
988 to avoid name mangling complications with C++.  However, the shared object
989 can actually be written in any language (preferrably C or C++) providing that it
990 follows C language calling conventions.
991
992 The definitions for {\bf bRC} and the arguments are {\bf
993 src/filed/fd-plugins.h} and so this header file needs to be included in
994 your plugin.  It along with {\bf src/lib/plugins.h} define basically the whole
995 plugin interface.  Within this header file, it includes the following
996 files:
997
998 \begin{verbatim}
999 #include <sys/types.h>
1000 #include "config.h"
1001 #include "bc_types.h"
1002 #include "lib/plugins.h"
1003 #include <sys/stat.h>
1004 \end{verbatim}
1005
1006 Aside from the {\bf bc\_types.h} and {\bf confit.h} headers, the plugin definition uses the
1007 minimum code from Bacula.  The bc\_types.h file is required to ensure that
1008 the data type defintions in arguments correspond to the Bacula core code.
1009
1010 The return codes are defined as:
1011 \begin{verbatim}
1012 typedef enum {
1013   bRC_OK    = 0,                         /* OK */
1014   bRC_Stop  = 1,                         /* Stop calling other plugins */
1015   bRC_Error = 2,                         /* Some kind of error */
1016   bRC_More  = 3,                         /* More files to backup */
1017 } bRC;
1018 \end{verbatim}
1019
1020
1021 At a future point in time, we hope to make the Bacula libbac.a into a
1022 shared object so that the plugin can use much more of Bacula's
1023 infrastructure, but for this first cut, we have tried to minimize the
1024 dependence on Bacula.
1025
1026 \section{loadPlugin}
1027 As previously mentioned, the {\bf loadPlugin} entry point in the plugin
1028 is called immediately after Bacula loads the plugin when the File daemon
1029 itself is first starting.  This entry point is only called once during the
1030 execution of the File daemon.  In calling the
1031 plugin, the first two arguments are information from Bacula that
1032 is passed to the plugin, and the last two arguments are information
1033 about the plugin that the plugin must return to Bacula.  The call is:
1034
1035 \begin{verbatim}
1036 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
1037 \end{verbatim}
1038
1039 and the arguments are:
1040
1041 \begin{description}
1042 \item [lbinfo]
1043 This is information about Bacula in general. Currently, the only value
1044 defined in the bInfo structure is the version, which is the Bacula plugin 
1045 interface version, currently defined as 1.  The {\bf size} is set to the
1046 byte size of the structure. The exact definition of the bInfo structure
1047 as of this writing is: 
1048
1049 \begin{verbatim}
1050 typedef struct s_baculaInfo {
1051    uint32_t size;
1052    uint32_t version;
1053 } bInfo;
1054 \end{verbatim}
1055
1056 \item [lbfuncs]
1057 The bFuncs structure defines the callback entry points within Bacula
1058 that the plugin can use register events, get Bacula values, set
1059 Bacula values, and send messages to the Job output or debug output.
1060
1061 The exact definition as of this writing is:
1062 \begin{verbatim}
1063 typedef struct s_baculaFuncs {
1064    uint32_t size;
1065    uint32_t version;
1066    bRC (*registerBaculaEvents)(bpContext *ctx, ...);
1067    bRC (*getBaculaValue)(bpContext *ctx, bVariable var, void *value);
1068    bRC (*setBaculaValue)(bpContext *ctx, bVariable var, void *value);
1069    bRC (*JobMessage)(bpContext *ctx, const char *file, int line,
1070        int type, time_t mtime, const char *fmt, ...);
1071    bRC (*DebugMessage)(bpContext *ctx, const char *file, int line,
1072        int level, const char *fmt, ...);
1073    void *(*malloc)(bpContext *ctx, const char *file, int line,
1074        size_t size);
1075    void (*free)(bpContext *ctx, const char *file, int line, void *mem);
1076 } bFuncs;
1077 \end{verbatim}
1078
1079 We will discuss these entry points and how to use them a bit later when
1080 describing the plugin code.
1081
1082
1083 \item [pInfo]
1084 When the loadPlugin entry point is called, the plugin must initialize
1085 an information structure about the plugin and return a pointer to
1086 this structure to Bacula.
1087
1088 The exact definition as of this writing is:
1089
1090 \begin{verbatim}
1091 typedef struct s_pluginInfo {
1092    uint32_t size;
1093    uint32_t version;
1094    const char *plugin_magic;
1095    const char *plugin_license;
1096    const char *plugin_author;
1097    const char *plugin_date;
1098    const char *plugin_version;
1099    const char *plugin_description;
1100 } pInfo;
1101 \end{verbatim}
1102
1103 Where:
1104  \begin{description}
1105  \item [version] is the current Bacula defined plugin interface version, currently
1106    set to 1. If the interface version differs from the current version of 
1107    Bacula, the plugin will not be run (not yet implemented).
1108  \item [plugin\_magic] is a pointer to the text string "*FDPluginData*", a
1109    sort of sanity check.  If this value is not specified, the plugin
1110    will not be run (not yet implemented).
1111  \item [plugin\_license] is a pointer to a text string that describes the
1112    plugin license. Bacula will only accept compatible licenses (not yet
1113    implemented).
1114  \item [plugin\_author] is a pointer to the text name of the author of the program.
1115    This string can be anything but is generally the author's name.
1116  \item [plugin\_date] is the pointer text string containing the date of the plugin.
1117    This string can be anything but is generally some human readable form of 
1118    the date.
1119  \item [plugin\_version] is a pointer to a text string containing the version of
1120    the plugin.  The contents are determined by the plugin writer.
1121  \item [plugin\_description] is a pointer to a string describing what the
1122    plugin does. The contents are determined by the plugin writer.
1123  \end{description}
1124
1125 The pInfo structure must be defined in static memory because Bacula does not
1126 copy it and may refer to the values at any time while the plugin is
1127 loaded. All values must be supplied or the plugin will not run (not yet
1128 implemented).  All text strings must be either ASCII or UTF-8 strings that
1129 are terminated with a zero byte.
1130
1131 \item [pFuncs]
1132 When the loadPlugin entry point is called, the plugin must initialize
1133 an entry point structure about the plugin and return a pointer to
1134 this structure to Bacula. This structure contains pointer to each
1135 of the entry points that the plugin must provide for Bacula. When
1136 Bacula is actually running the plugin, it will call the defined
1137 entry points at particular times.  All entry points must be defined.
1138
1139 The pFuncs structure must be defined in static memory because Bacula does not
1140 copy it and may refer to the values at any time while the plugin is
1141 loaded.
1142
1143 The exact definition as of this writing is:
1144
1145 \begin{verbatim}
1146 typedef struct s_pluginFuncs {
1147    uint32_t size;
1148    uint32_t version;
1149    bRC (*newPlugin)(bpContext *ctx);
1150    bRC (*freePlugin)(bpContext *ctx);
1151    bRC (*getPluginValue)(bpContext *ctx, pVariable var, void *value);
1152    bRC (*setPluginValue)(bpContext *ctx, pVariable var, void *value);
1153    bRC (*handlePluginEvent)(bpContext *ctx, bEvent *event, void *value);
1154    bRC (*startBackupFile)(bpContext *ctx, struct save_pkt *sp);
1155    bRC (*endBackupFile)(bpContext *ctx);
1156    bRC (*startRestoreFile)(bpContext *ctx, const char *cmd);
1157    bRC (*endRestoreFile)(bpContext *ctx);
1158    bRC (*pluginIO)(bpContext *ctx, struct io_pkt *io);
1159    bRC (*createFile)(bpContext *ctx, struct restore_pkt *rp);
1160    bRC (*setFileAttributes)(bpContext *ctx, struct restore_pkt *rp);
1161 } pFuncs;
1162 \end{verbatim}
1163
1164 The details of the entry points will be presented in
1165 separate sections below. 
1166
1167 Where:
1168  \begin{description}
1169  \item [size] is the byte size of the structure.
1170  \item [version] is the plugin interface version currently set to 1.
1171  \end{description}
1172
1173 Sample code for loadPlugin:
1174 \begin{verbatim}
1175   bfuncs = lbfuncs;                  /* set Bacula funct pointers */
1176   binfo  = lbinfo;
1177   *pinfo  = &pluginInfo;             /* return pointer to our info */
1178   *pfuncs = &pluginFuncs;            /* return pointer to our functions */
1179
1180    return bRC_OK;
1181 \end{verbatim}
1182
1183 where pluginInfo and pluginFuncs are statically defined structures. 
1184 See bpipe-fd.c for details.
1185
1186
1187
1188 \end{description}
1189
1190 \section{Plugin Entry Points}
1191 This section will describe each of the entry points (subroutines) within
1192 the plugin that the plugin must provide for Bacula, when they are called
1193 and their arguments. As noted above, pointers to these subroutines are
1194 passed back to Bacula in the pFuncs structure when Bacula calls the 
1195 loadPlugin() externally defined entry point.
1196
1197 \subsection{newPlugin(bpContext *ctx)}
1198   This is the entry point that Bacula will call
1199   when a new "instance" of the plugin is created. This typically
1200   happens at the beginning of a Job.  If 10 Jobs are running
1201   simultaneously, there will be at least 10 instances of the
1202   plugin.
1203
1204   The bpContext structure will be passed to the plugin, and
1205   during this call, if the plugin needs to have its private
1206   working storage that is associated with the particular
1207   instance of the plugin, it should create it from the heap
1208   (malloc the memory) and store a pointer to
1209   its private working storage in the {\bf pContext} variable.
1210   Note: since Bacula is a multi-threaded program, you must not
1211   keep any variable data in your plugin unless it is truely meant
1212   to apply globally to the whole plugin.  In addition, you must
1213   be aware that except the first and last call to the plugin
1214   (loadPlugin and unloadPlugin) all the other calls will be 
1215   made by threads that correspond to a Bacula job.  The 
1216   bpContext that will be passed for each thread will remain the
1217   same throughout the Job thus you can keep your privat Job specific
1218   data in it ({\bf bContext}).
1219
1220 \begin{verbatim}
1221 typedef struct s_bpContext {
1222   void *pContext;   /* Plugin private context */
1223   void *bContext;   /* Bacula private context */
1224 } bpContext;
1225
1226 \end{verbatim}
1227    
1228   This context pointer will be passed as the first argument to all
1229   the entry points that Bacula calls within the plugin.  Needless
1230   to say, the plugin should not change the bContext variable, which
1231   is Bacula's private context pointer for this instance (Job) of this
1232   plugin.
1233
1234 \subsection{freePlugin(bpContext *ctx)}
1235 This entry point is called when the
1236 this instance of the plugin is no longer needed (the Job is
1237 ending), and the plugin should release all memory it may
1238 have allocated for this particular instance (Job) i.e. the pContext.  
1239 This is not the final termination
1240 of the plugin signaled by a call to {\bf unloadPlugin}. 
1241 Any other instances (Job) will
1242 continue to run, and the entry point {\bf newPlugin} may be called
1243 again if other jobs start.
1244
1245 \subsection{getPluginValue(bpContext *ctx, pVariable var, void *value)} 
1246 Bacula will call this entry point to get
1247 a value from the plugin.  This entry point is currently not called.
1248
1249 \subsection{setPluginValue(bpContext *ctx, pVariable var, void *value)}
1250 Bacula will call this entry point to set
1251 a value in the plugin.  This entry point is currently not called.
1252  
1253 \subsection{handlePluginEvent(bpContext *ctx, bEvent *event, void *value)}
1254 This entry point is called when Bacula
1255 encounters certain events (discussed below). This is, in fact, the 
1256 main way that most plugins get control when a Job runs and how
1257 they know what is happening in the job. It can be likened to the
1258 {\bf RunScript} feature that calls external programs and scripts,
1259 and is very similar to the Bacula Python interface.
1260 When the plugin is called, Bacula passes it the pointer to an event
1261 structure (bEvent), which currently has one item, the eventType:
1262
1263 \begin{verbatim}
1264 typedef struct s_bEvent {
1265    uint32_t eventType;
1266 } bEvent;
1267 \end{verbatim}
1268
1269   which defines what event has been triggered, and for each event,
1270   Bacula will pass a pointer to a value associated with that event.
1271   If no value is associated with a particular event, Bacula will 
1272   pass a NULL pointer, so the plugin must be careful to always check
1273   value pointer prior to dereferencing it.
1274   
1275   The current list of events are:
1276
1277 \begin{verbatim}
1278 typedef enum {
1279   bEventJobStart        = 1,
1280   bEventJobEnd          = 2,
1281   bEventStartBackupJob  = 3,
1282   bEventEndBackupJob    = 4,
1283   bEventStartRestoreJob = 5,
1284   bEventEndRestoreJob   = 6,
1285   bEventStartVerifyJob  = 7,
1286   bEventEndVerifyJob    = 8,
1287   bEventBackupCommand   = 9,
1288   bEventRestoreCommand  = 10,
1289   bEventLevel           = 11,
1290   bEventSince           = 12,
1291 } bEventType;
1292
1293 \end{verbatim}
1294  
1295 Most of the above are self-explanatory.
1296
1297 \begin{description}
1298  \item [bEventJobStart] is called whenever a Job starts. The value
1299    passed is a pointer to a string that contains: "Jobid=nnn 
1300    Job=job-name". Where nnn will be replaced by the JobId and job-name
1301    will be replaced by the Job name. The variable is temporary so if you
1302    need the values, you must copy them.
1303
1304  \item [bEventJobEnd] is called whenever a Job ends. No value is passed.
1305
1306  \item [bEventStartBackupJob] is called when a Backup Job begins. No value
1307    is passed.
1308
1309  \item [bEventEndBackupJob] is called when a Backup Job ends. No value is 
1310    passed.
1311
1312  \item [bEventStartRestoreJob] is called when a Restore Job starts. No value
1313    is passed.
1314
1315  \item [bEventEndRestoreJob] is called when a Restore Job ends. No value is
1316    passed.
1317
1318  \item [bEventStartVerifyJob] is called when a Verify Job starts. No value
1319    is passed.
1320
1321  \item [bEventEndVerifyJob] is called when a Verify Job ends. No value
1322    is passed.
1323
1324  \item [bEventBackupCommand] is called prior to the bEventStartBackupJob and
1325    the plugin is passed the command string (everything after the equal sign
1326    in "Plugin =" as the value.
1327
1328    Note, if you intend to backup a file, this is an important first point to
1329    write code that copies the command string passed into your pContext area
1330    so that you will know that a backup is being performed and you will know
1331    the full contents of the "Plugin =" command (i.e. what to backup and
1332    what virtual filename the user wants to call it.
1333
1334  \item [bEventRestoreCommand] is called prior to the bEventStartRestoreJob and
1335    the plugin is passed the command string (everything after the equal sign
1336    in "Plugin =" as the value.
1337
1338    See the notes above concerning backup and the command string. This is the
1339    point at which Bacula passes you the original command string that was
1340    specified during the backup, so you will want to save it in your pContext
1341    area for later use when Bacula calls the plugin again.
1342
1343  \item [bEventLevel] is called when the level is set for a new Job. The value
1344    is a 32 bit integer stored in the void*, which represents the Job Level code.
1345
1346  \item [bEventSince] is called when the since time is set for a new Job. The 
1347    value is a time\_t time at which the last job was run.
1348 \end{description}
1349
1350 During each of the above calls, the plugin receives either no specific value or
1351 only one value, which in some cases may not be sufficient.  However, knowing the
1352 context of the event, the plugin can call back to the Bacula entry points it
1353 was passed during the {\bf loadPlugin} call and get to a number of Bacula variables.
1354 (at the current time few Bacula variables are implemented, but it easily extended
1355 at a future time and as needs require).
1356
1357 \subsection{startBackupFile(bpContext *ctx, struct save\_pkt *sp)}
1358 This entry point is called only if your plugin is a command plugin, and 
1359 it is called when Bacula encounters the "Plugin = " directive in
1360 the Include section of the FileSet.
1361 Called when beginning the backup of a file. Here Bacula provides you
1362 with a pointer to the {\bf save\_pkt} structure and you must fill in 
1363 this packet with the "attribute" data of the file.
1364
1365 \begin{verbatim}
1366 struct save_pkt {
1367    int32_t pkt_size;                  /* size of this packet */
1368    char *fname;                       /* Full path and filename */
1369    char *link;                        /* Link name if any */
1370    struct stat statp;                 /* System stat() packet for file */
1371    int32_t type;                      /* FT_xx for this file */
1372    uint32_t flags;                    /* Bacula internal flags */
1373    bool portable;                     /* set if data format is portable */
1374    char *cmd;                         /* command */
1375    int32_t pkt_end;                   /* end packet sentinel */
1376 };
1377 \end{verbatim}
1378
1379 The second argument is a pointer to the {\bf save\_pkt} structure for the file
1380 to be backed up.  The plugin is responsible for filling in all the fields 
1381 of the {\bf save\_pkt}. If you are backing up
1382 a real file, then generally, the statp structure can be filled in by doing
1383 a {\bf stat} system call on the file.  
1384
1385 If you are backing up a database or
1386 something that is more complex, you might want to create a virtual file.
1387 That is a file that does not actually exist on the filesystem, but represents 
1388 say an object that you are backing up.  In that case, you need to ensure
1389 that the {\bf fname} string that you pass back is unique so that it
1390 does not conflict with a real file on the system, and you need to 
1391 artifically create values in the statp packet.
1392
1393 Example programs such as {\bf bpipe-fd.c} show how to set these fields.
1394 You must take care not to store pointers the stack in the pointer fields such
1395 as fname and link, because when you return from your function, your stack entries
1396 will be destroyed. The solution in that case is to malloc() and return the pointer
1397 to it. In order to not have memory leaks, you should store a pointer to all memory
1398 allocated in your pContext structure so that in subsequent calls or at termination,
1399 you can release it back to the system.
1400
1401 Once the backup has begun, Bacula will call your plugin at the {\bf pluginIO}
1402 entry point to "read" the data to be backed up.  Please see the {\bf bpipe-fd.c}
1403 plugin for how to do I/O.
1404
1405 Example of filling in the save\_pkt as used in bpipe-fd.c:
1406
1407 \begin{verbatim}
1408    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
1409    time_t now = time(NULL);
1410    sp->fname = p_ctx->fname;
1411    sp->statp.st_mode = 0700 | S_IFREG;
1412    sp->statp.st_ctime = now;
1413    sp->statp.st_mtime = now;
1414    sp->statp.st_atime = now;
1415    sp->statp.st_size = -1;
1416    sp->statp.st_blksize = 4096;
1417    sp->statp.st_blocks = 1;
1418    p_ctx->backup = true;
1419    return bRC_OK; 
1420 \end{verbatim}
1421
1422 Note: the filename to be created has already been created from the 
1423 command string previously sent to the plugin and is in the plugin 
1424 context (p\_ctx->fname) and is a malloc()ed string.  This example
1425 creates a regular file (S\_IFREG), with various fields being created.
1426
1427 In general, the sequence of commands issued from Bacula to the plugin
1428 to do a backup while processing the "Plugin = " directive are:
1429
1430 \begin{enumerate}
1431  \item generate a bEventBackupCommand event to the specified plugin
1432        and pass it the command string.
1433  \item make a startPluginBackup call to the plugin, which
1434        fills in the data needed in save\_pkt to save as the file
1435        attributes and to put on the Volume and in the catalog.
1436  \item call Bacula's internal save\_file() subroutine to save the specified
1437        file.  The plugin will then be called at pluginIO() to "open"
1438        the file, and then to read the file data.
1439        Note, if you are dealing with a virtual file, the "open" operation
1440        is something the plugin does internally and it doesn't necessarily
1441        mean opening a file on the filesystem.  For example in the case of
1442        the bpipe-fd.c program, it initiates a pipe to the requested program.
1443        Finally when the plugin signals to Bacula that all the data was read,
1444        Bacula will call the plugin with the "close" pluginIO() function.
1445 \end{enumerate}
1446
1447
1448 \subsection{endBackupFile(bpContext *ctx)}
1449 Called at the end of backing up a file for a command plugin.  If the plugin's work
1450 is done, it should return bRC\_OK.  If the plugin wishes to create another
1451 file and back it up, then it must return bRC\_More (not yet implemented).
1452 This is probably a good time to release any malloc()ed memory you used to
1453 pass back filenames.
1454
1455 \subsection{startRestoreFile(bpContext *ctx, const char *cmd)}
1456 Called when the first record is read from the Volume that was 
1457 previously written by the command plugin.
1458
1459 \subsection{createFile(bpContext *ctx, struct restore\_pkt *rp)}
1460 Called for a command plugin to create a file during a Restore job before 
1461 restoring the data. 
1462 This entry point is called before any I/O is done on the file.  After
1463 this call, Bacula will call pluginIO() to open the file for write.
1464
1465 The data in the 
1466 restore\_pkt is passed to the plugin and is based on the data that was
1467 originally given by the plugin during the backup and the current user
1468 restore settings (e.g. where, RegexWhere, replace).  This allows the
1469 plugin to first create a file (if necessary) so that the data can
1470 be transmitted to it.  The next call to the plugin will be a
1471 pluginIO command with a request to open the file write-only.
1472
1473 This call must return one of the following values:
1474
1475 \begin{verbatim}
1476  enum {
1477    CF_SKIP = 1,       /* skip file (not newer or something) */
1478    CF_ERROR,          /* error creating file */
1479    CF_EXTRACT,        /* file created, data to extract */
1480    CF_CREATED         /* file created, no data to extract */
1481 };
1482 \end{verbatim}
1483
1484 in the restore\_pkt value {\bf create\_status}.  For a normal file,
1485 unless there is an error, you must return {\bf CF\_EXTRACT}.
1486
1487 \begin{verbatim}
1488  
1489 struct restore_pkt {
1490    int32_t pkt_size;                  /* size of this packet */
1491    int32_t stream;                    /* attribute stream id */
1492    int32_t data_stream;               /* id of data stream to follow */
1493    int32_t type;                      /* file type FT */
1494    int32_t file_index;                /* file index */
1495    int32_t LinkFI;                    /* file index to data if hard link */
1496    uid_t uid;                         /* userid */
1497    struct stat statp;                 /* decoded stat packet */
1498    const char *attrEx;                /* extended attributes if any */
1499    const char *ofname;                /* output filename */
1500    const char *olname;                /* output link name */
1501    const char *where;                 /* where */
1502    const char *RegexWhere;            /* regex where */
1503    int replace;                       /* replace flag */
1504    int create_status;                 /* status from createFile() */
1505    int32_t pkt_end;                   /* end packet sentinel */
1506
1507 };
1508 \end{verbatim}
1509
1510 Typical code to create a regular file would be the following:
1511
1512 \begin{verbatim}
1513    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
1514    time_t now = time(NULL);
1515    sp->fname = p_ctx->fname;   /* set the full path/filename I want to create */
1516    sp->type = FT_REG;
1517    sp->statp.st_mode = 0700 | S_IFREG;
1518    sp->statp.st_ctime = now;
1519    sp->statp.st_mtime = now;
1520    sp->statp.st_atime = now;
1521    sp->statp.st_size = -1;
1522    sp->statp.st_blksize = 4096;
1523    sp->statp.st_blocks = 1;
1524    return bRC_OK;
1525 \end{verbatim}
1526
1527 This will create a virtual file.  If you are creating a file that actually 
1528 exists, you will most likely want to fill the statp packet using the
1529 stat() system call.
1530
1531 Creating a directory is similar, but requires a few extra steps:
1532
1533 \begin{verbatim}
1534    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
1535    time_t now = time(NULL);
1536    sp->fname = p_ctx->fname;   /* set the full path I want to create */
1537    sp->link = xxx; where xxx is p_ctx->fname with a trailing forward slash
1538    sp->type = FT_DIREND
1539    sp->statp.st_mode = 0700 | S_IFDIR;
1540    sp->statp.st_ctime = now;
1541    sp->statp.st_mtime = now;
1542    sp->statp.st_atime = now;
1543    sp->statp.st_size = -1;
1544    sp->statp.st_blksize = 4096;
1545    sp->statp.st_blocks = 1;
1546    return bRC_OK;
1547 \end{verbatim}
1548
1549 The link field must be set with the full cononical path name, which always 
1550 ends with a forward slash.  If you do not terminate it with a forward slash,
1551 you will surely have problems later.
1552
1553 As with the example that creates a file, if you are backing up a real
1554 directory, you will want to do an stat() on the directory.  
1555
1556 Note, if you want the directory permissions and times to be correctly
1557 restored, you must create the directory {\bf after} all the file directories
1558 have been sent to Bacula. That allows the restore process to restore all the
1559 files in a directory using default directory options, then at the end, restore
1560 the directory permissions.  If you do it the other way around, each time you
1561 restore a file, the OS will modify the time values for the directory entry.
1562
1563 \subsection{setFileAttributes(bpContext *ctx, struct restore\_pkt *rp)}
1564 This is call not yet implemented.  Called for a command plugin.
1565
1566 See the definition of {\bf restre\_pkt} in the above section.
1567
1568 \subsection{endRestoreFile(bpContext *ctx)}
1569 Called when a command plugin is done restoring a file.
1570
1571 \subsection{pluginIO(bpContext *ctx, struct io\_pkt *io)}
1572 Called to do the input (backup) or output (restore) of data from or to a
1573 file for a command plugin. These routines simulate the Unix read(), write(), open(), close(), 
1574 and lseek() I/O calls, and the arguments are passed in the packet and
1575 the return values are also placed in the packet.  In addition for Win32
1576 systems the plugin must return two additional values (described below).
1577
1578 \begin{verbatim}
1579  enum {
1580    IO_OPEN = 1,
1581    IO_READ = 2,
1582    IO_WRITE = 3,
1583    IO_CLOSE = 4,
1584    IO_SEEK = 5
1585 };
1586
1587 struct io_pkt {
1588    int32_t pkt_size;                  /* Size of this packet */
1589    int32_t func;                      /* Function code */
1590    int32_t count;                     /* read/write count */
1591    mode_t mode;                       /* permissions for created files */
1592    int32_t flags;                     /* Open flags */
1593    char *buf;                         /* read/write buffer */
1594    const char *fname;                 /* open filename */
1595    int32_t status;                    /* return status */
1596    int32_t io_errno;                  /* errno code */
1597    int32_t lerror;                    /* Win32 error code */
1598    int32_t whence;                    /* lseek argument */
1599    boffset_t offset;                  /* lseek argument */
1600    bool win32;                        /* Win32 GetLastError returned */
1601    int32_t pkt_end;                   /* end packet sentinel */
1602 };
1603 \end{verbatim}
1604
1605 The particular Unix function being simulated is indicated by the {\bf func}, 
1606 which will have one of the IO\_OPEN, IO\_READ, ... codes listed above.  
1607 The status code that would be returned from a Unix call is returned in
1608 {\bf status} for IO\_OPEN, IO\_CLOSE, IO\_READ, and IO\_WRITE. The return value for 
1609 IO\_SEEK is returned in {\bf offset} which in general is a 64 bit value.
1610
1611 When there is an error on Unix systems, you must always set io\_error, and
1612 on a Win32 system, you must always set win32, and the returned value from
1613 the OS call GetLastError() in lerror.
1614
1615 For all except IO\_SEEK, {\bf status} is the return result.  In general it is
1616 a positive integer unless there is an error in which case it is -1.
1617
1618 The following describes each call and what you get and what you
1619 should return:
1620
1621 \begin{description}
1622  \item [IO\_OPEN]
1623    You will be passed fname, mode, and flags.
1624    You must set on return: status, and if there is a Unix error
1625    io\_errno must be set to the errno value, and if there is a 
1626    Win32 error win32 and lerror. 
1627
1628  \item [IO\_READ]
1629   You will be passed: count, and buf (buffer of size count).
1630   You must set on return: status to the number of bytes 
1631   read into the buffer (buf) or -1 on an error, 
1632   and if there is a Unix error
1633   io\_errno must be set to the errno value, and if there is a
1634   Win32 error, win32 and lerror must be set.
1635
1636  \item [IO\_WRITE]
1637   You will be passed: count, and buf (buffer of size count).
1638   You must set on return: status to the number of bytes 
1639   written from the buffer (buf) or -1 on an error, 
1640   and if there is a Unix error
1641   io\_errno must be set to the errno value, and if there is a
1642   Win32 error, win32 and lerror must be set.
1643
1644  \item [IO\_CLOSE]
1645   Nothing will be passed to you.  On return you must set 
1646   status to 0 on success and -1 on failure.  If there is a Unix error
1647   io\_errno must be set to the errno value, and if there is a
1648   Win32 error, win32 and lerror must be set.
1649
1650  \item [IO\_LSEEK]
1651   You will be passed: offset, and whence. offset is a 64 bit value
1652   and is the position to seek to relative to whence.  whence is one
1653   of the following SEEK\_SET, SEEK\_CUR, or SEEK\_END indicating to
1654   either to seek to an absolute possition, relative to the current 
1655   position or relative to the end of the file.
1656   You must pass back in offset the absolute location to which you 
1657   seeked. If there is an error, offset should be set to -1.
1658   If there is a Unix error
1659   io\_errno must be set to the errno value, and if there is a
1660   Win32 error, win32 and lerror must be set.
1661
1662   Note: Bacula will call IO\_SEEK only when writing a sparse file.
1663   
1664 \end{description}
1665
1666