压缩备份日志 java_自动压缩备份日志文件到指定备份目录脚本
#!/bin/bash
##############################################################
#***#***功能:
#***備份日志目錄的N天前的日志文件成壓縮文件,并移動到指定的統一備份目錄
#***#***參數:
#*** $1:N天之前 ,默認:30#*** $2:要備份的日志文件查詢格式,默認:*.log
#*** $3:要備份的日志所在絕對路徑,默認:/var/log
#*** $4:備份文件夾名稱,默認:backup-log_日期,示例:backup-log_2020-09-04-03-11-42#*** $5:壓縮后的本分文件夾最終存儲絕對路徑,默認:/data/backup-log
#***##############################################################
#1.進入目標目錄
# cd
#2.找出指定路路下的N天前的指定后綴的的所有普通文件
#find ./ -type f -mtime +30 -name "*.log"#3.需要備份的文件存在,且當前目錄下不存在則創建備份文件夾
# dirName= date "+%Y-%m-%d-%H-%M-%S"#mkdirdirName
#4.移動目標文件到備份目錄
#mv#5.備份成功則壓縮備份目錄,并刪除本分目錄
#
#6.移動備份壓縮文件到指定的全局備份文件夾
# 幫助functionhelp(){awk '/^\#\*{3}/ { print $2 }' $0}if [[ "$1" == "-h" ]];thenhelp
exitfi######################## 可修改參數 ###########################
# 備份N天前修改的文件
nDays=$1# 指定的文件后綴
suffix=$2# 操作目標日志目錄
targetLogPath=$3# 備份文件夾名稱
tarTargetDirName=$4# 統一壓縮日志文件備份目錄
tarGzLogFilesPath=$5# 參數檢測
##*************************** $1 *******************************#if [[ -z $nDays || $nDays -lt 1 ]];thennDays=30
fi
echo -e "備份N天之前的日志文件:$nDays 天之前"##*************************** $2 *******************************#if [ -z $suffix ];thensuffix="*.log"
fi
echo -e "備份的日志文件后綴為:$suffix"##*************************** $3 *******************************#if [ -z $targetLogPath ];thentargetLogPath="/var/log"
fi
echo -e "要備份的日志目錄為:$targetLogPath"##*************************** $4 *******************************#if [ -z $tarTargetDirName ];thentarTargetDirName="backup-log_"
fitarTargetDirName=$tarTargetDirName`date "+%Y-%m-%d-%H-%M-%S"`echo -e "備份日志存儲目錄為:$tarTargetDirName"##*************************** $5 *******************************#if [ -z $tarGzLogFilesPath ];thentarGzLogFilesPath="/data/backup-log"
fi
echo -e "壓縮后的備份日志存儲目錄為:$tarGzLogFilesPath"
echo -e "------------------------------------------------------"###############################################################
#1進入目標目錄if [ ! -d $targetLogPath ];then
echo $targetLogPath"目錄不存在"exitficd $targetLogPath
#2找出指定路路下的N天前的指定后綴的的所有普通文件
findTargetFiles=`find $targetLogPath -type f -mtime +$nDays -name "$suffix"`echo -e "需要備份的文件:"
echo$findTargetFiles
#3.1檢測文件是否存在
len=${#findTargetFiles}if [ $len -eq 0 ];then
echo "不存在需要備份的日志文件"exitfi#3.2創建備份文件存儲文件夾
# 創建備份文件夾functioncreateDirIfNotExist(){if [ ! -d $1 ];then
mkdir $1
else
echo $1"已存在"
fi}
tarTargetDirPath=$tarTargetDirName
createDirIfNotExist $tarTargetDirPath
#4移動目標文件到備份目錄mv -b $findTargetFiles $tarTargetDirPathif [ $? -ne 0 ];then
echo "移動日志文件到當前備份目錄失敗"exitfi#5.1備份成功則壓縮備份目錄
backGzFile=$tarTargetDirPath".tar.gz"
tar -zcvf $backGzFile $tarTargetDirPath
#5.2壓縮成功則刪除當前備份目錄if [ $? -ne 0 ];then
echo "備份失敗"exitelse
rm -rf $tarTargetDirPathecho "備份成功"
fi#6移動備份壓縮文件到指定的全局備份文件夾
createDirIfNotExist $tarGzLogFilesPathmv$backGzFile $tarGzLogFilesPathif [ $? -ne 0 ];then
echo "壓縮日志文件備份失敗"
else
echo "壓縮日志文件備份成功"
fiexit
總結
以上是生活随笔為你收集整理的压缩备份日志 java_自动压缩备份日志文件到指定备份目录脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中的%%%_JSP页面中%!%与
- 下一篇: mysql 8.0 慢查询_MySQL慢