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