本文共 3231 字,大约阅读时间需要 10 分钟。
自己写的一个用scp备份站点的脚本:错误日志写到 /var/log/test_err/err.log 中。备份的日志写在/var/log/www_back/`date +%Y-%m-%d`.log 中。过程就是:先根据条件判断是否需要备份,然后判断备份的文件是否已经有了。接着进行本地压缩。没有成功有邮件提醒报警(可以用手机邮箱直接短信提醒)。ps:测试过程中,出现问题,报警,手机直接就是五十几条短息。0.o,说多了都是泪啊~之后就是scp备份了。用一个循环。代码:[root@redhat script]# cat my_back.sh #!/bin/bash#backup web stationexec 2>>/var/log/test_err/err.loguser=136XXXXXXXX@wo.cnmyDir="/myback"sourDir="/usr/local/apache2/htdocs"my_tar=".tar.gz"log=/var/log/www_back/`date +%Y-%m-%d`.logcount=`ls -F $sourDir |grep "/$" |wc -l`month=`date +%m`day=`date +%d`year=`date +%Y`hour=`date +%H`min=`date +%M`dirname=$year-$month-$day#创建本地备份目录 if [ ! -d "$myDir" ]; then mkdir "$myDir" ficd $myDir#创建时间目录 if [ ! -d "$dirname" ]; then mkdir $dirname fi#判断是否备份完毕。my_time=`ls -F $dirname |wc -l` if [ "$my_time" -lt "$count" ]; then# --------------------------------------------------------------------- cd $myDir/$dirname for my_web in `ls -l $sourDir| sed -n '/^d/p'| awk '{print $8}'` do#判断文件是否之前已经备份--------------------------------------------------------------- web_tar=${my_web}${my_tar} echo $web_tar if [ ! -f "$web_tar" ]; then echo $my_web echo "###################################################" >>$log echo " $dirname-$hour-$min backup $my_web starting ..." >>$log cd $sourDir tar -zcvf $myDir/$dirname/$my_web.tar.gz ./$my_web#失败发邮件----------------------------------------------------------------------------- if [ $? -eq 0 ];then echo " $dirname-$hour-$min backup $my_web ok !!! " >>$log else echo " $dirname-$hour-$min backup $my_web false !!!" >>$log echo "the $my_web backup is down" | mail -s "backup for jesn" $user; fi cd $myDir/$dirname else echo " $dirname-$hour-$min backup $my_web alexit !!!" >> $log fi done else echo " $dirname-$hour-$min not any file to backup !!!" >> $log fi#####开始scp tran=`ls -l $myDir/$dirname | awk '{print $8}'` for i in $tran do echo "######################START SCP###########################">>$log time scp -r $myDir/$dirname/$i root@192.168.10.7:/backup if [ $? -eq 0 ];then echo " $dirname-$hour-$min SCP { $i } translation ok !!! " >>$log # echo "translation over !!!" | mail -s "backup for jesn" $user; else echo " $dirname-$hour-$min SCP { $i } translation false !!!" >>$log #echo " SCP $i translation down !!!" | mail -s "backup for jesn" $user; fi doneexit 0邮件提醒,很简单的。# vim /etc/mail.rcsetfrom=demo@126.com smtp=smtp.126.comsetsmtp-auth-user=demo smtp-auth-password=*** smtp-auth=login然后重启你的 sendmail服务。/etc/init.d/sendmail restart那么一切就ok了。
转载于:https://blog.51cto.com/jesnridy/1532512