#!/bin/bash
#################################################
# install redis #
#################################################
echo
echo "you have selected to install redis"
echo
# check if ther's an active and running redis-server
redis_running=$(ps aux | grep -v grep | grep -c redis-server)
redis_process=$(ps aux | grep -v grep | grep redis-server)
if [ "$redis_running" -gt 0 ]; then
echo " there are redis running on this server"
echo
redis_port=$(echo "$redis_process" | awk '{print $12}')
echo "with IP and Port/s:"
echo "$redis_port" # Displaying all the active redis and prots
echo
redis_version=$(/usr/local/redis/bin/redis-server --version | awk '{print $3}')
echo "redis version: $redis_version"
echo
echo "if you want to remove the existing redis,remove it manually."
echo
echo "redis installation will continue,please use available ports for your new redis."
echo
else
echo "No active redis found on this server. installation continues..."
echo
fi
# confirmation for installion
read -p "do you want to continue? [ yes / no ]: " user_input
user_input=${user_input:-yes}
echo
#convert user_choice to lowercase for case-insensitive comparison
user_input_lower=$(echo "$user_input" | tr '[:upper:]' '[:lower:]')
# check the user's input
if [ "$user_input_lower" == "yes" ]; then
echo "Redis installation confirmed."
echo
else
echo "Redis installation cancelled."
echo
exit
fi
# IP
host=$(hostname -I)
# Port
read -rp "Insert your redis port [ default is 6379 ] : " input_port
input_port=${input_port:-6379}
port=$(echo "$input_port" | awk '{print $1}')
echo
# Password
read -rp "Insert your redis password : " input_pass
password=$(echo "$input_pass" | awk '{print $1}')
systemctl daemon-reload
# downloading the redis binary files
if [ ! -f "/usr/local/redis/bin/redis-server" ]; then
mkdir -p /usr/local/redis/bin/
wget http://file.abcmoreonline.com:9899/script/dba/redis/redis-server -O /usr/local/redis/bin/redis-server
wget http://file.abcmoreonline.com:9899/script/dba/redis/redis-cli -O /usr/local/redis/bin/redis-cli
chmod +x /usr/local/redis/bin/redis*
fi
# redis data directory
if [ ! -d "/data/redis${port}/" ]; then
mkdir -p /data/redis${port}/
fi
# redis service file
if [ ! -f "/usr/lib/systemd/system/redis${port}.service" ]; then
cat >/usr/lib/systemd/system/redis${port}.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/redis/bin/redis-server /data/redis${port}/redis.conf --supervised systemd
ExecStop=/usr/local/redis/bin/redis-cli -h ${host} -p ${port} -a ${password} ADMIN_SHUTDOWN
Type=notify
User=redis
Group=redis
LimitNOFILE=65535
LimitNPROC=65535
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
StandardOutput=journal
StandardError=inherit
TimeoutStopSec=0
KillSignal=SIGTERM
SendSIGKILL=no
SuccessExitStatus=0
Restart=always
[Install]
WantedBy=multi-user.target
EOF
fi
# Redis configuration file
if [ ! -f "/data/redis${port}/redis.conf" ]; then
cat >/data/redis${port}/redis.conf <<EOF
#Redis server netlisten
port ${port}
# PID FILE PAHT
pidfile /data/redis${port}/redis_${port}.pid
dir /data/redis${port}
cluster-config-file /data/redis${port}/redis_cluster.conf
# bind ip address the usually bind localhost
bind ${host}
# start potocal model and enhance authour to process
protected-mode yes
cluster-enabled yes
cluster-node-timeout 15000
maxclients 100000
maxmemory 0
repl-backlog-size 200m
daemonize yes
masterauth ${password}
requirepass ${password}
rename-command SHUTDOWN "ADMIN_SHUTDOWN"
rename-command FLUSHALL "ADMIN_FLUSHALL"
rename-command FLUSHDB "ADMIN_FLUSHDB"
rename-command KEYS ""
# TCP CONNECT SQUENCE MANY
tcp-backlog 1024
# customer timeout (0 means unlimits)
timeout 0
# Tcp keep active time
tcp-keepalive 300
# isif on process modes run
daemonize no
# isif monitor model run
supervised no
# log level (choose: debug,verbose,notice,warning)
loglevel notice
# log fill path
logfile /data/redis${port}/redis_server_${port}.log
# database count, default 16 counts
databases 16
# isif allways shows Redis status
always-show-logo yes
# deposted save database snapshot of coonfidation and time
save 900 1
save 300 10
save 60 10000
# if the product error when in RDB file ,THE need stop write
stop-writes-on-bgsave-error yes
# if enable rdb file compress
rdbcompression yes
# if enable rdb file check
rdbchecksum yes
# red file of filename
dbfilename dump.rdb
# rdb file save path
dir ./
# copies provide older data (stale data)
replica-serve-stale-data yes
# replica is read-only
replica-read-only yes
# if in the disk sync disable TCP_NODELAY
repl-diskless-sync no
# copies delay
repl-diskless-sync-delay 5
# if disable tcp nodelay
repl-disable-tcp-nodelay no
# copies piror
replica-priority 100
# enable revoke and expired clear
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
# copies in the lazy flush
replica-lazy-flush no
# is enable AOF (Append-Only File) mode
appendonly no
# AOF FileName
appendfilename "appendonly.aof"
# AOF sync (choose: always,everysec,no)
appendfsync everysec
# Rewrite AOF file isif disable fsync
no-appendfsync-on-rewrite no
# autometic rewrite AOF file when need triiger condification of percent
auto-aof-rewrite-percentage 100
# autometic rewirte AOF MIN FILE
auto-aof-rewrite-min-size 64mb
# isif reload AOF file
aof-load-truncated yes
# in AOF file the ahead load RDB file content
aof-use-rdb-preamble yes
# lua script limit time
lua-time-limit 5000
# record slower query time
slowlog-log-slower-than 10000
# slow log max length
slowlog-max-len 128
# monitor delay events internal value (0 disable)
latency-monitor-threshold 0
# monitor keyworld notice events
notify-keyspace-events ""
# hash data constrcult max ziplist elements count and max ziplist value
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
# List data constrcult max ziplist size
list-max-ziplist-size -2
# List data constrcult press depth
list-compress-depth 0
# Set data constrcult max dataset elements count
set-max-intset-entries 512
# Zset data constrcult max ziplist elements count and max ziplist value size
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
# HyperLogLog data constrcult max databityes count
hll-sparse-max-bytes 3000
# Stream data constrcult max node size and max nodes count
stream-node-max-bytes 4096
stream-node-max-entries 100
# isif enable primary rehashing
activerehashing yes
# client output buffer limit set
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
# circle time check
hz 10
# isif enable dynamic hz
dynamic-hz yes
# isif incremental AOF file
aof-rewrite-incremental-fsync yes
# isif incremental fsync rdb file
rdb-save-incremental-fsync yes
EOF
# redis user
useradd redis
chown -R redis. /usr/local/redis
chown -R redis. /data/redis*
sudo systemctl daemon-reload
systemctl start redis${port}.service
systemctl status redis${port}.service
systemctl enable redis${port}.service
fi
echo
if sudo systemctl is-active --quiet "redis$port"; then
echo "Redis $port installed successfully!"
echo
else
echo "Redis $port installation failed."
echo
fi
cat <<"EOF"
/usr/local/redis/bin/redis-cli -a mypassword --cluster create 172.31.26.3:7001 172.31.26.3:7002 172.31.26.3:7003 172.31.26.3:7004 172.31.26.3:7005 172.31.26.3:7006 --cluster-replicas 1
/usr/local/redis/bin/redis-cli -a --cluster check 172.31.26.3:7001
EOF
/usr/local/redis/bin/redis-cli -h 172.31.28.201 -p 7001 -c -a 'password'
/usr/local/redis/bin/redis-cli -h 172.31.28.201 -p 7002 -c -a 'password'
get redis
type
set
hgetall