2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 #define BUILDING_DLL /* required for Windows plugin */
23 #include "fd_plugins.h"
29 #define PLUGIN_LICENSE "AGPLv3"
30 #define PLUGIN_AUTHOR "Your name"
31 #define PLUGIN_DATE "January 2010"
32 #define PLUGIN_VERSION "1"
33 #define PLUGIN_DESCRIPTION "Test File Daemon Plugin"
35 /* Forward referenced functions */
36 static bRC newPlugin(bpContext *ctx);
37 static bRC freePlugin(bpContext *ctx);
38 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
39 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
40 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
41 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
42 static bRC endBackupFile(bpContext *ctx);
43 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
44 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
45 static bRC endRestoreFile(bpContext *ctx);
46 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
47 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
48 static bRC checkFile(bpContext *ctx, char *fname);
51 /* Pointers to Bacula functions */
52 static bFuncs *bfuncs = NULL;
53 static bInfo *binfo = NULL;
55 static pInfo pluginInfo = {
57 FD_PLUGIN_INTERFACE_VERSION,
66 static pFuncs pluginFuncs = {
68 FD_PLUGIN_INTERFACE_VERSION,
70 /* Entry points into plugin */
71 newPlugin, /* new plugin instance */
72 freePlugin, /* free plugin instance */
87 * Plugin called here when it is first loaded
90 loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
92 bfuncs = lbfuncs; /* set Bacula funct pointers */
94 printf("plugin: Loaded: size=%d version=%d\n", bfuncs->size, bfuncs->version);
96 *pinfo = &pluginInfo; /* return pointer to our info */
97 *pfuncs = &pluginFuncs; /* return pointer to our functions */
103 * Plugin called here when it is unloaded, normally when
104 * Bacula is going to exit.
109 printf("plugin: Unloaded\n");
114 * Called here to make a new instance of the plugin -- i.e. when
115 * a new Job is started. There can be multiple instances of
116 * each plugin that are running at the same time. Your
117 * plugin instance must be thread safe and keep its own
120 static bRC newPlugin(bpContext *ctx)
123 bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId);
124 // printf("plugin: newPlugin JobId=%d\n", JobId);
125 bfuncs->registerBaculaEvents(ctx, 1, 2, 0);
130 * Release everything concerning a particular instance of a
131 * plugin. Normally called when the Job terminates.
133 static bRC freePlugin(bpContext *ctx)
136 bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId);
137 // printf("plugin: freePlugin JobId=%d\n", JobId);
142 * Called by core code to get a variable from the plugin.
143 * Not currently used.
145 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
147 // printf("plugin: getPluginValue var=%d\n", var);
152 * Called by core code to set a plugin variable.
153 * Not currently used.
155 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
157 // printf("plugin: setPluginValue var=%d\n", var);
162 * Called by Bacula when there are certain events that the
163 * plugin might want to know. The value depends on the
166 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
170 switch (event->eventType) {
172 printf("plugin: JobStart=%s\n", NPRT((char *)value));
175 printf("plugin: JobEnd\n");
177 case bEventStartBackupJob:
178 printf("plugin: BackupStart\n");
180 case bEventEndBackupJob:
181 printf("plugin: BackupEnd\n");
184 printf("plugin: JobLevel=%c %d\n", (int64_t)value, (int64_t)value);
187 printf("plugin: since=%d\n", (int64_t)value);
189 case bEventStartRestoreJob:
190 printf("plugin: StartRestoreJob\n");
192 case bEventEndRestoreJob:
193 printf("plugin: EndRestoreJob\n");
195 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:command */
196 case bEventRestoreCommand:
197 printf("plugin: backup command=%s\n", NPRT((char *)value));
199 case bEventBackupCommand:
200 printf("plugin: backup command=%s\n", NPRT((char *)value));
203 case bEventComponentInfo:
204 printf("plugin: Component=%s\n", NPRT((char *)value));
208 printf("plugin: unknown event=%d\n", event->eventType);
210 bfuncs->getBaculaValue(ctx, bVarFDName, (void *)&name);
211 // printf("FD Name=%s\n", name);
212 // bfuncs->JobMessage(ctx, __FILE__, __LINE__, 1, 0, "JobMesssage message");
213 // bfuncs->DebugMessage(ctx, __FILE__, __LINE__, 1, "DebugMesssage message");
218 * Called when starting to backup a file. Here the plugin must
219 * return the "stat" packet for the directory/file and provide
220 * certain information so that Bacula knows what the file is.
221 * The plugin can create "Virtual" files by giving them a
222 * name that is not normally found on the file system.
224 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
230 * Done backing up a file.
232 static bRC endBackupFile(bpContext *ctx)
238 * Do actual I/O. Bacula calls this after startBackupFile
239 * or after startRestoreFile to do the actual file
242 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
248 printf("plugin: IO_OPEN\n");
251 printf("plugin: IO_READ buf=%p len=%d\n", io->buf, io->count);
254 printf("plugin: IO_WRITE buf=%p len=%d\n", io->buf, io->count);
257 printf("plugin: IO_CLOSE\n");
263 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
268 static bRC endRestoreFile(bpContext *ctx)
274 * Called here to give the plugin the information needed to
275 * re-create the file on a restore. It basically gets the
276 * stat packet that was created during the backup phase.
277 * This data is what is needed to create the file, but does
278 * not contain actual file data.
280 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
286 * Called after the file has been restored. This can be used to
287 * set directory permissions, ...
289 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
294 /* When using Incremental dump, all previous dumps are necessary */
295 static bRC checkFile(bpContext *ctx, char *fname)