日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

压缩备份日志 java_自动压缩备份日志文件到指定备份目录脚本

發布時間:2024/9/30 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 压缩备份日志 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_自动压缩备份日志文件到指定备份目录脚本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。