redis基本操作脚本

环境:redis已经下载和make好了,datadir是/data1,basedir是/data1/redis-4.0.6

     可直接通过中控机远程操作多台,将basedir和脚本scp过去就可以

 1 #!/bin/bash
 2 
 3 #author:hushi
 4 #date:2017-12-8
 5 
 6 log_file=/tmp/redis_install.log
 7 datadir=/data1
 8 redis_datadir=/usr/local/redis-4.0.6.tgz
 9 
10 function helpfunc()
11 {
12     echo "Please check your parameter"
13     echo
14     echo "-p        specify the port of redis to be installed"
15     echo "Example:
16             bash redis_install.sh -p 9000"
17 }
18 
19 if  [ $# -lt 1 ]
20 then
21     helpfunc && exit 1
22 else
23     while getopts "p:" Option
24     do
25         case $Option in
26            p) port=$OPTARG;;
27            *) helpfunc; exit 1; ;;
28         esac
29     done
30 fi
31 
32 function before_install_check()
33 {
34     if [ -z "$port" ]; then
35         helpfunc && exit 1
36     fi
37     port=`echo $port | egrep "^[0-9]{4}$"`
38     if [ -z "$port" ]; then
39         helpfunc && exit 1   
40     fi
41     [ -e ${datadir} ] || mkdir ${datadir}
42     [ -e "${datadir}/redis-4.0.6" ] || /bin/cp ${redis_datadir} ${datadir}
43     
44     if ! echo $PATH|grep redis &>>${log_file};then
45         sed -i "$a export PATH=/data1/redis/redis-${version}/src:$PATH" ~/.bashrc && . ~/.bashrc
46     fi
47     
48     if netstat -antlp|grep ${port} &>>${log_file} ;then
49         echo -e "       \e[43;31m port ${port} is be used,please check and input again\e[0m" && exit 1
50     fi
51     
52 
53 }
54 
55 
56 function install()
57 {
58     cd ${datadir} && mkdir redis${port}
59     cp ${datadir}/redis-4.0.6/redis.conf  redis${port}/redis${port}.conf
60 
61     sed -i "s/6379/${port}/g" redis${port}/redis${port}.conf
62 
63  }
64 
65 
66 function main()
67 {
68     before_install_check
69     install
70 #    after_install_check
71 }
72 
73 main

 

#!/bin/bash

#author:hushi
#date:2017-12-12

log_file=/tmp/redis_replication.log
datadir=/data1
basedir=/data1/redis-4.0.6

function helpfunc()
{
    echo "Please check your parameter"
    echo
    echo "-m        specify the port of redis master "
    echo "-s        specify the port of redis slave  "
    echo "Example:
            bash redis_replication.sh  -m 9000 -s 9001"
}


if  [ $# -lt 1 ]
then
    helpfunc && exit 1
else
    while getopts "m:s:" Option
    do
        case $Option in
            m) master_port=$OPTARG;;
            s) slave_port=$OPTARG;;
            *) helpfunc; exit 1; ;;
        esac
    done
fi

function before_replication_check()
{ 
    if [ ! -e "${datadir}/redis${master_port}" ] || [ ! -e "${datadir}/redis${slave_port}"  ]   &>>${log_file};then
        echo -e "      \e[43;31m master port or slave port is not be initialized,please check error log ${log_file} \e[0m" && exit 1 
    fi

    if ! netstat -antlp|grep ${master_port} &>>${log_file} ;then
        echo -e "       \e[43;31m master port ${master_port} is not be started up ,please check and input again\e[0m" && exit 1
    fi
    if ! netstat -antlp|grep ${slave_port} &>>${log_file} ;then
        echo -e "       \e[43;31m slave port ${slave_port} is not be started up ,please check and input again\e[0m" && exit 1
    fi

}

function replication()
{
    sed -i '/^appendonly/ s/no/yes/'  ${datadir}/redis${slave_port}/redis${slave_port}.conf
    sed -i '/^maxmemory/ s/8gb/70gb/' ${datadir}/redis${slave_port}/redis${slave_port}.conf

    if   ${datadir}/redis-4.0.6/src/redis-cli -p ${slave_port} slaveof 127.0.0.1 ${master_port} &>>${log_file}
    then
        echo -e  "      \e[42;30m Replication is established succeed \e[0m"
        sed -i "/^port/a slaveof                127.0.0.1 ${master_port}" ${datadir}/redis${slave_port}/redis${slave_port}.conf
    else
        echo -e  "      \e[43;31m Replicaion is establieshed failed,please check error log  ${log_file}\e[0m"
    fi
}

function after_replication_check()
{
   la 
        
}


function main()
{
    before_replication_check    
    replication
#    after_replication_check
}

main

 

 1 #!/bin/bash
 2 
 3 #author:hushi
 4 #date:2017-12-11
 5 
 6 log_file=/tmp/maintain.log
 7 com_dir=/data1/redis-4.0.6/src
 8 
 9 
10 function usage()
11 {
12     echo "Please check your parameter"
13     echo
14     echo -e "-T        specify the \e[31m type \e[0m of redis to be operate,\e[35m 0 is stop redis, 1 is start redis, 2 is restart, 3 is kill redis\e[0m"
15     echo -e "-P        specify the \e[31m port \e[0m of redis to be operate"
16     echo "Example:
17             bash redis_maintain.sh -T 1 -P 7000 "
18 }
19 
20 if  [ $# -lt 1 ]
21 then
22     usage && exit 1
23 else
24     while getopts "P:T:" Option
25     do
26         case $Option in
27             P) port=$OPTARG;;
28             T) type=$OPTARG;;
29             *) usage; exit 1; ;;
30         esac
31     done
32 fi
33 
34 function start()
35 {
36      ${com_dir}/redis-server /data1/redis${port}/redis${port}.conf  &>>${log_file} && echo -e "\e[32m       redis ${port} port has already start\e[0m"|| echo -e  "\e[31m  redis ${port} port start failed\e[0m"
37 }
38 
39 function stop()
40 {
41     ${com_dir}/redis-cli -p ${port} shutdown  &>>${log_file} && echo -e "\e[32m       redis ${port} port has already stopped\e[0m" || echo -e "\e[31m  redis ${port} port stop failed\e[0m"
42 }
43 
44 function kill()
45 {
46     ps -ef|grep redis|grep ${port}|awk '{print $2}'|xargs kill -9 &>>${log_file} && echo -e "\e[32m        redis ${port} port has already stopped\e[0m" || echo -e "\e[31m  redis ${port} port stop failed\e[0m"
47 }
48 case $type in
49     0) stop;;
50     1) start;;
51     2) stop;start ;;
52     3) kill;;
53     *) echo "   type input fault,please check and input again "exit; ;;
54 esac

 

posted @ 2017-12-20 11:49  jiamiu  阅读(330)  评论(0编辑  收藏  举报