Created
March 1, 2011 13:45
-
-
Save smrchy/849141 to your computer and use it in GitHub Desktop.
Move some keys from one redis to another redis instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var sys = require("sys"), | |
| url = require("url"), | |
| _ = require('./underscore-min'), | |
| redisclient = require("redis"), | |
| redisNEW = redisclient.createClient(6379,'127.0.0.1'); | |
| redisOLD = redisclient.createClient(6379,'192.168.11.18') | |
| function moveHash(rep) { | |
| // Loop over all keys | |
| sys.puts('Total Hashes:'+ rep.length) | |
| _.each(rep, function(key) { | |
| redisOLD.hgetall(key, function(err, rep) { | |
| var mc = []; | |
| // Loop over all Hash fields | |
| _.each(rep, function(value, field) { | |
| mc.push(['hincrby',key,field,Number(value)]); | |
| }) | |
| redisNEW.multi(mc).exec() | |
| // sys.puts(JSON.stringify(mc)) | |
| }) | |
| }) | |
| } | |
| function moveList(rep) { | |
| sys.puts('Total Lists:'+ rep.length) | |
| _.each(rep, function(key) { | |
| redisOLD.lrange(key, 0, -1, function(err, rep){ | |
| var mc = []; | |
| _.each(rep, function(e) { | |
| mc.push(['rpush', key, e]) | |
| }); | |
| redisNEW.multi(mc).exec() | |
| }); | |
| }); | |
| } | |
| function moveZset(rep) { | |
| sys.puts('Total Zsets:'+ rep.length) | |
| _.each(rep, function(key) { | |
| redisOLD.zrange(key, 0, -1, 'WITHSCORES', function(err, rep) { | |
| var mc = []; | |
| // Loop over all elements fields | |
| _.each(rep, function(e, i) { | |
| if (i%2) { | |
| mc.push(['zadd',key,e,rep[i-1]]); | |
| } | |
| }) | |
| redisNEW.multi(mc).exec() | |
| }) | |
| }) | |
| } | |
| function Blog() { | |
| redisOLD.keys('Blog:*', function(err,rep) { | |
| var lists = []; | |
| var hashes = _.select(rep,function(e) { | |
| if (e.substr(-4,4) === ':log') { | |
| lists.push(e); | |
| return false; | |
| } | |
| return true; | |
| }) | |
| moveHash(hashes); | |
| moveList(lists); | |
| }) | |
| }() | |
| function Counter() { | |
| redisOLD.keys('Counter:*', function(err,rep) { | |
| var lists = []; | |
| var zsets = []; | |
| var keys = _.select(rep,function(e) { | |
| if (e.substr(-4,4) === ':log') { | |
| lists.push(e); | |
| return false; | |
| } | |
| else if (e.substr(0,12) === 'Counter:RES:') { | |
| zsets.push(e); | |
| return false; | |
| } | |
| return true; | |
| }) | |
| moveHash(keys); | |
| moveList(lists); | |
| moveZset(zsets); | |
| }) | |
| }() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment