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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With