2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2009 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 pipe plugin for the Bacula File Daemon
31 * Kern Sibbald, October 2007
35 #include "fd_plugins.h"
48 static const int dbglvl = 150;
50 #define PLUGIN_LICENSE "Bacula GPLv2"
51 #define PLUGIN_AUTHOR "Kern Sibbald"
52 #define PLUGIN_DATE "January 2008"
53 #define PLUGIN_VERSION "1"
54 #define PLUGIN_DESCRIPTION "Bacula Pipe File Daemon Plugin"
56 /* Forward referenced functions */
57 static bRC newPlugin(bpContext *ctx);
58 static bRC freePlugin(bpContext *ctx);
59 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
60 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
61 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
62 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
63 static bRC endBackupFile(bpContext *ctx);
64 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
65 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
66 static bRC endRestoreFile(bpContext *ctx);
67 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
68 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
70 static char *apply_rp_codes(struct plugin_ctx * p_ctx);
72 /* Pointers to Bacula functions */
73 static bFuncs *bfuncs = NULL;
74 static bInfo *binfo = NULL;
76 /* Plugin Information block */
77 static pInfo pluginInfo = {
79 FD_PLUGIN_INTERFACE_VERSION,
88 /* Plugin entry points for Bacula */
89 static pFuncs pluginFuncs = {
91 FD_PLUGIN_INTERFACE_VERSION,
93 /* Entry points into plugin */
94 newPlugin, /* new plugin instance */
95 freePlugin, /* free plugin instance */
109 * Plugin private context
113 FILE *fd; /* pipe file descriptor */
114 bool backup; /* set for backup (not needed) */
115 char *cmd; /* plugin command line */
116 char *fname; /* filename to "backup/restore" */
117 char *reader; /* reader program for backup */
118 char *writer; /* writer program for backup */
125 * loadPlugin() and unloadPlugin() are entry points that are
126 * exported, so Bacula can directly call these two entry points
127 * they are common to all Bacula plugins.
130 * External entry point called by Bacula to "load the plugin
132 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
134 bfuncs = lbfuncs; /* set Bacula funct pointers */
136 *pinfo = &pluginInfo; /* return pointer to our info */
137 *pfuncs = &pluginFuncs; /* return pointer to our functions */
143 * External entry point to unload the plugin
147 // printf("bpipe-fd: Unloaded\n");
152 * The following entry points are accessed through the function
153 * pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
154 * has its own set of entry points that the plugin must define.
157 * Create a new instance of the plugin i.e. allocate our private storage
159 static bRC newPlugin(bpContext *ctx)
161 struct plugin_ctx *p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx));
165 memset(p_ctx, 0, sizeof(struct plugin_ctx));
166 ctx->pContext = (void *)p_ctx; /* set our context pointer */
171 * Free a plugin instance, i.e. release our private storage
173 static bRC freePlugin(bpContext *ctx)
175 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
177 free(p_ctx->cmd); /* free any allocated command string */
179 free(p_ctx); /* free our private context */
185 * Return some plugin value (none defined)
187 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
193 * Set a plugin value (none defined)
195 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
201 * Handle an event that was generated in Bacula
203 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
205 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
209 * Most events don't interest us so we ignore them.
210 * the printfs are so that plugin writers can enable them to see
211 * what is really going on.
213 switch (event->eventType) {
215 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: JobStart=%s\n", (char *)value);
218 // printf("bpipe-fd: JobEnd\n");
220 case bEventStartBackupJob:
221 // printf("bpipe-fd: StartBackupJob\n");
223 case bEventEndBackupJob:
224 // printf("bpipe-fd: EndBackupJob\n");
227 // printf("bpipe-fd: JobLevel=%c %d\n", (int)value, (int)value);
230 // printf("bpipe-fd: since=%d\n", (int)value);
233 case bEventStartRestoreJob:
234 // printf("bpipe-fd: StartRestoreJob\n");
237 case bEventEndRestoreJob:
238 // printf("bpipe-fd: EndRestoreJob\n");
241 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
242 case bEventRestoreCommand:
243 // printf("bpipe-fd: EventRestoreCommand cmd=%s\n", (char *)value);
244 /* Fall-through wanted */
245 case bEventBackupCommand:
247 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: pluginEvent cmd=%s\n", (char *)value);
248 p_ctx->cmd = strdup((char *)value);
249 p = strchr(p_ctx->cmd, ':');
251 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Plugin terminator not found: %s\n", (char *)value);
254 *p++ = 0; /* terminate plugin */
258 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "File terminator not found: %s\n", (char *)value);
261 *p++ = 0; /* terminate file */
265 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Reader terminator not found: %s\n", (char *)value);
268 *p++ = 0; /* terminate reader string */
270 // printf("bpipe-fd: plugin=%s fname=%s reader=%s writer=%s\n",
271 // p_ctx->cmd, p_ctx->fname, p_ctx->reader, p_ctx->writer);
275 // printf("bpipe-fd: unknown event=%d\n", event->eventType);
282 * Start the backup of a specific file
284 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
286 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
287 time_t now = time(NULL);
288 sp->fname = p_ctx->fname;
290 sp->statp.st_mode = 0700 | S_IFREG;
291 sp->statp.st_ctime = now;
292 sp->statp.st_mtime = now;
293 sp->statp.st_atime = now;
294 sp->statp.st_size = -1;
295 sp->statp.st_blksize = 4096;
296 sp->statp.st_blocks = 1;
297 p_ctx->backup = true;
298 // printf("bpipe-fd: startBackupFile\n");
303 * Done with backup of this file
305 static bRC endBackupFile(bpContext *ctx)
308 * We would return bRC_More if we wanted startBackupFile to be
309 * called again to backup another file
316 * Bacula is calling us to do the actual I/O
318 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
320 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
326 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: IO_OPEN\n");
327 if (io->flags & (O_CREAT | O_WRONLY)) {
328 char *writer_codes = apply_rp_codes(p_ctx);
330 p_ctx->fd = popen(writer_codes, "w");
331 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: IO_OPEN fd=%d writer=%s\n",
332 p_ctx->fd, writer_codes);
334 io->io_errno = errno;
335 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
336 "Open pipe writer=%s failed: ERR=%s\n", writer_codes, strerror(errno));
346 p_ctx->fd = popen(p_ctx->reader, "r");
347 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: IO_OPEN fd=%p reader=%s\n",
348 p_ctx->fd, p_ctx->reader);
350 io->io_errno = errno;
351 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
352 "Open pipe reader=%s failed: ERR=%s\n", p_ctx->reader, strerror(errno));
356 sleep(1); /* let pipe connect */
361 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Logic error: NULL read FD\n");
364 io->status = fread(io->buf, 1, io->count, p_ctx->fd);
365 // bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: IO_READ buf=%p len=%d\n", io->buf, io->status);
366 if (io->status == 0 && ferror(p_ctx->fd)) {
367 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
368 "Pipe read error: ERR=%s\n", strerror(errno));
369 bfuncs->DebugMessage(ctx, fi, li, dbglvl,
370 "Pipe read error: ERR=%s\n", strerror(errno));
377 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Logic error: NULL write FD\n");
380 // printf("bpipe-fd: IO_WRITE fd=%p buf=%p len=%d\n", p_ctx->fd, io->buf, io->count);
381 io->status = fwrite(io->buf, 1, io->count, p_ctx->fd);
382 // printf("bpipe-fd: IO_WRITE buf=%p len=%d\n", io->buf, io->status);
383 if (io->status == 0 && ferror(p_ctx->fd)) {
384 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
385 "Pipe write error\n");
386 bfuncs->DebugMessage(ctx, fi, li, dbglvl,
387 "Pipe read error: ERR=%s\n", strerror(errno));
394 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Logic error: NULL FD on bpipe close\n");
397 io->status = pclose(p_ctx->fd);
401 io->offset = p_ctx->offset;
408 * Bacula is notifying us that a plugin name string was found, and
409 * passing us the plugin command, so we can prepare for a restore.
411 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
413 // printf("bpipe-fd: startRestoreFile cmd=%s\n", cmd);
418 * Bacula is notifying us that the plugin data has terminated, so
419 * the restore for this particular file is done.
421 static bRC endRestoreFile(bpContext *ctx)
423 // printf("bpipe-fd: endRestoreFile\n");
428 * This is called during restore to create the file (if necessary)
429 * We must return in rp->create_status:
432 * CF_SKIP -- skip processing this file
433 * CF_EXTRACT -- extract the file (i.e.call i/o routines)
434 * CF_CREATED -- created, but no content to extract (typically directories)
437 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
439 // printf("bpipe-fd: createFile\n");
440 if (strlen(rp->where) > 512) {
441 printf("Restore target dir too long. Restricting to first 512 bytes.\n");
443 strncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, 513);
444 ((struct plugin_ctx *)ctx->pContext)->replace = rp->replace;
445 rp->create_status = CF_EXTRACT;
450 * We will get here if the File is a directory after everything
451 * is written in the directory.
453 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
455 // printf("bpipe-fd: setFileAttributes\n");
459 /*************************************************************************
460 * Apply codes in writer command:
465 * 'always' => 'a', chr(97)
466 * 'ifnewer' => 'w', chr(119)
467 * 'ifolder' => 'o', chr(111)
468 * 'never' => 'n', chr(110)
470 * This function will allocate the required amount of memory with malloc.
471 * Need to be free()d manually.
472 * Inspired by edit_job_codes in lib/util.c
475 static char *apply_rp_codes(struct plugin_ctx * p_ctx)
480 int w_count = 0, r_count = 0;
483 char *imsg = p_ctx->writer;
490 while ((q = strstr(p, "%w"))) {
496 while ((q = strstr(p, "%r"))) {
504 * + number of "where" codes * (len(where)-2)
505 * - number of "replace" codes
507 omsg = (char*)malloc(strlen(imsg) + (w_count * (strlen(p_ctx->where)-2)) - r_count + 1);
509 fprintf(stderr, "Out of memory.");
514 //printf("apply_rp_codes: %s\n", imsg);
515 for (p=imsg; *p; p++) {
525 snprintf(add, 2, "%c", p_ctx->replace);
540 //printf("add_str %s\n", str);
542 //printf("omsg=%s\n", omsg);