2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * A simple pipe plugin for the Bacula File Daemon
22 * Kern Sibbald, October 2007
26 #include "fd_plugins.h"
39 static const int dbglvl = 150;
41 #define PLUGIN_LICENSE "AGPLv3"
42 #define PLUGIN_AUTHOR "Kern Sibbald"
43 #define PLUGIN_DATE "January 2008"
44 #define PLUGIN_VERSION "1"
45 #define PLUGIN_DESCRIPTION "Bacula Pipe File Daemon Plugin"
47 /* Forward referenced functions */
48 static bRC newPlugin(bpContext *ctx);
49 static bRC freePlugin(bpContext *ctx);
50 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
51 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
52 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
53 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
54 static bRC endBackupFile(bpContext *ctx);
55 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
56 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
57 static bRC endRestoreFile(bpContext *ctx);
58 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
59 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
60 static bRC checkFile(bpContext *ctx, char *fname);
62 static char *apply_rp_codes(struct plugin_ctx * p_ctx);
64 /* Pointers to Bacula functions */
65 static bFuncs *bfuncs = NULL;
66 static bInfo *binfo = NULL;
68 /* Plugin Information block */
69 static pInfo pluginInfo = {
71 FD_PLUGIN_INTERFACE_VERSION,
80 /* Plugin entry points for Bacula */
81 static pFuncs pluginFuncs = {
83 FD_PLUGIN_INTERFACE_VERSION,
85 /* Entry points into plugin */
86 newPlugin, /* new plugin instance */
87 freePlugin, /* free plugin instance */
102 * Plugin private context
106 BPIPE *pfd; /* bpipe 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));
158 memset(p_ctx, 0, sizeof(struct plugin_ctx));
159 ctx->pContext = (void *)p_ctx; /* set our context pointer */
164 * Free a plugin instance, i.e. release our private storage
166 static bRC freePlugin(bpContext *ctx)
168 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
173 free(p_ctx->cmd); /* free any allocated command string */
175 free(p_ctx); /* free our private context */
181 * Return some plugin value (none defined)
183 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
189 * Set a plugin value (none defined)
191 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
197 * Handle an event that was generated in Bacula
199 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
201 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) {
214 case bEventPluginCommand:
215 bfuncs->DebugMessage(ctx, fi, li, dbglvl,
216 "bpipe-fd: PluginCommand=%s\n", (char *)value);
219 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: JobStart=%s\n", (char *)value);
222 // printf("bpipe-fd: JobEnd\n");
224 case bEventStartBackupJob:
225 // printf("bpipe-fd: StartBackupJob\n");
227 case bEventEndBackupJob:
228 // printf("bpipe-fd: EndBackupJob\n");
231 // printf("bpipe-fd: JobLevel=%c %d\n", (int)value, (int)value);
234 // printf("bpipe-fd: since=%d\n", (int)value);
237 case bEventStartRestoreJob:
238 // printf("bpipe-fd: StartRestoreJob\n");
241 case bEventEndRestoreJob:
242 // printf("bpipe-fd: EndRestoreJob\n");
245 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
246 case bEventRestoreCommand:
247 // printf("bpipe-fd: EventRestoreCommand cmd=%s\n", (char *)value);
248 /* Fall-through wanted */
249 case bEventEstimateCommand:
250 /* Fall-through wanted */
251 case bEventBackupCommand:
253 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: pluginEvent cmd=%s\n", (char *)value);
254 p_ctx->cmd = strdup((char *)value);
255 p = strchr(p_ctx->cmd, ':');
257 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Plugin terminator not found: %s\n", (char *)value);
260 *p++ = 0; /* terminate plugin */
264 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "File terminator not found: %s\n", (char *)value);
267 *p++ = 0; /* terminate file */
271 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Reader terminator not found: %s\n", (char *)value);
274 *p++ = 0; /* terminate reader string */
276 // printf("bpipe-fd: plugin=%s fname=%s reader=%s writer=%s\n",
277 // p_ctx->cmd, p_ctx->fname, p_ctx->reader, p_ctx->writer);
281 // printf("bpipe-fd: unknown event=%d\n", event->eventType);
288 * Start the backup of a specific file
290 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
292 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
296 time_t now = time(NULL);
297 sp->fname = p_ctx->fname;
299 sp->statp.st_mode = 0700 | S_IFREG;
300 sp->statp.st_ctime = now;
301 sp->statp.st_mtime = now;
302 sp->statp.st_atime = now;
303 sp->statp.st_size = -1;
304 sp->statp.st_blksize = 4096;
305 sp->statp.st_blocks = 1;
306 p_ctx->backup = true;
307 // printf("bpipe-fd: startBackupFile\n");
312 * Done with backup of this file
314 static bRC endBackupFile(bpContext *ctx)
317 * We would return bRC_More if we wanted startBackupFile to be
318 * called again to backup another file
325 * Bacula is calling us to do the actual I/O
327 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
329 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
338 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: IO_OPEN\n");
339 if (io->flags & (O_CREAT | O_WRONLY)) {
340 char *writer_codes = apply_rp_codes(p_ctx);
342 p_ctx->pfd = open_bpipe(writer_codes, 0, "ws");
343 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: IO_OPEN fd=%p writer=%s\n",
344 p_ctx->pfd, writer_codes);
346 io->io_errno = errno;
347 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
348 "Open pipe writer=%s failed: ERR=%s\n", writer_codes, strerror(errno));
357 io->status = fileno(p_ctx->pfd->wfd);
359 p_ctx->pfd = open_bpipe(p_ctx->reader, 0, "rs");
360 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: IO_OPEN fd=%p reader=%s\n",
361 p_ctx->pfd, p_ctx->reader);
363 io->io_errno = errno;
364 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
365 "Open pipe reader=%s failed: ERR=%s\n", p_ctx->reader, strerror(errno));
368 io->status = fileno(p_ctx->pfd->rfd);
370 sleep(1); /* let pipe connect */
375 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Logic error: NULL read FD\n");
378 io->status = fread(io->buf, 1, io->count, p_ctx->pfd->rfd);
379 // bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: IO_READ buf=%p len=%d\n", io->buf, io->status);
380 if (!feof(p_ctx->pfd->rfd) && io->status == 0 && ferror(p_ctx->pfd->rfd)) {
381 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
382 "Pipe read error: ERR=%s\n", strerror(errno));
383 bfuncs->DebugMessage(ctx, fi, li, dbglvl,
384 "Pipe read error: ERR=%s\n", strerror(errno));
391 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Logic error: NULL write FD\n");
394 // printf("bpipe-fd: IO_WRITE fd=%p buf=%p len=%d\n", p_ctx->fd, io->buf, io->count);
395 io->status = fwrite(io->buf, 1, io->count, p_ctx->pfd->wfd);
396 // printf("bpipe-fd: IO_WRITE buf=%p len=%d\n", io->buf, io->status);
397 if (!feof(p_ctx->pfd->wfd) && io->status == 0 && ferror(p_ctx->pfd->wfd)) {
398 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
399 "Pipe write error\n");
400 bfuncs->DebugMessage(ctx, fi, li, dbglvl,
401 "Pipe write error: ERR=%s\n", strerror(errno));
408 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Logic error: NULL FD on bpipe close\n");
411 io->status = close_bpipe(p_ctx->pfd);
412 if (io->status != 0) {
413 bfuncs->JobMessage(ctx, fi, li, M_ERROR, 0, "bpipe-fd: Error closing for file %s: %d\n",
414 p_ctx->fname, io->status);
419 io->offset = p_ctx->offset;
427 * Bacula is notifying us that a plugin name string was found, and
428 * passing us the plugin command, so we can prepare for a restore.
430 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
432 // printf("bpipe-fd: startRestoreFile cmd=%s\n", cmd);
437 * Bacula is notifying us that the plugin data has terminated, so
438 * the restore for this particular file is done.
440 static bRC endRestoreFile(bpContext *ctx)
442 // printf("bpipe-fd: endRestoreFile\n");
447 * This is called during restore to create the file (if necessary)
448 * We must return in rp->create_status:
451 * CF_SKIP -- skip processing this file
452 * CF_EXTRACT -- extract the file (i.e.call i/o routines)
453 * CF_CREATED -- created, but no content to extract (typically directories)
456 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
458 // printf("bpipe-fd: createFile\n");
459 if (strlen(rp->where) > 512) {
460 printf("Restore target dir too long. Restricting to first 512 bytes.\n");
462 strncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, 513);
463 ((struct plugin_ctx *)ctx->pContext)->replace = rp->replace;
464 rp->create_status = CF_EXTRACT;
469 * We will get here if the File is a directory after everything
470 * is written in the directory.
472 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
474 // printf("bpipe-fd: setFileAttributes\n");
478 /* When using Incremental dump, all previous dumps are necessary */
479 static bRC checkFile(bpContext *ctx, char *fname)
484 /*************************************************************************
485 * Apply codes in writer command:
490 * 'always' => 'a', chr(97)
491 * 'ifnewer' => 'w', chr(119)
492 * 'ifolder' => 'o', chr(111)
493 * 'never' => 'n', chr(110)
495 * This function will allocate the required amount of memory with malloc.
496 * Need to be free()d manually.
497 * Inspired by edit_job_codes in lib/util.c
500 static char *apply_rp_codes(struct plugin_ctx * p_ctx)
505 int w_count = 0, r_count = 0;
508 char *imsg = p_ctx->writer;
515 while ((q = strstr(p, "%w"))) {
521 while ((q = strstr(p, "%r"))) {
529 * + number of "where" codes * (len(where)-2)
530 * - number of "replace" codes
532 omsg = (char*)malloc(strlen(imsg) + (w_count * (strlen(p_ctx->where)-2)) - r_count + 1);
534 fprintf(stderr, "Out of memory.");
539 //printf("apply_rp_codes: %s\n", imsg);
540 for (p=imsg; *p; p++) {
550 snprintf(add, 2, "%c", p_ctx->replace);
565 //printf("add_str %s\n", str);
567 //printf("omsg=%s\n", omsg);