codecamp

7. 数据批量导入与导出

数据批量导入与导出

通过以下脚本向 xhttpcache 里批量导入磁盘文件, 以下脚本会将参数指定的目录下的文件都导入到 xhttpcache 里.

#!/bin/bash

redis_cmd="redis-cli -p 7379 -x set "
function batch_convert() {
    for file in `ls $1`
    do
        if [ -d $1"/"$file ]
        then
            batch_convert $1"/"$file
        else
            key=$1"/"$file       
            #echo 33 $redis_cmd  ${key#*/}   $1"/"$file
            $redis_cmd ${key#*/} < $1"/"$file
        fi
    done
}

batch_convert $1

通过以下shell脚本,可以把 xhttpcache 里的数据按目录文件的形式导出到磁盘。

#!/bin/sh
# 
# xHttpCache数据备份脚本
# By: xSky

usage()
{
    echo "++++++++++++++++++++++++++++++++++++++++++++++++++++" 
    echo "+ xHttpCache data export shell tools by:xSky       " 
    echo "+ usage:                                           " 
    echo "+ $0 data_dir redis_host redis_port                " 
    echo "++++++++++++++++++++++++++++++++++++++++++++++++++++" 
}

#判断参数
if [ $# -ne 3 ]; then
    usage
    exit 0
fi

data_dir=$1
redis_host=$2
redis_port=$3
echo $data_dir $redis_host $redis_port

if [ -d $data_dir ];then
    #redis-cli -p 6579 scan a z | awk  '{print "redis-cli -p 6579 get "$0 " >"$0}'|sh
    cd $data_dir
    redis-cli -h $redis_host -p $redis_port scan a z | while read line
    do
        echo $line 
        bdir=`expr index $line "/"`
        file_dir=${line%/*}
        file_name=$line

        if [[ $bdir -gt 0 ]];then
            #echo 1 $bdir $file_dir $file_name
            mkdir -p $file_dir
        fi

        redis-cli -h $redis_host -p $redis_port get $line > $line

    done
    cd -
else
    echo [ERROR] the dir $data_dir do not exist
    exit 0
fi

6. 通过redis命令使用示例:
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }