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.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.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/ rsync_backup@172.16.1.41::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 from=13613871003@163.com
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" 13613871003@163.com <$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
原文地址:http://blog.51cto.com/lzhnb/2090615