自動備份5天前的日誌
阿新 • • 發佈:2018-12-14
好久沒更新部落格了,一直在忙瑣事,只將筆記記錄在了本地,今後會陸續更新自己平時做的筆記。
下面是自己寫的一個shell指令碼,功能是備份5天前的日誌,先壓縮,再刪除。
1 #!/bin/bash 2 echo "====start backup logs for 5 days ago" 3 m=`date +%Y-%m-%d --date='5 days ago'`; 4 m2=`date +%Y%m%d --date='5 days ago'`; 5 m3=`date -d "$m" +%s`; 6 if [ ! -d "/pjapp/billsys/logs/backupLog/" ];then 7 mkdir /pjapp/billsys/logs/backupLog 8 fi 9 if [ ! -d "/pjapp/billsys/logs/backupLog/authority-app/" ];then 10 mkdir /pjapp/billsys/logs/backupLog/authority-app 11 fi 12 if [ ! -d "/pjapp/billsys/logs/backupLog/nbms-batch/" ];then 13 mkdir /pjapp/billsys/logs/backupLog/nbms-batch 14 fi 15 if [ ! -d "/pjapp/billsys/logs/backupLog/nbms-busi/" ];then 16 mkdir /pjapp/billsys/logs/backupLog/nbms-busi 17 fi 18 if [ ! -d "/pjapp/billsys/logs/backupLog/nbms-cust/" ];then 19 mkdir /pjapp/billsys/logs/backupLog/nbms-cust 20 fi 21 if [ ! -d "/pjapp/billsys/logs/backupLog/nbms-htes/" ];then 22 mkdir /pjapp/billsys/logs/backupLog/nbms-htes 23 fi 24 if [ ! -d "/pjapp/billsys/logs/backupLog/nbms-mb/" ];then 25 mkdir /pjapp/billsys/logs/backupLog/nbms-mb 26 fi 27 if [ ! -d "/pjapp/billsys/logs/backupLog/nbms-pool/" ];then 28 mkdir /pjapp/billsys/logs/backupLog/nbms-pool 29 fi 30 if [ ! -d "/pjapp/billsys/logs/backupLog/nbms-rgct/" ];then 31 mkdir /pjapp/billsys/logs/backupLog/nbms-rgct 32 fi 33 if [ ! -d "/pjapp/billsys/logs/backupLog/scube-server/" ];then 34 mkdir /pjapp/billsys/logs/backupLog/scube-server 35 fi 36 if [ ! -d "/pjapp/billsys/logs/backupLog/flowsharp/" ];then 37 mkdir /pjapp/billsys/logs/backupLog/flowsharp 38 fi 39 ######################################################################authority-app 40 index=0 41 f=`ls /pjapp/billsys/logs/authority-app -1 -c` 42 for name in $f 43 do 44 n=`expr "$name" : '.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*'` 45 n1=`date -d "$n" +%s`; 46 if [ "$n" != "" ] && [ "$n1" -le "$m3" ] 47 then 48 f[$index]="/pjapp/billsys/logs/authority-app/$name" 49 else 50 f[$index]="" 51 fi 52 (( index ++ )) 53 done 54 echo "====package [authority-app] logs" 55 str=${f[@]} 56 if [ "${#str}" -gt 0 ] 57 then 58 for data in $str 59 do 60 echo `date -d today +"%Y-%m-%d %T"` "packaging...." >> backup.log 2>&1 61 tar -zcvPf $data.tar.gz $data >> backup.log 2>&1 62 done 63 echo "====delete old [authority-app] logs" 64 fi 65 #rm -rf $str 66 ######################################################################nbms-batch 67 indexA=0 68 fA=`ls /pjapp/billsys/logs/nbms-batch -1 -c` 69 for name in $fA 70 do 71 n=`expr "$name" : '.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*'` 72 n1=`date -d "$n" +%s`; 73 if [ "$n" != "" ] && [ "$n1" -le "$m3" ] 74 then 75 fA[$indexA]="/pjapp/billsys/logs/nbms-batch/$name" 76 else 77 fA[$indexA]="" 78 fi 79 (( indexA ++ )) 80 done 81 echo "====package [nbms-batch] logs" 82 strA=${fA[@]} 83 if [ "${#strA}" -gt 0 ] 84 then 85 for data in $strA 86 do 87 echo `date -d today +"%Y-%m-%d %T"` "packaging...." >> backup.log 2>&1 88 tar -zcvPf $data.tar.gz $data >> backup.log 2>&1 89 done 90 echo "====delete old [nbms-batch] logs" 91 #rm -rf $strA 92 fi 93 ######################################################################nbms-busi 94 indexB=0 95 fB=`ls /pjapp/billsys/logs/nbms-busi -1 -c` 96 for name in $fB 97 do 98 n=`expr "$name" : '.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*'` 99 n1=`date -d "$n" +%s`; 100 if [ "$n" != "" ] && [ "$n1" -le "$m3" ] 101 then 102 fB[$indexB]="/pjapp/billsys/logs/nbms-busi/$name" 103 else 104 fB[$indexB]="" 105 fi 106 (( indexB ++ )) 107 done 108 echo "====package [nbms-busi] logs" 109 strB=${fB[@]} 110 if [ "${#strB}" -gt 0 ] 111 then 112 for data in $strB 113 do 114 echo `date -d today +"%Y-%m-%d %T"` "packaging...." >> backup.log 2>&1 115 tar -zcvPf $data.tar.gz $data >> backup.log 2>&1 116 done 117 echo "====delete old [nbms-busi] logs" 118 #rm -rf $strB 119 fi 120 ######################################################################nbms-cust 121 indexC=0 122 fC=`ls /pjapp/billsys/logs/nbms-cust -1 -c` 123 for name in $fC 124 do 125 n=`expr "$name" : '.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*'` 126 n1=`date -d "$n" +%s`; 127 if [ "$n" != "" ] && [ "$n1" -le "$m3" ] 128 then 129 fC[$indexC]="/pjapp/billsys/logs/nbms-cust/$name" 130 else 131 fC[$indexC]="" 132 fi 133 (( indexC ++ )) 134 done 135 echo "====package [nbms-cust] logs" 136 strC=${fC[@]} 137 if [ "${#strC}" -gt 0 ] 138 then 139 for data in $strC 140 do 141 echo `date -d today +"%Y-%m-%d %T"` "packaging...." >> backup.log 2>&1 142 tar -zcvPf $data.tar.gz $data >> backup.log 2>&1 143 done 144 echo "====delete old [nbms-cust] logs" 145 #rm -rf $strC 146 fi 147 ######################################################################nbms-htes 148 indexD=0 149 fD=`ls /pjapp/billsys/logs/nbms-htes -1 -c` 150 for name in $fD 151 do 152 n=`expr "$name" : '.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*'` 153 n1=`date -d "$n" +%s`; 154 if [ "$n" != "" ] && [ "$n1" -le "$m3" ] 155 then 156 fD[$indexD]="/pjapp/billsys/logs/nbms-htes/$name" 157 else 158 fD[$indexD]="" 159 fi 160 (( indexD ++ )) 161 done 162 echo "====package [nbms-htes] logs" 163 strD=${fD[@]} 164 if [ "${#strD}" -gt 0 ] 165 then 166 for data in $strD 167 do 168 echo `date -d today +"%Y-%m-%d %T"` "packaging...." >> backup.log 2>&1 169 tar -zcvPf $data.tar.gz $data >> backup.log 2>&1 170 done 171 echo "====delete old [nbms-htes] logs" 172 #rm -rf $strD 173 fi 174 ######################################################################nbms-mb 175 indexE=0 176 fE=`ls /pjapp/billsys/logs/nbms-mb -1 -c` 177 for name in $fE 178 do 179 n=`expr "$name" : '.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*'` 180 n1=`date -d "$n" +%s`; 181 if [ "$n" != "" ] && [ "$n1" -le "$m3" ] 182 then 183 fE[$indexE]="/pjapp/billsys/logs/nbms-mb/$name" 184 else 185 fE[$indexE]="" 186 fi 187 (( indexE ++ )) 188 done 189 echo "====package [nbms-mb] logs" 190 strE=${fE[@]} 191 if [ "${#strE}" -gt 0 ] 192 then 193 for data in $strE 194 do 195 echo `date -d today +"%Y-%m-%d %T"` "packaging...." >> backup.log 2>&1 196 tar -zcvPf $data.tar.gz $data >> backup.log 2>&1 197 done 198 echo "====delete old [nbms-mb] logs" 199 #rm -rf $strE 200 fi 201 ######################################################################nbms-pool 202 indexF=0 203 fF=`ls /pjapp/billsys/logs/nbms-pool -1 -c` 204 for name in $fF 205 do 206 n=`expr "$name" : '.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*'` 207 n1=`date -d "$n" +%s`; 208 if [ "$n" != "" ] && [ "$n1" -le "$m3" ] 209 then 210 fF[$indexF]="/pjapp/billsys/logs/nbms-pool/$name" 211 else 212 fF[$indexF]="" 213 fi 214 (( indexF ++ )) 215 done 216 echo "====package [nbms-pool] logs" 217 strF=${fF[@]} 218 if [ "${#strF}" -gt 0 ] 219 then 220 for data in $strF 221 do 222 echo `date -d today +"%Y-%m-%d %T"` "packaging...." >> backup.log 2>&1 223 tar -zcvPf $data.tar.gz $data >> backup.log 2>&1 224 done 225 echo "====delete old [nbms-pool] logs" 226 #rm -rf $strF 227 fi 228 ######################################################################nbms-rgct 229 indexG=0 230 fG=`ls /pjapp/billsys/logs/nbms-rgct -1 -c` 231 for name in $fG 232 do 233 n=`expr "$name" : '.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*'` 234 n1=`date -d "$n" +%s`; 235 if [ "$n" != "" ] && [ "$n1" -le "$m3" ] 236 then 237 fG[$indexG]="/pjapp/billsys/logs/nbms-rgct/$name" 238 else 239 fG[$indexG]="" 240 fi 241 (( indexG ++ )) 242 done 243 echo "====package [nbms-rgct] logs" 244 strG=${fG[@]} 245 if [ "${#strG}" -gt 0 ] 246 then 247 for data in $strG 248 do 249 echo `date -d today +"%Y-%m-%d %T"` "packaging...." >> backup.log 2>&1 250 tar -zcvPf $data.tar.gz $data >> backup.log 2>&1 251 done 252 echo "====delete old [nbms-rgct] logs" 253 #rm -rf $strG 254 fi 255 ######################################################################scube-server 256 indexH=0 257 fH=`ls /pjapp/billsys/logs/scube-server -1 -c` 258 for name in $fH 259 do 260 n=`expr "$name" : '.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*'` 261 n1=`date -d "$n" +%s`; 262 if [ "$n" != "" ] && [ "$n1" -le "$m3" ] 263 then 264 fH[$indexH]="/pjapp/billsys/logs/scube-server/$name" 265 else 266 fH[$indexH]="" 267 fi 268 (( indexH ++ )) 269 done 270 echo "====package [scube-server] logs" 271 strH=${fH[@]} 272 if [ "${#strH}" -gt 0 ] 273 then 274 for data in $strH 275 do 276 echo `date -d today +"%Y-%m-%d %T"` "packaging...." >> backup.log 2>&1 277 tar -zcvPf $data.tar.gz $data >> backup.log 2>&1 278 done 279 echo "====delete old [scube-server] logs" 280 #rm -rf $strH 281 fi 282 ######################################################################flowsharp 283 indexJ=0 284 fJ=`ls /pjapp/billsys/logs/flowsharp -1 -c` 285 for name in $fJ 286 do 287 n=`expr "$name" : '.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*'` 288 n1=`date -d "$n" +%s`; 289 if [ "$n" != "" ] && [ "$n1" -le "$m3" ] 290 then 291 fJ[$indexJ]="/pjapp/billsys/logs/flowsharp/$name" 292 else 293 fJ[$indexJ]="" 294 fi 295 (( indexJ ++ )) 296 done 297 echo "====package [scube-server] logs" 298 strJ=${fJ[@]} 299 if [ "${#strJ}" -gt 0 ] 300 then 301 for data in $strJ 302 do 303 echo `date -d today +"%Y-%m-%d %T"` "packaging...." >> backup.log 2>&1 304 tar -zcvPf $data.tar.gz $data >> backup.log 2>&1 305 done 306 echo "====delete old [scube-server] logs" 307 #rm -rf $strJ 308 fi 309 ###################################################################### 310 echo "====move packaged logs" 311 mv /pjapp/billsys/logs/authority-app/*.tar.gz /pjapp/billsys/logs/backupLog/authority-app/ 312 mv /pjapp/billsys/logs/nbms-batch/*.tar.gz /pjapp/billsys/logs/backupLog/nbms-batch/ 313 mv /pjapp/billsys/logs/nbms-busi/*.tar.gz /pjapp/billsys/logs/backupLog/nbms-busi/ 314 mv /pjapp/billsys/logs/nbms-cust/*.tar.gz /pjapp/billsys/logs/backupLog/nbms-cust/ 315 mv /pjapp/billsys/logs/nbms-htes/*.tar.gz /pjapp/billsys/logs/backupLog/nbms-htes/ 316 mv /pjapp/billsys/logs/nbms-mb/*.tar.gz /pjapp/billsys/logs/backupLog/nbms-mb/ 317 mv /pjapp/billsys/logs/nbms-pool/*.tar.gz /pjapp/billsys/logs/backupLog/nbms-pool/ 318 mv /pjapp/billsys/logs/nbms-rgct/*.tar.gz /pjapp/billsys/logs/backupLog/nbms-rgct/ 319 mv /pjapp/billsys/logs/scube-server/*.tar.gz /pjapp/billsys/logs/backupLog/scube-server/ 320 mv /pjapp/billsys/logs/flowsharp/*.tar.gz /pjapp/billsys/logs/backupLog/flowsharp/ 321 echo "====backup logs complete" 322 exit 0