Skip to content
This repository has been archived by the owner on Jun 12, 2020. It is now read-only.

Commit

Permalink
BACKUP-105 use a regex to decide if source files should be excluded f…
Browse files Browse the repository at this point in the history
…rom the backup
  • Loading branch information
prohaska7 committed May 4, 2015
1 parent 2038691 commit c00dda9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ install plugin tokudb_backup soname 'tokudb_backup.so';

# Run a backup

1 Backup to the '/tmp/backup1047' directory. This blocks until the backup is complete.
Backup to the '/tmp/backup1047' directory. This blocks until the backup is complete.
```
set tokudb_backup_dir='/tmp/backup1047';
```
Expand All @@ -39,6 +39,15 @@ The ```set tokudb_backup_dir``` statement will succeed if the backup was taken.
select @@tokudb_backup_last_error, @@tokudb_backup_last_error_string;
```

# Exclude source files
Lets suppose that you want to exclude all 'lost+found' directories from the backup. The ```tokudb_backup_exclude``` session variable contains a regular expression that all source file names are compared with. If the source file name matches the exclude regular expression, then the source file is excluded from the backup.
```
set tokudb_backup_exclude='/lost\\+found($|/)';
```
```
set tokudb_backup_dir='/tmp/backup105';
\\\
# Monitor progress
The Tokutek hot backup updates the processlist state with progress information while it is running.
Expand Down Expand Up @@ -101,6 +110,13 @@ The ```tokudb_backup_throttle``` variable imposes an upper bound on the write ra
* type:str
* comment:error string of the last backup
## tokudb_backup_exclude
* name:tokudb_backup_exclude
* readonly:false
* scope:session
* type:str
* comment:exclude source file regular expression
# Build the hot backup plugin from source
1 Checkout the Percona Server source
Expand Down
15 changes: 12 additions & 3 deletions backup/backup.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@
#define BACKUP_H

#ident "Copyright (c) 2012-2013 Tokutek Inc. All rights reserved."
#ident "$Id: f3e40716a68df061474b60d63cede2cfc99f7be9 $"
#ident "$Id: 4edcb140608da110c9a225a606dc13e77301d274 $"

extern "C" {

// These public API's should be in C.

typedef int (*backup_poll_fun_t)(float progress, const char *progress_string, void*poll_extra);
typedef int (*backup_poll_fun_t)(float progress, const char *progress_string, void *poll_extra);

typedef void (*backup_error_fun_t)(int error_number, const char *error_string, void *error_extra);

// The exclude_copy callback if called for every file that will be copied.
// When it returns 0, the file is copied. Otherwise, the file copy is skipped.
typedef int (*backup_exclude_copy_fun_t)(const char *source_file,void *extra);

int tokubackup_create_backup(const char *source_dirs[], const char *dest_dirs[], int dir_count,
backup_poll_fun_t poll_fun, void *poll_extra,
backup_error_fun_t error_fun, void *error_extra) throw() __attribute__((visibility("default")));
backup_error_fun_t error_fun, void *error_extra,
backup_exclude_copy_fun_t check_fun, void *exclude_copy_extra)
throw() __attribute__((visibility("default")));
// Effect: Backup the directories in source_dirs into correspnding dest_dirs.
// Periodically call poll_fun.
// If poll_fun returns 0, then the backup continues.
Expand All @@ -41,6 +48,8 @@ int tokubackup_create_backup(const char *source_dirs[], const char *dest_dirs[],
// poll_extra: a value passed to poll_fun.
// error_fun: a function to call if an error happens
// error_extra: a value passed to error_fun.
// exclude_copy_fun: a function to call for every file being copied
// exclude_copy_extra: a valuue passed to the exclude_copy_fun
// Return value: 0 if the backup succeeded. Nonzero if the backup
// was stopped early (returning the result from the poll_fun) or the
// error code.
Expand Down
48 changes: 46 additions & 2 deletions tokudb_backup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <sql_acl.h> // SUPER_ACL
#include <sql_parse.h> // check_global_access
#include "backup/backup.h"
#include <regex.h>

#ifdef TOKUDB_BACKUP_PLUGIN_VERSION
#define stringify2(x) #x
Expand All @@ -40,7 +41,9 @@ static MYSQL_SYSVAR_STR(version, tokudb_backup_version, PLUGIN_VAR_NOCMDARG | PL
static MYSQL_THDVAR_ULONG(last_error, PLUGIN_VAR_THDLOCAL, "error from the last backup. 0 is success",
NULL, NULL, 0 /*default*/, 0 /*min*/, ~0ULL /*max*/, 1 /*blocksize*/);

static MYSQL_THDVAR_STR(last_error_string, PLUGIN_VAR_THDLOCAL | PLUGIN_VAR_MEMALLOC, "error string from the last backup", NULL, NULL, NULL);
static MYSQL_THDVAR_STR(last_error_string, PLUGIN_VAR_THDLOCAL + PLUGIN_VAR_MEMALLOC, "error string from the last backup", NULL, NULL, NULL);

static MYSQL_THDVAR_STR(exclude, PLUGIN_VAR_THDLOCAL + PLUGIN_VAR_MEMALLOC, "exclude source file regular expression", NULL, NULL, NULL);

static int tokudb_backup_check_dir(THD *thd, struct st_mysql_sys_var *var, void *save, struct st_mysql_value *value);
static void tokudb_backup_update_dir(THD *thd, struct st_mysql_sys_var *var, void *var_ptr, const void *save);
Expand All @@ -66,9 +69,30 @@ static struct st_mysql_sys_var *tokudb_backup_system_variables[] = {
MYSQL_SYSVAR(dir),
MYSQL_SYSVAR(last_error),
MYSQL_SYSVAR(last_error_string),
MYSQL_SYSVAR(exclude),
NULL,
};

struct tokudb_backup_exclude_copy_extra {
THD *_thd;
char *exclude_string;
regex_t *re;
};

static int tokudb_backup_exclude_copy_fun(const char *source_file, void *extra) {
tokudb_backup_exclude_copy_extra *exclude_extra = static_cast<tokudb_backup_exclude_copy_extra *>(extra);
int r = 0;
if (0) fprintf(stderr, "%s %s\n", __FUNCTION__, source_file);
if (exclude_extra->exclude_string) {
int regr = regexec(exclude_extra->re, source_file, 0, NULL, 0);
if (regr == 0) {
if (1) fprintf(stderr, "tokudb backup exclude %s\n", source_file);
r = 1;
}
}
return r;
}

struct tokudb_backup_progress_extra {
THD *_thd;
char *_the_string;
Expand Down Expand Up @@ -531,6 +555,19 @@ static void tokudb_backup_run(THD *thd, const char *dest_dir) {
return;
}

char *exclude_string = THDVAR(thd, exclude);
regex_t exclude_re;
if (exclude_string) {
int regr = regcomp(&exclude_re, exclude_string, REG_EXTENDED);
if (regr) {
error = EINVAL;
char reg_error[100+strlen(exclude_string)];
snprintf(reg_error, sizeof reg_error, "tokudb backup exclude %s regcomp %d", exclude_string, regr);
tokudb_backup_set_error(thd, error, reg_error);
return;
}
}

const char *source_dirs[MYSQL_MAX_DIR_COUNT] = {};
const char *dest_dirs[MYSQL_MAX_DIR_COUNT] = {};
int count = sources.set_valid_dirs_and_get_count(source_dirs, MYSQL_MAX_DIR_COUNT);
Expand All @@ -544,7 +581,14 @@ static void tokudb_backup_run(THD *thd, const char *dest_dir) {
// do the backup
tokudb_backup_progress_extra progress_extra = { thd, NULL };
tokudb_backup_error_extra error_extra = { thd };
error = tokubackup_create_backup(source_dirs, dest_dirs, count, tokudb_backup_progress_fun, &progress_extra, tokudb_backup_error_fun, &error_extra);
tokudb_backup_exclude_copy_extra exclude_copy_extra = { thd, exclude_string, &exclude_re };
error = tokubackup_create_backup(source_dirs, dest_dirs, count,
tokudb_backup_progress_fun, &progress_extra,
tokudb_backup_error_fun, &error_extra,
tokudb_backup_exclude_copy_fun, &exclude_copy_extra);

if (exclude_string)
regfree(&exclude_re);

// cleanup
thd_proc_info(thd, "tokudb backup done"); // must be a static string
Expand Down

0 comments on commit c00dda9

Please sign in to comment.