Export data from one Redis db to another Redis db PHP script:
Copy Code code as follows:
$from = ' 127.0.0.1:6200/6 ';
$to = ' 127.0.0.1:6200/8 ';
$from _redis = Redis_init ($from);
$to _redis = Redis_init ($to);
$keys = $from _redis->keys (' * ');
$count = 0;
$total = count ($keys);
foreach ($keys as $key) {
if (+ + $count% 100 = 1) {
echo "$count/$total \ n";
}
$type = $from _redis->type ($key);
Switch ($type) {
Case redis::redis_string:
$val = $from _redis->get ($key);
$to _redis->set ($key, $val);
Break
Case Redis::redis_list:
$list = $from _redis->lrange ($key, 0,-1);
foreach ($list as $val) {
$to _redis->rpush ($key, $val);
}
Break
Case Redis::redis_hash:
$hash = $from _redis->hgetall ($key);
$to _redis->hmset ($key, $hash);
Break
Case Redis::redis_zset:
$zset = $from _redis->zrange ($key, 0,-1, true);
foreach ($zset as $val => $score) {
$to _redis->zadd ($key, $score, $val);
}
Break
}
}
function Redis_init ($conf) {
$redis = new Redis ();
Preg_match ('/^ (^:]+) (: [0-9]+)? \\/(. +)?/', $conf, $ms);
$host = $ms [1];
$port = Trim ($ms [2], ': ');
$db = $ms [3];
$redis->connect ($host, $port);
$redis->select ($DB);
return $redis;
}