1. 此脚本支持两个版本的redis部署,更改镜像,可以修改版本
2. 此脚本支持容器的自定义过期时间
展示效果如下图:
#!/bin/bash
set -e
Color_Text()
{
echo -e " \e[0;$2m$1\e[0m"
}
Echo_Yellow()
{
echo "$(Color_Text "$1" "33")"
}
Echo_Yellown()
{
echo -n "$(Color_Text "$1" "33")"
}
Echo_Red()
{
echo "$(Color_Text "$1" "31")"
}
Echo_Green()
{
echo "$(Color_Text "$1" "32")"
}
Echo_Pink()
{
echo "$(Color_Text "$1" "45")"
}
#镜像启动命令
cmd="/usr/local/bin/redis-server /usr/local/etc/redis/redis.conf"
#redis配置文件
redis_conf="/usr/local/etc/redis/redis.conf"
#redis实例名检查
check_reids_name()
{
set +e
if [ -z ${redis_name} ];then
Echo_Yellown " 输入为空或者不合法,请重新输入 -- Redis Name:"
read redis_name
check_reids_name
elif [ ${#redis_name} -lt 2 ]; then
Echo_Yellown " 请输入2位及以上的Redis Name,请重新输入 -- Redis Name:"
read redis_name
check_reids_name
else
docker ps |grep " ${redis_name}$" >/dev/null 2>&1
if [ $? -eq 0 ];then
Echo_Yellown " Redis Name 已存在,请重新输入 -- Redis Name:"
read redis_name
check_reids_name
fi
fi
set -e
}
#获取redis实例名
get_redis_name()
{
echo ''
Echo_Yellow " Redis Name 建议3个字符以上(可用英文和数字). "
Echo_Yellown " 请输入 -- Redis Name: "
read redis_name
check_reids_name
redisname="--name ${redis_name}"
if [ -z ${redis_name} ];then
Echo_Red " 未输入有效信息,随机生成 Redis Name !"
redisname=""
echo ''
else
redisname="--name ${redis_name}"
fi
echo ''
}
#获取redis自定义配置文件
get_redis_conf()
{
if [ -f /tmp/redis.conf ];then
my_redis_conf="/tmp/redis.conf"
redisconf="-v ${my_redis_conf}:${redis_conf}"
else
redisconf=""
cmd=""
fi
}
#创建redis容器
create_redis()
{
set +e
redis_image="jixson/redis-alpine:${redis_version}"
docker run -d --network mac_net ${redisname} ${redisconf} ${redis_image} ${cmd} >/dev/null 2>&1
docker ps |grep " ${redis_name}$" >/dev/null 2>&1
if [ $? -eq 0 ];then
docker_id=$(docker ps |grep " ${redis_name}$" | awk '{print $1}')
docker_ip=$(docker inspect ${docker_id} |grep -A 80 "NetworkSettings" | grep -A 50 "Networks" |grep "IPAddress" | awk -F'"' '{print $4}')
else
Echo_Red " 容器创建失败,请 检查配置文件 或 联系运维!"
exit 1
fi
set -e
}
#添加定时任务,清理无用镜像
docker_clear()
{
at now + ${save_time} days <<< "/usr/bin/docker stop ${redis_name} && /usr/bin/docker rm ${redis_name}" >/dev/null 2>&1
docker rm $(docker ps -a -q) >/dev/null 2>&1
}
#镜像信息罗列
info_version_5.0.6()
{
echo ''
Echo_Green " +-------------------------------------------+"
Echo_Green " | redis_version:5.0.6 |"
Echo_Green " | redis_build_id:f65e186a21099559 |"
Echo_Green " | redis_mode:standalone |"
Echo_Green " | os:Linux 5.2.11-1.el7.elrepo.x86_64 |"
Echo_Green " +-------------------------------------------+"
echo ''
}
info_version_3.2.3()
{
echo ''
Echo_Green " +-------------------------------------------+"
Echo_Green " | redis_version:3.2.3 |"
Echo_Green " | redis_build_id:238612bc08a76720 |"
Echo_Green " | redis_mode:standalone |"
Echo_Green " | os:Linux 5.2.11-1.el7.elrepo.x86_64 |"
Echo_Green " +-------------------------------------------+"
echo ''
}
describe_info()
{
echo ''
Echo_Green " +-------------------------------------------+"
Echo_Green " | 如需自定义Redis配置文件,请上传至/tmp |"
Echo_Green " | 配置文件命名必须是 redis.conf |"
Echo_Green " | 配置文件模板见 /tmp/redis.conf.bak |"
Echo_Green " | 配置文件模板内,路径相关的无需更改!!! |"
Echo_Green " +-------------------------------------------+"
echo ''
echo -n ' '
Echo_Pink " +--------------------------------------------------------------+ "
echo -n ' '
Echo_Pink " | 注意: 此 Redis 未做持久化,容器重启或者删除后数据将清空!!!| "
echo -n ' '
Echo_Pink " +--------------------------------------------------------------+ "
echo ''
Echo_Yellown " 请按 Enter 键,确认继续."
read var
echo ''
}
#容器保留时间
save_time()
{
echo ''
Echo_Red " 1: 10 天 (Default)"
Echo_Red " 2: 30 天"
Echo_Red " 3: 60 天"
Echo_Yellown " 请选择实例使用时间 (输入对应数字即可 ): "
read save_time
echo ''
case "${save_time}" in
1)
Echo_Yellow " Redis 实例使用时间限制: 10 天"
export save_time="10"
;;
2)
Echo_Yellow " Redis 实例使用时间限制: 30 天"
export save_time="30"
;;
3)
Echo_Yellow " Redis 实例使用时间限制: 60 天"
export save_time="60"
;;
*)
Echo_Yellow " 未输入有效选项,默认 Redis 实例使用时间限制: 10 天"
export save_time="10"
;;
esac
}
#实例创建信息
create_info()
{
Echo_Yellow " 您创建的 Redis 信息如下,请牢记:"
echo ''
Echo_Yellow " Redis 实例名: ${redis_name}"
echo ''
Echo_Yellow " Redis-IP: ${docker_ip} "
echo ''
if [ -f /tmp/redis.conf ];then
Echo_Yellow " Redis服务端口: 您已上传Redis配置文件,请按配置文件内的端口访问."
rm -rf /tmp/redis.conf
echo ''
else
Echo_Yellow " Redis服务端口: 6379"
echo ''
fi
echo -n ' '
Echo_Pink " 此实例将于 ${save_time} 天 后自动删除,请注意本地保存数据!!!"
echo ''
echo ''
}
#实例版本选择
redis_version_select()
{
echo ''
Echo_Red " 1: redis_version:3.2.3 (Default)"
Echo_Red " 2: redis_version:5.0.6"
Echo_Yellown " 请选择 Redis Version (输入对应数字即可 ): "
read redis_version_select
echo ''
case "${redis_version_select}" in
1)
Echo_Yellow " 您选择的 Redis版本 是: 3.2.3"
export redis_version="3.2.3"
info_version_3.2.3
describe_info
save_time
;;
2)
Echo_Yellow " 您选择的 Redis版本 是: 5.0.6"
export redis_version="5.0.6"
info_version_5.0.6
describe_info
save_time
;;
*)
Echo_Yellow " 未输入有效选项,默认 Redis版本 : 3.2.3"
export redis_version="3.2.3"
info_version_3.2.3
describe_info
save_time
;;
esac
}
#标记
sign()
{
Echo_Yellown " 请按 Enter键 开始创建,退出请按 Ctrl + C . "
read var
echo ''
}
clear
#创建入口
create()
{
redis_version_select
get_redis_name
get_redis_conf
sign
create_redis
create_info
docker_clear
}
create