Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2815283
  • 博文数量: 587
  • 博客积分: 6356
  • 博客等级: 准将
  • 技术积分: 6410
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-23 10:54
个人简介

器量大者,福泽必厚

文章分类

全部博文(587)

文章存档

2019年(3)

2018年(1)

2017年(29)

2016年(39)

2015年(66)

2014年(117)

2013年(136)

2012年(58)

2011年(34)

2010年(50)

2009年(38)

2008年(16)

分类: LINUX

2011-05-23 14:13:30

工作中的脚本:
1:关闭未备案的网站
#!/bin/sh
for var in `cat closesite.txt`
do
a=`echo $var|cut -c 1`
b=`echo $var|cut -c 2`
c=`echo $var|cut -c 3`
cd /usr/vhome/${a}/${b}/${c}
echo `ls  /usr/vhome/${a}/${b}/${c}` >/home/*/list.txt
site=`cat /home/***/list.txt | grep "${var}"|grep -v grep`
#echo "${site}"
if [ "${site}" != ""  ]  ; then
cd /usr/vhome/${a}/${b}/${c}/${var}
mv www www_nobeian
mkdir -m0 www
echo "${var} has been closed"
echo "${var} is closed because of not licensed on `date`" >> /home/***/closesite.log 
else
echo "${var} does not exist on the machine">>/home/***/closeerror.log
fi
done


2:机器因为硬件故障,crash了, 从备份中恢复


root# cat jieya.sh
#!/bin/sh
domain=$1
ls /home/***/bak/63.219.16.23-alldomain-1103 | grep $domain > domain.txt
domainfile=`cat domain.txt`
if [ "$domainfile" = "" ]  ;  then
  echo  "$domain is not in the machine"  >>error.txt
else
echo "$domainfile is going to extract"
sleep 5
a=`echo $domain|cut -c 1`
b=`echo $domain|cut -c 2`
c=`echo $domain|cut -c 3`
mkdir -p /usr/vhome/${a}/${b}/${c}/${domain}
#Backup data
tar -zxvf /usr/home/***/bak/63.219.16.23-alldomain-1103/63.219.16.23-${domain}-20110324.tar.gz -C /usr/vhome/${a}/${b}/${c}/${domain}
#tar -zxvf /home/***/bak/61.155.152.166-${domain}-20110421.tar.gz -C /usr/vhome/${a}/${b}/${c}/${domain}/
echo "Chown nobody is in Action"
chown nobody:nobody /usr/vhome/${a}/${b}/${c}/${domain}
echo "$domainfile has been finished"
sleep 3
fi

root# cat extractall.sh
#!/bin/sh
for var in `cat domainlist.txt`
do
 sh jieya.sh $var
done
分别为extractall.sh和jieya.sh两个文件, jieya.sh是一个文件的解压过程,然后extractall.sh中使用一个循环,把所有的都解压了!

3:备份apache和pureftp日志文件
root# cat backuplog.sh
#!/bin/sh
echo "backup log "
Backup_date=`date +%y%m%d`
mv /usr/local/logcenter/apache.log    /usr/local/logcenter/apache.log.${Backup_date}
touch /usr/local/logcenter/apache.log
/usr/local/apache2/bin/apachectl -f /usr/local/confcenter/httpd.conf -k graceful
cd /usr/local/logcenter
tar zcvf apache.log.${Backup_date}.tar.gz   apache.log.${Backup_date}
mv   apache.log.${Backup_date}.tar.gz  /Backup_web_mount_point/log/apachelog/
if [ -e /Backup_web_mount_point/log/apachelog/apache.log.${Backup_date}.tar.gz  ]  ; then
   rm -f /usr/local/logcenter/apache.log.${Backup_date}
   echo "apache.log.${Backup_date} Backup finished"  >>/home/***/logbackup.log               
else
   echo "apache.log.${Backup_date} Backup failed"  >>/home/***/logbackup.log
fi

echo ""  >>/home/***/logbackup.log
echo ""  >>/home/***/logbackup.log
echo ""  >>/home/***/logbackup.log
echo ""  >>/home/***/logbackup.log
cd  /usr/local/xncenter/logcenter
cp pureftpd.log pureftpd.log.${Backup_date}
echo "">/usr/local/logcenter/pureftpd.log
tar zcvf pureftpd.log.${Backup_date}.tar.gz  pureftpd.log.${Backup_date}
mv  pureftpd.log.${Backup_date}.tar.gz   /Backup_web_mount_point/log/ftplog/
if [ -e /Backup_web_mount_point/log/ftplog/pureftpd.log.${Backup_date}.tar.gz  ]  ; then
   rm -f /usr/local/logcenter/pureftpd.log.${Backup_date}
   echo "pureftpd.log.${Backup_date} Backup finished"  >>/home/***/logbackup.log
else
   echo "pureftpd.log.${Backup_date} Backup failed"  >>/home/***/logbackup.log
fi
find /Backup_web_mount_point/log/apachelog  -mtime +60 | xargs rm -rf +60 | xargs rm -rf
find    /Backup_web_mount_point/log/ftplog  -mtime +60 | xargs rm -rf +60 | xargs rm -rf

ce下关闭站点的脚本,软件接删除,目录移动到/web/bak_nolicensed目录下!

#!/bin/sh
cd /web
echo `ls  /web` >/home/***/list.txt
for var in `cat /home/***/closesite.txt`
do
site=`cat /home/***/list.txt | grep "${var}"|grep -v grep`
#echo "${site}"
if [ "${site}" != ""  ]  ; then

if test -L  /web/${var}   ; then
  echo " ${var} is a Link"
  echo "${var} has been deleted" >> /home/***/deletedlink.txt
  rm -f /web/${var}
else

#chmod -R 000  /web/${var}/web
mv /web/${var}  /web/bak_nolicensed/
echo "${var} has been closed"
echo "${var} is closed because of not licensed on `date`" >> /home/***/closesite.log 
fi
else

echo "${var} does not exist on the machine">>/home/***/closeerror.log
fi

done

留为备份!

***代表用户名!

5:关闭未备案的脚本:

#!/bin/sh

date=`date +%Y-%m-%d`

cd /vhost/Apache2Conf/

cp httpd-vhosts.conf  httpd-vhosts_${date}.conf

sleep 10

for i in `cat /home/closedomain/closedomain.txt`

do

sed -i -e "s/ServerName $i/#ServerName $i/g"       /vhost/Apache2Conf/httpd-vhosts.conf

sed -i -e "s/www\.${i}[]/closed{i}/g" /vhost/Apache2Conf/httpd-vhosts.conf

done

6:配置文件中删除域名的相关配置信息:
delete.sh内容如下:
#!/bin/sh
domain=$1
cat  /usr/local/confcenter/vhost.conf | grep -ni  ${domain} | grep begin >begindomain.txt
cat  /usr/local/confcenter/vhost.conf | grep -ni  ${domain} | grep end >enddomain.txt
countbegin=`cat begindomain.txt|wc -l`
domainname=`cat begindomain.txt`
if [ "${countbegin}" == "1" ] ; then  
cat begindomain.txt | awk -F: '{print $1}' >linebegine.txt
cat enddomain.txt | awk -F: '{print $1}' >lineend.txt
beginline=`cat linebegine.txt`
endline=`cat lineend.txt`
sed -i "${beginline},${endline}d"  /usr/local/confcenter/vhost.conf

elif [ "${countbegin}" == "3" ] ; then
 echo "${domain}">>particular.txt
elif [ "${domain}" == "" ] ; then
 echo  "${domain}" >>noexistdomain.txt
else
cat begindomain.txt | awk -F: '{print $1}' >linebegine.txt
cat enddomain.txt | awk -F: '{print $1}' >lineend.txt
sed -n '1p' linebegine.txt>beginline1.txt
sed -n '2p' linebegine.txt>beginline2.txt
begineline1=`cat beginline1.txt`
begineline2=`cat beginline2.txt`
sed -n '1p' lineend.txt>endline1.txt
sed -n '2p' lineend.txt>endline2.txt
endline1=`cat endline1.txt `
endline2=`cat endline2.txt`
echo ${begineline1}
echo ${begineline2}
echo ${endline1}
echo ${endline2}
sed -i "${begineline1},${endline1}d"  /usr/local/confcenter/vhost.conf
#sed -i "${begineline2},${endline2}d"  /usr/local/confcenter/vhost.conf
fi 


alldomaindelete.sh用于删除所有的域名信息,内容如下:
#!/bin/sh
for var in `cat domaindelete.txt`
do
sh delete.sh ${var}
done

7:从配置文件里提取域名,并写成ip  域名的方式
tiquall.sh文件内容:
#!/bin/sh
cat /usr/local/confcenter/vhost.conf | grep ServerName | grep -v grep|uniq >servername.txt
sed -e 's/#//g' servername.txt   >servernameclean.txt
awk '{print $2}' servernameclean.txt >domain.txt 
for var in `cat domain.txt`
do
sh tiqusingle.sh $var
done
tiqusingle.sh文件内容:
#!/bin/sh
domainname=$1
echo ${domainname} >testwwwdomain.txt
if grep -q -i "www\." /home/******/testwwwdomain.txt
then
domainame=$1
realdomainname=$1
else
realdomainname=$1
domainname={domainname}
fi
a=`echo $domainname|cut -c5`
b=`echo $domainname|cut -c6`
c=`echo $domainname|cut -c7`
domain=`echo $domainname|cut -c5-`
echo -e "175.*.*.165  \c" >>result.txt
echo -e "${realdomainname}   \c" >>result.txt
echo -e "/usr/vhome/${a}/${b}/${c}/${domain}" >>result.txt


6:nsupdate修改dns中的A记录
注意:该脚本运行后useross.dccache.com的解析立即会生效!但修改相关目录下的*.zone(大概有70多个)需要点时间,就是说,运行该脚本后:dig @192.168.1.106 useross.dccache.com 会立即生效,但zone文件里的配置生效需要延迟(我测试的时候大概是10分钟左右),搞的我当时还以为该脚本没有运行成功!
例如:


  1. [root@ungeoxliu key]# cat key.conf  | head -n 4
  2. key "ungeo11" {
  3.         algorithm hmac-md5;
  4.         secret "5***WpBW7FELdtB3kNNHzFMusdnfVMQlAedFcjumUcFfA6AMUbPFUO7c0n7uyZ74eDeZp0I7tgPnD1M/uxJFw==";
  5. };
  6. [root@ungeoxliu liuxiaojie]# cat modifydomain.sh
  7. #!/bin/sh
  8. cat /usr/local/bind9/etc/key/key.conf | grep key |awk '{print $2}' > /home/liuxiaojie/keyname.txt
  9. cat /usr/local/bind9/etc/key/key.conf | grep secret |awk '{print $2}' > /home/liuxiaojie/secretname.txt
  10. paste -d " " keyname.txt secretname.txt >all.txt
  11. #sed -i 's/^/ /g' secretname.txt
  12. sed -i 's/;//g' all.txt
  13. sed -i 's/"//g' all.txt
  14. cat /home/liuxiaojie/all.txt | while read line
  15. do
  16. key=` echo $line|awk '{print $1}'`
  17. secret=` echo $line|awk '{print $2}'`
  18. #echo ${key}
  19. #echo ${secret}
  20. /usr/local/bind9/bin/nsupdate -y ${key}:${secret} < /home/liuxiaojie/c.sh
  21. echo "test"
  22. done
  23. [root@ungeoxliu liuxiaojie]# cat all.txt |head -1
  24. ungeo11 5I7KzWpBW7FELdtB3kNNHzFMusdnfVMQlAedFcjumUcFfA6AMUbPFUO7c0n7uyZ74eDeZp0I7tgPnD1M/uxJFw==
  25. [root@ungeoxliu liuxiaojie]#

7:
  1. CREATE TABLE `fee_polling` (
  2. `id` int(11) NOT NULL auto_increment COMMENT '自动增长列,主键',
  3. `domainid` int(11) default NULL COMMENT '域的id号',
  4. `OKBS` int(11) default NULL COMMENT '输出带宽',
  5. `IKBS` int(11) default NULL COMMENT '输入带宽',
  6. `BIB` int(11) default NULL COMMENT '入进流量',
  7. `BOB` int(11) default NULL COMMENT '出输流量',
  8. `OBS` int(11) default NULL COMMENT '出输带宽=bob/60',
  9. `IBS` int(11) default NULL COMMENT '输入宽带= bib/60',
  10. `NREQ` int(11) default NULL COMMENT '秒每请求数',
  11. `time` timestamp NULL default NULL COMMENT '采集时间',
  12. `authtime` timestamp NOT NULL default CURRENT_TIMESTAMP,
  13. PRIMARY KEY (`id`),
  14. KEY `a` (`domainid`,`time`)
  15. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计费模块集采的数据项\r\n带宽,流量,每秒请求数\r\ndomainid对应domain的id\r\n\r\n'
  16. 书写的shell脚本:
  17. [root@ungeoxliu data]# cat my.sh
  18. #!/bin/sh
  19. #0 3 * * * /bin/bash /data/scripts/billing/log2user2.sh > /dev/null 2>&1 &
  20. DIR_BASE="/data/ftp/dcclog/upload/dd/tmp"
  21. mkdir -p ${DIR_BASE}
  22. cd ${DIR_BASE}
  23. mysql -uroot -p123456 -N dcc2 -e "select id,host from dcc_hosts where status=0" > /data/ftp/domain.list
  24. cat /data/ftp/domain.list|awk '{print $2}' > /data/ftp/puredomain.list
  25. echo $(date -d "-1 day" "+%Y%m%d") > /data/ftp/datetime.list
  26. mytime=`cat /data/ftp/datetime.list`
  27. cat /data/ftp/datetime.list | while read datetime
  28. do
  29. find /data/ftp/dcclog/upload/[1-9]*/ -type f -name "squidlog-billing-${datetime}.tgz" | while read file
  30. do
  31. dir_base=$(dirname ${file} | awk -F'/' '{print $NF}')
  32. dir_target="${DIR_BASE}/${dir_base}"
  33. mkdir -p ${dir_target} && tar zxf ${file} -C ${dir_target}
  34. done
  35. done
  36. for var in `cat /data/ftp/puredomain.list`
  37. do
  38. find ${DIR_BASE} -name ${var}-${mytime} > /data/ftp/domainlog.txt
  39. >/data/ftp/domainalllog.txt
  40. for log in `cat /data/ftp/domainlog.txt`
  41. do
  42. echo "This is a test"
  43. cat ${log}>> /data/ftp/domainalllog.txt
  44. done
  45. firstcolumn=`cat /data/ftp/domainalllog.txt |awk '{print $1}'|head -n 1 `
  46. cat /data/ftp/domainalllog.txt | awk '{print $2,$3,$4,$5}' >/data/ftp/fourcolumn.txt
  47. awk '{a[$1]+=$2;b[$1]+=$3;c[$1]+=$4}END{for(i in a)print i,a[i],b[i],c[i]}' /data/ftp/fourcolumn.txt > /data/ftp/fourcolumnmiddle.txt
  48. sed -i -e "s/^/${firstcolumn} /g" /data/ftp/fourcolumnmiddle.txt
  49. cat /data/ftp/fourcolumnmiddle.txt | while read line
  50. do
  51. echo $line
  52. echo $line> /data/ftp/singleline.txt
  53. domainid=`cat /data/ftp/domain.list|grep ${var}| awk '{print $1}' `
  54. #echo ${domainid}
  55. okbs=0
  56. ikbs=0
  57. bib=`cat /data/ftp/singleline.txt |awk '{print $3}' `
  58. bob=`cat /data/ftp/singleline.txt |awk '{print $4}' `
  59. obs=`expr $bob / 60`
  60. ibs=`expr $bib / 60`
  61. nreq=`cat /data/ftp/singleline.txt |awk '{print $5}'`
  62. mytime=`cat /data/ftp/singleline.txt |awk '{print $1" " $2}'`
  63. authtime="0000-00-00 00:00:00"
  64. mysql -u root -p'123456' -D dcc2 -e "insert into fee_polling(domainid,okbs,ikbs,bib,bob,obs,ibs,nreq,time,authtime) values('$domainid','$okbs','ikbs','$bib','$bob','$obs','$ibs','$nreq','$mytime','$authtime') "
  65. done
  66. done
 8:
  1. apache重启!
  2. [root@IDC-D-2626 scripts]# cat apache.sh
  3. #/bin/sh
  4. /usr/local/apache2/bin/apachectl -f /vhost/Apache2Conf/httpd.conf -k graceful
  5. 备份脚本:
  6. [root@IDC-D-2626 scripts]# cat backupconf.sh
  7. #!/bin/sh
  8. echo "backup conf "
  9. Backup_date=`date +%y%m%d`
  10. HOST_IP=`/sbin/ifconfig eth0 | grep ***.***.*** |awk '{print $2}'|awk -F: '{print $2}'`
  11. cd /usr/vhome
  12. tar zcvf confcenter_${HOST_IP}_${Backup_date}.tar.gz Apache2Conf
  13. cp /usr/vhome/confcenter_${HOST_IP}_${Backup_date}.tar.gz /Backup_web_mount_point/conf
  14. if [ -e /Backup_web_mount_point/conf/confcenter_${HOST_IP}_${Backup_date}.tar.gz ] ; then
  15. rm -f /usr/vhome/confcenter_${HOST_IP}_${Backup_date}.tar.gz
  16. echo "confcenter_${HOST_IP}_${Backup_date}.tar.gz Backup finished" >>/home/liuxiaojie/confbackup.log
  17. else
  18. echo "confcenter_${HOST_IP}_${Backup_date}.tar.gz Backup failed" >>/home/liuxiaojie/confbackup.log
  19. fi
  20. ftp -n 10.11.12.182 >error 2>&1 <
  21. user linuxweb linuxweb
  22. bin
  23. prompt
  24. cd /***.***.***.104/conf
  25. lcd /Backup_web_mount_point/conf
  26. put confcenter_${HOST_IP}_${Backup_date}.tar.gz
  27. bye
  28. EOC
  29. find /Backup_web_mount_point/conf/ -mtime +7 | xargs rm -rf
  30. 没间隔一些日志,error_log文件有时候就会变的很大,甚至有时候撑满磁盘!清理该文件
  31. [root@IDC-D-2626 scripts]# cat cleanlog.sh
  32. #!/bin/sh
  33. echo "">/usr/local/apache2/logs/error_log
  34. db备份脚本:将整个var压缩,上传到ftp空间下!
  35. [root@idc-d-2469 dbbackup]# cat mysqldbbackup.sh
  36. #!/bin/sh
  37. Backup_date=`date +%Y%m%d`
  38. cd /home/mysqldb
  39. tar zcvf /usr/dbbackup/db/148var_${Backup_date}.tar.gz ./var
  40. sh /usr/dbbackup/put.sh
  41. find /usr/dbbackup/db -mtime +6 | xargs rm -rf
  42. [root@idc-d-2469 dbbackup]# cat put.sh
  43. #!/bin/sh
  44. Backup_date=`date +%Y%m%d`
  45. ftp -n 10.4.2.246 >/usr/dbbackup/error 2>&1 <
  46. user linuxweb linuxweb
  47. bin
  48. prompt
  49. cd /dbbackup/***.***.***.148/
  50. lcd /usr/dbbackup/db
  51. put 148var_${Backup_date}.tar.gz
  52. bye
  53. EOC
9:ncftp来上传备份文件!
  1. 另外一种情况:
  2. 这种情况:在客户端没有ftp命令, 系统采用了最小的安装方式:
  3. 我先在ftp server上创建一个用户:
  4. useradd ungeosvnback
  5. passwd ungeosvnback
  6. 查看vsftp已经安装:
  7. [root@host009 ~]# rpm -qa | grep vsftp
  8. vsftpd-2.0.5-24.el5
  9. 启动vsftp
  10. /etc/init.d/vsftpd start
  11. lsof -i :21 查看是否已经成功启动!
  12. 防火墙上添加规则使得客户端能够连接:
  13. iptables -A INPUT -s 116.***.***.120 -j ACCEPT ##这里最好用内网地址,但我的机器上只有一个外网,只好用它了!
  14. 在客户端上安装ftp软件:
  15. tar jxvf ncftp-3.2.5-src.tar.bz2
  16. cd ncftp-3.2.5
  17. ./configure --prefix=/usr/local/ncftp
  18. make
  19. make install
  20. 即可安装!
  21. 我的脚本如下:
  22. [root@develop-res home]# cat backupsvn.sh
  23. #!/bin/sh
  24. find /home/bak/svnback*.tar.gz -mtime +1 | xargs rm -rf
  25. Backup_date=`date +%Y%m%d`
  26. cd /home
  27. tar zcvf /home/bak/svnback-${Backup_date}.tar.gz svnsource
  28. /usr/local/ncftp/bin/ncftpput -u ungeosvnback -p ungeosvnbackroot811026 -R 116.***.***.117 /home/ungeosvnback /home/bak/svnback-${Backup_date}.tar.gz
  29. -u:指定连接的用户
  30. -p:指定密码
  31. -R:Recursive mode; copy whole directory trees.
  32. 116.***.***.117 :指定备份服务器的地址
  33. /home/ungeosvnback :指定目的地址
  34. /home/bak/svnback-${Backup_date}.tar.gz :指定要上传的文件

阅读(1247) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~