2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2008 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 John Walker.
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 pipe plugin for the Bacula File Daemon
31 * Kern Sibbald, October 2007
34 #include "fd-plugins.h"
44 #define PLUGIN_LICENSE "GPLv2"
45 #define PLUGIN_AUTHOR "Kern Sibbald"
46 #define PLUGIN_DATE "January 2008"
47 #define PLUGIN_VERSION "1"
48 #define PLUGIN_DESCRIPTION "Pipe File Daemon Plugin"
50 /* Forward referenced functions */
51 static bRC newPlugin(bpContext *ctx);
52 static bRC freePlugin(bpContext *ctx);
53 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
54 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
55 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
56 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
57 static bRC endBackupFile(bpContext *ctx);
58 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
59 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
60 static bRC endRestoreFile(bpContext *ctx);
61 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
62 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
65 /* Pointers to Bacula functions */
66 static bFuncs *bfuncs = NULL;
67 static bInfo *binfo = NULL;
69 /* Plugin Information block */
70 static pInfo pluginInfo = {
72 FD_PLUGIN_INTERFACE_VERSION,
81 /* Plugin entry points for Bacula */
82 static pFuncs pluginFuncs = {
84 FD_PLUGIN_INTERFACE_VERSION,
86 /* Entry points into plugin */
87 newPlugin, /* new plugin instance */
88 freePlugin, /* free plugin instance */
102 * Plugin private context
106 FILE *fd; /* pipe file descriptor */
107 bool backup; /* set for backup (not needed) */
108 char *cmd; /* plugin command line */
109 char *fname; /* filename to "backup/restore" */
110 char *reader; /* reader program for backup */
111 char *writer; /* writer program for backup */
115 * loadPlugin() and unloadPlugin() are entry points that are
116 * exported, so Bacula can directly call these two entry points
117 * they are common to all Bacula plugins.
120 * External entry point called by Bacula to "load the plugin
122 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
124 bfuncs = lbfuncs; /* set Bacula funct pointers */
126 *pinfo = &pluginInfo; /* return pointer to our info */
127 *pfuncs = &pluginFuncs; /* return pointer to our functions */
133 * External entry point to unload the plugin
137 // printf("bpipe-fd: Unloaded\n");
142 * The following entry points are accessed through the function
143 * pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
144 * has its own set of entry points that the plugin must define.
147 * Create a new instance of the plugin i.e. allocate our private storage
149 static bRC newPlugin(bpContext *ctx)
151 struct plugin_ctx *p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx));
152 memset(p_ctx, 0, sizeof(struct plugin_ctx));
153 ctx->pContext = (void *)p_ctx; /* set our context pointer */
158 * Free a plugin instance, i.e. release our private storage
160 static bRC freePlugin(bpContext *ctx)
162 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
164 free(p_ctx->cmd); /* free any allocated command string */
166 free(p_ctx); /* free our private context */
171 * Return some plugin value (none defined)
173 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
179 * Set a plugin value (none defined)
181 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
187 * Handle an event that was generated in Bacula
189 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
191 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
194 switch (event->eventType) {
196 // printf("bpipe-fd: JobStart=%s\n", (char *)value);
199 // printf("bpipe-fd: JobEnd\n");
201 case bEventStartBackupJob:
202 // printf("bpipe-fd: BackupStart\n");
204 case bEventEndBackupJob:
205 // printf("bpipe-fd: BackupEnd\n");
208 // printf("bpipe-fd: JobLevel=%c %d\n", (int)value, (int)value);
211 // printf("bpipe-fd: since=%d\n", (int)value);
214 case bEventStartRestoreJob:
217 case bEventEndRestoreJob:
220 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
221 case bEventRestoreCommand:
222 printf("bpipe-fd: EventRestoreCommand cmd=%s\n", (char *)value);
223 case bEventBackupCommand:
225 printf("bpipe-fd: pluginEvent cmd=%s\n", (char *)value);
226 p_ctx->cmd = strdup((char *)value);
227 p = strchr(p_ctx->cmd, ':');
229 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Plugin terminator not found: %s\n", (char *)value);
232 *p++ = 0; /* terminate plugin */
236 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "File terminator not found: %s\n", (char *)value);
239 *p++ = 0; /* terminate file */
243 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Reader terminator not found: %s\n", (char *)value);
246 *p++ = 0; /* terminate reader string */
248 printf("bpipe-fd: plugin=%s fname=%s reader=%s writer=%s\n",
249 p_ctx->cmd, p_ctx->fname, p_ctx->reader, p_ctx->writer);
253 printf("bpipe-fd: unknown event=%d\n", event->eventType);
259 * Start the backup of a specific file
261 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
263 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
264 time_t now = time(NULL);
265 sp->fname = p_ctx->fname;
266 sp->statp.st_mode = 0700 | S_IFREG;
267 sp->statp.st_ctime = now;
268 sp->statp.st_mtime = now;
269 sp->statp.st_atime = now;
270 sp->statp.st_size = -1;
271 sp->statp.st_blksize = 4096;
272 sp->statp.st_blocks = 1;
273 p_ctx->backup = true;
274 // printf("bpipe-fd: startBackupFile\n");
279 * Done with backup of this file
281 static bRC endBackupFile(bpContext *ctx)
284 * We would return bRC_More if we wanted startBackupFile to be
285 * called again to backup another file
291 * Bacula is calling us to do the actual I/O
293 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
295 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
301 // printf("bpipe-fd: IO_OPEN\n");
302 if (io->flags & (O_CREAT | O_WRONLY)) {
303 p_ctx->fd = popen(p_ctx->writer, "w");
304 printf("bpipe-fd: IO_OPEN writer=%s\n", p_ctx->writer);
306 io->io_errno = errno;
307 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0,
308 "Open pipe writer=%s failed: ERR=%s\n", p_ctx->writer, strerror(errno));
312 p_ctx->fd = popen(p_ctx->reader, "r");
313 // printf("bpipe-fd: IO_OPEN reader=%s\n", p_ctx->reader);
315 io->io_errno = errno;
316 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0,
317 "Open pipe reader=%s failed: ERR=%s\n", p_ctx->reader, strerror(errno));
321 sleep(1); /* let pipe connect */
326 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Logic error: NULL read FD\n");
329 io->status = fread(io->buf, 1, io->count, p_ctx->fd);
330 // printf("bpipe-fd: IO_READ buf=%p len=%d\n", io->buf, io->status);
331 if (io->status == 0 && ferror(p_ctx->fd)) {
332 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0,
333 "Pipe read error: ERR=%s\n", strerror(errno));
334 // printf("Error reading pipe\n");
341 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Logic error: NULL write FD\n");
344 // printf("bpipe-fd: IO_WRITE fd=%p buf=%p len=%d\n", p_ctx->fd, io->buf, io->count);
345 io->status = fwrite(io->buf, 1, io->count, p_ctx->fd);
346 // printf("bpipe-fd: IO_WRITE buf=%p len=%d\n", io->buf, io->status);
347 if (io->status == 0 && ferror(p_ctx->fd)) {
348 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0,
349 "Pipe write error\n");
350 // printf("Error writing pipe\n");
357 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Logic error: NULL FD\n");
360 io->status = pclose(p_ctx->fd);
364 io->offset = p_ctx->offset;
370 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
372 // printf("bpipe-fd: startRestoreFile cmd=%s\n", cmd);
376 static bRC endRestoreFile(bpContext *ctx)
378 // printf("bpipe-fd: endRestoreFile\n");
382 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
384 // printf("bpipe-fd: createFile\n");
388 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
390 // printf("bpipe-fd: setFileAttributes\n");