标签:xxxx move mkdir directory 修改 lda fun 自动 模块
我目前运维的软件系统需要定时升级检修更新程序包,更新方法是新增、替换、回退名称类似“xxx.xxxx.xxx.xxxx.xx_1.0.0.v199002050011850-150968.jar”的jar包。其中xxx代表模块名,1.0.0为系统版本号,“v”后面19900205为日期,日期后为更新程序的版本号和子版本号。
以往人工更新程序包的具体操作为
1.替换:将工作目录中相同模块名称的旧版本号jar包删除,将新版本号jar包上传到工作目录;
2.新增:将新包直接上传到工作目录;
3.回退:将工作目录中相同模块名称的新版本号jar包删除,将旧版本号jar包上传到工作目录;
4.更新权限和属组。
由于原操作方法是全人工操作,可能出现纰漏。一旦更新的jar包数量较大时费时费力,所以需要用脚本代替人工进行工作。具体需求如下:
1.自动判断筛选出版本更新、版本回退、重复、新增的程序包数量和明细报告并打印到屏幕;
2.将符合条件的程序包更新替换或者回退;
3.备份工作目录中被更新替换或者被回退的原文件;
4.报告和更新回退操作中间需要有流程控制,即需要人工确认、异常中断提示等操作;
5.权限属组需要修改。
1 #!/bin/bash 2 #auther:hmy 3 #version:1.0 4 #backup old files & update new files 5 6 #define work directories 7 #You should change them as your situation. 8 old_dir=/lab/pms_former 9 new_dir=/lab/pms_later 10 rubbish_ud=/lab/rubbish/update 11 rubbish_rb=/lab/rubbish/rollback 12 13 14 #define check function 15 function check_dir(){ 16 file_same_num=0 17 file_update_num=0 18 file_create_num=0 19 file_rollback_num=0 20 21 oldarray=() 22 file_updated=() 23 file_created=() 24 file_rollback=() 25 file_same=() 26 27 for f_old in ${old_dir}/* 28 do 29 f_old_mid=${f_old##/*/} 30 oldarray+=(${f_old_mid%_*}) 31 done 32 33 for new in ${new_dir}/* 34 do 35 new_mid=${new##/*/} 36 new_name=${new_mid%_*} 37 new_version=${new_mid##*v} 38 39 if [[ ! "${oldarray[*]}" =~ "${new_name}" ]]; 40 then 41 file_create_num=$((file_create_num+1)) 42 file_created+=(${new_mid}) 43 dir_file_created_new+=(${new}) 44 fi 45 46 for old in ${old_dir}/* 47 do 48 old_mid=${old##/*/} 49 old_name=${old_mid%_*} 50 old_version=${old_mid##*v} 51 52 if [ "${new_name}" = "${old_name}" -a "${new_version}" = "${old_version}" ]; 53 then 54 file_same_num=$((file_same_num+1)) 55 file_same+=(${new_mid}) 56 57 elif [ "${new_name}" = "${old_name}" -a "${new_version%-*}" \> "${old_version%-*}" ]; 58 then 59 file_update_num=$((file_update_num+1)) 60 file_updated+=(${new_mid}) 61 dir_file_updated_new+=(${new}) 62 dir_file_updated_old+=(${old}) 63 64 elif [ "${new_name}" = "${old_name}" -a "${new_version%-*}" \< "${old_version%-*}" ]; 65 then 66 file_rollback_num=$((file_rollback_num+1)) 67 file_rollback+=(${new_mid}) 68 dir_file_rollback_new+=(${new}) 69 dir_file_rollback_old+=(${old}) 70 fi 71 done 72 done 73 } 74 75 #define action function 76 function action(){ 77 mkdir -p ${rubbish_ud} 78 mkdir -p ${rubbish_rb} 79 80 echo "Moving created files:" 81 for i in ${dir_file_created_new[*]} 82 do 83 echo $i 84 cp $i ${old_dir} 85 done 86 echo -e "Done !!\n" 87 sleep 1 88 89 echo "Moving updated files:" 90 for i in ${dir_file_updated_new[*]} 91 do 92 echo $i 93 cp $i ${old_dir} 94 done 95 echo -e "Done !!\n" 96 sleep 1 97 98 echo "Backup old updated files:" 99 for i in ${dir_file_updated_old[*]} 100 do 101 echo $i 102 mv $i ${rubbish_ud} 103 done 104 echo -e "Done !!\n" 105 sleep 1 106 107 echo "Moving rollback files:" 108 for i in ${dir_file_rollback_new[*]} 109 do 110 echo $i 111 cp $i ${old_dir} 112 done 113 echo -e "Done !!\n" 114 sleep 1 115 116 echo "Backup old rollback files:" 117 for i in ${dir_file_rollback_old[*]} 118 do 119 echo $i 120 mv $i ${rubbish_rb} 121 done 122 echo "Done !!" 123 124 chown -R weblogic:weblogic ${old_dir}/* 125 chmod 755 ${old_dir}/* 126 } 127 128 129 #core procedure 130 echo "Confirm the former directory :" 131 sleep 1 132 echo -e "${old_dir}\n" 133 sleep 1 134 echo "confirm the later directory :" 135 sleep 1 136 echo -e "${new_dir}\n" 137 read -p "[Y/N]" dir 138 139 if [ "${dir}" == "y" -o "${dir}" == "Y" ]; 140 then 141 sleep 1 142 check_dir 143 echo "===================================================================" 144 echo "same file: ${file_same_num}" 145 echo "Details : " 146 for i in ${file_same[*]} 147 do echo -e "${i}" 148 done 149 echo "-------------------------------------------------------------------" 150 sleep 1 151 echo "update file: ${file_update_num}" 152 echo "Details : " 153 for i in ${file_updated[*]} 154 do echo -e "${i}" 155 done 156 echo "-------------------------------------------------------------------" 157 sleep 1 158 echo "roll back file: ${file_rollback_num}" 159 echo "Details : " 160 for i in ${file_rollback[*]} 161 do echo -e "${i}" 162 done 163 echo "-------------------------------------------------------------------" 164 sleep 1 165 echo "create file: ${file_create_num}" 166 echo "These are : " 167 for i in ${file_created[*]} 168 do echo -e "${i}" 169 done 170 echo "===================================================================" 171 read -p "Press Y to continue,N to quit:[y/n]: " input_check 172 if [ "${input_check}" == "y" -o "${input_check}" == "y" ]; 173 then 174 sleep 1 175 action 176 elif [ "${input_check}" == "n" -o "${input_check}" == "N" ]; 177 then exit 178 else echo "Please check your input." 179 fi 180 elif [ "${dir}" == "n" -o "${dir}" == "N" ]; 181 then echo "Modify directory in this scripy." 182 else 183 echo "Please check your input." 184 fi
根据测试脚本可以基本完成需求的功能,但是大致有几个点仍可以改善:
1.遍历了两遍old_dir目录,算法逻辑可以优化
2.代码耦合度高
3.操作函数 action 中如果 move 或 copy 的操作失败,也会提示“Done !!”。可以根据mv、cp操作的返回码来进行结束提示
4.可以增加日志记录的功能
代码重构这里就不进行了。
一个在linux上更新、备份文件的bash script案例
标签:xxxx move mkdir directory 修改 lda fun 自动 模块
原文地址:https://www.cnblogs.com/hypertechnology/p/14241536.html