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