## 2(50分)在题目1基础上完成负载均衡及高可用的搭建并测试。搭建全网备份服务器备份所有的配置文件和数据库的数据及存储的数据。给出操作步骤
### 负载均衡配置 lb01(10.0.0.5),lb02(10.0.0.6)上配置
#### 安装nginx与web服务器上安装方式相同
#### 配置nginx,用它做负载均衡
~~~
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream server_pools {
server 10.0.0.7:80 weight=1;
server 10.0.0.8:80 weight=1;
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 80;
server_name www.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
~~~
### keepalived 高可用 lb01(10.0.0.5),lb02(10.0.0.6)上配置
#### 安装并启动 keepalived
~~~
yum install keepalived -y
/etc/init.d/keepalived start
mkdir /server/scripts/ -p
cat >/server/scripts/check_lb.sh<<EOF
#!/bin/bash
if [ `ps -ef |grep nginx|grep -v grep` -eq 0 ];then
/etc/init.d/keepalived stop
fi
EOF
chmod +x /server/scripts/check_lb.sh
~~~
#### lb01配置文件
~~~
cat > /etc/keepalived/keepalived.conf<<EOF
global_defs {
router_id LB01
}
vrrp_script chekc_lb{
script "/server/scripts/chekc_lb.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
track_script{
check_lb
}
}
EOF
~~~
#### lb02配置文件
~~~
cat > /etc/keepalived/keepalived.conf<<EOF
global_defs {
router_id LB02
}
vrrp_script chekc_lb{
script "/server/scripts/chekc_lb.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
track_script{
check_lb
}
}
EOF
~~~
### 数据库备份
~~~
# 格式:mysqldump -h链接ip -P(大写)端口 -u用户名 -p密码数据库名>d:XX.sql(路劲)
/server/scripts/database_backup.sh
#!/bin/ash
mkdir /database_bak -p
$Time=$(date +%F)
mysqldump -h172.16.1.51 -uroot -poldboy123 dedecms > /database_bak/dedecms_${Time}.sql;
mysqldump -h172.16.1.51 -uroot -poldboy123 discuz > /database_bak/discuz_${Time}.sql;
mysqldump -h172.16.1.51 -uroot -poldboy123 wordpress > /database_bak/wordpress_${Time}.sql;
定时任务
00 00 * * * /bin/sh /server/scripts/database_backup.sh >/dev/null 2>&1
~~~
### 全网备份
~~~
安装配置rsync
yum install rsync -y
echo "123456" >/etc/rsync.password
chmod 600 /etc/rsync.password
备份脚本
cat /server/scripts/backup.sh
#!/bin/bash
Ip=$(ifconfig eth1|awk -F "[ :]+" 'NR==2 {print $4}')
Path=/backup
User=rsync_backup
Backup_server=172.16.1.41
Module=backup
Password_file=/etc/rsync.password
if [ $(date +%w) -eq 0 ]; then
Time=$(date +%F_%w)
else
Time=$(date +%F)
fi
BackupPath=$Path/$Ip
if [ ! -d $BackupPath ];then
mkdir $BackupPath -p
fi
cd / &&\
tar zcfh $BackupPath/backup_$Time.tar.gz etc apps/nginx/html var/spool/cron/root database_bak &&\
md5sum $BackupPath/backup_$Time.tar.gz >$BackupPath/flag_$Time.log &&\
rsync -az $Path/ $User@${Backup_server}::${Module} --password-file=${Password_file} &&\
find /backup/ -type f -mtime +30 \( -name "*.log" -o -name "*.tar.gz" \) | xargs rm -f
定时任务
# 定时备份 by Z
00 02 * * * /bin/sh /server/scripts/backup.sh >/dev/null 2>&1
检查备份脚本
cat /server/scripts/check_backup.sh
#!/bin/bash
IP=$(ifconfig eth1|awk -F "[:]+" 'NR==2{print $4}')
Path=/backup
if [ $(date +%w) -eq 0 ]
then
Time=$(date +%F_%w)
else
Time=$(date +%F)
fi
find /backup -type f -name "*flag*${Time}*.log" |xargs md5sum -c >> ${Path}/${Time}_result.log
mail -s "${Time} backup result" 207150680@qq.com < ${Path}/${Time}_result.log
find /backup/ -type f -mtime +180 -regextype 'posix-extended' ! -regex '.*_0.(tar.gz|log)' | xargs rm -f
定时任务
00 05 * * * /bin/sh /server/scripts/check_backup.sh >/dev/null 2>&1
~~~