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