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 case bEventStartBackupJob:
232 bfuncs->AddExclude(ctx, "/home/kern/bacula/regress/README");
234 case bEventRestoreObject:
235 printf("Plugin RestoreObject\n");
237 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "test-plugin-fd: End restore objects\n");
240 rop = (restore_object_pkt *)value;
241 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "test-plugin-fd: len=%d JobId=%d oname=%s\n",
242 rop->object_len, rop->JobId, rop->object_name);
244 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
245 case bEventRestoreCommand:
246 /* Fall-through wanted */
247 case bEventBackupCommand:
249 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "test-plugin-fd: pluginEvent cmd=%s\n", (char *)value);
250 p_ctx->cmd = strdup((char *)value);
251 p = strchr(p_ctx->cmd, ':');
253 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Plugin terminator not found: %s\n", (char *)value);
256 *p++ = 0; /* terminate plugin */
260 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "File terminator not found: %s\n", (char *)value);
263 *p++ = 0; /* terminate file */
267 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Reader terminator not found: %s\n", (char *)value);
270 *p++ = 0; /* terminate reader string */
272 printf("test-plugin-fd: plugin=%s fname=%s reader=%s writer=%s\n",
273 p_ctx->cmd, p_ctx->fname, p_ctx->reader, p_ctx->writer);
277 printf("test-plugin-fd: unknown event=%d\n", event->eventType);
284 * Start the backup of a specific file
286 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
288 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
292 time_t now = time(NULL);
293 sp->fname = (char *)"james.xml";
294 sp->object = (char *)"This is test data for the restore object.";
295 sp->object_len = strlen(sp->object);
297 sp->type = FT_RESTORE_FIRST;
298 sp->statp.st_mode = 0700 | S_IFREG;
299 sp->statp.st_ctime = now;
300 sp->statp.st_mtime = now;
301 sp->statp.st_atime = now;
302 sp->statp.st_size = sp->object_len;
303 sp->statp.st_blksize = 4096;
304 sp->statp.st_blocks = 1;
305 p_ctx->backup = true;
306 printf("test-plugin-fd: startBackupFile\n");
311 * Done with backup of this file
313 static bRC endBackupFile(bpContext *ctx)
316 * We would return bRC_More if we wanted startBackupFile to be
317 * called again to backup another file
324 * Bacula is calling us to do the actual I/O
326 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
328 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
339 * Bacula is notifying us that a plugin name string was found, and
340 * passing us the plugin command, so we can prepare for a restore.
342 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
344 printf("test-plugin-fd: startRestoreFile cmd=%s\n", cmd);
349 * Bacula is notifying us that the plugin data has terminated, so
350 * the restore for this particular file is done.
352 static bRC endRestoreFile(bpContext *ctx)
354 printf("test-plugin-fd: endRestoreFile\n");
359 * This is called during restore to create the file (if necessary)
360 * We must return in rp->create_status:
363 * CF_SKIP -- skip processing this file
364 * CF_EXTRACT -- extract the file (i.e.call i/o routines)
365 * CF_CREATED -- created, but no content to extract (typically directories)
368 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
370 printf("test-plugin-fd: createFile\n");
371 if (strlen(rp->where) > 512) {
372 printf("Restore target dir too long. Restricting to first 512 bytes.\n");
374 strncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, 513);
375 ((struct plugin_ctx *)ctx->pContext)->replace = rp->replace;
376 rp->create_status = CF_EXTRACT;
381 * We will get here if the File is a directory after everything
382 * is written in the directory.
384 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
386 printf("test-plugin-fd: setFileAttributes\n");