3 Copyright (C) 2007-2010 Kern Sibbald
5 You may freely use this code to create your own plugin provided
6 it is to write a plugin for Bacula licensed under AGPLv3
7 (as Bacula is), and in that case, you may also remove
8 the above Copyright and this notice as well as modify
14 #define BUILDING_DLL /* required for Windows plugin */
17 #include "fd_plugins.h"
23 #define PLUGIN_LICENSE "AGPLv3"
24 #define PLUGIN_AUTHOR "Your name"
25 #define PLUGIN_DATE "January 2010"
26 #define PLUGIN_VERSION "1"
27 #define PLUGIN_DESCRIPTION "Test File Daemon Plugin"
29 /* Forward referenced functions */
30 static bRC newPlugin(bpContext *ctx);
31 static bRC freePlugin(bpContext *ctx);
32 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
33 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
34 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
35 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
36 static bRC endBackupFile(bpContext *ctx);
37 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
38 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
39 static bRC endRestoreFile(bpContext *ctx);
40 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
41 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
44 /* Pointers to Bacula functions */
45 static bFuncs *bfuncs = NULL;
46 static bInfo *binfo = NULL;
48 static pInfo pluginInfo = {
50 FD_PLUGIN_INTERFACE_VERSION,
59 static pFuncs pluginFuncs = {
61 FD_PLUGIN_INTERFACE_VERSION,
63 /* Entry points into plugin */
64 newPlugin, /* new plugin instance */
65 freePlugin, /* free plugin instance */
79 * Plugin called here when it is first loaded
82 loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
84 bfuncs = lbfuncs; /* set Bacula funct pointers */
86 printf("plugin: Loaded: size=%d version=%d\n", bfuncs->size, bfuncs->version);
88 *pinfo = &pluginInfo; /* return pointer to our info */
89 *pfuncs = &pluginFuncs; /* return pointer to our functions */
95 * Plugin called here when it is unloaded, normally when
96 * Bacula is going to exit.
101 printf("plugin: Unloaded\n");
106 * Called here to make a new instance of the plugin -- i.e. when
107 * a new Job is started. There can be multiple instances of
108 * each plugin that are running at the same time. Your
109 * plugin instance must be thread safe and keep its own
112 static bRC newPlugin(bpContext *ctx)
115 bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId);
116 // printf("plugin: newPlugin JobId=%d\n", JobId);
117 bfuncs->registerBaculaEvents(ctx, 1, 2, 0);
122 * Release everything concerning a particular instance of a
123 * plugin. Normally called when the Job terminates.
125 static bRC freePlugin(bpContext *ctx)
128 bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId);
129 // printf("plugin: freePlugin JobId=%d\n", JobId);
134 * Called by core code to get a variable from the plugin.
135 * Not currently used.
137 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
139 // printf("plugin: getPluginValue var=%d\n", var);
144 * Called by core code to set a plugin variable.
145 * Not currently used.
147 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
149 // printf("plugin: setPluginValue var=%d\n", var);
154 * Called by Bacula when there are certain events that the
155 * plugin might want to know. The value depends on the
158 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
162 switch (event->eventType) {
164 printf("plugin: JobStart=%s\n", NPRT((char *)value));
167 printf("plugin: JobEnd\n");
169 case bEventStartBackupJob:
170 printf("plugin: BackupStart\n");
172 case bEventEndBackupJob:
173 printf("plugin: BackupEnd\n");
176 printf("plugin: JobLevel=%c %d\n", (int64_t)value, (int64_t)value);
179 printf("plugin: since=%d\n", (int64_t)value);
181 case bEventStartRestoreJob:
182 printf("plugin: StartRestoreJob\n");
184 case bEventEndRestoreJob:
185 printf("plugin: EndRestoreJob\n");
188 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:command */
189 case bEventRestoreCommand:
190 printf("plugin: backup command=%s\n", NPRT((char *)value));
193 case bEventBackupCommand:
194 printf("plugin: backup command=%s\n", NPRT((char *)value));
198 printf("plugin: unknown event=%d\n", event->eventType);
200 bfuncs->getBaculaValue(ctx, bVarFDName, (void *)&name);
201 // printf("FD Name=%s\n", name);
202 // bfuncs->JobMessage(ctx, __FILE__, __LINE__, 1, 0, "JobMesssage message");
203 // bfuncs->DebugMessage(ctx, __FILE__, __LINE__, 1, "DebugMesssage message");
208 * Called when starting to backup a file. Here the plugin must
209 * return the "stat" packet for the directory/file and provide
210 * certain information so that Bacula knows what the file is.
211 * The plugin can create "Virtual" files by giving them a
212 * name that is not normally found on the file system.
214 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
220 * Done backing up a file.
222 static bRC endBackupFile(bpContext *ctx)
228 * Do actual I/O. Bacula calls this after startBackupFile
229 * or after startRestoreFile to do the actual file
232 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
238 printf("plugin: IO_OPEN\n");
241 printf("plugin: IO_READ buf=%p len=%d\n", io->buf, io->count);
244 printf("plugin: IO_WRITE buf=%p len=%d\n", io->buf, io->count);
247 printf("plugin: IO_CLOSE\n");
253 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
258 static bRC endRestoreFile(bpContext *ctx)
264 * Called here to give the plugin the information needed to
265 * re-create the file on a restore. It basically gets the
266 * stat packet that was created during the backup phase.
267 * This data is what is needed to create the file, but does
268 * not contain actual file data.
270 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
276 * Called after the file has been restored. This can be used to
277 * set directory permissions, ...
279 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)