]> git.sur5r.net Git - bacula/docs/blob - docs/manuals/en/concepts/newfeatures.tex
e9cc2e1d9d0594336d4a49b27ff138d39bde82b2
[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{libdbi framework}
446 As a general guideline, Bacula has support for a few catalog database drivers
447 coded natively by the Bacula team.  With the libdbi implementation, which is a
448 Bacula driver that uses libdbi to access the catalog, we have an open field to
449 use many different kinds database engines following the needs of users.
450
451 The according to libdbi (http://libdbi.sourceforge.net/) project: libdbi
452 implements a database-independent abstraction layer in C, similar to the
453 DBI/DBD layer in Perl. Writing one generic set of code, programmers can
454 leverage the power of multiple databases and multiple simultaneous database
455 connections by using this framework.
456
457 Currently the libdbi driver in Bacula project only supports the same drivers
458 natively coded in Bacula.  However the libdbi project has support for many
459 others database engines. You can view the list at
460 http://libdbi-drivers.sourceforge.net/. In the future all those drivers can be
461 supported by Bacula, however, they must be tested properly by the Bacula team.
462
463 Some of benefits of using libdbi are:
464 \begin{itemize}
465 \item The possibility to use proprietary databases engines in which your
466   proprietary licenses prevent the Bacula team from developing the driver.
467  \item The possibility to use the drivers written for the libdbi project.
468  \item The possibility to use other database engines without recompiling Bacula
469    to use them.  Just change one line in bacula-dir.conf
470  \item Abstract Database access, this is, unique point to code and profiling
471    catalog database access.
472  \end{itemize}
473  
474  The following drivers have been tested:
475  \begin{itemize}
476  \item PostgreSQL, with and without batch insert
477  \item Mysql, with and without batch insert
478  \item SQLite
479  \item SQLite3
480  \end{itemize}
481
482  In the future, we will test and approve to use others databases engines
483  (proprietary or not) like DB2, Oracle, Microsoft SQL.
484
485  To compile Bacula to support libdbi we need to configure the code with the
486  --with-dbi and --with-dbi-driver=[database] ./configure options, where
487  [database] is the database engine to be used with Bacula (of course we can
488  change the driver in file bacula-dir.conf, see below).  We must configure the
489  access port of the database engine with the option --with-db-port, because the
490  libdbi framework doesn't know the default access port of each database.
491
492 The next phase is checking (or configuring) the bacula-dir.conf, example:
493 \begin{verbatim}
494 Catalog {
495   Name = MyCatalog
496   dbdriver = dbi:mysql; dbaddress = 127.0.0.1; dbport = 3306
497   dbname = regress; user = regress; password = ""
498 }
499 \end{verbatim}
500
501 The parameter {\bf dbdriver} indicates that we will use the driver dbi with a
502 mysql database.  Currently the drivers supported by Bacula are: postgresql,
503 mysql, sqlite, sqlite3; these are the names that may be added to string "dbi:".
504
505 The following limitations apply when Bacula is set to use the libdbi framework:
506  - Not tested on the Win32 platform
507  - A little performance is lost if comparing with native database driver. 
508    The reason is bound with the database driver provided by libdbi and the 
509    simple fact that one more layer of code was added.
510
511 It is important to remember, when compiling Bacula with libdbi, the
512 following packages are needed:
513  \begin{itemize}
514   \item libdbi version 1.0.0, http://libdbi.sourceforge.net/
515   \item libdbi-drivers 1.0.0, http://libdbi-drivers.sourceforge.net/
516  \end{itemize}
517  
518  You can download them and compile them on your system or install the packages
519  from your OS distribution.
520
521
522 \section{Display Autochanger Content}
523 \index[general]{StatusSlots}
524
525 The {\bf status slots storage=\lt{}storage-name\gt{}} command displays autochanger content.
526
527 \footnotesize
528 \begin{verbatim}
529  Slot |  Volume Name  |  Status  |  Media Type       |   Pool     |
530 ------+---------------+----------+-------------------+------------|
531     1 |         00001 |   Append |  DiskChangerMedia |    Default |
532     2 |         00002 |   Append |  DiskChangerMedia |    Default |
533     3*|         00003 |   Append |  DiskChangerMedia |    Scratch |
534     4 |               |          |                   |            |
535 \end{verbatim}
536 \normalsize
537
538 If you an asterisk ({\bf *}) appears after the slot number, you must run an
539 {\bf update slots} command to synchronize autochanger content with your
540 catalog.
541
542 \section{Miscellaneous}
543 \index[general]{Misc New Features}
544
545 \subsection{Virtual Tape Emulation}
546
547 We now have a Virtual Tape emulator that allows us to run though 99.9\% of
548 the tape code but actually reading and writing to a disk file. Used with the
549 \textbf{disk-changer} script, you can now emulate an autochanger with 10 drives
550 and 700 slots. This feature is most useful in testing.  It is enabled
551 by using {\bf Device Type = vtape} in the Storage daemon's Device
552 directive. This feature is only implemented on Linux machines.
553
554 \subsection{Bat Enhancements}
555 Bat (the Bacula Administration Tool) GUI program has been significantly
556 enhanced and stabilized. In particular, there are new table based status 
557 commands; it can now be easily localized using Qt4 Linguist.
558
559 The Bat communications protocol has been significantly enhanced to improve
560 GUI handling.
561
562 \subsection{RunScript Enhancements}
563 The {\bf RunScript} resource has been enhanced to permit multiple
564 commands per RunScript.  Simply specify multiple {\bf Command} directives
565 in your RunScript.
566
567 \begin{verbatim}
568 Job {
569   Name = aJob
570   RunScript {
571     Command = "/bin/echo test"
572     Command = "/bin/echo an other test"
573     Command = "/bin/echo 3 commands in the same runscript"
574     RunsWhen = Before
575   }
576  ...
577 }
578 \end{verbatim}
579
580 A new Client RunScript {\bf RunsWhen} keyword of {\bf AfterVSS} has been implemented, which
581 runs the command after the Volume Shadow Copy has been made.
582
583 Console commands can be specified within a RunScript by using:
584 {\bf Console = \lt{}command\gt{}}, however, this command has not been 
585 carefully tested and debugged and is known to easily crash the Director.
586 We would appreciate feedback.  Due to the recursive nature of this command, we
587 may remove it before the final release.
588
589 \subsection{Status Enhancements}
590 The bconsole {\bf status dir} output has been enhanced to indicate
591 Storage daemon job spooling and despooling activity.
592
593 \subsection{Connect Timeout}
594 The default connect timeout to the File
595 daemon has been set to 3 minutes. Previously it was 30 minutes.
596
597 \subsection{ftruncate for NFS Volumes}
598 If you write to a Volume mounted by NFS (say on a local file server),
599 in previous Bacula versions, when the Volume was recycled, it was not
600 properly truncated because NFS does not implement ftruncate (file 
601 truncate). This is now corrected in the new version because we have
602 written code (actually a kind user) that deletes and recreates the Volume,
603 thus accomplishing the same thing as a truncate.
604
605 \subsection{Support for Ubuntu}
606 The new version of Bacula now recognizes the Ubuntu (and Kubuntu)
607 version of Linux, and thus now provides correct autostart routines.
608 Since Ubuntu officially supports Bacula, you can also obtain any
609 recent release of Bacula from the Ubuntu repositories.
610
611 \subsection{Recycle Pool = \lt{}pool-name\gt{}}
612 The new \textbf{RecyclePool} directive defines to which pool the Volume will
613 be placed (moved) when it is recycled. Without this directive, a Volume will
614 remain in the same pool when it is recycled. With this directive, it can be
615 moved automatically to any existing pool during a recycle. This directive is
616 probably most useful when defined in the Scratch pool, so that volumes will
617 be recycled back into the Scratch pool.
618
619 \subsection{FD Version}
620 The File daemon to Director protocol now includes a version 
621 number, which although there is no visible change for users, 
622 will help us in future versions automatically determine
623 if a File daemon is not compatible.
624
625 \subsection{Max Run Sched Time = \lt{}time-period-in-seconds\gt{}}
626 The time specifies the maximum allowed time that a job may run, counted from
627 when the job was scheduled. This can be useful to prevent jobs from running
628 during working hours. We can see it like \texttt{Max Start Delay + Max Run
629   Time}.
630
631 \subsection{Max Wait Time = \lt{}time-period-in-seconds\gt{}}
632
633 Previous \textbf{MaxWaitTime} directives aren't working as expected, instead
634 of checking the maximum allowed time that a job may block for a resource,
635 those directives worked like \textbf{MaxRunTime}. Some users are reporting to
636 use \textbf{Incr/Diff/Full Max Wait Time} to control the maximum run time of
637 their job depending on the level. Now, they have to use
638 \textbf{Incr/Diff/Full Max Run Time}.  \textbf{Incr/Diff/Full Max Wait Time}
639 directives are now deprecated.
640
641 \subsection{Incremental|Differential Max Wait Time = \lt{}time-period-in-seconds\gt{}} 
642 Theses directives have been deprecated in favor of
643 \texttt{Incremental|Differential Max Run Time}.
644
645 \subsection{Max Run Time directives}
646 Using \textbf{Full/Diff/Incr Max Run Time}, it's now possible to specify the
647 maximum allowed time that a job can run depending on the level.
648
649 \addcontentsline{lof}{figure}{Job time control directives}
650 \includegraphics{\idir different_time.eps}
651
652 \subsection{Statistics Enhancements}
653 If you (or probably your boss) want to have statistics on your backups to
654 provide some \textit{Service Level Agreement} indicators, you could use a few
655 SQL queries on the Job table to report how many:
656
657 \begin{itemize}
658 \item jobs have run
659 \item jobs have been successful
660 \item files have been backed up
661 \item ...
662 \end{itemize}
663
664 However, these statistics are accurate only if your job retention is greater
665 than your statistics period. Ie, if jobs are purged from the catalog, you won't
666 be able to use them. 
667
668 Now, you can use the \textbf{update stats [days=num]} console command to fill
669 the JobStat table with new Job records. If you want to be sure to take in
670 account only \textbf{good jobs}, ie if one of your important job has failed but
671 you have fixed the problem and restarted it on time, you probably want to
672 delete the first \textit{bad} job record and keep only the successful one. For
673 that simply let your staff do the job, and update JobStat table after two or
674 three days depending on your organization using the \textbf{[days=num]} option.
675
676 These statistics records aren't used for restoring, but mainly for
677 capacity planning, billings, etc.
678
679 The Bweb interface provides a statistics module that can use this feature. You
680 can also use tools like Talend or extract information by yourself.
681
682 The {\textbf Statistics Retention = \lt{}time\gt{}} director directive defines
683 the length of time that Bacula will keep statistics job records in the Catalog
684 database after the Job End time. (In \texttt{JobStat} table) When this time
685 period expires, and if user runs \texttt{prune stats} command, Bacula will
686 prune (remove) Job records that are older than the specified period.
687
688 You can use the following Job resource in your nightly \textbf{BackupCatalog}
689 job to maintain statistics.
690 \begin{verbatim}
691 Job {
692   Name = BackupCatalog
693   ...
694   RunScript {
695     Console = "update stats days=3"
696     Console = "prune stats yes"
697     RunsWhen = After
698     RunsOnClient = no
699   }
700 }
701 \end{verbatim}
702
703 \subsection{SpoolSize = \lt{}size-specification-in-bytes\gt{}}
704 A new job directive permits to specify the spool size per job. This is used
705 in advanced job tunning. {\bf SpoolSize={\it bytes}}
706
707
708 \section{Building Bacula Plugins}
709 There is currently one sample program {\bf example-plugin-fd.c} and
710 one working plugin {\bf bpipe-fd.c} that can be found in the Bacula
711 {\bf src/plugins/fd} directory.  Both are built with the following:
712
713 \begin{verbatim}
714  cd <bacula-source>
715  ./configure <your-options>
716  make
717  ...
718  cd src/plugins/fd
719  make
720  make test
721 \end{verbatim}
722
723 After building Bacula and changing into the src/plugins/fd directory,
724 the {\bf make} command will build the {\bf bpipe-fd.so} plugin, which 
725 is a very useful and working program.
726
727 The {\bf make test} command will build the {\bf example-plugin-fd.so}
728 plugin and a binary named {\bf main}, which is build from the source
729 code located in {\bf src/filed/fd\_plugins.c}. 
730
731 If you execute {\bf ./main}, it will load and run the example-plugin-fd
732 plugin simulating a small number of the calling sequences that Bacula uses
733 in calling a real plugin.  This allows you to do initial testing of 
734 your plugin prior to trying it with Bacula.
735
736 You can get a good idea of how to write your own plugin by first 
737 studying the example-plugin-fd, and actually running it.  Then
738 it can also be instructive to read the bpipe-fd.c code as it is 
739 a real plugin, which is still rather simple and small.
740
741 When actually writing your own plugin, you may use the example-plugin-fd.c
742 code as a template for your code.
743
744
745 %%
746 %%
747
748 \chapter{Bacula FD Plugin API}
749 To write a Bacula plugin, you create a dynamic shared object
750 program (or dll on Win32) with a particular name and two 
751 exported entry points, place it in the {\bf Plugins Directory}, which is defined in the
752 {\bf bacula-fd.conf} file in the {\bf Client} resource, and when the FD
753 starts, it will load all the plugins that end with {\bf -fd.so} (or {\bf -fd.dll}
754 on Win32) found in that directory.
755
756 Once the File daemon loads the plugins, it asks the OS for the
757 two entry points (loadPlugin and unloadPlugin) then calls the
758 {\bf loadPlugin} entry point (see below).
759
760 Bacula passes information to the plugin through this call and it gets
761 back information that it needs to use the plugin.  Later, Bacula
762  will call particular functions that are defined by the
763 {\bf loadPlugin} interface.  
764
765 When Bacula is finished with the plugin 
766 (when Bacula is going to exit), it will call the {\bf unloadPlugin}
767 entry point.
768
769 The two entry points are:
770
771 \begin{verbatim}
772 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
773
774 and
775
776 bRC unloadPlugin()
777 \end{verbatim}
778
779 both these external entry points to the shared object are defined as C entry points
780 to avoid name mangling complications with C++.  However, the shared object
781 can actually be written in any language (preferrably C or C++) providing that it
782 follows C language calling conventions.
783
784 The definitions for {\bf bRC} and the arguments are {\bf
785 src/filed/fd-plugins.h} and so this header file needs to be included in
786 your plugin.  It along with {\bf src/lib/plugins.h} define basically the whole
787 plugin interface.  Within this header file, it includes the following
788 files:
789
790 \begin{verbatim}
791 #include <sys/types.h>
792 #include "config.h"
793 #include "bc_types.h"
794 #include "lib/plugins.h"
795 #include <sys/stat.h>
796 \end{verbatim}
797
798 Aside from the {\bf bc\_types.h} and {\bf confit.h} headers, the plugin definition uses the
799 minimum code from Bacula.  The bc\_types.h file is required to ensure that
800 the data type defintions in arguments correspond to the Bacula core code.
801
802 The return codes are defined as:
803 \begin{verbatim}
804 typedef enum {
805   bRC_OK    = 0,                         /* OK */
806   bRC_Stop  = 1,                         /* Stop calling other plugins */
807   bRC_Error = 2,                         /* Some kind of error */
808   bRC_More  = 3,                         /* More files to backup */
809 } bRC;
810 \end{verbatim}
811
812
813 At a future point in time, we hope to make the Bacula libbac.a into a
814 shared object so that the plugin can use much more of Bacula's
815 infrastructure, but for this first cut, we have tried to minimize the
816 dependence on Bacula.
817
818 \section{loadPlugin}
819 As previously mentioned, the {\bf loadPlugin} entry point in the plugin
820 is called immediately after Bacula loads the plugin when the File daemon
821 itself is first starting.  This entry point is only called once during the
822 execution of the File daemon.  In calling the
823 plugin, the first two arguments are information from Bacula that
824 is passed to the plugin, and the last two arguments are information
825 about the plugin that the plugin must return to Bacula.  The call is:
826
827 \begin{verbatim}
828 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
829 \end{verbatim}
830
831 and the arguments are:
832
833 \begin{description}
834 \item [lbinfo]
835 This is information about Bacula in general. Currently, the only value
836 defined in the bInfo structure is the version, which is the Bacula plugin 
837 interface version, currently defined as 1.  The {\bf size} is set to the
838 byte size of the structure. The exact definition of the bInfo structure
839 as of this writing is: 
840
841 \begin{verbatim}
842 typedef struct s_baculaInfo {
843    uint32_t size;
844    uint32_t version;
845 } bInfo;
846 \end{verbatim}
847
848 \item [lbfuncs]
849 The bFuncs structure defines the callback entry points within Bacula
850 that the plugin can use register events, get Bacula values, set
851 Bacula values, and send messages to the Job output or debug output.
852
853 The exact definition as of this writing is:
854 \begin{verbatim}
855 typedef struct s_baculaFuncs {
856    uint32_t size;
857    uint32_t version;
858    bRC (*registerBaculaEvents)(bpContext *ctx, ...);
859    bRC (*getBaculaValue)(bpContext *ctx, bVariable var, void *value);
860    bRC (*setBaculaValue)(bpContext *ctx, bVariable var, void *value);
861    bRC (*JobMessage)(bpContext *ctx, const char *file, int line,
862        int type, time_t mtime, const char *fmt, ...);
863    bRC (*DebugMessage)(bpContext *ctx, const char *file, int line,
864        int level, const char *fmt, ...);
865 } bFuncs;
866 \end{verbatim}
867
868 We will discuss these entry points and how to use them a bit later when
869 describing the plugin code.
870
871
872 \item [pInfo]
873 When the loadPlugin entry point is called, the plugin must initialize
874 an information structure about the plugin and return a pointer to
875 this structure to Bacula.
876
877 The exact definition as of this writing is:
878
879 \begin{verbatim}
880 typedef struct s_pluginInfo {
881    uint32_t size;
882    uint32_t version;
883    const char *plugin_magic;
884    const char *plugin_license;
885    const char *plugin_author;
886    const char *plugin_date;
887    const char *plugin_version;
888    const char *plugin_description;
889 } pInfo;
890 \end{verbatim}
891
892 Where:
893  \begin{description}
894  \item [version] is the current Bacula defined plugin interface version, currently
895    set to 1. If the interface version differs from the current version of 
896    Bacula, the plugin will not be run (not yet implemented).
897  \item [plugin\_magic] is a pointer to the text string "*FDPluginData*", a
898    sort of sanity check.  If this value is not specified, the plugin
899    will not be run (not yet implemented).
900  \item [plugin\_license] is a pointer to a text string that describes the
901    plugin license. Bacula will only accept compatible licenses (not yet
902    implemented).
903  \item [plugin\_author] is a pointer to the text name of the author of the program.
904    This string can be anything but is generally the author's name.
905  \item [plugin\_date] is the pointer text string containing the date of the plugin.
906    This string can be anything but is generally some human readable form of 
907    the date.
908  \item [plugin\_version] is a pointer to a text string containing the version of
909    the plugin.  The contents are determined by the plugin writer.
910  \item [plugin\_description] is a pointer to a string describing what the
911    plugin does. The contents are determined by the plugin writer.
912  \end{description}
913
914 The pInfo structure must be defined in static memory because Bacula does not
915 copy it and may refer to the values at any time while the plugin is
916 loaded. All values must be supplied or the plugin will not run (not yet
917 implemented).  All text strings must be either ASCII or UTF-8 strings that
918 are terminated with a zero byte.
919
920 \item [pFuncs]
921 When the loadPlugin entry point is called, the plugin must initialize
922 an entry point structure about the plugin and return a pointer to
923 this structure to Bacula. This structure contains pointer to each
924 of the entry points that the plugin must provide for Bacula. When
925 Bacula is actually running the plugin, it will call the defined
926 entry points at particular times.  All entry points must be defined.
927
928 The pFuncs structure must be defined in static memory because Bacula does not
929 copy it and may refer to the values at any time while the plugin is
930 loaded.
931
932 The exact definition as of this writing is:
933
934 \begin{verbatim}
935 typedef struct s_pluginFuncs {
936    uint32_t size;
937    uint32_t version;
938    bRC (*newPlugin)(bpContext *ctx);
939    bRC (*freePlugin)(bpContext *ctx);
940    bRC (*getPluginValue)(bpContext *ctx, pVariable var, void *value);
941    bRC (*setPluginValue)(bpContext *ctx, pVariable var, void *value);
942    bRC (*handlePluginEvent)(bpContext *ctx, bEvent *event, void *value);
943    bRC (*startBackupFile)(bpContext *ctx, struct save_pkt *sp);
944    bRC (*endBackupFile)(bpContext *ctx);
945    bRC (*startRestoreFile)(bpContext *ctx, const char *cmd);
946    bRC (*endRestoreFile)(bpContext *ctx);
947    bRC (*pluginIO)(bpContext *ctx, struct io_pkt *io);
948    bRC (*createFile)(bpContext *ctx, struct restore_pkt *rp);
949    bRC (*setFileAttributes)(bpContext *ctx, struct restore_pkt *rp);
950 } pFuncs;
951 \end{verbatim}
952
953 The details of the entry points will be presented in
954 separate sections below. 
955
956 Where:
957  \begin{description}
958  \item [size] is the byte size of the structure.
959  \item [version] is the plugin interface version currently set to 1.
960  \end{description}
961
962 Sample code for loadPlugin:
963 \begin{verbatim}
964   bfuncs = lbfuncs;                  /* set Bacula funct pointers */
965   binfo  = lbinfo;
966   *pinfo  = &pluginInfo;             /* return pointer to our info */
967   *pfuncs = &pluginFuncs;            /* return pointer to our functions */
968
969    return bRC_OK;
970 \end{verbatim}
971
972 where pluginInfo and pluginFuncs are statically defined structures. 
973 See bpipe-fd.c for details.
974
975
976
977 \end{description}
978
979 \section{Plugin Entry Points}
980 This section will describe each of the entry points (subroutines) within
981 the plugin that the plugin must provide for Bacula, when they are called
982 and their arguments. As noted above, pointers to these subroutines are
983 passed back to Bacula in the pFuncs structure when Bacula calls the 
984 loadPlugin() externally defined entry point.
985
986 \subsection{newPlugin(bpContext *ctx)}
987   This is the entry point that Bacula will call
988   when a new "instance" of the plugin is created. This typically
989   happens at the beginning of a Job.  If 10 Jobs are running
990   simultaneously, there will be at least 10 instances of the
991   plugin.
992
993   The bpContext structure will be passed to the plugin, and
994   during this call, if the plugin needs to have its private
995   working storage that is associated with the particular
996   instance of the plugin, it should create it from the heap
997   (malloc the memory) and store a pointer to
998   its private working storage in the {\bf pContext} variable.
999   Note: since Bacula is a multi-threaded program, you must not
1000   keep any variable data in your plugin unless it is truely meant
1001   to apply globally to the whole plugin.  In addition, you must
1002   be aware that except the first and last call to the plugin
1003   (loadPlugin and unloadPlugin) all the other calls will be 
1004   made by threads that correspond to a Bacula job.  The 
1005   bpContext that will be passed for each thread will remain the
1006   same throughout the Job thus you can keep your privat Job specific
1007   data in it ({\bf bContext}).
1008
1009 \begin{verbatim}
1010 typedef struct s_bpContext {
1011   void *pContext;   /* Plugin private context */
1012   void *bContext;   /* Bacula private context */
1013 } bpContext;
1014
1015 \end{verbatim}
1016    
1017   This context pointer will be passed as the first argument to all
1018   the entry points that Bacula calls within the plugin.  Needless
1019   to say, the plugin should not change the bContext variable, which
1020   is Bacula's private context pointer for this instance (Job) of this
1021   plugin.
1022
1023 \subsection{freePlugin(bpContext *ctx)}
1024 This entry point is called when the
1025 this instance of the plugin is no longer needed (the Job is
1026 ending), and the plugin should release all memory it may
1027 have allocated for this particular instance (Job) i.e. the pContext.  
1028 This is not the final termination
1029 of the plugin signaled by a call to {\bf unloadPlugin}. 
1030 Any other instances (Job) will
1031 continue to run, and the entry point {\bf newPlugin} may be called
1032 again if other jobs start.
1033
1034 \subsection{getPluginValue(bpContext *ctx, pVariable var, void *value)} 
1035 Bacula will call this entry point to get
1036 a value from the plugin.  This entry point is currently not called.
1037
1038 \subsection{setPluginValue(bpContext *ctx, pVariable var, void *value)}
1039 Bacula will call this entry point to set
1040 a value in the plugin.  This entry point is currently not called.
1041  
1042 \subsection{handlePluginEvent(bpContext *ctx, bEvent *event, void *value)}
1043 This entry point is called when Bacula
1044 encounters certain events (discussed below). This is, in fact, the 
1045 main way that most plugins get control when a Job runs and how
1046 they know what is happening in the job. It can be likened to the
1047 {\bf RunScript} feature that calls external programs and scripts,
1048 and is very similar to the Bacula Python interface.
1049 When the plugin is called, Bacula passes it the pointer to an event
1050 structure (bEvent), which currently has one item, the eventType:
1051
1052 \begin{verbatim}
1053 typedef struct s_bEvent {
1054    uint32_t eventType;
1055 } bEvent;
1056 \end{verbatim}
1057
1058   which defines what event has been triggered, and for each event,
1059   Bacula will pass a pointer to a value associated with that event.
1060   If no value is associated with a particular event, Bacula will 
1061   pass a NULL pointer, so the plugin must be careful to always check
1062   value pointer prior to dereferencing it.
1063   
1064   The current list of events are:
1065
1066 \begin{verbatim}
1067 typedef enum {
1068   bEventJobStart        = 1,
1069   bEventJobEnd          = 2,
1070   bEventStartBackupJob  = 3,
1071   bEventEndBackupJob    = 4,
1072   bEventStartRestoreJob = 5,
1073   bEventEndRestoreJob   = 6,
1074   bEventStartVerifyJob  = 7,
1075   bEventEndVerifyJob    = 8,
1076   bEventBackupCommand   = 9,
1077   bEventRestoreCommand  = 10,
1078   bEventLevel           = 11,
1079   bEventSince           = 12,
1080 } bEventType;
1081
1082 \end{verbatim}
1083  
1084 Most of the above are self-explanatory.
1085
1086 \begin{description}
1087  \item [bEventJobStart] is called whenever a Job starts. The value
1088    passed is a pointer to a string that contains: "Jobid=nnn 
1089    Job=job-name". Where nnn will be replaced by the JobId and job-name
1090    will be replaced by the Job name. The variable is temporary so if you
1091    need the values, you must copy them.
1092
1093  \item [bEventJobEnd] is called whenever a Job ends. No value is passed.
1094
1095  \item [bEventStartBackupJob] is called when a Backup Job begins. No value
1096    is passed.
1097
1098  \item [bEventEndBackupJob] is called when a Backup Job ends. No value is 
1099    passed.
1100
1101  \item [bEventStartRestoreJob] is called when a Restore Job starts. No value
1102    is passed.
1103
1104  \item [bEventEndRestoreJob] is called when a Restore Job ends. No value is
1105    passed.
1106
1107  \item [bEventStartVerifyJob] is called when a Verify Job starts. No value
1108    is passed.
1109
1110  \item [bEventEndVerifyJob] is called when a Verify Job ends. No value
1111    is passed.
1112
1113  \item [bEventBackupCommand] is called prior to the bEventStartBackupJob and
1114    the plugin is passed the command string (everything after the equal sign
1115    in "Plugin =" as the value.
1116
1117    Note, if you intend to backup a file, this is an important first point to
1118    write code that copies the command string passed into your pContext area
1119    so that you will know that a backup is being performed and you will know
1120    the full contents of the "Plugin =" command (i.e. what to backup and
1121    what virtual filename the user wants to call it.
1122
1123  \item [bEventRestoreCommand] is called prior to the bEventStartRestoreJob and
1124    the plugin is passed the command string (everything after the equal sign
1125    in "Plugin =" as the value.
1126
1127    See the notes above concerning backup and the command string. This is the
1128    point at which Bacula passes you the original command string that was
1129    specified during the backup, so you will want to save it in your pContext
1130    area for later use when Bacula calls the plugin again.
1131
1132  \item [bEventLevel] is called when the level is set for a new Job. The value
1133    is a 32 bit integer stored in the void*, which represents the Job Level code.
1134
1135  \item [bEventSince] is called when the since time is set for a new Job. The 
1136    value is a time\_t time at which the last job was run.
1137 \end{description}
1138
1139 During each of the above calls, the plugin receives either no specific value or
1140 only one value, which in some cases may not be sufficient.  However, knowing the
1141 context of the event, the plugin can call back to the Bacula entry points it
1142 was passed during the {\bf loadPlugin} call and get to a number of Bacula variables.
1143 (at the current time few Bacula variables are implemented, but it easily extended
1144 at a future time and as needs require).
1145
1146 \subsection{startBackupFile(bpContext *ctx, struct save\_pkt *sp)}
1147 Called when beginning the backup of a file. Here Bacula provides you
1148 with a pointer to the {\bf save\_pkt} structure and you must fill in 
1149 this packet with the "attribute" data of the file.
1150
1151 \begin{verbatim}
1152 struct save_pkt {
1153    int32_t pkt_size;                  /* size of this packet */
1154    char *fname;                       /* Full path and filename */
1155    char *link;                        /* Link name if any */
1156    struct stat statp;                 /* System stat() packet for file */
1157    int32_t type;                      /* FT_xx for this file */
1158    uint32_t flags;                    /* Bacula internal flags */
1159    bool portable;                     /* set if data format is portable */
1160    char *cmd;                         /* command */
1161    int32_t pkt_end;                   /* end packet sentinel */
1162 };
1163 \end{verbatim}
1164
1165 The second argument is a pointer to the {\bf save\_pkt} structure for the file
1166 to be backed up.  The plugin is responsible for filling in all the fields 
1167 of the {\bf save\_pkt}. If you are backing up
1168 a real file, then generally, the statp structure can be filled in by doing
1169 a {\bf stat} system call on the file.  
1170
1171 If you are backing up a database or
1172 something that is more complex, you might want to create a virtual file.
1173 That is a file that does not actually exist on the filesystem, but represents 
1174 say an object that you are backing up.  In that case, you need to ensure
1175 that the {\bf fname} string that you pass back is unique so that it
1176 does not conflict with a real file on the system, and you need to 
1177 artifically create values in the statp packet.
1178
1179 Example programs such as {\bf bpipe-fd.c} show how to set these fields.
1180 You must take care not to store pointers the stack in the pointer fields such
1181 as fname and link, because when you return from your function, your stack entries
1182 will be destroyed. The solution in that case is to malloc() and return the pointer
1183 to it. In order to not have memory leaks, you should store a pointer to all memory
1184 allocated in your pContext structure so that in subsequent calls or at termination,
1185 you can release it back to the system.
1186
1187 Once the backup has begun, Bacula will call your plugin at the {\bf pluginIO}
1188 entry point to "read" the data to be backed up.  Please see the {\bf bpipe-fd.c}
1189 plugin for how to do I/O.
1190
1191 Example of filling in the save\_pkt as used in bpipe-fd.c:
1192
1193 \begin{verbatim}
1194    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
1195    time_t now = time(NULL);
1196    sp->fname = p_ctx->fname;
1197    sp->statp.st_mode = 0700 | S_IFREG;
1198    sp->statp.st_ctime = now;
1199    sp->statp.st_mtime = now;
1200    sp->statp.st_atime = now;
1201    sp->statp.st_size = -1;
1202    sp->statp.st_blksize = 4096;
1203    sp->statp.st_blocks = 1;
1204    p_ctx->backup = true;
1205    return bRC_OK; 
1206 \end{verbatim}
1207
1208 Note: the filename to be created has already been created from the 
1209 command string previously sent to the plugin and is in the plugin 
1210 context (p\_ctx->fname) and is a malloc()ed string.  This example
1211 creates a regular file (S\_IFREG), with various fields being created.
1212
1213 In general, the sequence of commands issued from Bacula to the plugin
1214 to do a backup while processing the "Plugin = " directive are:
1215
1216 \begin{enumerate}
1217  \item generate a bEventBackupCommand event to the specified plugin
1218        and pass it the command string.
1219  \item make a startPluginBackup call to the plugin, which
1220        fills in the data needed in save\_pkt to save as the file
1221        attributes and to put on the Volume and in the catalog.
1222  \item call Bacula's internal save\_file() subroutine to save the specified
1223        file.  The plugin will then be called at pluginIO() to "open"
1224        the file, and then to read the file data.
1225        Note, if you are dealing with a virtual file, the "open" operation
1226        is something the plugin does internally and it doesn't necessarily
1227        mean opening a file on the filesystem.  For example in the case of
1228        the bpipe-fd.c program, it initiates a pipe to the requested program.
1229        Finally when the plugin signals to Bacula that all the data was read,
1230        Bacula will call the plugin with the "close" pluginIO() function.
1231 \end{enumerate}
1232
1233
1234 \subsection{endBackupFile(bpContext *ctx)}
1235 Called at the end of backing up a file.  If the plugin's work
1236 is done, it should return bRC\_OK.  If the plugin wishes to create another
1237 file and back it up, then it must return bRC\_More (not yet implemented).
1238 This is probably a good time to release any malloc()ed memory you used to
1239 pass back filenames.
1240
1241 \subsection{startRestoreFile(bpContext *ctx, const char *cmd)}
1242 Not yet implemented.
1243
1244 \subsection{createFile(bpContext *ctx, struct restore\_pkt *rp)}
1245 Called to create a file during a Restore job before restoring the data. 
1246 This entry point is called before any I/O is done on the file.  After
1247 this call, Bacula will call pluginIO() to open the file for write.
1248
1249 The data in the 
1250 restore\_pkt is passed to the plugin and is based on the data that was
1251 originally given by the plugin during the backup and the current user
1252 restore settings (e.g. where, RegexWhere, replace).  This allows the
1253 plugin to first create a file (if necessary) so that the data can
1254 be transmitted to it.  The next call to the plugin will be a
1255 pluginIO command with a request to open the file write-only.
1256
1257 This call must return one of the following values:
1258
1259 \begin{verbatim}
1260  enum {
1261    CF_SKIP = 1,       /* skip file (not newer or something) */
1262    CF_ERROR,          /* error creating file */
1263    CF_EXTRACT,        /* file created, data to extract */
1264    CF_CREATED         /* file created, no data to extract */
1265 };
1266 \end{verbatim}
1267
1268 in the restore\_pkt value {\bf create\_status}.  For a normal file,
1269 unless there is an error, you must return {\bf CF\_EXTRACT}.
1270
1271 \begin{verbatim}
1272  
1273 struct restore_pkt {
1274    int32_t pkt_size;                  /* size of this packet */
1275    int32_t stream;                    /* attribute stream id */
1276    int32_t data_stream;               /* id of data stream to follow */
1277    int32_t type;                      /* file type FT */
1278    int32_t file_index;                /* file index */
1279    int32_t LinkFI;                    /* file index to data if hard link */
1280    uid_t uid;                         /* userid */
1281    struct stat statp;                 /* decoded stat packet */
1282    const char *attrEx;                /* extended attributes if any */
1283    const char *ofname;                /* output filename */
1284    const char *olname;                /* output link name */
1285    const char *where;                 /* where */
1286    const char *RegexWhere;            /* regex where */
1287    int replace;                       /* replace flag */
1288    int create_status;                 /* status from createFile() */
1289    int32_t pkt_end;                   /* end packet sentinel */
1290
1291 };
1292 \end{verbatim}
1293
1294 \subsection{setFileAttributes(bpContext *ctx, struct restore\_pkt *rp)}
1295 This is call not yet implemented.
1296
1297 See the definition of {\bf restre\_pkt} in the above section.
1298
1299 \subsection{endRestoreFile(bpContext *ctx)}
1300 Called when done restoring a file.
1301
1302 \subsection{pluginIO(bpContext *ctx, struct io\_pkt *io)}
1303 Called to do the input (backup) or output (restore) of data from or to a
1304 file. These routines simulate the Unix read(), write(), open(), close(), 
1305 and lseek() I/O calls, and the arguments are passed in the packet and
1306 the return values are also placed in the packet.  In addition for Win32
1307 systems the plugin must return two additional values (described below).
1308
1309 \begin{verbatim}
1310  enum {
1311    IO_OPEN = 1,
1312    IO_READ = 2,
1313    IO_WRITE = 3,
1314    IO_CLOSE = 4,
1315    IO_SEEK = 5
1316 };
1317
1318 struct io_pkt {
1319    int32_t pkt_size;                  /* Size of this packet */
1320    int32_t func;                      /* Function code */
1321    int32_t count;                     /* read/write count */
1322    mode_t mode;                       /* permissions for created files */
1323    int32_t flags;                     /* Open flags */
1324    char *buf;                         /* read/write buffer */
1325    const char *fname;                 /* open filename */
1326    int32_t status;                    /* return status */
1327    int32_t io_errno;                  /* errno code */
1328    int32_t lerror;                    /* Win32 error code */
1329    int32_t whence;                    /* lseek argument */
1330    boffset_t offset;                  /* lseek argument */
1331    bool win32;                        /* Win32 GetLastError returned */
1332    int32_t pkt_end;                   /* end packet sentinel */
1333 };
1334 \end{verbatim}
1335
1336 The particular Unix function being simulated is indicated by the {\bf func}, 
1337 which will have one of the IO\_OPEN, IO\_READ, ... codes listed above.  
1338 The status code that would be returned from a Unix call is returned in
1339 {\bf status} for IO\_OPEN, IO\_CLOSE, IO\_READ, and IO\_WRITE. The return value for 
1340 IO\_SEEK is returned in {\bf offset} which in general is a 64 bit value.
1341
1342 When there is an error on Unix systems, you must always set io\_error, and
1343 on a Win32 system, you must always set win32, and the returned value from
1344 the OS call GetLastError() in lerror.
1345
1346 For all except IO\_SEEK, {\bf status} is the return result.  In general it is
1347 a positive integer unless there is an error in which case it is -1.
1348
1349 The following describes each call and what you get and what you
1350 should return:
1351
1352 \begin{description}
1353  \item [IO\_OPEN]
1354    You will be passed fname, mode, and flags.
1355    You must set on return: status, and if there is a Unix error
1356    io\_errno must be set to the errno value, and if there is a 
1357    Win32 error win32 and lerror. 
1358
1359  \item [IO\_READ]
1360   You will be passed: count, and buf (buffer of size count).
1361   You must set on return: status to the number of bytes 
1362   read into the buffer (buf) or -1 on an error, 
1363   and if there is a Unix error
1364   io\_errno must be set to the errno value, and if there is a
1365   Win32 error, win32 and lerror must be set.
1366
1367  \item [IO\_WRITE]
1368   You will be passed: count, and buf (buffer of size count).
1369   You must set on return: status to the number of bytes 
1370   written from the buffer (buf) or -1 on an error, 
1371   and if there is a Unix error
1372   io\_errno must be set to the errno value, and if there is a
1373   Win32 error, win32 and lerror must be set.
1374
1375  \item [IO\_CLOSE]
1376   Nothing will be passed to you.  On return you must set 
1377   status to 0 on success and -1 on failure.  If there is a Unix error
1378   io\_errno must be set to the errno value, and if there is a
1379   Win32 error, win32 and lerror must be set.
1380
1381  \item [IO\_LSEEK]
1382   You will be passed: offset, and whence. offset is a 64 bit value
1383   and is the position to seek to relative to whence.  whence is one
1384   of the following SEEK\_SET, SEEK\_CUR, or SEEK\_END indicating to
1385   either to seek to an absolute possition, relative to the current 
1386   position or relative to the end of the file.
1387   You must pass back in offset the absolute location to which you 
1388   seeked. If there is an error, offset should be set to -1.
1389   If there is a Unix error
1390   io\_errno must be set to the errno value, and if there is a
1391   Win32 error, win32 and lerror must be set.
1392
1393   Note: Bacula will call IO\_SEEK only when writing a sparse file.
1394   
1395 \end{description}
1396
1397
1398