#!/bin/sh
set -e
Path=/data/redis
VERSION="5.0.7"
REDIS_PWD=Yonyou123

########################################################################################## image
if [ "`docker images | grep redis`" ]; then
    echo 'Detecting image exist. Will not pull again.'
    docker images redis
else
    echo 'Begin to pull image ...'
    docker pull redis:$VERSION
fi

########################################################################################## install

mkdir -p $Path/conf && cd $Path/conf
cat <<EOF > redis.conf
port 6379
bind 0.0.0.0
daemonize no
databases 16
requirepass "$REDIS_PWD"
always-show-logo yes
save 900 1
save 300 10
save 60 10000
dbfilename dump.rdb
dir /data
appendonly yes
logfile ""
protected-mode yes
appendfilename "appendonly.aof"
# replicaof <masterip> <masterport>
# masterauth <master-password>
replica-priority 100
# replica-announce-ip 5.5.5.5
# replica-announce-port 26379
timeout 0
tcp-keepalive 300
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
replica-lazy-flush no
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
# repl-ping-replica-period 10
# repl-timeout 60
repl-disable-tcp-nodelay no
# repl-backlog-ttl 3600
# min-replicas-to-write 1
# min-replicas-max-lag 10
# maxclients 10000
# maxmemory <bytes>
# maxmemory-policy noeviction
# replica-ignore-maxmemory yes
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no

appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 100000
slowlog-max-len 128
latency-monitor-threshold 0
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
# proto-max-bulk-len 512mb
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
EOF


docker run -d --restart=always --hostname redis -p 6379:6379 \
    -v $Path/conf/redis.conf:/etc/redis.conf -v $Path/data:/data \
    --name redis redis:$VERSION redis-server /etc/redis.conf
docker logs -f redis

exit 0
