Shows how to use log-based incremental backup.
#include <test_util.h>
static const char *const home = "WT_HOME_LOG";
static const char *const home_full = "WT_HOME_LOG_FULL";
static const char *const home_incr = "WT_HOME_LOG_INCR";
static const char *const full_out = "./backup_full";
static const char *const incr_out = "./backup_incr";
static const char *const uri = "table:logtest";
#define CONN_CONFIG "create,cache_size=100MB,log=(enabled=true,file_max=100K,remove=false)"
#define MAX_ITERATIONS 5
#define MAX_KEYS 10000
static int
compare_backups(int i)
{
int ret;
char buf[1024], msg[32];
if (i == 0)
(void)snprintf(
buf, sizeof(buf), "../../wt -R -h %s dump logtest > %s.%d", home, full_out, i);
else
(void)snprintf(
buf, sizeof(buf), "../../wt -R -h %s.%d dump logtest > %s.%d", home_full, i, full_out, i);
error_check(system(buf));
(void)snprintf(
buf, sizeof(buf), "../../wt -R -h %s.%d dump logtest > %s.%d", home_incr, i, incr_out, i);
error_check(system(buf));
(void)snprintf(buf, sizeof(buf), "cmp %s.%d %s.%d", full_out, i, incr_out, i);
ret = system(buf);
if (i == 0)
(void)snprintf(msg, sizeof(msg), "%s", "MAIN");
else
(void)snprintf(msg, sizeof(msg), "%d", i);
printf("Iteration %s: Tables %s.%d and %s.%d %s\n", msg, full_out, i, incr_out, i,
ret == 0 ? "identical" : "differ");
if (ret != 0)
exit(1);
if (i != 0) {
(void)snprintf(buf, sizeof(buf), "rm -rf %s.%d %s.%d %s.%d %s.%d", home_full, i, home_incr,
i, full_out, i, incr_out, i);
error_check(system(buf));
}
return (ret);
}
static void
setup_directories(void)
{
int i;
char buf[1024];
for (i = 0; i < MAX_ITERATIONS; i++) {
(void)snprintf(buf, sizeof(buf), "rm -rf %s.%d && mkdir %s.%d", home_incr, i, home_incr, i);
error_check(system(buf));
if (i == 0)
continue;
(void)snprintf(buf, sizeof(buf), "rm -rf %s.%d && mkdir %s.%d", home_full, i, home_full, i);
error_check(system(buf));
}
}
static void
{
int i;
char k[32], v[32];
error_check(session->
open_cursor(session, uri, NULL, NULL, &cursor));
for (i = 0; i < MAX_KEYS; i++) {
(void)snprintf(k, sizeof(k), "key.%d.%d", iter, i);
(void)snprintf(v, sizeof(v), "value.%d.%d", iter, i);
error_check(cursor->
insert(cursor));
}
error_check(cursor->
close(cursor));
}
static void
{
int j, ret;
char buf[1024], h[256];
const char *filename, *hdir;
if (i != 0) {
(void)snprintf(h, sizeof(h), "%s.%d", home_full, i);
hdir = h;
} else
hdir = home_incr;
error_check(session->
open_cursor(session,
"backup:", NULL, NULL, &cursor));
while ((ret = cursor->
next(cursor)) == 0) {
error_check(cursor->
get_key(cursor, &filename));
if (i == 0)
for (j = 0; j < MAX_ITERATIONS; j++) {
(void)snprintf(h, sizeof(h), "%s.%d", home_incr, j);
(void)snprintf(buf, sizeof(buf), "cp %s/%s %s/%s", home, filename, h, filename);
error_check(system(buf));
}
else {
(void)snprintf(h, sizeof(h), "%s.%d", home_full, i);
(void)snprintf(buf, sizeof(buf), "cp %s/%s %s/%s", home, filename, hdir, filename);
error_check(system(buf));
}
}
error_check(cursor->
close(cursor));
}
static void
{
int j, ret;
char buf[1024], h[256];
const char *filename;
error_check(session->
open_cursor(session,
"backup:", NULL,
"target=(\"log:\")", &cursor));
while ((ret = cursor->
next(cursor)) == 0) {
error_check(cursor->
get_key(cursor, &filename));
(void)snprintf(h, sizeof(h), "%s.0", home_incr);
(void)snprintf(buf, sizeof(buf), "cp %s/%s %s/%s", home, filename, h, filename);
error_check(system(buf));
for (j = i; j < MAX_ITERATIONS; j++) {
(void)snprintf(h, sizeof(h), "%s.%d", home_incr, j);
(void)snprintf(buf, sizeof(buf), "cp %s/%s %s/%s", home, filename, h, filename);
error_check(system(buf));
}
}
error_check(session->
truncate(session,
"log:", cursor, NULL, NULL));
error_check(cursor->
close(cursor));
}
int
main(int argc, char *argv[])
{
int i;
char cmd_buf[256];
(void)argc;
(void)testutil_set_progname(argv);
(void)snprintf(cmd_buf, sizeof(cmd_buf), "rm -rf %s && mkdir %s", home, home);
error_check(system(cmd_buf));
setup_directories();
error_check(wt_conn->
open_session(wt_conn, NULL, NULL, &session));
error_check(session->
create(session, uri,
"key_format=S,value_format=S"));
printf("Adding initial data\n");
add_work(session, 0);
printf("Taking initial backup\n");
take_full_backup(session, 0);
for (i = 1; i < MAX_ITERATIONS; i++) {
printf("Iteration %d: adding data\n", i);
add_work(session, i);
printf("Iteration %d: taking full backup\n", i);
take_full_backup(session, i);
printf("Iteration %d: taking incremental backup\n", i);
take_incr_backup(session, i);
printf("Iteration %d: dumping and comparing data\n", i);
error_check(compare_backups(i));
}
error_check(wt_conn->
close(wt_conn, NULL));
printf("Final comparison: dumping and comparing data\n");
error_check(compare_backups(0));
return (EXIT_SUCCESS);
}