Shell Export and import Redis

Source: Internet
Author: User
Tags redis

1. Export Redis

#!/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=valuelis    T.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 _h Ost-p $REDIS _port-n $REDIS _db >> $TEMPFILE done


2, sorting out the results of the export

Cat $TEMPFILE |xargs-n 2|awk-f "" v keyname= $KEYNAME ' {print "Hset" KeyName "" $, "" $ ""} "> $OUTFILE
The Xargs-n 2 is a line of 2 lines, and a newline character is replaced with a space

And then using awk to separate the spaces, $ is Keynam $ is the value of the key

Convert again to Hset format, where you use the KeyName variable, which is defined with-V.

Finally converted to Hset keyname KEY "VALUE" format.


3, the results of the import transformation

Cat $OUTFILE | Redis-cli-h localhost-p 6379

Two, directly with the Redis dump and restore export and import

Redis-cli--raw Dump Test | head-c-1 | Redis-cli-x Restore Test1 0




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.