自動化部署安裝nfs+rsync+sersync+nfs客戶端+SMTP
每臺計算機裏面都有一個zsf用戶,密碼為zsf
web服務器使用nfs服務器的/data目錄來做後端存儲
rsync對nfs服務器的目錄做實時備份,使用sersync+rsync,每天定時備份配置文件,本地保存7天,rsync服務器上保存180天。
使用管理服務器管理所有的設備。分發hosts文件,管理服務器上搭建yum倉庫,管理服務器能連接外網,然後集群內所有服務器都在局域網內。
對系統做簡單的優化,
所有需要備份的文件都打包到/backup目錄下,腳本都存放到/scripts目錄下。
需要分發的配置文件都放到/distribution目錄下
服務器的優化:
#關閉SELinx and iptables
/usr/sbin/setenforce 0
/bin/sedsed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config
/etc/init.d/iptables stop
#優化開機啟動項
/sbin/chkconfig --list | grep 3:on |grep -vE "sshd|cront|rsyslog|network" |awk '{print "chkconfig --level 3 "$1" off"}'|bash
1,完成本地yum源的搭建:
mkdir /media/cdrom ;mount /dev/sr0 /media/cdrom
rm -rf /etc/yum.repos.d/*
cat >>/etc/yum.repos.d/centos.repo<<EOF
[centos_yum]
name=centos
baseurl=file:///media/cdrom
enabled=1
gpgcheck=0
EOF
yum clean all
yum list
yum -y install vsftpd.x86_64
[root@localhost ~]# /etc/init.d/vsftpd start
Starting vsftpd for vsftpd: [ OK ]
[root@localhost ~]# chkconfig vsftpd on
創建本地rpm包存放的位置
mkdir /var/ftp/yum_packet
cd /var/ftp/yum_packet/
cp /media/cdrom/Packages/* ./
yum -y install createrepo.noarch
createrepo ./
######分發/etc/hosts /ect/yum.repo.d/centos.repo######
#個性化設置vim
cat >>/root/.vimrc <<EOF
set ruler
set nohlsearch
set shiftwidth=2
set tabstop=4
set expandtab
set cindent
set autoindent
syntax on
EOF
#修改hosts文件
cat >>/etc/hosts<<EOF
172.16.1.5 lb01
172.16.1.5 lb02
172.16.1.7 web02
172.16.1.8 web01
172.16.1.51 db01
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.61 m01
EOF
mkdir /distribution
cp /root/.vimrc /distribution/vimrc
cp /etc/hosts /distribution/hosts
#完成ssh的無密碼連接
yum -y install expect(未完成)
假設現在已經完成秘鑰的分發了。
#寫一個分發文件文件的腳本:
vim /scripts/file_fen.sh
#!/bin/bash
#這是一個批量分發文件的腳本
. /etc/init.d/functions
function Pan {
if [ $? -eq 0 ];then
action "172.16.1.$IP分發" /bin/true
else
action "172.16.1.$IP分發" /bin/false
fi
}
if [ $# -ne 1 ];then
echo -e "Usage:/bin/sh /scripts/file_fen.sh host\n"
else
IP=`echo $1`
ssh [email protected].$IP "[ -d /distribution ] || mkdir -p /distribution " >/dev/null 2>&1
ssh [email protected].$IP "rm -rf /distribution/*"
/usr/bin/scp /distribution/* [email protected].$IP:/distribution/ >>/dev/null 2>&1
Pan
ssh [email protected].$IP "cp /distribution/hosts /etc/hosts"
ssh [email protected].$IP "rm -rf /etc/yum.repos.d/* && cp /distribution/centos.repo /etc/yum.repos.d/"
ssh [email protected].$IP "[ -d /tools ] || mkdir -p /tools ;cp /distribution/sersync2.5.4_64bit_binary_stable_final.tar
.gz /tools/"
ssh [email protected].$IP "cp /distribution/vimrc /root/.vimrc"
fi
#備份的腳本
vim /distribution/backup.sh
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
. /etc/init.d/functions
Data=`date +"%F" -d "-1 days"`
Ip=`ifconfig eth2|awk -F "[ :]+" 'NR==2 {print $4}'`
yum -y install rsync.x86_64 >/dev/null 2>&1
echo "password" >/etc/rsync.password
chmod 600 /etc/rsync.password
[ ! -d /backup/$Ip ] && mkdir -p /backup/$Ip
mkdir -p /backup/$Ip/$Data
tar -czPf /backup/$Ip/$Data/config.tar.gz /etc /distribution /scripts
rsync -az /backup/ [email protected]::backup/ --password-file=/etc/rsync.password
#搭建rsync服務:
vim /distribution/rsync.sh
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
. /etc/init.d/functions
#HN=host name
HN=backup
function Pan {
if [ $? -eq 0 ];then
action "$Str" /bin/true
else
action "$Str" /bin/false
fi
}
hostname $HN && \
sed -i "s#HOSTNAME=localhost.localdomain#HOSTNAME=$HN#g" /etc/sysconfig/network
Str="更改主機名為$HN" && Pan
#安裝配置rsync服務,並啟動daemon
yum -y install rsync.x86_64 >/dev/null 2>&1
rpc -qa rsync >/dev/null 2>&1
Str="安裝rsync服務" && Pan
cat >/etc/rsyncd.conf<<EOF
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
read only = false
list = false
hosts allow = 172.16.1.0/24
ignore erros
[backup]
comment="這是一個備份配置文件的目錄"
path = /backup
auth users=rsync_backup
secrets file = /etc/rsync.password
[nfsdata]
comment="這是一個網站存儲目錄實時備份的目錄"
path = /nfsdata
auth users=nfs_backup
secrets file = /etc/rsync_nfs.password
EOF
id rsync >/dev/null 2>&1
[ $? -ne 0 ] && /usr/sbin/useradd rsync -s /sbin/nologin -M
[ ! -d /backup ] && /bin/mkdir -p /backup
/bin/chown -R rsync:rsync /backup
/bin/echo "rsync_backup:password" >/etc/rsync.password
/bin/chmod 600 /etc/rsync.password
[ ! -d /nfsdata ] && /bin/mkdir -p /nfsdata
/bin/chown -R rsync:rsync /nfsdata
/bin/echo "nfs_backup:nfspassword" >/etc/rsync_nfs.password
/bin/chmod 600 /etc/rsync_nfs.password
/usr/bin/rsync --daemon >
echo "/usr/bin/rsync --daemon" >>/etc/rc.local
#配置SMTP第三方郵件服務
/etc/init.d/postfix stop
cat >>/etc/mail.rc<<EOF
set smtp=smtps://smtp.qq.com:465
set smtp-auth-password=dcnhslrkexdbbgbh
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
EOF
#nfs服務搭建的配置文件:
vim /distribution/nfs.sh
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
. /etc/init.d/functions
#HN=host name
HN=nfs01
function Pan {
if [ $? -eq 0 ];then
action "$Str" /bin/true
else
action "$Str" /bin/false
fi
}
hostname $HN && \
sed -i "s#HOSTNAME=localhost.localdomain#HOSTNAME=$HN#g" /etc/sysconfig/network
Str="更改主機名為$HN" && Pan
yum -y install nfs-utils.x86_64 rpcbind.x86_64 >/dev/null 2>&1
rpm -qa nfs-utils rpcbind >/dev/null 2>&1
Srt="安裝NFS服務及組件" && Pan
/etc/init.d/rpcbind start >/dev/null 2>&1 && \
/etc/init.d/nfs start >/dev/null 2>&1
Str="啟動NFS服務和RPCbind服務" && Pan
chkconfig --level 3 rpcbind on && chkconfig --level 3 nfs on >/dev/null 2>&1
Str="把nfs和rpcbind設置成3級別開機自啟動" && Pan
echo "/data 172.16.1.0/24(rw,sync,root_squash,no_all_squash,anonuid=888,anongid=888)" > /etc/exports
id webuser >/dev/null 2>&1
[ $? -ne 0 ] && /usr/sbin/useradd -u 888 webuser -s /sbin/nologin -M
[ ! -d /data ] && mkdir -p /data
/bin/chown -R webuser:webuser /data
exportfs -arv >/dev/null 2>&1
yum -y install rsync.x86_64 >/dev/null 2>&1
rpc -qa rsync >/dev/null 2>&1
Str="安裝rsync服務" && Pan
cd /tools
tar -xzf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86/sersync2 /sbin/sersync
[ -d /etc/sersync/ ] || mkdir /etc/sersync/
mv GNU-Linux-x86/confxml.xml /etc/sersync/confxml.xml
cd /etc/sersync/ && cp confxml.xml{,.bak}
sed -i 's#<localpath watch="/opt/tongbu">#<localpath watch="/data">#g' /etc/sersync/confxml.xml
sed -i 's#<remote ip="127.0.0.1" name="tongbu1"/>#<remote ip="172.16.1.41" name="nfsdata"/>#g' /etc/sersync/confxml.xml
sed -i 's#<commonParams params="-artuz"/>#<commonParams params="-az"/>#g' /etc/sersync/confxml.xml
sed -i 's#<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>#<auth start="true" users="nfs_backup" passwordfil
e="/etc/rsync_nfs.password"/>#g' /etc/sersync/confxml.xml
echo "nfspassword" >/etc/rsync_nfs.password
chmod 600 /etc/rsync_nfs.password
/sbin/sersync -dro /etc/sersync/confxml.xml >/dev/null 2>&1
echo "/sbin/sersync -dro /etc/confxml.xml" >>/etc/rc.local
###web(nfs客戶端)####
vim /distribution/web02.sh
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
. /etc/init.d/functions
#HN=host name
HN=web02
function Pan {
if [ $? -eq 0 ];then
action "$Str" /bin/true
else
action "$Str" /bin/false
fi
}
hostname $HN && \
sed -i "s#HOSTNAME=localhost.localdomain#HOSTNAME=$HN#g" /etc/sysconfig/network
id webuser 2&>1 /dev/null
[ $? -ne 0 ] && /usr/sbin/useradd -u 888 webuser -s /sbin/nologin -M
yum -y install nfs-utils.x86_64 >/dev/null 2>&1
[ ! -d /mnt/data ] && mkdir -p /mnt/data
mount 172.16.1.31:/data -t nfs /mnt/data
Str="掛載nfs服務上的/data" && Pan
echo "mount 172.16.1.31:/data -t nfs /mnt/data" >>/etc/rc.local
vim /scripts/auth_deploy.sh
#!/bin/bash
. /etc/init.d/functions
for i in 41 31 7
do
/bin/sh /scripts/file_fen.sh $i
ssh [email protected].$i "echo "00 * * * * * /bin/sh /distribution/backup.sh ">>/etc/crontab"
ssh [email protected].$i "/bin/sh /distribution/optimization.sh"
if [ $i -eq 41 ];then
ssh [email protected].$i "/bin/sh /distribution/rsync.sh"
echo '##############腳本執行成功,rsync and sersync服務搭建完成#############'
fi
if [ $i -eq 31 ];then
ssh [email protected].$i "/bin/sh /distribution/nfs.sh" && \
echo '##############腳本執行成功,nfs服務搭建完成#############'
fi
if [ $i -eq 7 ];then
ssh [email protected].$i "/bin/sh /distribution/web02.sh" && \
echo '##############腳本執行成功,web02服務搭建完成#############'
fi
done
##############################################
在管理服務器上執行sh /scripts/auto_deploy.sh,等待執行完成後分別在對應的服務器上檢查
在nfs服務器上:
在web服務器上
在rsync服務器上。
自動化部署安裝nfs+rsync+sersync+nfs客戶端+SMTP