需求
保障数据安全,防止因数据丢失造成不必要的损失
需要备份的文件
- 各种配置文件
- 防火墙
- 日志
- 开机自启动
- 脚本
- 定时任务
规划
No 主机名 IP 备份目录 脚本目录 备注 1 WEB1 10.0.0.10 /backup /scripts Web服务器 2 Mysql 10.0.0.11 /backup /scripts 数据库和存储服务器 3 LVS-MASTER 10.0.0.12 /backup /scripts 数据库主负载 4 LVS-BACKUP 10.0.0.13 /backup /scripts 数据库备负载 5 BACKUP 10.0.0.14 /backup /scripts 备份服务器
部署
No 主机名 IP 备份目录 脚本目录 备注 5 BACKUP 10.0.0.14 /backup /scripts 备份服务器
基础环境
[root@node1 ~]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core) [root@node1 ~]# uname -r 3.10.0-229.el7.x86_64 [root@node1 ~]# uname -m x86_64 [root@node1 ~]# systemctl stop firewalld.service #关闭防火墙,配置完成设置防火墙规则 如果没有关闭防火墙,会出现如下错误 rsync: failed to connect to 172.16.1.41: No route to host (113) rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
查看安装包
[root@node1 ~]# rpm -qa rsync #如果不显示任何信息,使用yum安装 rsync-3.0.9-18.el7.x86_64 [root@node1 ~]# yum install -y rsync
配置rsyncd.conf
### rsync_config start ### uid = rsync #用户名 gid = rsync #用户组 use chroot = no #不使用chroot max connections = 200 #最大连接数 timeout = 300 #超时时间 pid file = /var/run/rsyncd.pid #进程文件 lock file = /var/run/rsync.lock #锁文件 log file = /var/log/rsyncd.log #日志文件 ignore errors #忽略一些无关的IO错误 read only = false #只读 list = false #列表 hosts allow = 10.0.0.0/24 #允许的主机 #hosts deny = 0.0.0.0/32 #禁止的主机 auth users = rsync_bak #认证用户,如果没有这行,表示匿名用户,此用户与系统无关。 secrets file = /etc/rsync.password #密码文件 [backup] #模块 path = /backup #备份路径 [test] #增加的模块 path = /test #备份路径 #rsync_config end##
创建 rsync 账户
[root@node1 ~]# useradd rsync -s /sbin/nologin -M
如果没有创建用户,客户端推送会出现如下错误 @ERROR: invalid uid rsync rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
创建备份目录
[root@node1 ~]# mkdir -p /backup [root@node1 ~]# ls -ld /backup/ drwxr-xr-x 2 root root 6 Nov 13 08:30 /backup/ [root@node1 ~]# chown -R rsync.rsync /backup/ [root@node1 ~]# ls -ld /backup/ drwxr-xr-x 2 rsync rsync 6 Nov 13 08:30 /backup/
如果没有给/backup/授权rsync用户文件拥有者的权限,会出现如下错误 rsync: mkstemp ".1.upENcr" (in backup) failed: Permission denied (13) rsync: mkstemp ".10.ut1KcG" (in backup) failed: Permission denied (13) rsync: mkstemp ".2.DodKcV" (in backup) failed: Permission denied (13)
创建密码文件
[root@node1 ~]# echo "rsync_bak:123456"i > /etc/rsync.password [root@node1 ~]# cat /etc/rsync.password [root@node1 ~]# chmod 600 /etc/rsync.password [root@node1 ~]# ls -l /etc/rsync.password 如果没有创建密码文件、密码文件错误或没有给600权限,会出现如下错误 @ERROR: auth failed on module backup rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
启动服务
[root@node1 ~]# rsync --daemon [root@node1 ~]# lsof -i :873
开机自启动
[root@node1 ~]# echo "rsync --daemon" >>/etc/rc.local [root@node1 ~]# tail -l /etc/rc.local
客户端配置
No 主机名 IP 备份目录 脚本目录 备注 1 WEB1 172.16.122.191 /backup /scripts Web服务器 2 MySQL 172.16.122.10 /backup /scripts 数据库和存储服务器 3 LVS-MASTER 172.16.122.15 /backup /scripts 数据库主负载 4 LVS-BACKUP 172.16.122.14 /backup /scripts 数据库备负载
创建密码文件
echo "123456" >/etc/rsync.password cat /etc/rsync.password chmod 600 /etc/rsync.password ls -l /etc/rsync.password 如果没有密码文件,会出现如下错误 rsync: could not open password file "/etc/rsync.password": No such file or directory (2) Password: #等待输入密码状态 Q5:如果密码错误或没有给600权限,会出现如下错误 password file must not be other-accessible continuing without password file Password: #等待输入密码状态
创建目录
mkdir /backup #推送或拉取目录 ls -ld /backup
测试同步
Push: # rsync -avz /backup/ rsync_bak@172.16.122.11::backup --password-file=/etc/rsync.password
排除异步
--exclude=a #排除单个文件
--exclude={a,b,c,d} #排除多个文件
--exclude-from=file #排除文件中的列表
@wanglan 2017-06-05 11:46 字数 8009 阅读 0
全网备份
方案
1. 需求
保障数据安全,防止因数据丢失造成不必要的损失。
2. 分析
需要备份的文件
- 各种配置文件 *
- 防火墙 *
- 日志
- 开机自启动
- 脚本 *
- 定时任务 *
3. 规划
| No | 主机名 | IP | 备份目录 | 脚本目录 | 备注 |
|---|---|---|---|---|---|
| 1 | WEB1 | 172.16.122.191 | /backup | /scripts | Web服务器 |
| 2 | MySQL | 172.16.122.10 | /backup | /scripts | 数据库和存储服务器 |
| 3 | LVS-MASTER | 172.16.122.15 | /backup | /scripts | 数据库主负载 |
| 4 | LVS-BACKUP | 172.16.122.14 | /backup | /scripts | 数据库备负载 |
| 5 | BACKUP | 172.16.122.11 | /backup | /scripts | 备份服务器 |

4. 部署
服务端配置
| No | 主机名 | IP | 备份目录 | 脚本目录 | 备注 |
|---|---|---|---|---|---|
| 5 | BACKUP | 172.16.122.11 | /backup | /scripts | 备份服务器 |
基础环境# cat /etc/redhat-releaseCentOS release 6.7 (Final)# uname -r2.6.32-573.el6.x86_64# uname -mx86_64# /etc/init.d/iptables stop #关闭防火墙,配置完成设置防火墙规则。iptables: Setting chains to policy ACCEPT: filter [ OK ]iptables: Flushing firewall rules: [ OK ]iptables: Unloading modules: [ OK ]Q:如果没有关闭防火墙,会出现如下错误rsync: failed to connect to 172.16.1.41: No route to host (113)rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]查看安装包# rpm -qa rsync #如果不显示任何信息,使用yum安装rsync-3.0.6-12.el6.x86_64# yum install rsync -y配置rsyncd.confvim /etc/rsyncd.conf### rsync_config start ###uid = rsync #用户名gid = rsync #用户组use chroot = no #不使用chrootmax connections = 200 #最大连接数timeout = 300 #超时时间pid file = /var/run/rsyncd.pid #进程文件lock file = /var/run/rsync.lock #锁文件log file = /var/log/rsyncd.log #日志文件ignore errors #忽略一些无关的IO错误read only = false #只读list = false #列表hosts allow = 172.16.122.0/24 #允许的主机#hosts deny = 0.0.0.0/32 #禁止的主机auth users = rsync_bak #认证用户,如果没有这行,表示匿名用户,此用户与系统无关。secrets file = /etc/rsync.password #密码文件[backup] #模块path = /backup #备份路径[test] #增加的模块path = /test #备份路径#rsync_config end##创建 rsync 账户useradd rsync -s /sbin/nologin -MQ1:如果没有创建用户,客户端推送会出现如下错误@ERROR: invalid uid rsyncrsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]创建备份目录# mkdir -p /backup# ls -ld /backup# chown -R rsync.rsync /backup/# ls -l /backup/Q2:如果没有给/backup/授权rsync用户文件拥有者的权限,会出现如下错误rsync: mkstemp ".1.upENcr" (in backup) failed: Permission denied (13)rsync: mkstemp ".10.ut1KcG" (in backup) failed: Permission denied (13)rsync: mkstemp ".2.DodKcV" (in backup) failed: Permission denied (13)创建密码文件# echo "rsync_bak:123456"i > /etc/rsync.password# cat /etc/rsync.password# chmod 600 /etc/rsync.password# ls -l /etc/rsync.passwordQ3:如果没有创建密码文件、密码文件错误或没有给600权限,会出现如下错误@ERROR: auth failed on module backuprsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]启动服务# rsync --daemon# lsof -i:873开机自启动# echo "rsync --daemon" >>/etc/rc.local# tail -1 /etc/rc.local
客户端配置
| No | 主机名 | IP | 备份目录 | 脚本目录 | 备注 |
|---|---|---|---|---|---|
| 1 | WEB1 | 172.16.122.191 | /backup | /scripts | Web服务器 |
| 2 | MySQL | 172.16.122.10 | /backup | /scripts | 数据库和存储服务器 |
| 3 | LVS-MASTER | 172.16.122.15 | /backup | /scripts | 数据库主负载 |
| 4 | LVS-BACKUP | 172.16.122.14 | /backup | /scripts | 数据库备负载 |
创建密码文件# echo "123456" >/etc/rsync.password# cat /etc/rsync.password# chmod 600 /etc/rsync.password# ls -l /etc/rsync.passwordQ4:如果没有密码文件,会出现如下错误rsync: could not open password file "/etc/rsync.password": No such file or directory (2)Password: #等待输入密码状态Q5:如果密码错误或没有给600权限,会出现如下错误password file must not be other-accessiblecontinuing without password filePassword: #等待输入密码状态创建目录# mkdir /backup #推送或拉取目录# ls -ld /backup测试同步Push:# rsync -avz /backup/ rsync_bak@172.16.122.11::backup --password-file=/etc/rsync.password排除同步--exclude=a #排除单个文件--exclude={a,b,c,d} #排除多个文件--exclude-from=file #排除文件中的列表
4.1 Web 服务器备份
Web 备份目录结构
[root@WEB1 scripts]# tree /scripts//scripts/└── bak.sh[root@WEB1 ~]# tree /backup//backup/└── 172.16.122.191├── flag_2016-10-20└── WEB1_2016-10-20.tar.gz
bak.sh
#!/bin/bash## mail:liyao@xindaichina.com## 8:00 crontab backup## function:backup local data# 定义变量Date=$(date +%F)IP=`/sbin/ifconfig eth1 | awk -F '[ :]+' 'NR==2{print $4}'`bak_dir=/backuptomcat_file=usr/local/tomcatServer_ip=172.16.122.11# 判断是否存在该目录[ ! -d $bak_dir ] && mkdir -p $bak_dir[ ! -d $bak_dir/$IP ] && mkdir -p $bak_dir/$IP# 导出开启自启动chkconfig --list | grep "3:on" >/tmp/chk_conf.txt# 保存防火墙配置/etc/init.d/iptables save &>/dev/null# 打包Tomcat配置文件、日志文件、防火墙、定时任务、脚本、开启自启动cd / &&/bin/tar zcf $bak_dir/$IP/`hostname`_$Date.tar.gz $tomcat_file/conf/ $tomcat_file/logs/catalina-daemon-$Date.out etc/sysconfig/iptables var/spool/cron/root scirpts/ tmp/chk_conf.txt &>/dev/null# 记录打包文件的MD5SUM指纹信息find $bak_dir -type f -name "*.tar.gz" | xargs md5sum >$bak_dir/$IP/flag_$(date +%F)# 删除7天以前的backup目录下文件find $bak_dir -type f -name "*.tar.gz" -name "flag*" -mtime +7 | xargs rm -f# 推送至服务端rsync -az $bak_dir/ rsync_bak@${Server_ip}::backup --password-file=/etc/rsync.password
4.2 MySQL 服务器备份
MySQL 备份目录结构
[root@zhrt-R1 scripts]# tree /scripts//scripts/└── bak.sh[root@zhrt-R1 ~]# tree /backup//backup/└── 172.16.122.10├── flag_2016-10-20└── zhrt-R1_2016-10-20.tar.gz
MySQL bak.sh
#!/bin/bash## mail:liyao@xindaichina.com## at 8:00 crontab#### function:backup local data# 调用系统脚本. /etc/rc.d/init.d/functions# 定义变量Date=$(date +%F)IP=`/sbin/ifconfig eth1 | awk -F '[ :]+' 'NR==2{print $4}'`bak_dir=/backupServer_ip=172.16.122.11# 判断是否存在该目录[ ! -d $bak_dir ] && mkdir -p $bak_dir[ ! -d $bak_dir/$IP ] && mkdir -p $bak_dir/$IP# 导出开启自启动chkconfig --list | grep "3:on" >/tmp/chk_conf.txt# 保存防火墙配置/etc/init.d/iptables save &>/dev/null# 导出LVS配置ipvsadm -Ln >/tmp/lvs_conf.txt# 打包防火墙配置、定时任务、脚本、开机自启动配置、LVS配置cd / &&/bin/tar zcf $bak_dir/$IP/`hostname`_$Date.tar.gz etc/sysconfig/iptables var/spool/cron/root scirpts/ tmp/chk_conf.txt tmp/lvs_conf.txt &>/dev/null# 记录打包文件的MD5SUM指纹信息find $bak_dir -type f -name "*.tar.gz" | xargs md5sum >$bak_dir/$IP/flag_$(date +%F)# 数据库全备mysqldump zhrtchina_online | gzip >$bak_dir/$IP/mysql_data_$Date.sql.gz# 删除7天以前的打包文件find $bak_dir -type f -name "*.tar.gz" -name "flag*" -mtime +7 | xargs rm -f# 推送至服务端rsync -az $bak_dir/ rsync_bak@${Server_ip}::backup --password-file=/etc/rsync.password
4.3 LVS 服务器备份
LVS-MASTER 备份目录结构
[root@LVS-MASTER ~]# tree /scripts//scripts/└── bak.sh[root@LVS-MASTER ~]# tree /backup//backup/└── 172.16.122.15├── flag_2016-10-20└── LVS-MASTER_2016-10-20.tar.gz
LVS-BACKUP 备份目录结构
[root@LVS-BACKUP ~]# tree /scripts//scripts/└── bak.sh[root@LVS-BACKUP ~]# tree /backup//backup/└── 172.16.122.14├── flag_2016-10-20└── LVS-BACKUP_2016-10-20.tar.gz
LVS-MASTER bak.sh
#!/bin/bash## mail:liyao@xindaichina.com## at 8:00 crontab#### function:backup local data# 定义变量Date=$(date +%F)IP=`/sbin/ifconfig eth1 | awk -F '[ :]+' 'NR==2{print $4}'`bak_dir=/backupServer_ip=172.16.122.11# 判断是否存在该目录[ ! -d $bak_dir ] && mkdir -p $bak_dir[ ! -d $bak_dir/$IP ] && mkdir -p $bak_dir/$IP# 导出开启自启动chkconfig --list | grep "3:on" >/tmp/chk_conf.txt# 保存防火墙配置/etc/init.d/iptables save &>/dev/null# 导出LVS配置ipvsadm -Ln >/tmp/lvs_conf.txt# 打包防火墙配置、定时任务、脚本、开机自启动配置、LVS配置cd / &&/bin/tar zcf $bak_dir/$IP/`hostname`_$Date.tar.gz etc/sysconfig/iptables var/spool/cron/root scirpts/ tmp/chk_conf.txt tmp/lvs_conf.txt &>/dev/null# 记录打包文件的MD5SUM指纹信息find $bak_dir -type f -name "*.tar.gz" | xargs md5sum >$bak_dir/$IP/flag_$(date +%F)# 删除7天以前的打包文件find $bak_dir -type f -name "*.tar.gz" -name "flag*" -mtime +7 | xargs rm -f# 推送至服务端rsync -az $bak_dir/ rsync_bak@${Server_ip}::backup --password-file=/etc/rsync.password
LVS-BACKUP bak.sh
#!/bin/bash## mail:liyao@xindaichina.com## at 8:00 crontab#### function:backup local data# 定义变量Date=$(date +%F)IP=`ip addr show dev eth1 | awk -F '[ :/]+' 'NR==4{print $3}'` *** 这条和上面的有区别 ***bak_dir=/backupServer_ip=172.16.122.11# 判断是否存在该目录[ ! -d $bak_dir ] && mkdir -p $bak_dir[ ! -d $bak_dir/$IP ] && mkdir -p $bak_dir/$IP# 导出开启自启动chkconfig --list | grep "3:on" >/tmp/chk_conf.txt# 保存防火墙配置/etc/init.d/iptables save &>/dev/null# 导出LVS配置ipvsadm -Ln >/tmp/lvs_conf.txt# 打包防火墙配置、定时任务、脚本、开机自启动配置、LVS配置cd / &&/bin/tar zcf $bak_dir/$IP/`hostname`_$Date.tar.gz etc/sysconfig/iptables var/spool/cron/root scirpts/ tmp/chk_conf.txt tmp/lvs_conf.txt &>/dev/null# 记录打包文件的MD5SUM指纹信息find $bak_dir -type f -name "*.tar.gz" | xargs md5sum >$bak_dir/$IP/flag_$(date +%F)# 删除7天以前的打包文件find $bak_dir -type f -name "*.tar.gz" -name "flag*" -mtime +7 | xargs rm -f# 推送至服务端rsync -az $bak_dir/ rsync_bak@${Server_ip}::backup --password-file=/etc/rsync.password
5. 验证备份完整性
| No | 主机名 | IP | 备份目录 | 脚本目录 | 备注 |
|---|---|---|---|---|---|
| 5 | BACKUP | 172.16.122.11 | /backup | /scripts | 备份服务器 |
备份服务器目录结构
[root@zhrt-V1 scripts]# tree /backup/backup├── 172.16.122.10│ ├── flag_2016-10-21│ ├── mysql_data_2016-10-21.sql.gz│ └── zhrt-R1_2016-10-21.tar.gz├── 172.16.122.14│ ├── flag_2016-10-21│ └── LVS-BACKUP_2016-10-21.tar.gz├── 172.16.122.15│ ├── flag_2016-10-21│ └── LVS-MASTER_2016-10-21.tar.gz└── 172.16.122.191├── flag_2016-10-21└── WEB1_2016-10-21.tar.gz[root@zhrt-V1 scripts]# tree /scripts//scripts/├── bak.sh└── check.sh
发送邮件配置
tail -1 /etc/mail.rcset from=用户名@163.com smtp=smtp.163.com smtp-auth-user=用户名 smtp-auth-password=密码 smtp-auth=login注:密码是指认证密码,不是登录密码。需要先开启SMTP认证
check.sh
#!/bin/bash# 定义变量bak_dir=/backupDate=$(date +%F)log=/tmp/check_$Date.logfind $bak_dir/ -type f -name "flag_$Date" | xargs md5sum -c >$log