Shell Export and import Redis

Source: Internet
Author: User

1. Export Redis

[Plain]View Plain copy
  1. #!/bin/bash
  2. Redis_host=localhost
  3. redis_port=6379
  4. Redis_db=1
  5. keyname=redis:hash:*
  6. Keyfile=key.txt
  7. echo "KEYS $KEYNAME" | Redis-cli-h $REDIS _host-p $REDIS _port-n $REDIS _db > $KEYFILE
  8. Outfile=valuelist.txt
  9. tempfile= $OUTFILE. tmp
  10. echo > $TEMPFILE
  11. For key in ' Cat $KEYFILE '
  12. Do
  13. Echo $key
  14. #echo "Hgetall $key" | Redis-cli-h $REDIS _host-p $REDIS _port-n $REDIS _db >> $TEMPFILE
  15. echo "GET $key" | Redis-cli-h $REDIS _host-p $REDIS _port-n $REDIS _db >> $TEMPFILE
  16. Done


2. Organize and export the results

[Plain]View Plain copy
    1. 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
    1. Cat $OUTFILE | Redis-cli-h localhost-p 6379

Export and import directly with Redis's dump and restore

[Plain]View Plain copy
      1. Redis-cli--raw Dump Test | head-c-1 | Redis-cli-x Restore Test1 0

Shell Export and import Redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.