Created
May 15, 2014 18:40
-
-
Save abcarroll/d57ca7425f168432ca2e to your computer and use it in GitHub Desktop.
Dumps contents of a redis database relatively well into a text file via predis
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
<?php | |
/* Load predis object into $redis | |
here */ | |
$out_file = 'REDIS-DUMP.txt'; | |
$fp = fopen($out_file, 'w+'); | |
echo "Starting... \n"; | |
echo "KEYS... "; | |
$objects = $redis->keys("*"); | |
echo "OK\n"; | |
$count = count($objects); | |
$x = 0; | |
foreach($objects as $o) { | |
$type = $redis->type($o); | |
if($type == 'string') { | |
fwrite($fp, "SET $o " . $redis->get($o) . "\n"); | |
} elseif($type == 'hash') { | |
$hash = $redis->hgetall($o); | |
foreach($hash as $key => $val) { | |
fwrite($fp, "HSET $o $key $val\n"); | |
} | |
} elseif($type == 'set') { | |
$set = $redis->smembers($o); | |
foreach($set as $key => $val) { | |
fwrite($fp, "SADD $o $val\n"); | |
} | |
} elseif($type == 'zset') { | |
$zset = $redis->zrange($o, 0, -1, 'WITHSCORES'); | |
foreach($zset as $both) { | |
fwrite($fp, "ZADD $o {$both[1]} {$both[0]}\n"); | |
} | |
} else { | |
die("Type '$type' not supported in key $o\n"); | |
} | |
$ttl = $redis->ttl($o); | |
if($ttl >= 0) { | |
fwrite($fp, "EXPIRE $o $ttl\n"); | |
} elseif($ttl == -1) { | |
fwrite($fp, "PERSIST $o\n"); | |
} else { | |
die("Unexpected value $ttl from 'TTL $o'\n"); | |
} | |
$x++; | |
if( ($x % 1000) == 0) { | |
echo "$x / $count...\n"; | |
} | |
} | |
echo "Wrote to $out_file\nComplete. Fin.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment