2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2011 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 three of the GNU Affero 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 Affero 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 delta plugin for the Bacula File Daemon
34 #include "fd_plugins.h"
35 #include "fd_common.h"
45 static const int dbglvl = 0;
47 #define PLUGIN_LICENSE "Bacula AGPLv3"
48 #define PLUGIN_AUTHOR "Eric Bollengier"
49 #define PLUGIN_DATE "November 2010"
50 #define PLUGIN_VERSION "1"
51 #define PLUGIN_DESCRIPTION "Bacula Delta Test Plugin"
53 /* Forward referenced functions */
54 static bRC newPlugin(bpContext *ctx);
55 static bRC freePlugin(bpContext *ctx);
56 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
57 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
58 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
59 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
60 static bRC endBackupFile(bpContext *ctx);
61 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
62 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
63 static bRC endRestoreFile(bpContext *ctx);
64 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
65 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
67 /* Pointers to Bacula functions */
68 static bFuncs *bfuncs = NULL;
69 static bInfo *binfo = NULL;
71 /* Plugin Information block */
72 static pInfo pluginInfo = {
74 FD_PLUGIN_INTERFACE_VERSION,
83 /* Plugin entry points for Bacula */
84 static pFuncs pluginFuncs = {
86 FD_PLUGIN_INTERFACE_VERSION,
88 /* Entry points into plugin */
89 newPlugin, /* new plugin instance */
90 freePlugin, /* free plugin instance */
101 NULL /* no checkFile */
104 #define get_self(x) ((delta_test*)((x)->pContext))
105 #define FO_DELTA (1<<28) /* Do delta on file */
106 #define FO_OFFSETS (1<<30) /* Keep block offsets */
114 POOLMEM *fname; /* Filename to save */
120 delta_test(bpContext *bpc) {
126 fname = get_pool_memory(PM_FNAME);
129 free_and_null_pool_memory(fname);
134 * loadPlugin() and unloadPlugin() are entry points that are
135 * exported, so Bacula can directly call these two entry points
136 * they are common to all Bacula plugins.
139 * External entry point called by Bacula to "load the plugin
141 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
143 bfuncs = lbfuncs; /* set Bacula funct pointers */
145 *pinfo = &pluginInfo; /* return pointer to our info */
146 *pfuncs = &pluginFuncs; /* return pointer to our functions */
148 /* Activate this plugin only in developer mode */
157 * External entry point to unload the plugin
161 // Dmsg(NULL, dbglvl, "delta-test-fd: Unloaded\n");
166 * The following entry points are accessed through the function
167 * pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
168 * has its own set of entry points that the plugin must define.
171 * Create a new instance of the plugin i.e. allocate our private storage
173 static bRC newPlugin(bpContext *ctx)
175 delta_test *self = new delta_test(ctx);
179 ctx->pContext = (void *)self; /* set our context pointer */
184 * Free a plugin instance, i.e. release our private storage
186 static bRC freePlugin(bpContext *ctx)
188 delta_test *self = get_self(ctx);
197 * Return some plugin value (none defined)
199 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
205 * Set a plugin value (none defined)
207 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
213 * Handle an event that was generated in Bacula
215 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
217 delta_test *self = get_self(ctx);
227 * Most events don't interest us so we ignore them.
228 * the printfs are so that plugin writers can enable them to see
229 * what is really going on.
231 switch (event->eventType) {
232 case bEventPluginCommand:
234 // "delta-test-fd: PluginCommand=%s\n", (char *)value);
237 // Dmsg(ctx, dbglvl, "delta-test-fd: JobStart=%s\n", (char *)value);
240 // Dmsg(ctx, dbglvl, "delta-test-fd: JobEnd\n");
242 case bEventStartBackupJob:
243 // Dmsg(ctx, dbglvl, "delta-test-fd: StartBackupJob\n");
245 case bEventEndBackupJob:
246 // Dmsg(ctx, dbglvl, "delta-test-fd: EndBackupJob\n");
249 // Dmsg(ctx, dbglvl, "delta-test-fd: JobLevel=%c %d\n", (int)value, (int)value);
250 self->level = (int)(intptr_t)value;
253 // Dmsg(ctx, dbglvl, "delta-test-fd: since=%d\n", (int)value);
256 case bEventStartRestoreJob:
257 // Dmsg(ctx, dbglvl, "delta-test-fd: StartRestoreJob\n");
260 case bEventEndRestoreJob:
261 // Dmsg(ctx, dbglvl, "delta-test-fd: EndRestoreJob\n");
264 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
265 case bEventRestoreCommand:
266 // Dmsg(ctx, dbglvl, "delta-test-fd: EventRestoreCommand cmd=%s\n", (char *)value);
267 /* Fall-through wanted */
269 case bEventBackupCommand:
270 Dmsg(ctx, dbglvl, "delta-test-fd: pluginEvent cmd=%s\n", (char *)value);
271 if (self->level == 'I' || self->level == 'D') {
272 bfuncs->getBaculaValue(ctx, bVarAccurate, (void *)&accurate);
273 if (!accurate) { /* can be changed to FATAL */
275 "Accurate mode should be turned on when using the "
276 "delta-test plugin\n");
283 // Dmsg(ctx, dbglvl, "delta-test-fd: unknown event=%d\n", event->eventType);
289 static const char *files[] = {
295 static int nb_files = 4;
298 * Start the backup of a specific file
300 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
302 delta_test *self = get_self(ctx);
306 time_t now = time(NULL);
307 sp->fname = (char *)"/delta.txt";
309 sp->statp.st_mode = 0700 | S_IFREG;
310 sp->statp.st_ctime = now;
311 sp->statp.st_mtime = now;
312 sp->statp.st_atime = now;
313 sp->statp.st_size = -1;
314 sp->statp.st_blksize = 4096;
315 sp->statp.st_blocks = 1;
316 if (self->level == 'I' || self->level == 'D') {
317 bRC state = bfuncs->checkChanges(ctx, sp);
318 /* Should always be bRC_OK */
319 sp->type = (state == bRC_Seen)? FT_NOCHG : FT_REG;
320 sp->flags |= (FO_DELTA|FO_OFFSETS);
321 self->delta = sp->delta_seq + 1;
323 pm_strcpy(self->fname, files[self->delta % nb_files]);
324 Dmsg(ctx, dbglvl, "delta-test-fd: delta_seq=%i delta=%i fname=%s\n",
325 sp->delta_seq, self->delta, self->fname);
326 // Dmsg(ctx, dbglvl, "delta-test-fd: startBackupFile\n");
331 * Done with backup of this file
333 static bRC endBackupFile(bpContext *ctx)
336 * We would return bRC_More if we wanted startBackupFile to be
337 * called again to backup another file
344 * Bacula is calling us to do the actual I/O
346 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
348 delta_test *self = get_self(ctx);
358 Dmsg(ctx, dbglvl, "delta-test-fd: IO_OPEN\n");
359 if (io->flags & (O_CREAT | O_WRONLY)) {
360 /* TODO: if the file already exists, the result is undefined */
361 if (stat(io->fname, &statp) == 0) { /* file exists */
362 self->fd = fopen(io->fname, "r+");
364 self->fd = fopen(io->fname, "w"); /* file doesn't exist,create it */
367 io->io_errno = errno;
369 "Open failed: ERR=%s\n", strerror(errno));
374 self->fd = fopen(self->fname, "r");
376 io->io_errno = errno;
378 "Open failed: ERR=%s\n", strerror(errno));
386 Jmsg(ctx, M_FATAL, "Logic error: NULL read FD\n");
392 /* first time, read 300, then replace 50-250 by other data */
393 if (self->delta == 0) {
394 io->status = fread(io->buf, 1, 400, self->fd);
396 io->offset = self->delta * 100 / 2; /* chunks are melted */
397 io->status = fread(io->buf, 1, 100, self->fd);
399 Dmsg(ctx, dbglvl, "delta-test-fd: READ offset=%lld\n", (int64_t)io->offset);
402 if (io->status == 0 && ferror(self->fd)) {
404 "Pipe read error: ERR=%s\n", strerror(errno));
406 "Pipe read error: ERR=%s\n", strerror(errno));
409 Dmsg(ctx, dbglvl, "offset=%d\n", io->offset);
414 Jmsg(ctx, M_FATAL, "Logic error: NULL write FD\n");
417 Dmsg(ctx, dbglvl, "delta-test-fd: WRITE count=%lld\n", (int64_t)io->count);
418 io->status = fwrite(io->buf, 1, io->count, self->fd);
419 if (io->status == 0 && ferror(self->fd)) {
421 "Pipe write error\n");
423 "Pipe read error: ERR=%s\n", strerror(errno));
430 Jmsg(ctx, M_FATAL, "Logic error: NULL FD on delta close\n");
433 io->status = fclose(self->fd);
438 Jmsg(ctx, M_FATAL, "Logic error: NULL FD on delta close\n");
441 Dmsg(ctx, dbglvl, "delta-test-fd: SEEK offset=%lld\n", (int64_t)io->offset);
442 io->status = fseek(self->fd, io->offset, io->whence);
443 Dmsg(ctx, dbglvl, "after SEEK=%lld\n", (int64_t)ftell(self->fd));
450 * Bacula is notifying us that a plugin name string was found, and
451 * passing us the plugin command, so we can prepare for a restore.
453 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
455 // Dmsg(ctx, dbglvl, "delta-test-fd: startRestoreFile cmd=%s\n", cmd);
460 * Bacula is notifying us that the plugin data has terminated, so
461 * the restore for this particular file is done.
463 static bRC endRestoreFile(bpContext *ctx)
465 // Dmsg(ctx, dbglvl, "delta-test-fd: endRestoreFile\n");
470 * This is called during restore to create the file (if necessary)
471 * We must return in rp->create_status:
474 * CF_SKIP -- skip processing this file
475 * CF_EXTRACT -- extract the file (i.e.call i/o routines)
476 * CF_CREATED -- created, but no content to extract (typically directories)
479 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
481 delta_test *self = get_self(ctx);
482 pm_strcpy(self->fname, rp->ofname);
483 rp->create_status = CF_EXTRACT;
488 * We will get here if the File is a directory after everything
489 * is written in the directory.
491 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
493 // Dmsg(ctx, dbglvl, "delta-test-fd: setFileAttributes\n");