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

Let's say I have a DB in my cluster named my_db. Can I clone this DB so that in the same cluster I will have my_db_cloned without using dump / export?

+3


source to share


4 answers


You can copy data from one table to another via ReQL as follows: 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 create a loop to recreate all tables:

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

      



This version does not store 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 requests, I recommend running them in node.js or with a Python or Ruby client. Running them in Data Explorer will most likely not work because it takes lengthy queries after something like a minute.

+6


source


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 half a second between the second and third commands to make sure the tables are available before inserting them. (In the upcoming 1.16 release, there will be a command tableWait

to remove the guesswork.)

Note that this does not copy secondary indexes.

+2


source


I had the same problem. So I built a command line tool using @ DanielMewes suggestion.

https://github.com/internalfx/thinker

Hope it helps, save me a lot of time.

+2


source


There is currently no dedicated clone command. You will need to dump the database and restore it under a different name to get a second copy of the data.

0


source







All Articles