脚本打包方式
由于Redis实际迁移场景中,部分配置文件、数据文件等存放在Redis安装路径外,为方便打包源成分,我们提供了一个打包脚本示例。脚本会将Redis安装路径外源成分按照其原目录结构打包在“/打包路径/Redis安装路径/bin(或src)/source_related”目录下。
前提条件
在进行打包前,需要先启动Redis实例。如果使用了
操作步骤
- 请在源虚拟机的任意位置创建一个.sh脚本文件(例如pack_redis_sources.sh),并将pack_redis_sources.sh添加到该文件中(该脚本为一个可用的示例,可根据需求进行相应修改)。
- 启动Redis后,执行该脚本以完成打包过程。请将“/path/to”替换为脚本的实际路径。
1
bash /path/to/pack_redis_sources.sh
若Redis配置了密码,则需要输入密码(可在Redis配置文件中的requirepass字段找到)。输入完成后,脚本将自动进行打包。根据提示,您可以获取已打包完成的安装包。
- 如果执行脚本过程中出现以下报错,请按照如下步骤进行操作。
1
-bash: ./autoBuild.sh: /bin/bash^M: bad interpreter: No such file or directory
- 安装dos2unix。
1
yum install dos2unix
- 安装成功后执行以下命令。
1
dos2unix pack_redis_sources.sh
- 执行完成后请重新启动脚本。
若您无可用Yum源安装失败,请执行以下命令,执行完成后请重新启动脚本。1
sed -i 's/\r//g' pack_redis_sources.sh
- 安装dos2unix。
- 打包完成后请手动删除生成的source_related目录和devkit_redis_conf_data.json文件。
pack_redis_sources.sh
该脚本会自动识别Redis(以及哨兵节点)的IP地址和端口号信息,请确认是否需要修改这些信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | #!/bin/bash # 检查Redis是否已启动 redis_pid=$(pgrep -f redis-server) if [ -z "$redis_pid" ]; then echo "The Redis service is not running. The script exits." exit 1 else echo "The Redis service is running. PID: $redis_pid" fi # 检查Redis哨兵是否已启动 sentinel_pid=$(pgrep -f redis-sentinel) if [ -z "$sentinel_pid" ]; then echo "The Redis sentinel is not running." is_sentinel_mode=false else echo "The Redis sentinel is running. PID: $sentinel_pid" is_sentinel_mode=true fi # 获取Redis服务的IP地址和端口号 # 使用ps命令获取redis-server和redis-sentinel的信息 redis_info=$(ps -ef | grep redis | grep -v grep) # 提取redis-server的IP地址和端口号 redis_server_info=$(echo "$redis_info" | grep 'redis-server' | awk '{print $NF}') redis_host=$(echo "$redis_server_info" | cut -d ':' -f1) redis_port=$(echo "$redis_server_info" | cut -d ':' -f2) # 如果未获取到Redis的IP地址和端口,则设置默认值 if [ -z "$redis_host" ]; then redis_host="127.0.0.1" redis_port="6379" echo "Failed to obtain the IP address and port of redis-server. Use the default values: $redis_host:$redis_port" else echo "Obtained the IP address and port of redis-server: $redis_host:$redis_port" fi # 获取redis-sentinel的IP地址和端口号(如果存在) redis_sentinel_host="127.0.0.1" redis_sentinel_port="26379" sentinel_info=$(echo "$redis_info" | grep 'redis-sentinel') if [ -n "$sentinel_info" ]; then # 使用正则表达式匹配并提取IP地址和端口部分 sentinel_info=$(echo "$sentinel_info" | grep -oP '\d+\.\d+\.\d+\.\d+:\d+|\*:\d+') # 提取类似 *:26379 或 IP:端口 # 提取IP地址和端口 sentinel_host=$(echo "$sentinel_info" | cut -d ':' -f1) sentinel_port=$(echo "$sentinel_info" | cut -d ':' -f2) # 如果IP地址为 '*' 或为空,设置为默认值 if [ "$sentinel_host" == "*" ] || [ -z "$sentinel_host" ]; then sentinel_host="127.0.0.1" echo "The IP address of redis-sentinel is set to the default value: $sentinel_host" fi # 如果端口为空或为 '*', 设置为默认端口 if [ -z "$sentinel_port" ] || [ "$sentinel_port" == "*" ]; then sentinel_port="26379" echo "The port of redis-sentinel is set to the default value: $sentinel_port" fi redis_sentinel_host=$sentinel_host redis_sentinel_port=$sentinel_port echo "Obtained the IP address and port of redis-sentinel: $redis_sentinel_host:$redis_sentinel_port" else echo "Failed to obtain redis-sentinel information. Use the default values: $redis_sentinel_host:$redis_sentinel_port" fi # 获取redis-server可执行文件的路径 redis_exe_path=$(readlink -f /proc/$redis_pid/exe) # 获取父目录路径 redis_parent_dir=$(dirname "$redis_exe_path") # 去掉路径末尾的斜杠(如果有的话) redis_parent_dir=$(echo "$redis_parent_dir" | sed 's/\/$//') # 判断redis-server的父目录是src安装目录还是bin安装目录 if [[ "$redis_parent_dir" == */src ]]; then installation_dir=$(dirname "$redis_parent_dir") echo "src installation directory identified: $installation_dir" elif [[ "$redis_parent_dir" == */bin ]]; then installation_dir="$redis_parent_dir" echo "bin installation directory identified: $installation_dir" # 检查bin的父目录是否存在src/redis-server parent_dir=$(dirname "$redis_parent_dir") if [ -f "$parent_dir/src/redis-server" ]; then # 直接设置installation_dir为$parent_dir (src目录) installation_dir="$parent_dir" redis_parent_dir="$parent_dir/src" # 更新redis_parent_dir为src echo "Installation directory: $installation_dir" else echo "$parent_dir does not contain src/redis-server. Use bin as the installation directory." installation_dir="$redis_parent_dir" fi else echo "The given directory is neither the src directory nor the bin directory. Check whether the path is correct." exit 1 fi # 获取用户输入的IP地址,默认是$redis_host read -p "The identified IP address of the Redis server is $redis_host. To change the IP address, input the correct one. To use the identified IP address, press Enter: " redis_host_input redis_host=${redis_host_input:-$redis_host} # 获取用户输入的端口号,默认是$redis_port read -p "The identified port of the Redis server is $redis_port. To change the port, input the correct one. To use the identified port, press Enter: " redis_port_input redis_port=${redis_port_input:-$redis_port} # 获取用户输入的秘钥,默认是空 read -s -p "Input the password of the Redis server (If no password is configured, press Enter): " redis_password echo redis_password=${redis_password:-""} if [ -n "$sentinel_info" ]; then # 让用户修改Redis哨兵的IP地址和端口号 read -p "The identified IP address of the Redis sentinel is $redis_sentinel_host. To change the IP address, input the correct one. To use the identified IP address, press Enter: " redis_sentinel_host_input redis_sentinel_host=${redis_sentinel_host_input:-$redis_sentinel_host} read -p "The identified port of the Redis sentinel is $redis_sentinel_port. To change the port, input the correct one. To use the identified port, press Enter: " redis_sentinel_port_input redis_sentinel_port=${redis_sentinel_port_input:-$redis_sentinel_port} else echo "Your current server does not start the sentinel node." fi # 用户输入后可以增加验证IP和端口的格式,例如使用正则检查输入 # 检查IP格式 if [[ ! "$redis_host" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then echo "Error: Invalid Redis server IP address format!" exit 1 fi # 检查端口号是否为有效数字 if [[ ! "$redis_port" =~ ^[0-9]+$ ]]; then echo "Error: Invalid Redis server port!" exit 1 fi # 检查Redis哨兵IP地址格式 if [[ ! "$redis_sentinel_host" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then echo "Error: Invalid Redis sentinel IP address format!" exit 1 fi # 检查Redis哨兵端口号是否为有效数字 if [[ ! "$redis_sentinel_port" =~ ^[0-9]+$ ]]; then echo "Error: Invalid Redis sentinel port!" exit 1 fi # 切换到Redis安装目录,确保可以执行redis-cli cd "$redis_parent_dir" echo "Go to the installation directory: $redis_parent_dir" # 获取Redis配置文件路径 echo "Obtaining the Redis configuration file path..." if [ -z "$redis_password" ]; then config_file_path=$(./redis-cli -h "$redis_host" -p "$redis_port" INFO | grep config_file | cut -d ':' -f2 | tr -d '[:space:]') else config_file_path=$(./redis-cli -h "$redis_host" -p "$redis_port" -a "$redis_password" INFO | grep config_file | cut -d ':' -f2 | tr -d '[:space:]') fi if [ -z "$config_file_path" ]; then echo "Failed to obtain the Redis configuration file path. Check that the Redis service is started and the password is correct." exit 1 fi config_file_path=$(realpath "$config_file_path") echo "Configuration file path: '$config_file_path'" # 获取安装目录中的配置文件路径 if [[ "$config_file_path" == "$installation_dir"* ]]; then redis_conf_in_package=true echo "The configuration file is in the installation directory. Skip the step of copying the configuration file." else redis_conf_in_package=false echo "The configuration file is not in the installation directory. Copying the configuration file..." # 获取配置文件父目录的绝对路径 config_file_parent_dir=$(dirname "$config_file_path") # 构造目标目录,目标目录为 redis_parent_dir 下的 source_related + 配置文件父目录 target_dir="$redis_parent_dir/source_related$config_file_parent_dir" # 创建目标目录(如果不存在) mkdir -p "$target_dir" cp "$config_file_path" "$target_dir" echo "The configuration file has been copied to $target_dir." fi # 获取Redis数据目录 echo "Obtaining the Redis data directory..." if [ -z "$redis_password" ]; then redis_data_dir=$(./redis-cli -h "$redis_host" -p "$redis_port" CONFIG GET dir | awk 'NR==2 {print substr($0, index($0,$2))}' | tr -d '"') else redis_data_dir=$(./redis-cli -h "$redis_host" -p "$redis_port" -a "$redis_password" CONFIG GET dir | awk 'NR==2 {print substr($0, index($0,$2))}' | tr -d '"') fi # 输出Redis数据目录的原始返回值 # 检查数据目录是否为空 if [ -z "$redis_data_dir" ]; then echo "The Redis data directory is not set. Check the dir setting in the configuration file." else echo "Redis data directory: $redis_data_dir" fi # 获取dbfilename和appendfilename echo "Obtaining dbfilename and appendfilename..." if [ -z "$redis_password" ]; then dbfilename=$(./redis-cli -h "$redis_host" -p "$redis_port" CONFIG GET dbfilename | awk 'NR==2 {print substr($0, index($0,$2))}' | tr -d '"') appendfilename=$(./redis-cli -h "$redis_host" -p "$redis_port" CONFIG GET appendfilename | awk 'NR==2 {print substr($0, index($0,$2))}' | tr -d '"') else dbfilename=$(./redis-cli -h "$redis_host" -p "$redis_port" -a "$redis_password" CONFIG GET dbfilename | awk 'NR==2 {print substr($0, index($0,$2))}' | tr -d '"') appendfilename=$(./redis-cli -h "$redis_host" -p "$redis_port" -a "$redis_password" CONFIG GET appendfilename | awk 'NR==2 {print substr($0, index($0,$2))}' | tr -d '"') fi echo "dbfilename: $dbfilename" echo "appendfilename: $appendfilename" # 检查Redis数据文件 if [ -n "$redis_data_dir" ]; then # 判断Redis数据目录是否在安装目录下 if [[ "$redis_data_dir" != "$installation_dir"* ]]; then redis_dir_in_package=false echo "The Redis data directory is not under the installation directory. Creating a directory and copying data files..." # 创建目标目录,确保目标目录的完整路径 mkdir -p "$redis_parent_dir/source_related$redis_data_dir" # 检查dbfilename是否为空 if [ -n "$dbfilename" ]; then if [ -f "$redis_data_dir/$dbfilename" ]; then echo "Copying the dbfilename file..." cp "$redis_data_dir/$dbfilename" "$redis_parent_dir/source_related$redis_data_dir" else echo "dbfilename not found: $redis_data_dir/$dbfilename" fi else echo "dbfilename not set. Skip the step of copying the dbfilename file." fi # 检查appendfilename是否为空,如果为空则设置为默认的appendonly.aof if [ -z "$appendfilename" ]; then appendfilename="appendonly.aof" echo "appendfilename not set. The default value $appendfilename is used" fi # 检查appendfilename文件是否存在并进行复制 if [ -f "$redis_data_dir/$appendfilename" ]; then echo "Copying the appendfilename file..." cp "$redis_data_dir/$appendfilename" "$redis_parent_dir/source_related$redis_data_dir" else echo "appendfilename not found: $redis_data_dir/$appendfilename. Skip the copy operation." fi echo "The Redis data files have been copied to $redis_parent_dir/source_related$redis_data_dir." else redis_dir_in_package=true echo "The Redis data directory is under the installation directory. Skip the step of copying data files." fi else echo "The Redis data directory is not set. Skip the step of copying data files." fi # 获取logfile配置 if [ -z "$redis_password" ]; then logfile=$(./redis-cli -h "$redis_host" -p "$redis_port" CONFIG GET logfile | sed -n '2p') else logfile=$(./redis-cli -h "$redis_host" -p "$redis_port" -a "$redis_password" CONFIG GET logfile | sed -n '2p') fi # 判断是否配置了logfile if [ -n "$logfile" ]; then echo "Log file path: $logfile" # 如果logfile不在包内,则拷贝 if [[ "$logfile" != "$installation_dir"* ]]; then redis_logfile_in_package=false echo "The log file is not in the installation directory. Copying the log file..." mkdir -p "$redis_parent_dir/source_related$(dirname "$logfile")" cp "$logfile" "$redis_parent_dir/source_related$logfile" echo "The log file has been copied to the target directory: $redis_parent_dir/source_related$logfile" else redis_logfile_in_package=true echo "The log file is in the installation directory. Skip the step of copying the log file." fi else echo "The log file path is not set. Skip the step of copying the log file." fi # 获取pidfile配置 if [ -z "$redis_password" ]; then pidfile=$(./redis-cli -h "$redis_host" -p "$redis_port" CONFIG GET pidfile | sed -n '2p') else pidfile=$(./redis-cli -h "$redis_host" -p "$redis_port" -a "$redis_password" CONFIG GET pidfile | sed -n '2p') fi # 判断是否配置了pidfile if [ -n "$pidfile" ]; then echo "PID file path: $pidfile" # 仅保存 pidfile 路径 else echo "The PID file path is not set. Skip PID file processing." fi if [ -n "$sentinel_pid" ]; then # 获取Sentinel配置文件路径 echo "Obtaining the sentinel configuration file path..." if [ -z "$redis_password" ]; then sentinel_config_file_path=$(./redis-cli -h "$redis_sentinel_host" -p "$redis_sentinel_port" INFO | grep config_file | cut -d ':' -f2 | tr -d '[:space:]') else sentinel_config_file_path=$(./redis-cli -h "$redis_sentinel_host" -p "$redis_sentinel_port" -a "$redis_password" INFO | grep config_file | cut -d ':' -f2 | tr -d '[:space:]') fi sentinel_config_file_path=$(realpath "$sentinel_config_file_path") # 判断sentinel_config_file_path文件是否存在 if [ -e "$sentinel_config_file_path" ]; then echo "Redis sentinel configuration file path: '$sentinel_config_file_path'" # 获取安装目录中的sentinel.conf配置文件路径 if [[ "$sentinel_config_file_path" == "$installation_dir"* ]]; then sentinel_conf_in_package=true echo "The sentinel configuration file is in the installation directory. Skip the step of copying the configuration file." else sentinel_conf_in_package=false echo "The sentinel configuration file is not in the installation directory. Copying the configuration file..." # 获取配置文件父目录的绝对路径 config_file_parent_dir=$(dirname "$sentinel_config_file_path") # 构造目标目录,目标目录为redis_parent_dir下的 source_related + 配置文件父目录 target_dir="$redis_parent_dir/source_related$config_file_parent_dir" # 创建目标目录(如果不存在) mkdir -p "$target_dir" cp "$sentinel_config_file_path" "$target_dir" echo "The sentinel configuration file has been copied to $target_dir." fi # 获取Redis哨兵数据目录(从 sentinel.conf 中读取) sentinel_data_dir=$(grep -i "^dir" "$sentinel_config_file_path" | cut -d ' ' -f2) # 如果sentinel_data_dir是./,则将其设置为空 if [ "$sentinel_data_dir" == "./" ]; then sentinel_data_dir="" echo "Redis sentinel data directory: $sentinel_data_dir" else echo "Redis sentinel data directory: $sentinel_data_dir" fi else echo "Redis sentinel configuration file path not found. Skip the subsequent operations. Check that the file exists." fi else echo "Redis sentinel node not detected. Skip configuration file copying and data directory processing." fi # 创建输出文件路径(加上.json 后缀) output_file="$redis_parent_dir/devkit_redis_conf_data.json" # 获取Redis和Sentinel的配置 redis_conf="${config_file_path:-}" redis_dir="${redis_data_dir:-}" redis_dir_in_package=${redis_dir_in_package:-false} redis_logfile_in_package=${redis_logfile_in_package:-false} redis_dbfile="${redis_data_dir}/${dbfilename:-}" redis_aof="${redis_data_dir}/${appendfilename:-}" redis_logfile="${logfile:-}" redis_pidfile="${pidfile:-}" sentinel_conf="${sentinel_config_file_path:-}" sentinel_dir="${sentinel_data_dir:-}" redis_conf_in_package=${redis_conf_in_package:-false} sentinel_conf_in_package=${sentinel_conf_in_package:-false} is_sentinel_mode=${is_sentinel_mode:-false} sentinel_dir=$(echo "$sentinel_dir" | sed 's/^"\(.*\)"$/\1/') # 构建JSON数据 json_data=$(cat <<EOF { "redis": { "redis_conf": "$redis_conf", "redis_conf_in_package": $redis_conf_in_package, "redis_dir": "$redis_dir", "redis_dir_in_package": $redis_dir_in_package, "redis_dbfile": "$redis_dbfile", "redis_aof": "$redis_aof", "redis_logfile": "$redis_logfile", "redis_logfile_in_package": $redis_logfile_in_package, "redis_pidfile": "$redis_pidfile" }, "sentinel": { "sentinel_conf": "$sentinel_conf", "is_sentinel_mode": $is_sentinel_mode, "sentinel_conf_in_package": $sentinel_conf_in_package, "sentinel_dir": "$sentinel_dir" } } EOF ) # 将JSON数据写入到文件中 echo "$json_data" > "$output_file" # 输出完成信息 echo "The configuration data has been written into $output_file." # 打包安装目录 echo "Packaging the installation directory..." # 切换到 installation_dir 的父级目录 cd "$installation_dir/.." || exit # 生成tar包,打包的内容是installation_dir的子目录(即$(basename "$installation_dir")) tar -zcf "redis.tar.gz" "$(basename "$installation_dir")" # 获取当前工作目录的绝对路径(此时即为installation_dir的父级目录) final_tar_path="$(pwd)/redis.tar.gz" # 打包完成,输出最终tar包的绝对路径 echo "Packaging completed. The installation directory has been packaged to $final_tar_path." |
父主题: Redis源成分打包