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
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 */
118 * loadPlugin() and unloadPlugin() are entry points that are
119 * exported, so Bacula can directly call these two entry points
120 * they are common to all Bacula plugins.
123 * External entry point called by Bacula to "load the plugin
125 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
127 bfuncs = lbfuncs; /* set Bacula funct pointers */
129 *pinfo = &pluginInfo; /* return pointer to our info */
130 *pfuncs = &pluginFuncs; /* return pointer to our functions */
136 * External entry point to unload the plugin
140 // printf("bpipe-fd: Unloaded\n");
145 * The following entry points are accessed through the function
146 * pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
147 * has its own set of entry points that the plugin must define.
150 * Create a new instance of the plugin i.e. allocate our private storage
152 static bRC newPlugin(bpContext *ctx)
154 struct plugin_ctx *p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx));
155 memset(p_ctx, 0, sizeof(struct plugin_ctx));
156 ctx->pContext = (void *)p_ctx; /* set our context pointer */
161 * Free a plugin instance, i.e. release our private storage
163 static bRC freePlugin(bpContext *ctx)
165 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
167 free(p_ctx->cmd); /* free any allocated command string */
169 free(p_ctx); /* free our private context */
174 * Return some plugin value (none defined)
176 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
182 * Set a plugin value (none defined)
184 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
190 * Handle an event that was generated in Bacula
192 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
194 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
197 switch (event->eventType) {
199 // printf("bpipe-fd: JobStart=%s\n", (char *)value);
202 // printf("bpipe-fd: JobEnd\n");
204 case bEventStartBackupJob:
205 // printf("bpipe-fd: BackupStart\n");
207 case bEventEndBackupJob:
208 // printf("bpipe-fd: BackupEnd\n");
211 // printf("bpipe-fd: JobLevel=%c %d\n", (int)value, (int)value);
214 // printf("bpipe-fd: since=%d\n", (int)value);
217 case bEventStartRestoreJob:
220 case bEventEndRestoreJob:
223 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
224 case bEventRestoreCommand:
225 printf("bpipe-fd: EventRestoreCommand cmd=%s\n", (char *)value);
226 case bEventBackupCommand:
228 printf("bpipe-fd: pluginEvent cmd=%s\n", (char *)value);
229 p_ctx->cmd = strdup((char *)value);
230 p = strchr(p_ctx->cmd, ':');
232 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Plugin terminator not found: %s\n", (char *)value);
235 *p++ = 0; /* terminate plugin */
239 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "File terminator not found: %s\n", (char *)value);
242 *p++ = 0; /* terminate file */
246 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Reader terminator not found: %s\n", (char *)value);
249 *p++ = 0; /* terminate reader string */
251 printf("bpipe-fd: plugin=%s fname=%s reader=%s writer=%s\n",
252 p_ctx->cmd, p_ctx->fname, p_ctx->reader, p_ctx->writer);
256 printf("bpipe-fd: unknown event=%d\n", event->eventType);
262 * Start the backup of a specific file
264 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
266 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
267 time_t now = time(NULL);
268 sp->fname = p_ctx->fname;
269 sp->statp.st_mode = 0700 | S_IFREG;
270 sp->statp.st_ctime = now;
271 sp->statp.st_mtime = now;
272 sp->statp.st_atime = now;
273 sp->statp.st_size = -1;
274 sp->statp.st_blksize = 4096;
275 sp->statp.st_blocks = 1;
276 p_ctx->backup = true;
277 // printf("bpipe-fd: startBackupFile\n");
282 * Done with backup of this file
284 static bRC endBackupFile(bpContext *ctx)
287 * We would return bRC_More if we wanted startBackupFile to be
288 * called again to backup another file
294 * Apply codes in writer command:
299 * 'always' => 'a', chr(97)
300 * 'ifnewer' => 'w', chr(119)
301 * 'ifolder' => 'o', chr(111)
302 * 'never' => 'n', chr(110)
304 * This function will allocate the required amount of memory with malloc.
305 * Need to be free()d manually.
306 * Inspired by edit_job_codes in lib/util.c
309 char *apply_rp_codes(struct plugin_ctx * p_ctx)
314 int w_count = 0, r_count = 0;
317 char *imsg = p_ctx->writer;
324 while ((q = strstr(p, "%w"))) {
330 while ((q = strstr(p, "%r"))) {
338 * + number of "where" codes * (len(where)-2)
339 * - number of "replace" codes
341 omsg = (char*)malloc(strlen(imsg) + (w_count * (strlen(p_ctx->where)-2)) - r_count + 1);
343 fprintf(stderr, "Out of memory.");
348 //printf("apply_rp_codes: %s\n", imsg);
349 for (p=imsg; *p; p++) {
359 snprintf(add, 2, "%c", p_ctx->replace);
374 //printf("add_str %s\n", str);
376 //printf("omsg=%s\n", omsg);
382 * Bacula is calling us to do the actual I/O
384 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
386 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
392 // printf("bpipe-fd: IO_OPEN\n");
393 if (io->flags & (O_CREAT | O_WRONLY)) {
394 char *writer_codes = apply_rp_codes(p_ctx);
396 p_ctx->fd = popen(writer_codes, "w");
397 printf("bpipe-fd: IO_OPEN writer=%s\n", writer_codes);
399 io->io_errno = errno;
400 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0,
401 "Open pipe writer=%s failed: ERR=%s\n", writer_codes, strerror(errno));
411 p_ctx->fd = popen(p_ctx->reader, "r");
412 // printf("bpipe-fd: IO_OPEN reader=%s\n", p_ctx->reader);
414 io->io_errno = errno;
415 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0,
416 "Open pipe reader=%s failed: ERR=%s\n", p_ctx->reader, strerror(errno));
420 sleep(1); /* let pipe connect */
425 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Logic error: NULL read FD\n");
428 io->status = fread(io->buf, 1, io->count, p_ctx->fd);
429 // printf("bpipe-fd: IO_READ buf=%p len=%d\n", io->buf, io->status);
430 if (io->status == 0 && ferror(p_ctx->fd)) {
431 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0,
432 "Pipe read error: ERR=%s\n", strerror(errno));
433 // printf("Error reading pipe\n");
440 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Logic error: NULL write FD\n");
443 // printf("bpipe-fd: IO_WRITE fd=%p buf=%p len=%d\n", p_ctx->fd, io->buf, io->count);
444 io->status = fwrite(io->buf, 1, io->count, p_ctx->fd);
445 // printf("bpipe-fd: IO_WRITE buf=%p len=%d\n", io->buf, io->status);
446 if (io->status == 0 && ferror(p_ctx->fd)) {
447 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0,
448 "Pipe write error\n");
449 // printf("Error writing pipe\n");
456 bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Logic error: NULL FD\n");
459 io->status = pclose(p_ctx->fd);
463 io->offset = p_ctx->offset;
469 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
471 // printf("bpipe-fd: startRestoreFile cmd=%s\n", cmd);
475 static bRC endRestoreFile(bpContext *ctx)
477 // printf("bpipe-fd: endRestoreFile\n");
481 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
483 // printf("bpipe-fd: createFile\n");
484 if (strlen(rp->where) > 512) {
485 printf("Restore target dir too long. Restricting to first 512 bytes.\n");
487 strncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, 513);
488 ((struct plugin_ctx *)ctx->pContext)->replace = rp->replace;
492 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
494 // printf("bpipe-fd: setFileAttributes\n");