1. 程式人生 > >全網數據備份+inotify實時監測

全網數據備份+inotify實時監測

全網數據備份+inotify實時監測

第1章 備份要求及邏輯圖

1.1 總體邏輯圖
技術分享圖片
1.2 全網備份要求
1.2.1 全網備份基本要求
已知3臺服務器主機名分別為A(web01)、B(backup)、C(nfs01)
要求:每天晚上00點整在Web服務器A上打包備份系統配置文件、網站程序目錄及訪問日誌並通過rsync命令推送備份服務器B上備份保留(備份思路可以是先在本地按日期打包,然後再推到備份服務器B上)
1.2.2 全網備份具體要求
1)Web服務器A和備份服務器B的備份目錄必須都為/backup
2)要備份的系統配置文件包括但不限於:
a.定時任務服務的配置文件(/var/spool/cron/root)。
b.開機自啟動的配置文件(/etc/rc.local)。

c.日常腳本的目錄(/server/scripts)。
d.防火墻iptables的配置文件(/etc/sysconfig/iptables)。
3)Web服務器站點目錄假定為(/var/html/www)。
4)Web服務器A訪問日誌路徑假定為(/app/logs)
5)Web服務器保留打包後的7天的備份數據即可(本地留存不能多於7天,因為太多硬盤會滿)
6)備份服務器B上,保留每周一的所有數據副本,其它要保留6個月的數據副本。
7)備份服務器B上要按照備份數據服務器的IP為目錄保存備份,備份的文件按照時間名字保存
特別提示:本題在工作中是網站生產環境全網備份項目方案的一個小型模擬,很有意義。
1.3 nfs集群後端共享存儲要求

1.3.1 要求
1)在NFS服務端C(nfs01)上共享/data/w_shared及/data/r_shared兩個文件目錄,允許從
NFS客戶端A(web01)、B(backup)上分別掛載共享目錄後可實現從A(web01)、B(backup)上只讀/data/r_shared,可寫/data/w_shared。
2)NFS客戶端A(web01)上的掛載點為/data/b_w(寫),/data/b_r(讀),
NFS客戶端B(backup)上的掛載點為/data/w_你的名字英文(寫),/data/r_你名字英文(讀)。
3)從NFS客戶端B(backup)上的NFS可寫掛載點目錄創建任意文件,從NFS客戶端A(web01)上可以刪除這個創建的文件,反之也可以。
1.4 解決網站集群後端NFS共享存儲單點實現實時數據要求
1.4.1 要求
當用戶通過web服務器將數據寫入到NFS服務器C(nfs01)時,同時復制到備份服務器B(backup)
1.4.2 邏輯圖
技術分享圖片

第2章 部署前的規劃

2.1 服務器規劃

服務器角色 主機名 IP地址
web web01 外:10.0.0.8/24 內:172.16.1.8/24
nfs nfs01 外:10.0.0.31/24 內:172.16.1.31/24
backup backup 外:10.0.0.41/24 內:172.16.1.41/24

2.2 服務器目錄規劃
2.2.1 web01
/var/www/html/、 /app/logs
/server/scripts /etc/sysconfig/iptabes
/var/spool/cron/root /etc/rc.local
2.2.2 backup
/backup data/r_liuzhonghe /data/w_liuzhonghe /server/scripts
2.2.3 nfs01
/backup /data/r_shared /data/w_shared /server/scripts

第3章 部署

3.1 環境準備
3.1.1 系統基本優化腳本(所有服務器執行)


#/bin/bash
###################################################
##The scripts is by liuzhonghe
##QQ:1362427127
###################################################
##增加用戶並提權
useradd oldboy
echo "123456"|passwd --stdin oldboy
\cp /etc/sudoers{,.ori}
echo "oldboy ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
tail -1 /etc/sudoers
visudo -c
##關閉selinux
sed -i "s#SELINUX=enforcing#SELINUX=disabled#gp" /etc/sysconfig/selinux
grep "SELINUX=disabled" /etc/sysconfig/selinux
setenforce 0
getenforce
##關閉iptables
/etc/init.d/iptables stop
chkconfig iptables off
##精簡開機自啟動
chkconfig --list |grep -E "crond|network|sshd|sysstat|rsyslog"|awk ‘{print "chkconfig",$1,"off"}‘|bash chkconfig --list |grep 3:on

##設置中文字符集
cp /etc/sysconfig/i18n{,.ori}
echo ‘LANG="zh_CN.UTF-8"‘ >/etc/sysconfig/i18n
source /etc/sysconfig/i18n
##時間同步
echo ‘#time sync by liuzhonghe at 2018-1-15‘ >>/var/spool/cron/root
echo ‘/5 /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1‘ >>/var/spool/cron/root
crontab -l
##命令行安全
echo ‘export TMOUT=300‘ >>/etc/profile
echo ‘export HISTSIZE=5‘ >>/etc/profile
echo ‘export HISTFILESIZE=5‘ >>/etc/profile
tail -3 /etc/profile
source /etc/profile
##加大文件描述(配置完成後只有重新登錄才有效)
echo ‘
- nofile 65535‘ >>/etc/security/limits.conf
tail -1 /etc/security/limits.conf
##內核優化
cat >>/etc/sysctl.conf<<EOF
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_sysncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxcorn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
#以下參數是對iptables防火墻的優化,防火墻不開會提示,可以忽略不理
net.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_tcp_timeout_established = 180
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
EOF
##更新yum源並添加epel源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
##優化ssh
/bin/cp /etc/ssh/sshd_config /etc/ssh/sshd_config.ori
sed -ir ‘13 iPort 52113\nPermitRootLogin no\nPermitEmptyPasswords no\nUseDNS no\nGSSAPIAuthentication no‘ /etc/ssh/sshd_config
/etc/init.d/sshd reload
##安裝必要的小軟件
yum install lrzsz nmap tree dos2unix nc -y


3.1.2 配置主機名解析/etc/hosts(所有服務器上執行)


cat >> /etc/hosts <<EOF
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.7 web02
172.16.1.8 web01
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.61 m01
EOF


3.2 全網備份
3.2.1 backup服務器搭建rsync服務
創建腳本存放的目錄
mkdir -p /server/scripts
腳本


#!/bin/bash
User=rsync
Authuser=rsync_backup
Path=/backup/
Password=123456
#install rsync
yum install rsync -y
#useradd User
useradd $User -s /sbin/nologin -M
#touch /etc/rsyncd.conf
cat >> /etc/rsyncd.conf << EOF
#rsync_config____start
#created byliuzhonghe
##rsyncd.conf start##
uid = $User
gid = $User
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
#log file = /rsync/rsyncd.log
[backup]
path = $Path
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = $Authuser
secrets file = /etc/rsync.password
#rsync_config_end
EOF
#touch backup dir
/bin/mkdir -p $Path
#xiu gai quan xian
/bin/chown -R $User.$User $Path
#touch mima wenjian
echo "$Authuser:$Password">> /etc/rsync.password
#geng gai mi ma quan xian
chmod 600 /etc/rsync.password

#onboot and jiaru kai ji zi qi dong
/usr/bin/rsync --daemon
echo "/usr/bin/rsync --daemon" >> /etc/rc.local


3.2.2 web01和nfs01配置rsync和本地備份目錄


#!/bin/bash
#####auth is liuzhonghe#######
Passwdfile=/etc/rsync.password
Passwd=123456
Backdir=/backup/
#installl rsync
/usr/bin/yum install rsync -y
#jian li mima wenjian
echo "$Passwd" > $Passwdfile
/bin/chmod 600 $Passwdfile
#jian li bei fen mu lu
mkdir -p $Backdir


註意:在web01和nfs01執行上述腳本文件

3.2.3 在web01上打包推送並配置定時任務
? 打包推送腳本


#!/bin/bash
Ip=$(ifconfig eth1|awk -F "[ :]+" ‘NR==2 {print$4}‘)
Path=/backup/$Ip
if [ $(date +%w) -eq 1 ]
then
Time=week$(date +%F%w )
else
Time=$(date +%F )
fi

mkdir -p $Path
cd / &&\
tar zcfh $Path/back$Time.tar.gz etc/rc.local server/scripts/ var/www/html/ app/logs/ etc/sysconfig/iptables var/spool/cron/root
md5sum $Path/back
$Time.tar.gz >>$Path/flag_$Time.log &&\
rsync -az /backup/ [email protected]::backup/ --password-file=/etc/rsync.password &&\

#del
find /backup/ -type f -mtime +7 ( -name ".tar.gz" -o -name ".log" )|xargs rm -f


? 配置定時任務
cat >> /var/spool/cron/root << EOF
#backup
00 00 * /bin/sh /server/scripts/backup.sh >/dev/null 2>&1
EOF
3.2.4 backup服務器檢查發送郵件並配置定時任務
? 配置郵件服務並備份
cp /etc/mail.rc{,.ori}
cat >> /etc/mail.rc << EOF
set [email protected]
set smtp=pop.163.com
set smtp-auth-user=13613871003
set smtp-auth-password=LZHliucd199459
set smtp-auth=login
EOF
? 檢查並發送郵件


Path=/backup
find $Path -type f -name "flag_$(date +%F).log"|xargs md5sum -c >>/backup/$(date +%F)_result.log
mail -s "ceshi" [email protected] <$Path/$(date +%F)_result.log
find /backup/ -type f ! -name "
_1.tar.gz" -mtime +180|xargs rm -f


? 配置定時任務
cat >> /var/spool/cron/root << EOF
#check
00 04 * /bin/sh /server/scripts/del.sh >/dev/null 2>&1
EOF
至此全網備份配置完成!!!
3.3 網站集群後端NFS共享存儲搭建及優化
3.3.1 搭建NFS共享存儲


#!/bin/bash
##安裝nfs-utils rpcbind
yum install nfs-utils rpcbind -y
##創建共享目錄
/bin/mkdir /data/w_shared /data/r_shared -p
##更改共享目錄權限
chown -R nfsnobody.nfsnobody /data/w_shared /data/r_shared
##編輯配置文件/etc/exports
cat >> /etc/exports << EOF
/data/w_shared 172.16.1.0/24(rw,sync,all_squash,anonuid=65534,anongid=65534)
/data/r_shared 172.16.1.0/24(ro,sync,all_squash,anonuid=65534,anongid=65534)
EOF
##優化
/bin/cat >> /etc/sysctl.conf << EOF
/proc/sys/net/core/rmem_default = 8388608
/proc/sys/net/core/rmem_max = 8388608
/proc/sys/net/core/wmem_default = 16777216
/proc/sys/net/core/wmem_max = 16777216
EOF
##啟動服務
/etc/init.d/rpcbind start &&\
/etc/init.d/nfs start
##加入開機自動
echo "/etc/init.d/rpcbind start" >> /etc/rc.local
echo "/etc/init.d/nfs start" >> /etc/rc.local
##測試
showmount -e localhost


3.3.2 配置NFS客戶端web01


##安裝nfs-utils rpcbind
yum install nfs-utils rpcbind -y
##啟動rpcbind服務並加入開機自啟動
/etc/init.d/rpcbind start
echo "/etc/init.d/rpcbind start" >> /etc/rc.local
##創建掛載目錄
/bin/mkdir /data/b_w /data/b_r -p
##掛載
mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,rsize=131072,wsize=131072 172.16.1.31:/data/w_shared /data/b_w
mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,rsize=131072,wsize=131072 172.16.1.31:/data/r_shared /data/b_r
加入開機自啟動
echo "mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,rsize=131072,wsize=131072 172.16.1.31:/data/w_shared /data/b_w" >> /etc/rc.local
echo "mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,rsize=131072,wsize=131072 172.16.1.31:/data/r_shared /data/b_r" >> /etc/rc.local


3.3.3 配置NFS客戶端backup


##安裝nfs-utils rpcbind
yum install nfs-utils rpcbind -y
##啟動rpcbind服務並加入開機自啟動
/etc/init.d/rpcbind start
echo "/etc/init.d/rpcbind start" >> /etc/rc.local
##創建掛載目錄
/bin/mkdir /data/w_liuzhonghe /data/r_liuzhonghe -p
##掛載
mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,rsize=131072,wsize=131072 172.16.1.31:/data/w_shared /data/w_liuzhonghe
mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,rsize=131072,wsize=131072 172.16.1.31:/data/r_shared /data/r_liuzhonghe
##加入開機自啟動
echo "mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,rsize=131072,wsize=131072 172.16.1.31:/data/w_shared /data/w_liuzhonghe" >> /etc/rc.local
echo "mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,rsize=131072,wsize=131072 172.16.1.31:/data/r_shared /data/r_liuzhonghe" >> /etc/rc.local


3.4 NFS實時同步(inotify+rsync)
3.4.1 nfs01服務器安裝inotify和優化
yum install inotify-tools -y
echo 655350 >/proc/sys/fs/inotify/max_user_watches
echo 655350 >/proc/sys/fs/inotify/max_queued_events

3.4.2編寫inotify監測腳本


cat /server/scripts/inotify.sh
#!/bin/bash
Path=/data/ w_shared
Ip=172.16.1.41
/usr/bin/inotifywait -mrq --format ‘%w%f‘ -e create,close_write,delete $Path \
|while read file
do
if [ -f $file ];then
rsync -az $file --delete rsync_backup@$Ip::backup --password-file=/etc/rsync.password
else
cd $Path &&\
rsync -az ./ --delete rsync_backup@$Ip::backup --password-file=/etc/rsync.password
fi
done


3.4.3 編寫inotify服務腳本


cat /etc/init.d/inotify
#!/bin/bash
#chkconfig: 2345 38 46
####################################
#this scripts is created by xxxx
####################################
. /etc/init.d/functions

if [ $# -ne 1 ];then
usage: $0 {start|stop}
exit 1
fi

case "$1" in
start)
/bin/bash /server/scripts/inotify.sh &
echo $$ >/var/run/inotify.pid
if [ ps -ef|grep inotify|wc -l -gt 2 ];then
action "inotify service is started" /bin/true
else
action "inotify service is started" /bin/false
fi
;;
stop)
kill -9 cat /var/run/inotify.pid >/dev/null 2>&1
pkill inotifywait
sleep 2
if [ ps -ef|grep inotify|grep -v grep|wc -l -eq 0 ];then
action "inotify service is stopped" /bin/true
else
action "inotify service is stopped" /bin/false
fi
;;
*)
usage: $0 {start|stop}
exit 1
esac


3.4.4 啟動服務並加入chkconfig管理
chmod +x /etc/init.d/inotify
chkconfig --add inotify
chkconfig inotify on
chkconfig --list inotify
/etc/init.d/inotify start

全網數據備份+inotify實時監測