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 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 #define PLUGIN_LICENSE "GPLv2"
49 #define PLUGIN_AUTHOR "Kern Sibbald"
50 #define PLUGIN_DATE "January 2008"
51 #define PLUGIN_VERSION "1"
52 #define PLUGIN_DESCRIPTION "Bacula Pipe File Daemon Plugin"
54 /* Forward referenced functions */
55 static bRC newPlugin(bpContext *ctx);
56 static bRC freePlugin(bpContext *ctx);
57 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
58 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
59 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
60 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
61 static bRC endBackupFile(bpContext *ctx);
62 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
63 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
64 static bRC endRestoreFile(bpContext *ctx);
65 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
66 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
68 static char *apply_rp_codes(struct plugin_ctx * p_ctx);
70 /* Pointers to Bacula functions */
71 static bFuncs *bfuncs = NULL;
72 static bInfo *binfo = NULL;
74 /* Plugin Information block */
75 static pInfo pluginInfo = {
77 FD_PLUGIN_INTERFACE_VERSION,
86 /* Plugin entry points for Bacula */
87 static pFuncs pluginFuncs = {
89 FD_PLUGIN_INTERFACE_VERSION,
91 /* Entry points into plugin */
92 newPlugin, /* new plugin instance */
93 freePlugin, /* free plugin instance */
107 * Plugin private context
111 FILE *fd; /* pipe file descriptor */
112 bool backup; /* set for backup (not needed) */
113 char *cmd; /* plugin command line */
114 char *fname; /* filename to "backup/restore" */
115 char *reader; /* reader program for backup */
116 char *writer; /* writer program for backup */
123 * loadPlugin() and unloadPlugin() are entry points that are
124 * exported, so Bacula can directly call these two entry points
125 * they are common to all Bacula plugins.
128 * External entry point called by Bacula to "load the plugin
130 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
132 bfuncs = lbfuncs; /* set Bacula funct pointers */
134 *pinfo = &pluginInfo; /* return pointer to our info */
135 *pfuncs = &pluginFuncs; /* return pointer to our functions */
141 * External entry point to unload the plugin
145 // printf("bpipe-fd: Unloaded\n");
150 * The following entry points are accessed through the function
151 * pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
152 * has its own set of entry points that the plugin must define.
155 * Create a new instance of the plugin i.e. allocate our private storage
157 static bRC newPlugin(bpContext *ctx)
159 struct plugin_ctx *p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx));
163 memset(p_ctx, 0, sizeof(struct plugin_ctx));
164 ctx->pContext = (void *)p_ctx; /* set our context pointer */
169 * Free a plugin instance, i.e. release our private storage
171 static bRC freePlugin(bpContext *ctx)
173 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
175 free(p_ctx->cmd); /* free any allocated command string */
177 free(p_ctx); /* free our private context */
183 * Return some plugin value (none defined)
185 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
191 * Set a plugin value (none defined)
193 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
199 * Handle an event that was generated in Bacula
201 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
203 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
207 * Most events don't interest us so we ignore them.
208 * the printfs are so that plugin writers can enable them to see
209 * what is really going on.
211 switch (event->eventType) {
213 bfuncs->DebugMessage(ctx, fi, li, 50, "bpipe-fd: JobStart=%s\n", (char *)value);
216 // printf("bpipe-fd: JobEnd\n");
218 case bEventStartBackupJob:
219 // printf("bpipe-fd: StartBackupJob\n");
221 case bEventEndBackupJob:
222 // printf("bpipe-fd: EndBackupJob\n");
225 // printf("bpipe-fd: JobLevel=%c %d\n", (int)value, (int)value);
228 // printf("bpipe-fd: since=%d\n", (int)value);
231 case bEventStartRestoreJob:
232 // printf("bpipe-fd: StartRestoreJob\n");
235 case bEventEndRestoreJob:
236 // printf("bpipe-fd: EndRestoreJob\n");
239 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
240 case bEventRestoreCommand:
241 printf("bpipe-fd: EventRestoreCommand cmd=%s\n", (char *)value);
242 /* Fall-through wanted */
243 case bEventBackupCommand:
245 bfuncs->DebugMessage(ctx, fi, li, 50, "bpipe-fd: pluginEvent cmd=%s\n", (char *)value);
246 p_ctx->cmd = strdup((char *)value);
247 p = strchr(p_ctx->cmd, ':');
249 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Plugin terminator not found: %s\n", (char *)value);
252 *p++ = 0; /* terminate plugin */
256 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "File terminator not found: %s\n", (char *)value);
259 *p++ = 0; /* terminate file */
263 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Reader terminator not found: %s\n", (char *)value);
266 *p++ = 0; /* terminate reader string */
268 printf("bpipe-fd: plugin=%s fname=%s reader=%s writer=%s\n",
269 p_ctx->cmd, p_ctx->fname, p_ctx->reader, p_ctx->writer);
273 printf("bpipe-fd: unknown event=%d\n", event->eventType);
279 * Start the backup of a specific file
281 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
283 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
284 time_t now = time(NULL);
285 sp->fname = p_ctx->fname;
287 sp->statp.st_mode = 0700 | S_IFREG;
288 sp->statp.st_ctime = now;
289 sp->statp.st_mtime = now;
290 sp->statp.st_atime = now;
291 sp->statp.st_size = -1;
292 sp->statp.st_blksize = 4096;
293 sp->statp.st_blocks = 1;
294 p_ctx->backup = true;
295 // printf("bpipe-fd: startBackupFile\n");
300 * Done with backup of this file
302 static bRC endBackupFile(bpContext *ctx)
305 * We would return bRC_More if we wanted startBackupFile to be
306 * called again to backup another file
313 * Bacula is calling us to do the actual I/O
315 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
317 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
323 bfuncs->DebugMessage(ctx, fi, li, 50, "bpipe-fd: IO_OPEN\n");
324 if (io->flags & (O_CREAT | O_WRONLY)) {
325 char *writer_codes = apply_rp_codes(p_ctx);
327 p_ctx->fd = popen(writer_codes, "w");
328 bfuncs->DebugMessage(ctx, fi, li, 50, "bpipe-fd: IO_OPEN fd=%d writer=%s\n",
329 p_ctx->fd, writer_codes);
331 io->io_errno = errno;
332 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
333 "Open pipe writer=%s failed: ERR=%s\n", writer_codes, strerror(errno));
343 p_ctx->fd = popen(p_ctx->reader, "r");
344 bfuncs->DebugMessage(ctx, fi, li, 50, "bpipe-fd: IO_OPEN fd=%p reader=%s\n",
345 p_ctx->fd, p_ctx->reader);
347 io->io_errno = errno;
348 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
349 "Open pipe reader=%s failed: ERR=%s\n", p_ctx->reader, strerror(errno));
353 sleep(1); /* let pipe connect */
358 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Logic error: NULL read FD\n");
361 io->status = fread(io->buf, 1, io->count, p_ctx->fd);
362 // bfuncs->DebugMessage(ctx, fi, li, 50, "bpipe-fd: IO_READ buf=%p len=%d\n", io->buf, io->status);
363 if (io->status == 0 && ferror(p_ctx->fd)) {
364 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
365 "Pipe read error: ERR=%s\n", strerror(errno));
366 bfuncs->DebugMessage(ctx, fi, li, 50,
367 "Pipe read error: ERR=%s\n", strerror(errno));
374 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Logic error: NULL write FD\n");
377 // printf("bpipe-fd: IO_WRITE fd=%p buf=%p len=%d\n", p_ctx->fd, io->buf, io->count);
378 io->status = fwrite(io->buf, 1, io->count, p_ctx->fd);
379 // printf("bpipe-fd: IO_WRITE buf=%p len=%d\n", io->buf, io->status);
380 if (io->status == 0 && ferror(p_ctx->fd)) {
381 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
382 "Pipe write error\n");
383 bfuncs->DebugMessage(ctx, fi, li, 50,
384 "Pipe read error: ERR=%s\n", strerror(errno));
391 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Logic error: NULL FD on bpipe close\n");
394 io->status = pclose(p_ctx->fd);
398 io->offset = p_ctx->offset;
405 * Bacula is notifying us that a plugin name string was found, and
406 * passing us the plugin command, so we can prepare for a restore.
408 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
410 // printf("bpipe-fd: startRestoreFile cmd=%s\n", cmd);
415 * Bacula is notifying us that the plugin data has terminated, so
416 * the restore for this particular file is done.
418 static bRC endRestoreFile(bpContext *ctx)
420 // printf("bpipe-fd: endRestoreFile\n");
425 * This is called during restore to create the file (if necessary)
426 * We must return in rp->create_status:
429 * CF_SKIP -- skip processing this file
430 * CF_EXTRACT -- extract the file (i.e.call i/o routines)
431 * CF_CREATED -- created, but no content to extract (typically directories)
434 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
436 // printf("bpipe-fd: createFile\n");
437 if (strlen(rp->where) > 512) {
438 printf("Restore target dir too long. Restricting to first 512 bytes.\n");
440 strncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, 513);
441 ((struct plugin_ctx *)ctx->pContext)->replace = rp->replace;
442 rp->create_status = CF_EXTRACT;
447 * We will get here if the File is a directory after everything
448 * is written in the directory.
450 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
452 // printf("bpipe-fd: setFileAttributes\n");
456 /*************************************************************************
457 * Apply codes in writer command:
462 * 'always' => 'a', chr(97)
463 * 'ifnewer' => 'w', chr(119)
464 * 'ifolder' => 'o', chr(111)
465 * 'never' => 'n', chr(110)
467 * This function will allocate the required amount of memory with malloc.
468 * Need to be free()d manually.
469 * Inspired by edit_job_codes in lib/util.c
472 static char *apply_rp_codes(struct plugin_ctx * p_ctx)
477 int w_count = 0, r_count = 0;
480 char *imsg = p_ctx->writer;
487 while ((q = strstr(p, "%w"))) {
493 while ((q = strstr(p, "%r"))) {
501 * + number of "where" codes * (len(where)-2)
502 * - number of "replace" codes
504 omsg = (char*)malloc(strlen(imsg) + (w_count * (strlen(p_ctx->where)-2)) - r_count + 1);
506 fprintf(stderr, "Out of memory.");
511 //printf("apply_rp_codes: %s\n", imsg);
512 for (p=imsg; *p; p++) {
522 snprintf(add, 2, "%c", p_ctx->replace);
537 //printf("add_str %s\n", str);
539 //printf("omsg=%s\n", omsg);