1. Export Redis
[Plain]View Plain copy
- #!/bin/bash
- Redis_host=localhost
- redis_port=6379
- Redis_db=1
- keyname=redis:hash:*
- Keyfile=key.txt
- echo "KEYS $KEYNAME" | Redis-cli-h $REDIS _host-p $REDIS _port-n $REDIS _db > $KEYFILE
- Outfile=valuelist.txt
- tempfile= $OUTFILE. tmp
- echo > $TEMPFILE
- For key in ' Cat $KEYFILE '
- Do
- Echo $key
- #echo "Hgetall $key" | Redis-cli-h $REDIS _host-p $REDIS _port-n $REDIS _db >> $TEMPFILE
- echo "GET $key" | Redis-cli-h $REDIS _host-p $REDIS _port-n $REDIS _db >> $TEMPFILE
- Done
2. Organize and export the results
[Plain]View Plain copy
- Cat $TEMPFILE |xargs-n 2|awk-f "" "-V keyname= $KEYNAME ' {print ' hset ' KEYNAME '" $, "\" "$ $" \ "} ' > $OUTFILE
One of the Xargs-n 2 is a line of 2 lines, the line break with a space instead
Then use awk to separate the spaces, which is the value of Keynam.
Convert to Hset format again, which uses the KeyName variable, which is predefined with-V.
Finally, convert to Hset KEYNAME KEY "VALUE" format.
3. Result of import conversion
[Plain]View Plain copy
- Cat $OUTFILE | Redis-cli-h localhost-p 6379
Export and import directly with Redis's dump and restore
[Plain]View Plain copy
- Redis-cli--raw Dump Test | head-c-1 | Redis-cli-x Restore Test1 0
Shell Export and import Redis