場景描述:
某應用系統,相關備份文件存放在目錄:/usr/data_bak/backups
隨着時間的累積,系統每天產生的備份文件不斷增大,當天產生的備份文件大於10G。
導致的問題:
- 系統盤磁盤空間不是很大,單個備份文件過大,存放3天左右,系統盤磁盤空間就會告警,導致新的備份文件不能正常生成。
- 備份文件在默認配置目錄,應該歸檔在統一的目錄
處理方式:
腳本內容,每天早上6點10分將備份文件,移動到指定備份目錄。
腳本名稱:mv_cron.sh
腳本內容:
#! /bin/bash path1="/usr/data_bak/" path2="/home/data_bak/pg_bak/" timelimit1="+6" timelimit2="-17" for i in "backups"; do list_newfiles=`cd $path1$i && find -iname "*.zip" -type f | awk '{print substr($1,3)}'` echo "" echo "target folder: $path2" echo "Trying to move those files in $path1$i" echo "$list_newfiles" echo "" OLD_IFS=$IFS IFS=$'\n' arr_newfiles=($list_newfiles) for s in ${arr_newfiles[@]}; do #echo "$s" isfindthisfile=`find $path2 -iname $s` if [ -z "$isfindthisfile" ]; then echo "$path1$i/$s is not in target folder,try to copy!" mv "$path1$i/$s" "$path2/$s.TMP" mv "$path2/$s.TMP" "$path2/$s" echo "$path1$i/$s has been moved successfully!!!" else echo "$path1$i/$s is allready in target folder,trying to copy next !" fi done done IFS=$OLD_IFS
腳本說明:
path1 是當前系統備份文件所在目錄,
path2 是目標存放備份文件的目錄。
第7行,可以根據自己實際備份文件的后綴名,進行查找,這里是.zip文件。
定時任務配置:
用系統自帶crontab配置,每天早上6點10分執行腳本。crontab -e 里邊添加如下內容:
10 06 * * * /data/mv_cron.sh
