rsync命令及单向实时同步部署

rsync命令

概述
  • 数据镜像备份工具,支持快速完全备份和增量备份的工具,支持本地复制,远程同步等,类似于scp命令;
  • 同步之前需要先登录进行用户身份认证,一般使用两种协议进行同步:ssh协议和rsync协议
特性
  • 能更新整个目录树和文件系统
  • 有选择性的保留符号链接,硬链接,文件属性,权限,设备以及时间等;
  • 安装时无特殊权限要求
  • 传输多个文件时效率更高
  • 能用ssh或自定义端口作为传入入口端口
工作原理
  • 两个概念:原地址src,目标地址dst,以及以哪一方为基准。
  • ssh登录验证使用ssh协议作为基础,root用户身份认证,然后进行数据同步
  • rsync登录验证:使用rsync 协议进行用户身份认证(非系统用户),然后进行数据同步
  • 数据同步方式:推送(上传)、拉取(下载)
数据备份 拉下载
数据恢复 推上传
NFS服务器/filesrc
rsync服务器/filedst
rsync服务器/filedst
NFS服务器/filesrc
实验演示
1、ssh协议数据同步:将NFS服务器数据备份到rsync服务器
下行同步(下载)
格式:
# rsync -avz 目标地址:/目标目录/* /本地目录
示例:
[root@yzl2 ~]# rsync -avz root@192.168.154.128:/filesrc/* /filedst/
...
-a: 归档模式,递归并保留对象属性
-v: 显示同步过程
-z: 在传输文件时进行压缩
上行同步(上传)
格式:
# rsync -avz /本地目录/* 服务器地址:/服务器目录
示例:
[root@yzl2 filedst]# rsync -avz /filedst/* root@192.168.154.128:/filesrc/
注意:
# 生产环境尽量使用单独创建用户,避免权限溢出
创建用来做数据同步的用户,并给予用户对目录的相应权限,一般使用ACL设置权限
[root@yzl2 filedst]# useradd t1
[root@yzl2 filedst]# setfacl -m u:t1:rwx /filedst
[root@yzl2 filedst]# getfacl /filedst/
getfacl: Removing leading '/' from absolute path names
# file: filedst/
# owner: root
# group: root
user::rwx
user:t1:rwx
group::r-x
mask::rwx
other::r-x
2、rsync协议数据同步(单向同步)
搭建rsync服务
  • 创建主配置文件(/etc/rsyncd.conf)
[root@yzl1 etc]# vim rsyncd.conf
    address=192.168.154.128			# rsync服务绑定IP
    port 873						# 默认服务端口873
    log file = /var/log/rsyncd.log	# 日志文件位置
    pid file = /var/run/rsyncd.pid	# 进程号文件位置
    [web]							# 共享名:用来连接是写在url上的
      comment = web directory backup	# 共享描述话语 
      path = /filesrc				# 实际共享目录
      read only = no				# 是否仅允许只读
      dont compress = *.gz *.bz2	# 哪些文件类型不进行压缩
      auth users = c1				# 登录用户名(非系统用户,需自行创建)
      secrets file = /etc/rsyncd_users.db # 认证所需账户密码文件(需自行创建-同上)
  • 创建认证所需账户密码文件
[root@yzl1 etc]# vim /etc/rsyncd_users.db
	c1:123456
[root@yzl1 etc]# chmod 600 /etc/rsyncd_users.db 	# 必须修改权限,否则登录报错
  • 启动服务
[root@yzl1 etc]# rsync --daemon
[root@yzl1 etc]# ss -antulp |grep 873
tcp    LISTEN     0      5      192.168.154.128:873                   *:*                   users:(("rsync",pid=6031,fd=3))
  • 设置映射用户对共享目录有权限®
[root@yzl1 filesrc]# setfacl -m u:nobody:rwx /filesrc
下行同步(下载)/ 上行同步(上传)
格式:
rsync -avz rsync://用户名@服务器地址/共享模块名  /本地目录
示例:
[root@yzl2 filedst]# rsync -avz rsync://c1@yzl1/web /filedst/		# 已经写好/etc/hosts文件
拓展:
# --delete 删除本地比服务器多出来的文件(即源地址没有,但目标地址里多余)
[root@yzl2 filedst]# rsync -avz --delete rsync://c1@yzl1/web /filedst/

拓展:rsync协议免密登录
  • export RSYNC_PASSWORD=虚拟用户密码(客户端生成)
[root@yzl1 filedst]# export RSYNC_PASSWORD="123456" 

3、rsync+inotify实时同步
  • 定期同步缺点:执行备份时间不固定,延期明显,实时性差;当同步源长期不变时,定期同步会浪费资源
  • 实时同步优点:实时性!节省资源。
inotify简介
  • 监控文件系统,及时向应用程序相应的事件警告,要求内核2.6.13以上

  • 两个监控命令:

    • inotifywait:用于持续监控,实时输出结果(常用)
    • inotifywatch:用于短期监控,任务完成后再出结果

    https://sourceforge.net/projects/inotify-tools/下载链接

inotify部署
[root@yzl1 ~]# yum -y install gcc*
[root@yzl1 ~]# tar -xvf inotify-tools-3.14.tar.gz
[root@yzl1 ~]# cd inotify-tools-3.14
[root@yzl1 inotify-tools-3.14]# ./configure && make && make install
inotifywait命令格式
格式:
inotifywait -mrq -e 监控动作1,监控动作2  /监控目录  & #放入后台
示例:
[root@yzl1 inotify-tools-3.13]# inotifywait -wrq -e create,modify,delete /filesrc/
# -m: 时钟保持事件监听状态
# -r: 递归查询目录
# -q: 只打印监控事件的信息
监控动作: modify,create,attrib(权限),move,delete

[root@localhost filesrc]# inotifywait -mrq -e create,delete,modify /filesrc
/filesrc/ CREATE 1.txt
/filesrc/ DELETE 1.txt

利用rsync+inotifywait结合脚本实现单向实时同步
[root@localhost filesrc]# vim ~/src.sh
    #!/bin/bash
    a="inotifywait -mrq -e create,delete /filesrc"
    b="rsync -avz /filesrc/* root@192.168.154.131:/filedst"
    $a | while read directory event file	# 固定写法,while判断是否接收到监控记录
    do
            $b
    done
# 注意:用户登录时要求免密码验证!!!
    
[root@localhost filesrc]# bash ~/src.sh &
[2] 10729
[root@localhost filesrc]# touch t2.sh
[root@localhost filesrc]# sending incremental file list
t1.sh
t2.sh

sent 167 bytes  received 56 bytes  148.67 bytes/sec
total size is 84  speedup is 0.38

  • 后台执行脚本,服务器在filesrc目录下添加文件时,屏幕能实时输出修改信息,并且客户端同步实时数据备份
  • 注意需要关闭防火墙和selinux
拓展:调整inotify监控的文件数量
调整inotify内核参数(/etc/sysctl.conf)
mak_queue_events监控队列大小
mak_user_instances最多监控实例数
max_user_watches每个实例最多监控文件数
问题:
  • 段错误(吐核)
[root@yzl2 inotify-tools-3.13]# inotifywait -wrq -e create,modify,delete /filedst/
段错误(吐核)
产生段错误的原因主要有:
解引用空指针
访问不可访问的内存空间(如内核空间)
访问不存在的内存地址
试图写一个只读内存空间(如代码段)
栈溢出(函数递归调用)
使用未初始化的指针(定义时没有初始化或者已经回收)

[root@yzl1 filesrc]# ulimit -a
core file size          (blocks, -c) 0		# 此处值为0,表示不允许吐核
data seg size           (kbytes, -d) unlimited
...
[root@yzl1 filesrc]# ulimit -c unlimited	# 修改大小为无限制
[root@yzl1 filesrc]# ulimit -a          
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
...
再执行inotifywait,再次吐核。
之后 ls 就会出现core.8966文件,数字后缀不同机器会不一样,这个不用在意。这个就是核心转储文件,我们成功通过操作查看到了。
[root@yzl1 filesrc]# ls
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  core.8966  test

# gdb  可执行文件名 + 核心转储文件名
[root@yzl1 filesrc]# gdb inotifywait core.8966 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-94.axs7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redflag-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /usr/local/bin/inotifywait...done.
[New LWP 8966]
Core was generated by `inotifywait -mrq -e create,delete /filesrc/'.
Program terminated with signal 11, Segmentation fault.
#0  0x00007f9456421b9f in __libc_start_main (main=0x401430 <main>, 
    argc=5, argv=0x7ffead55f0d8, init=<optimized out>, 
    fini=<optimized out>, rtld_fini=<optimized out>, 
    stack_end=0x7ffead55f0c8) at libc-start.c:239
239		    afct->preinit (&head->l_audit[cnt].cookie);
Missing separate debuginfos, use: debuginfo-install glibc-2.17-157.axs7.1.x86_64
(gdb) bt
#0  0x00007f9456421b9f in __libc_start_main (main=0x401430 <main>, 
    argc=5, argv=0x7ffead55f0d8, init=<optimized out>, 
    fini=<optimized out>, rtld_fini=<optimized out>, 
    stack_end=0x7ffead55f0c8) at libc-start.c:239
#1  0x0000000000401af4 in _start ()

[root@yzl1 ~]# find / -type f -name libc-start.c
/root/glibc-2.18/csu/libc-start.c
/root/glibc-2.18/sysdeps/unix/sysv/linux/powerpc/libc-start.c
[root@yzl1 ~]# vim +239g /root/glibc-2.18/csu/libc-start.c
...
236       for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
237         {
238           if (afct->preinit != NULL)
239             afct->preinit (&head->l_audit[cnt].cookie);
240 
241           afct = afct->next;
242         }
...
  • 这里出现的这个问题没有解决,但是总算是找到出错原因了,7.3系统默认的glibc版本库是2.17,因为之前做测试需要,临时升级到2.18,导致glibc库在升级过程中修改了内置参数,此处出现吐核错误,我目前的技术水平暂时无法解决这个吐核错误,但是将glibc版本恢复到默认的2.17之后,此问题可以避免了!
  • 另外升级glibc到2.18,可能会导致ssh-copy-id命令也丢失(不完全测试),升级慎用。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值