首页 > 其他分享 >rsync推送案例练习与总结

rsync推送案例练习与总结

时间:2023-06-16 23:12:39浏览次数:41  
标签:SRC rsync 练习 etc error 推送 backup 服务端

案例实践:
客户端:
  1.客户端提前准备存放的备份的目录,目录规则如下:/backup/主机名_IP_时间
  2.客户端在本地打包备份(系统配置文件、应用配置等)拷贝至/backup/主机名_IP_时间
  3.客户端最后将备份的数据进行推送至备份服务器
  4.客户端每天凌晨1点定时执行脚本
  5.客户端服务器本地保留最近7的数据,避免浪费磁盘空间

服务端:
  1.服务端部署rsync,用于接收客户端推送过来的备份数据
  2.服务端需要每天校验客服端推送过来的数据是否完整
  3.服务端需要每天校验的结果通知给管理员
  4.服务端仅保留6个月的备份数据,其余的全部删除


客户端:
  1.推送备份文件的SHELL脚本:

vim /shell/client_push.sh

  SHELL脚本:

    #!/bin/bash
    #1.定义变量
      export RSYNC_PASSWORD="123456" #配置rsync虚拟用户的匹配密码
      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin #定义环境变量
      SRC=/backup #备份文件的目录
      HOST=$(hostname)
      ADDR=$(ifconfig eth0|awk -F " " 'NR==2 {print $2}') #取本经IP
      DATE=$(date +%F) #时间
      DEST=${HOST}_${ADDR}_${DATE} #拼接目录名称:主机名_IP_时间
    #2.创建目录
      [ -d $SRC/$DEST ] || mkdir -pv $SRC/$DEST #创建备份数据存储的目录

    #3.备份文件
      #cd到根目录
      cd / && \

      #打包备份的文件
      [ -f $SRC/$DEST/sys.tar.gz ] || tar czf $SRC/$DEST/sys.tar.gz etc/fstab etc/passwd && \
      [ -f $SRC/$DEST/other.tar.gz ] || tar czf $SRC/$DEST/other.tar.gz var/spool/cron shell && \
      #打标记:服务端后面做校验用(先打包)
      [ -f SRC/$DEST/flag_${DATE} ] || md5sum $SRC/$DEST/*.tar.gz >$SRC/$DEST/flag_${DATE}
    #4.rysnc推送到备份服务器上(注意要限速)
      /usr/bin/rsync -avz --bwlimit=1 $SRC//$DEST [email protected]::backup
    #5.删除7天前的目录
      find $SRC -type d -mtime +7 |xargs rm -rf

 

 

  2.配置定时任务

 

crontab -e
10 01 * * * /usr/bin/bash /shell/client_push.sh >&/dev/null

 

服务端:
  1.安装rysnc
    

yum install rsync -y

 

  2.配置rysnc进程守护:
    

vim /etc/rsyncd.conf
uid = rsync #定义使用rsync的虚拟用户
    gid = rsync #定义使用rsync的虚拟组
    port = 873 #定义监控端口
    ake super = yes #无需让rsync以root身份运行,不改变文件属性,伪装成root用户执行
    use chroot = no #禁锢推送的数据至某个目录,不允许跳出目录
    max connections = 200 #最大连接数
    timeout = 600 #超时时间
    ignore errors #忽略错误
    read only = false #只读(不能只读)对备份数据可读写
    list = false #是否显示模块
    auth users = rsync_backup #定义虚拟用户,作为连接认证用户
    secrets file = /etc/rsync.passwd #定义rsync服务用户连接认证密码文件路径
    log file = /var/log/rsyncd.log
    ######################################
    [backup] #模块
    comment = welcome to oldboyedu backup!
    path = /backup #备份存储的目录
 
 3.按配置文件进行部署:
    创建rsync的虚拟用户     
useradd -M -s /sbin/nologin rsync
    创建密码文件,并赋权  
 echo "rsync_backup:123456" >/etc/rsync.passwd
 chmod 600 /etc/rsync.passwd
    
    创建存储备份数据的目录,并赋权 
 mkdir /backup
 chown -R rsync.rsync /backup/  

  

  4.启动rsync服务    

systemctl start rsyncd.service #启动rsync服务
systemctl enable rsyncd.service #将rsync设定为开机自启动
systemctl status rsyncd.service #查看服务运行状态
systemctl disable rsyncd.service #不开机自启动 lsof -i:873;netstat -lntp|grep 873


  
5.服务端SHELL脚本:

vim /shell/check_client_push.sh

 

#!/bin/bash
    #1.定义变量
      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
      SRC=/backup
      DATE=$(date +%F)
    #2.校验备份文件的完整性
      md5sum -c $SRC/*_$DATE/flag_$DATE >$SRC/result_$DATE
    #3.发送邮件到管理员
      mail -s "${DATE}备份校验" **********@qq.com </backup/result_$DATE
    #4.备份保留180天
      find $SRC -type d -mtime +180 |xargs rm -rf

  6.安装邮件服务

yum install mailx -y

  

  7.配置邮箱服务:

vim /etc/mail.rc   
set from=**********@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=********@qq.com
set smtp-auth-password=*********#授权密码
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
   8.编写服务端定时任务:    
crontab -e
30 01 * * * /bin/bash /shell/check_client_push.sh >&/dev/null

 



rsync:参数
  -a #归档模式,等价于 -rlptgoD
  -v #输出详细信息,显示同步的文件名和目录名,
  -z #传输时进行压缩
  -r #递归地复制目录及其所有内容
  -t #保持文件时间信息,仅更新目录时间戳和文件修改时间
  -o #保持文件属主信息,维持文件属主为目标主机上文件属主的用户
  -p #保持文件权限,指定归档模式下嵌入文件权限
  -g #维持文件属主为目标主机上文件属主的群组
  --delete #删除目标文件夹中那些源文件夹中不存在的文件
  --bwlimit=KBPS #限制带宽,单位为千字节/秒
  --exclude=PATTERN #排除某些不需要的文件或目录,可以指定通配符进行模式匹配
  --exclude-form=file #文件名所在的目录文件
  --partial #断点传输


常见错误:
  1、@ERRPR:chdir failed
    错误原因:
      服务器端没有提供访问的目录 /backup
    处理方法:
    需要在服务器端创建,并赋予权限rsync管理权限
    mkdir /backup
    chown -R rsync.rsync /backup/

  2.@ERROR: auth failed on module backup
  服务端/etc/rsync.password 配置文件是否有问题
  错误原因:
    1)客户端密码文件的权限不是600
    2)服务端密码文件不是600
    3)服务端密码文件不存在(名字写错了/没有创建/配置文件参数写错了)
    4)服务端密码文件里保存的用户名和密码不正确

  3.@ERROR:invalid uid rsync
    useradd rsync -s /sbin/nologin -M

  4.@ERROR: chroot failed
   rsyncerror: error starting client-server protocol (code 5) at main.c(1522)[receiver=3.0.3]
   服务器端的目录不存在或无权限,创建目录并修正权限可解决问题。
    mkdir /backup
    chown -R rsync.rsync /backup/

  5.@ERROR: auth failed on module tee
  rsync error: error starting client-serverprotocol (code 5) at main.c(1522) [receiver=3.0.3]
    服务器端该模块(tee)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。
    提供正确的用户名密码解决此问题。

  6.rsync: --passwork-file=/etc/rsync.password: unknown option
  rsync: --passwork-file=/etc/rsync.password:unknown option
  rsync error: syntax or usage error (code 1)at main.c(1422) [client=3.0.6]
  错误原因:
    /etc/rsync.passwd文件名称写错
  解决方法:
    更正/etc/rsync.passwd文件名称

  7.rsync: write failed on "/backup/ ": No space lefton device (28)
  rsync:write failed on "/backup/": No space left on device(28)
  rsyncerror: error in file IO (code 11) at receiver.c(302) [receiver=3.0.7]
  rsync:connection unexpectedly closed (2721 bytes received so far) [generator]
  rsyncerror: error in rsync protocol data stream (code 12) at io.c(601) [generator=3.0.7]
  问题原因:
    磁盘空间不够,所以无法操作。
  解决方法:
    可以通过df /home/backup2010 来查看可用空间和已用空间

  8.rsync: failed to connect to 10.0.0.41: Connection timed out(110)
  rsync:failed to connect to 10.0.0.41: Connection timed out (110)
  rsyncerror: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]

  检查服务器的端口netstat ?tunlp,远程telnet测试。
  可能因为客户端或者服务端的防火墙开启 导致无法通信,可以设置规则放行 rsync(873端口) 或者直接关闭防火墙。
  关服务端selinux 和iptabs 防火墙

  systemctl stop firewalld.service
  systemctl disable firewalld
  setenforce 0 #临时关闭selinux
  sed -i.back 's#SELINUX=enforcing#SELINUX=disable#g' /etc/selinux/config #永久关闭selinux


  9.rsync: mkstemp ".passwd.8mPoUx" (in backup) failed: Permission denied (13)
  sent 644 bytes received 122 bytes 306.40 bytes/sec
  total size is 1,332 speedup is 1.74
  rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]

  setenforce 0 #临时关闭selinux
  sed -i.back 's#SELINUX=enforcing#SELINUX=disable#g' /etc/selinux/config #永久关闭selinux

  10. @ERROR: module is read only
    sendingincremental file list
    ERROR:module is read only
    rsyncerror: syntax or usage error (code 1) at main.c(866) [receiver=3.0.6]
    rsync:read error: Connection reset by peer (104)
    rsyncerror: error in rsync protocol data stream (code 12) at io.c(759)[sender=3.0.6]

    原因:
      服务器端权限设置read为only只读权限。
    解决方法:
      read only = false

  11. @ERROR: failed to openlock file
    @ERROR:failed to open lock file rsync error: error starting client-server protocol(code 5) at main.c(1495) [receiver=3.0.6]
    解决:
      配置文件 rsync.conf 中添加lock file = rsyncd.lock 即可解决

  12. password file must not be other-accessible
    passwordfile must not be other-accessible
    passwordfile must not be other-accessible
    continuingwithout password file
    Password:

    原因:
      这是因为rsync.passwd的权限不对,应该设置为600。
    解决方法:
      chmod 600 rsync.passwd

标签:SRC,rsync,练习,etc,error,推送,backup,服务端
From: https://www.cnblogs.com/woniu51/p/17486660.html

相关文章

  • dubbo-redis练习
    1. 项目需求    1351.1 注册学生phone必须唯一,如果已将存在了手机号,注册失败int addstudent(Student student);返回值int1:注册成功2:手机号已存在name至少两个字符,age必须大于01.2 查询学生根据id查询,此学生先到redis查询学生,如果redis没有此学生,从数据库查,把查询到的学生......
  • EasyCVR平台如何推送RTMP流实现上级平台级联?
    EasyCVR可拓展性强、视频能力灵活、部署轻快,可支持的主流标准协议有GB28181、RTSP/Onvif、RTMP等,以及厂家私有协议与SDK接入,包括海康Ehome、海大宇等设备的SDK等,能对外分发RTSP、RTMP、FLV、HLS、WebRTC等格式的视频流。有用户反馈,现场的设备是运动相机,不支持国标和其他协议接入......
  • 算法练习-day9
    栈和队列232.用栈实现队列题意:请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty)实现MyQueue类:voidpush(intx):将元素x推到队列的末尾intpop():从队列的开头移除并返回元素intpeek():返回队列开头的元素booleanempty():如果......
  • python基础语法练习题
    """一、必做题1、下面变量名正确的是(ABD)A.nameB.num1C.1_numD.name_A_12、Python不支持的数据类型有(A)A、charB、intC、floatD、list3、python源程序执行的方式(B)A编译执行B解析执行C直接执行D边编译边执行4、Python语言语句块的标记是(C)A分号B......
  • 算法练习-day8
    字符串28.找出字符串中第一个匹配项的下标题意:给你两个字符串haystack和needle,请你在haystack字符串中找出needle字符串的第一个匹配项的下标(下标从0开始)。如果needle不是haystack的一部分,则返回-1。示例:    思路:本题有两种思路:1.暴力求解法,只需要一次遍历,以i为haystack字......
  • 公众号已关注用户,扫描带参二维码没有事件(SCAN)推送?公众号认证权限
    公众号启用了服务器配置,关注、取消关注事件,CLICK事件,服务器都能收到事件推送,但是没有扫描带参二维码事件(SCAN)推送公众号需要认证才支持公众号认证权限:权限......
  • JDBC练习-添加
      /**添加*1.insertintotb_brand(brand_name,company_name,ordered,description,status)values(?,?,?,?,?);*2.参数:需要id之外所有参数信息*3.结果:boolean**/@TestpublicvoidtestBrand1()throwsException{......
  • 2小时解不完的数据库练习题,来挑战一下吧!
    写在前面我已经记不起来,有多久没更新文章了。5月中旬我还在上班,中旬以后一系列发生的事情,真的远远超出了可承受范围,只能硬着头皮面对!我是谁,我应该是谁,又能怎样,只能向前·····数据库实例class表course表score表student表teacher表实际语句1、查询所有的课程的......
  • 软件应用与开发-Web应用与开发《高并发在线试题练习对战系统》
    本作品是一个针对大学生对于计算机二级、java、c、python基础知识的自学,为后续的学习增强基础,于是设计了一个在线做题+对战的平台,实现了1对1的在线对战功能,界面简洁大方,可以在多种客户端使用如手机、电脑等。面向的用户是计算机专业和需要考级的大学生。主要功能是进行选择题的训......
  • SystemVerilog接口练习
           结合《SystemVerilog验证测试平台编写指南》和《FPGA应用开发和仿真》,在gVim里敲代码,学习一下接口的用法。1interfacemembus2#(3parameterLEN=256,DW=84)5(6inputwireclk,7inputwirerst8);9logic[$clog2(......