2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2010 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation, which is
11 listed in the file LICENSE.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * A simple test plugin for the Bacula File Daemon derived from
30 * the bpipe plugin, but used for testing new features.
32 * Kern Sibbald, October 2007
36 #include "fd_plugins.h"
49 static const int dbglvl = 000;
51 #define PLUGIN_LICENSE "Bacula GPLv2"
52 #define PLUGIN_AUTHOR "Kern Sibbald"
53 #define PLUGIN_DATE "April 2010"
54 #define PLUGIN_VERSION "1"
55 #define PLUGIN_DESCRIPTION "Bacula Test File Daemon Plugin"
57 /* Forward referenced functions */
58 static bRC newPlugin(bpContext *ctx);
59 static bRC freePlugin(bpContext *ctx);
60 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
61 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
62 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
63 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
64 static bRC endBackupFile(bpContext *ctx);
65 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
66 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
67 static bRC endRestoreFile(bpContext *ctx);
68 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
69 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
71 /* Pointers to Bacula functions */
72 static bFuncs *bfuncs = NULL;
73 static bInfo *binfo = NULL;
75 /* Plugin Information block */
76 static pInfo pluginInfo = {
78 FD_PLUGIN_INTERFACE_VERSION,
87 /* Plugin entry points for Bacula */
88 static pFuncs pluginFuncs = {
90 FD_PLUGIN_INTERFACE_VERSION,
92 /* Entry points into plugin */
93 newPlugin, /* new plugin instance */
94 freePlugin, /* free plugin instance */
108 * Plugin private context
112 FILE *fd; /* pipe file descriptor */
113 bool backup; /* set for backup (not needed) */
114 char *cmd; /* plugin command line */
115 char *fname; /* filename to "backup/restore" */
116 char *reader; /* reader program for backup */
117 char *writer; /* writer program for backup */
124 * loadPlugin() and unloadPlugin() are entry points that are
125 * exported, so Bacula can directly call these two entry points
126 * they are common to all Bacula plugins.
129 * External entry point called by Bacula to "load the plugin
131 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
133 bfuncs = lbfuncs; /* set Bacula funct pointers */
135 *pinfo = &pluginInfo; /* return pointer to our info */
136 *pfuncs = &pluginFuncs; /* return pointer to our functions */
142 * External entry point to unload the plugin
146 // printf("test-plugin-fd: Unloaded\n");
151 * The following entry points are accessed through the function
152 * pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
153 * has its own set of entry points that the plugin must define.
156 * Create a new instance of the plugin i.e. allocate our private storage
158 static bRC newPlugin(bpContext *ctx)
160 struct plugin_ctx *p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx));
164 memset(p_ctx, 0, sizeof(struct plugin_ctx));
165 ctx->pContext = (void *)p_ctx; /* set our context pointer */
170 * Free a plugin instance, i.e. release our private storage
172 static bRC freePlugin(bpContext *ctx)
174 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
179 free(p_ctx->cmd); /* free any allocated command string */
181 free(p_ctx); /* free our private context */
187 * Return some plugin value (none defined)
189 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
195 * Set a plugin value (none defined)
197 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
203 * Handle an event that was generated in Bacula
205 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
207 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
208 restore_object_pkt *rop;
216 * Most events don't interest us so we ignore them.
217 * the printfs are so that plugin writers can enable them to see
218 * what is really going on.
220 switch (event->eventType) {
222 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "test-plugin-fd: JobStart=%s\n", (char *)value);
225 case bEventEndBackupJob:
228 case bEventStartRestoreJob:
229 case bEventEndRestoreJob:
231 /* End of Dir FileSet commands, now we can add excludes */
232 case bEventEndFileSet:
233 bfuncs->NewOptions(ctx);
234 bfuncs->AddWild(ctx, "*.c", ' ');
235 bfuncs->AddWild(ctx, "*.cpp", ' ');
236 bfuncs->AddOptions(ctx, "ei"); /* exclude, ignore case */
237 bfuncs->AddExclude(ctx, "/home/kern/bacula/regress/README");
239 case bEventStartBackupJob:
241 case bEventRestoreObject:
242 printf("Plugin RestoreObject\n");
244 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "test-plugin-fd: End restore objects\n");
247 rop = (restore_object_pkt *)value;
248 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "test-plugin-fd: len=%d JobId=%d oname=%s\n",
249 rop->object_len, rop->JobId, rop->object_name);
251 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
252 case bEventRestoreCommand:
253 /* Fall-through wanted */
254 case bEventBackupCommand:
256 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "test-plugin-fd: pluginEvent cmd=%s\n", (char *)value);
257 p_ctx->cmd = strdup((char *)value);
258 p = strchr(p_ctx->cmd, ':');
260 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Plugin terminator not found: %s\n", (char *)value);
263 *p++ = 0; /* terminate plugin */
267 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "File terminator not found: %s\n", (char *)value);
270 *p++ = 0; /* terminate file */
274 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Reader terminator not found: %s\n", (char *)value);
277 *p++ = 0; /* terminate reader string */
279 printf("test-plugin-fd: plugin=%s fname=%s reader=%s writer=%s\n",
280 p_ctx->cmd, p_ctx->fname, p_ctx->reader, p_ctx->writer);
284 printf("test-plugin-fd: unknown event=%d\n", event->eventType);
291 * Start the backup of a specific file
293 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
295 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
299 time_t now = time(NULL);
300 sp->object_name = (char *)"james.xml";
301 sp->object = (char *)"This is test data for the restore object.";
302 sp->object_len = strlen(sp->object);
304 sp->type = FT_RESTORE_FIRST;
305 sp->statp.st_mode = 0700 | S_IFREG;
306 sp->statp.st_ctime = now;
307 sp->statp.st_mtime = now;
308 sp->statp.st_atime = now;
309 sp->statp.st_size = sp->object_len;
310 sp->statp.st_blksize = 4096;
311 sp->statp.st_blocks = 1;
312 p_ctx->backup = true;
313 printf("test-plugin-fd: startBackupFile\n");
318 * Done with backup of this file
320 static bRC endBackupFile(bpContext *ctx)
323 * We would return bRC_More if we wanted startBackupFile to be
324 * called again to backup another file
331 * Bacula is calling us to do the actual I/O
333 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
335 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
346 * Bacula is notifying us that a plugin name string was found, and
347 * passing us the plugin command, so we can prepare for a restore.
349 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
351 printf("test-plugin-fd: startRestoreFile cmd=%s\n", cmd);
356 * Bacula is notifying us that the plugin data has terminated, so
357 * the restore for this particular file is done.
359 static bRC endRestoreFile(bpContext *ctx)
361 printf("test-plugin-fd: endRestoreFile\n");
366 * This is called during restore to create the file (if necessary)
367 * We must return in rp->create_status:
370 * CF_SKIP -- skip processing this file
371 * CF_EXTRACT -- extract the file (i.e.call i/o routines)
372 * CF_CREATED -- created, but no content to extract (typically directories)
375 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
377 printf("test-plugin-fd: createFile\n");
378 if (strlen(rp->where) > 512) {
379 printf("Restore target dir too long. Restricting to first 512 bytes.\n");
381 strncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, 513);
382 ((struct plugin_ctx *)ctx->pContext)->replace = rp->replace;
383 rp->create_status = CF_EXTRACT;
388 * We will get here if the File is a directory after everything
389 * is written in the directory.
391 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
393 printf("test-plugin-fd: setFileAttributes\n");