A simple batch backup log script

Original link: https://blog.kelu.org/tech/2022/09/25/backup-logs-with-shell-script.html

Continue above. After the password-free login above, this article simply makes a regular log backup and integrates the logs on other machines locally. The logic is relatively simple and can be modified as needed. The final effect is to organize the files of several machines into a compressed package and save it locally every day, and delete the backup one year ago. I won’t go into details here, just look at the code directly.

 #!/bin/bash #应用名APPNAME = "ESMS" #远程主机列表TARGET_IPS = "app1:auth|ticket,app2:auth|base,app3:ticket|manager,app4:manager|base" #远程主机文件目录TARGET_FOLDER = "/var/local/logs" #远程主机用户及密码TARGET_USER_NAME = "kelu" #备份几天前的日志文件DAYS_AGO = 1 #本地备份目录LOCAL_FOLDER = "/backup/logs" #循环备份文件backupdate = ` date -d " ${ DAYS_AGO } day ago" +%Y-%m-%d ` deletedate = ` date -d "1 year ago" +%Y-%m-%d ` backup_file (){ localFolder = $1 remoteIp = $2 remoteBackupFolder = $3 errorIgnore = $4 if [ ! -d ${ localFolder } ] ; then mkdir -p ${ localFolder } fi if [ $errorIgnore ] ; then scp -r ${ remoteIp } : ${ remoteBackupFolder } ${ localFolder } > /dev/null 2>&1 return 0 else scp -r ${ remoteIp } : ${ remoteBackupFolder } ${ localFolder } 1>/dev/null fi } run_backup (){ echo ">>>>>>>>>>>>>> $backupdate 开始备份..." errorFlag = 0 IFS = "," arrayIP =( $TARGET_IPS ) for ipInfo in ${ arrayIP [@] } do echo ">>>>>>>>>>>>>> ${ ipInfo } " IFS = ":" arrayIPInfo =( $ipInfo ) remoteIp = ${ arrayIPInfo [0] } subInfo = ${ arrayIPInfo [1] } # echo -e "主机IP:\t\t${remoteIp}" # echo -e "主机备份目录:\t${subInfo}" IFS = "|" arrayProgram =( $subInfo ) for program in ${ arrayProgram [@] } do localFolder = ${ LOCAL_FOLDER } / ${ APPNAME } - ${ backupdate } / ${ remoteIp } / ${ program } remoteFolder = $TARGET_FOLDER / ${ program } / ${ backupdate } / * localBatchFolder = ${ LOCAL_FOLDER } / ${ APPNAME } - ${ backupdate } / ${ remoteIp } /batch/ ${ program } remoteBatchFolder = $TARGET_FOLDER /batch/ ${ program } / ${ backupdate } / * backup_file $localFolder $remoteIp $remoteFolder if [ $? -ne 0 ] ; then errorFlag = 1 fi backup_file $localBatchFolder $remoteIp $remoteBatchFolder "ignore" if [ $? -ne 0 ] ; then errorFlag = 1 fi done done if [ $errorFlag -ne 0 ] ; then echo ">>>>>>>>>>>>>> $backupdate 备份结束,部分异常,请确认!" else echo ">>>>>>>>>>>>>> $backupdate 备份完成。" fi cd ${ LOCAL_FOLDER } tar czvf ${ APPNAME } - ${ backupdate } .tgz ${ APPNAME } - ${ backupdate } 1>/dev/null rm -r ${ APPNAME } - ${ backupdate } rm -rf ${ APPNAME } - ${ deletedate } .tgz } run_backup

This article is reprinted from: https://blog.kelu.org/tech/2022/09/25/backup-logs-with-shell-script.html
This site is for inclusion only, and the copyright belongs to the original author.