zoukankan      html  css  js  c++  java
  • 全网备份

    需求

    保障数据安全,防止因数据丢失造成不必要的损失

    需要备份的文件

    • 各种配置文件
    • 防火墙
    • 日志
    • 开机自启动
    • 脚本
    • 定时任务

    规划

    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 #排除文件中的列表
    

     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 备份服务器

    image_1avgjjksh1rtp1r961klki831cdf9.png-64.2kB

     

    4. 部署

     

    服务端配置

    No主机名IP备份目录脚本目录备注
    5 BACKUP 172.16.122.11 /backup /scripts 备份服务器
     
    1. 基础环境
    2. # cat /etc/redhat-release
    3. CentOS release 6.7 (Final)
    4. # uname -r
    5. 2.6.32-573.el6.x86_64
    6. # uname -m
    7. x86_64
    8. # /etc/init.d/iptables stop #关闭防火墙,配置完成设置防火墙规则。
    9. iptables: Setting chains to policy ACCEPT: filter [ OK ]
    10. iptables: Flushing firewall rules: [ OK ]
    11. iptables: Unloading modules: [ OK ]
    12. Q:如果没有关闭防火墙,会出现如下错误
    13. rsync: failed to connect to 172.16.1.41: No route to host (113)
    14. rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
    15. 查看安装包
    16. # rpm -qa rsync #如果不显示任何信息,使用yum安装
    17. rsync-3.0.6-12.el6.x86_64
    18. # yum install rsync -y
    19. 配置rsyncd.conf
    20. vim /etc/rsyncd.conf
    21. ### rsync_config start ###
    22. uid = rsync #用户名
    23. gid = rsync #用户组
    24. use chroot = no #不使用chroot
    25. max connections = 200 #最大连接数
    26. timeout = 300 #超时时间
    27. pid file = /var/run/rsyncd.pid #进程文件
    28. lock file = /var/run/rsync.lock #锁文件
    29. log file = /var/log/rsyncd.log #日志文件
    30. ignore errors #忽略一些无关的IO错误
    31. read only = false #只读
    32. list = false #列表
    33. hosts allow = 172.16.122.0/24 #允许的主机
    34. #hosts deny = 0.0.0.0/32 #禁止的主机
    35. auth users = rsync_bak #认证用户,如果没有这行,表示匿名用户,此用户与系统无关。
    36. secrets file = /etc/rsync.password #密码文件
    37. [backup] #模块
    38. path = /backup #备份路径
    39. [test] #增加的模块
    40. path = /test #备份路径
    41. #rsync_config end##
    42. 创建 rsync 账户
    43. useradd rsync -s /sbin/nologin -M
    44. Q1:如果没有创建用户,客户端推送会出现如下错误
    45. @ERROR: invalid uid rsync
    46. rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
    47. 创建备份目录
    48. # mkdir -p /backup
    49. # ls -ld /backup
    50. # chown -R rsync.rsync /backup/
    51. # ls -l /backup/
    52. Q2:如果没有给/backup/授权rsync用户文件拥有者的权限,会出现如下错误
    53. rsync: mkstemp ".1.upENcr" (in backup) failed: Permission denied (13)
    54. rsync: mkstemp ".10.ut1KcG" (in backup) failed: Permission denied (13)
    55. rsync: mkstemp ".2.DodKcV" (in backup) failed: Permission denied (13)
    56. 创建密码文件
    57. # echo "rsync_bak:123456"i > /etc/rsync.password
    58. # cat /etc/rsync.password
    59. # chmod 600 /etc/rsync.password
    60. # ls -l /etc/rsync.password
    61. Q3:如果没有创建密码文件、密码文件错误或没有给600权限,会出现如下错误
    62. @ERROR: auth failed on module backup
    63. rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
    64. 启动服务
    65. # rsync --daemon
    66. # lsof -i:873
    67. 开机自启动
    68. # echo "rsync --daemon" >>/etc/rc.local
    69. # 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 数据库备负载
     
    1. 创建密码文件
    2. # echo "123456" >/etc/rsync.password
    3. # cat /etc/rsync.password
    4. # chmod 600 /etc/rsync.password
    5. # ls -l /etc/rsync.password
    6. Q4:如果没有密码文件,会出现如下错误
    7. rsync: could not open password file "/etc/rsync.password": No such file or directory (2)
    8. Password: #等待输入密码状态
    9. Q5:如果密码错误或没有给600权限,会出现如下错误
    10. password file must not be other-accessible
    11. continuing without password file
    12. Password: #等待输入密码状态
    13. 创建目录
    14. # mkdir /backup #推送或拉取目录
    15. # ls -ld /backup
    16. 测试同步
    17. Push
    18. # rsync -avz /backup/ rsync_bak@172.16.122.11::backup --password-file=/etc/rsync.password
    19. 排除同步
    20. --exclude=a #排除单个文件
    21. --exclude={a,b,c,d} #排除多个文件
    22. --exclude-from=file #排除文件中的列表
    ---------------- 本地备份并推送至服务端 --------------
     

    4.1 Web 服务器备份

    Web 备份目录结构

     
    1. [root@WEB1 scripts]# tree /scripts/
    2. /scripts/
    3. └── bak.sh
    4. [root@WEB1 ~]# tree /backup/
    5. /backup/
    6. └── 172.16.122.191
    7. ├── flag_2016-10-20
    8. └── WEB1_2016-10-20.tar.gz

    bak.sh

     
    1. #!/bin/bash
    2. ## mail:liyao@xindaichina.com
    3. ## 8:00 crontab backup
    4. ## function:backup local data
    5. # 定义变量
    6. Date=$(date +%F)
    7. IP=`/sbin/ifconfig eth1 | awk -F '[ :]+' 'NR==2{print $4}'`
    8. bak_dir=/backup
    9. tomcat_file=usr/local/tomcat
    10. Server_ip=172.16.122.11
    11. # 判断是否存在该目录
    12. [ ! -d $bak_dir ] && mkdir -p $bak_dir
    13. [ ! -d $bak_dir/$IP ] && mkdir -p $bak_dir/$IP
    14. # 导出开启自启动
    15. chkconfig --list | grep "3:on" >/tmp/chk_conf.txt
    16. # 保存防火墙配置
    17. /etc/init.d/iptables save &>/dev/null
    18. # 打包Tomcat配置文件、日志文件、防火墙、定时任务、脚本、开启自启动
    19. cd / &&
    20. /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
    21. # 记录打包文件的MD5SUM指纹信息
    22. find $bak_dir -type f -name "*.tar.gz" | xargs md5sum >$bak_dir/$IP/flag_$(date +%F)
    23. # 删除7天以前的backup目录下文件
    24. find $bak_dir -type f -name "*.tar.gz" -name "flag*" -mtime +7 | xargs rm -f
    25. # 推送至服务端
    26. rsync -az $bak_dir/ rsync_bak@${Server_ip}::backup --password-file=/etc/rsync.password
     

    4.2 MySQL 服务器备份

    MySQL 备份目录结构

     
    1. [root@zhrt-R1 scripts]# tree /scripts/
    2. /scripts/
    3. └── bak.sh
    4. [root@zhrt-R1 ~]# tree /backup/
    5. /backup/
    6. └── 172.16.122.10
    7. ├── flag_2016-10-20
    8. └── zhrt-R1_2016-10-20.tar.gz

    MySQL bak.sh

     
    1. #!/bin/bash
    2. ## mail:liyao@xindaichina.com
    3. ## at 8:00 crontab
    4. ##
    5. ## function:backup local data
    6. # 调用系统脚本
    7. . /etc/rc.d/init.d/functions
    8. # 定义变量
    9. Date=$(date +%F)
    10. IP=`/sbin/ifconfig eth1 | awk -F '[ :]+' 'NR==2{print $4}'`
    11. bak_dir=/backup
    12. Server_ip=172.16.122.11
    13. # 判断是否存在该目录
    14. [ ! -d $bak_dir ] && mkdir -p $bak_dir
    15. [ ! -d $bak_dir/$IP ] && mkdir -p $bak_dir/$IP
    16. # 导出开启自启动
    17. chkconfig --list | grep "3:on" >/tmp/chk_conf.txt
    18. # 保存防火墙配置
    19. /etc/init.d/iptables save &>/dev/null
    20. # 导出LVS配置
    21. ipvsadm -Ln >/tmp/lvs_conf.txt
    22. # 打包防火墙配置、定时任务、脚本、开机自启动配置、LVS配置
    23. cd / &&
    24. /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
    25. # 记录打包文件的MD5SUM指纹信息
    26. find $bak_dir -type f -name "*.tar.gz" | xargs md5sum >$bak_dir/$IP/flag_$(date +%F)
    27. # 数据库全备
    28. mysqldump zhrtchina_online | gzip >$bak_dir/$IP/mysql_data_$Date.sql.gz
    29. # 删除7天以前的打包文件
    30. find $bak_dir -type f -name "*.tar.gz" -name "flag*" -mtime +7 | xargs rm -f
    31. # 推送至服务端
    32. rsync -az $bak_dir/ rsync_bak@${Server_ip}::backup --password-file=/etc/rsync.password
     

    4.3 LVS 服务器备份

    LVS-MASTER 备份目录结构

     
    1. [root@LVS-MASTER ~]# tree /scripts/
    2. /scripts/
    3. └── bak.sh
    4. [root@LVS-MASTER ~]# tree /backup/
    5. /backup/
    6. └── 172.16.122.15
    7. ├── flag_2016-10-20
    8. └── LVS-MASTER_2016-10-20.tar.gz

    LVS-BACKUP 备份目录结构

     
    1. [root@LVS-BACKUP ~]# tree /scripts/
    2. /scripts/
    3. └── bak.sh
    4. [root@LVS-BACKUP ~]# tree /backup/
    5. /backup/
    6. └── 172.16.122.14
    7. ├── flag_2016-10-20
    8. └── LVS-BACKUP_2016-10-20.tar.gz

    LVS-MASTER bak.sh

     
    1. #!/bin/bash
    2. ## mail:liyao@xindaichina.com
    3. ## at 8:00 crontab
    4. ##
    5. ## function:backup local data
    6. # 定义变量
    7. Date=$(date +%F)
    8. IP=`/sbin/ifconfig eth1 | awk -F '[ :]+' 'NR==2{print $4}'`
    9. bak_dir=/backup
    10. Server_ip=172.16.122.11
    11. # 判断是否存在该目录
    12. [ ! -d $bak_dir ] && mkdir -p $bak_dir
    13. [ ! -d $bak_dir/$IP ] && mkdir -p $bak_dir/$IP
    14. # 导出开启自启动
    15. chkconfig --list | grep "3:on" >/tmp/chk_conf.txt
    16. # 保存防火墙配置
    17. /etc/init.d/iptables save &>/dev/null
    18. # 导出LVS配置
    19. ipvsadm -Ln >/tmp/lvs_conf.txt
    20. # 打包防火墙配置、定时任务、脚本、开机自启动配置、LVS配置
    21. cd / &&
    22. /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
    23. # 记录打包文件的MD5SUM指纹信息
    24. find $bak_dir -type f -name "*.tar.gz" | xargs md5sum >$bak_dir/$IP/flag_$(date +%F)
    25. # 删除7天以前的打包文件
    26. find $bak_dir -type f -name "*.tar.gz" -name "flag*" -mtime +7 | xargs rm -f
    27. # 推送至服务端
    28. rsync -az $bak_dir/ rsync_bak@${Server_ip}::backup --password-file=/etc/rsync.password

    LVS-BACKUP bak.sh

     
    1. #!/bin/bash
    2. ## mail:liyao@xindaichina.com
    3. ## at 8:00 crontab
    4. ##
    5. ## function:backup local data
    6. # 定义变量
    7. Date=$(date +%F)
    8. IP=`ip addr show dev eth1 | awk -F '[ :/]+' 'NR==4{print $3}'` *** 这条和上面的有区别 ***
    9. bak_dir=/backup
    10. Server_ip=172.16.122.11
    11. # 判断是否存在该目录
    12. [ ! -d $bak_dir ] && mkdir -p $bak_dir
    13. [ ! -d $bak_dir/$IP ] && mkdir -p $bak_dir/$IP
    14. # 导出开启自启动
    15. chkconfig --list | grep "3:on" >/tmp/chk_conf.txt
    16. # 保存防火墙配置
    17. /etc/init.d/iptables save &>/dev/null
    18. # 导出LVS配置
    19. ipvsadm -Ln >/tmp/lvs_conf.txt
    20. # 打包防火墙配置、定时任务、脚本、开机自启动配置、LVS配置
    21. cd / &&
    22. /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
    23. # 记录打包文件的MD5SUM指纹信息
    24. find $bak_dir -type f -name "*.tar.gz" | xargs md5sum >$bak_dir/$IP/flag_$(date +%F)
    25. # 删除7天以前的打包文件
    26. find $bak_dir -type f -name "*.tar.gz" -name "flag*" -mtime +7 | xargs rm -f
    27. # 推送至服务端
    28. 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 备份服务器

    备份服务器目录结构

     
    1. [root@zhrt-V1 scripts]# tree /backup
    2. /backup
    3. ├── 172.16.122.10
    4.    ├── flag_2016-10-21
    5.    ├── mysql_data_2016-10-21.sql.gz
    6.    └── zhrt-R1_2016-10-21.tar.gz
    7. ├── 172.16.122.14
    8.    ├── flag_2016-10-21
    9.    └── LVS-BACKUP_2016-10-21.tar.gz
    10. ├── 172.16.122.15
    11.    ├── flag_2016-10-21
    12.    └── LVS-MASTER_2016-10-21.tar.gz
    13. └── 172.16.122.191
    14. ├── flag_2016-10-21
    15. └── WEB1_2016-10-21.tar.gz
    16. [root@zhrt-V1 scripts]# tree /scripts/
    17. /scripts/
    18. ├── bak.sh
    19. └── check.sh

    发送邮件配置

     
    1. tail -1 /etc/mail.rc
    2. set from=用户名@163.com smtp=smtp.163.com smtp-auth-user=用户名 smtp-auth-password=密码 smtp-auth=login
    3. 注:密码是指认证密码,不是登录密码。需要先开启SMTP认证

    check.sh

     
    1. #!/bin/bash
    2. # 定义变量
    3. bak_dir=/backup
    4. Date=$(date +%F)
    5. log=/tmp/check_$Date.log
    6. find $bak_dir/ -type f -name "flag_$Date" | xargs md5sum -c >$log

      

  • 相关阅读:
    PHP多进程(四) 内部多进程
    STL map and multimap
    Understanding Function Objects
    Working with Bit Flags Using STL
    STL Algorithms
    STL set and multiset
    Understanding Smart Pointers
    More Effective C++ 学习笔记(1)
    Adaptive Container: stack and queue
    第一个 Python 程序
  • 原文地址:https://www.cnblogs.com/wanglan/p/7832690.html
Copyright © 2011-2022 走看看