shell脚本批量压缩log文件并备份

需求:

每天有调度任务执行,会产生大量log等文件。导致服务器空间报警不足。又不能直接rm掉。所以压缩后弄到备份机器上。

log目录多如:

根据文件名匹配出6个月前的文件。进行压缩备份。并rm掉以省出空间。

 压缩后:

 

 

脚本描述:

work_path:自动获取当前文件所在绝对路径;

set timeout:expect的超时时间。单位S,设置-1时为永不超时。

 

#clear six months ago files to tar and scp...

echo "Please wait..."  
work_path=$(dirname $(readlink -f $0))
#get now_path
fileEnd=${work_path##*/};
m=`date -d "6 months ago" +%Y%m`  
#now=`date -d "0 months ago" +%Y%m`  
index=0  
f=`ls $work_path -1 -c`  
for name in $f  
do  
    n=`expr "$name" : '.*\([0-9]\{8\}\).*'`  
    if [ "$n" != "" ] && [ "$n" -le "$m"31 ]   
    then  
        f[$index]="$work_path/$name"  
    else  
        f[$index]=""  
    fi  
    (( index ++ ))  
done  
  
str=${f[@]}  
if [ "${#str}" -gt 0 ]  
then  

    tar -zcvf $work_path/logbak_$m.tar.gz $str && rm -rf $str
    echo "tar.gz finished!"
else  
    echo "No files found."  
    exit 0  
fi  
  
echo "tar.gz maked, now delete old files."  
#rm -fr $str  

echo "rm finished,start scp to ptfLogsBackup"
backupMachine=你的远程备份ip;
backupMachinePath=备份ip的目的path;

set timeout 300
##if bak_file not exists,creating...
/usr/bin/expect<<EOF
spawn ssh 远程ip的username@$backupMachine "\[ -d $backupMachinePath/$fileEnd \] && echo ok, file exists.. || mkdir -p $backupMachinePath/$fileEnd"
expect "password:"
send "远程ip的password\r"
expect EOF
EOF
#scp sending...
/usr/bin/expect<<EOF
spawn bash -c "scp -r $work_path/*.tar.gz 远程ip的username@$backupMachine:$backupMachinePath/$fileEnd && rm -rf $work_path/*.tar.gz"
expect "password:"
send "远程ip的password\r"
expect EOF
EOF

echo "scp tar.gz files finished!"

echo "done."  
exit 0  

 

参考:

http://cache.baiducontent.com/c?m=9d78d513d9821bf806b3837e7c01a76c0e208b744ca0c76309c39238841551563161f4ca23356677c4c40c7077ac5e2ce1e74702207727a09ab89f3baaace32e38f85623046b9206528d16f58d0067d621e347f4ea5ca2adf04592aad085820544ca245427dfedda075c529d33b60932e4bb9b4c17540db9ee2d66ff5d752b9a264aa1408de6376a0286f7d85f4bd42aa7204bd1f06b&p=b43ec45b86cc43fa0be2963e45&newp=882a9706c0875ab70be2960c4553d8224216ed603cd5c44324b9d71fd325001c1b69e7bf24201001d8c4786d03a54f5feef23078341766dada9fca458ae7c46b39cc3c2c&user=baidu&fm=sc&query=shell+%B8%F9%BE%DD%C8%D5%C6%DA%D1%B9%CB%F5%CE%C4%BC%FE&qid=84fa9ad00000d3a8&p1=4

了解shell中、、有的地方写的有点烂。每天学习一点

 

转载于:https://www.cnblogs.com/haohao111/p/9790846.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值