Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RethinkDB: Is there a way to clone a DB in the same cluster by GUI or rql?

Tags:

rethinkdb

Say I have a DB on my cluster named my_db. Can I clone this DB so that on the same cluster I'll have my_db_cloned without dealing with dump / export?

like image 226
Kludge Avatar asked Nov 11 '14 15:11

Kludge


2 Answers

You can copy data from one table into another through ReQL like this: r.table(destination).insert(r.table(source))

This can also be run in a loop over all tables in a given database:

r.db(source).tableList().forEach(function (t) {
  return r.db(destination).table(t).insert(r.db(source).table(t));
})

Similarly, you can construct a loop to recreate all tables:

r.db(source).tableList().forEach(function (t) {
  return r.db(destination).tableCreate(t);
})

This version does not keep custom primary key or durability settings, but you get the idea.

Finally to clone secondary indexes

r.db(source).tableList().forEach(function (t) {
  return r.db(source).table(t).indexStatus().map(function (idx) {
    return r.db(destination).table(t).indexCreate(
      idx('index'), idx('function'), {geo: idx('geo'), multi: idx('multi')});
  });
})

For all of these queries, I recommend running them in node.js, or with the Python or Ruby client. Running them in the Data Explorer will likely not work because it times out long running queries after something like a minute.

like image 109
Daniel Mewes Avatar answered Sep 20 '22 01:09

Daniel Mewes


You can do this in ReQL:

r.dbCreate('my_db_cloned')
r.db('my_db').tableList().forEach(r.db('my_db_cloned').tableCreate(r.row))
r.db('my_db').tableList().forEach(r.db('my_db_cloned').table(r.row).insert(r.db('my_db').table(r.row)))

You will need to wait a half second between the second and third commands to make sure the tables are available before you insert into them. (In the upcoming 1.16 release, there will be a tableWait command to remove the guesswork.)

Note that this does not copy secondary indices.

like image 34
mlucy Avatar answered Sep 19 '22 01:09

mlucy