前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux之rsync数据同步服务

Linux之rsync数据同步服务

作者头像
星哥玩云
发布2022-07-14 09:49:52
1.5K0
发布2022-07-14 09:49:52
举报
文章被收录于专栏:开源部署开源部署

1.Rsync基本概述 Rsync是开源多功能同步工具,支持多种操作系统 Rsync支持本地复制(优于scp,cp)与远程同步 Rsync支持全量备份,增量备份 Rsync基于C/S架构,默认监听tcp873端口

2.Rsync优点 支持增量备份,第一次全量备份,第二次增量备份。边复制边比较边统计,传输效率高。 数据集中备份,客户端可以推送数据至服务端,也可以从服务端获取数据,与客户端为参照物。 保持文件属性,符号链接,硬链接,权限,时间等。 安全方式传输,Rsync本身不对数据加密,使用ssh作为传输端口。 指定排除文件,排除无需同步的文件或目录。 进程方式同步,rsync运行在C/S架构,通过进程方式传输文件或数据。

.缺点: 1.大量小文件同步会比较慢,需要比对时间较长,可能造成Rsync进程停止 解决思路:将小文件进行打包,然后再同步,减小比对时间,传输效率更高 2.同步大文件会出现中断情况,而且长时间同步会造成网络资源耗尽 解决思路:配置限速同步,未同步完之前修改为隐藏文件,同步完后修改为正常文件

Rsync命令格式: rsync [选项] 源文件 [user@]host::目录 rsync [选项] 源文件 rsync:// [user@]host:[:port]/目录 -a选项 :递归传输目录 :保持文件或目录的时间、文件属性、属组、时间、软链接 :显示同步的过程 :设备 -z选项 :传输时进行压缩以提高效率 -v选项 :详细模式输出,打印速率,文件数量等 -e选项 :使用的信道协议,指定替代rsh的shell程序 --delete 让目标目录和源目录数据保持一致 --partial 断点续传 --bwllimit=100 限速传输 --exclude-from=file 文件名所在的目录 --exclude=PATTERN 指定排除不需要传输的文件模式

rsync服务部署 环境: 两台主机:源服务器:192.168.56.11 目标服务器:192.68.56.12 需求: 把源服务器上的/etc目录实时同步到目标服务器的/tmp/目录下

在目标服务器上做以下操作:

1.关闭防火墙和selinux [root@linuxidc ~]# systemctl stop firewalld [root@linuxidc ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.Fedoraproject.FirewallD1.service. [root@linuxidc ~]# setenforce 0 [root@linuxidc ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux

2.安装rsync服务端软件 [root@linuxidc ~]# yum install rsync -y

3.配置rsync.conf配置文件

[root@linuxidc ~]# cat >> /etc/rsyncd.conf <<EOF

    log file = /var/log/rsyncd.log // 日志文件位置,启动rsync后自动产生     pidfile = /var/run/rsyncd.pid //pid文件存放位置     lock file = /var/run/rsync.lock //支持max connections参数的锁文件     secrets file = /etc/rsync.pass //用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件     [etc_from_client] //自定义同步名称     path = /heyuanjie/ //rsync服务端数据存放路径     ,客户端的数据将同步至此目录     comment = gaosiao     uid = root //设置rsync运行权限为root     gid = root //设置rsync运行权限为root     port = 873 //默认端口     ignore errors //表示出现错误忽略错误     use chroot = no // 默认为true,修改为no,增加对目录文件软连接的备份     read only = no //设置rsync服务端为读写权限     list = no //不显示rsync服务端资源列表     max connection= 200 //最大连接数     timeout = 600 //设置超时时间     auth users = admin //执行数据同步的用户名,可设置多个,用英文状态下逗号隔开     hosts allow = 192.168.56.11 //允许进行数据同步的客户端ip,可设置多个,逗号隔开     hosts deny = 192.168.1.1 //禁止数据同步的客户端IP地址     EOF

4.创建用户认证文件,并设置文件权限

[root@linuxidc ~]# echo 'admin:123456' > /etc/rsync.pass [root@linuxidc ~]# cat /etc/rsync.pass admin:123456 [root@linuxidc ~]# chmod 600 /etc/rsync [root@linuxidc ~]# ll /etc/rsync -rw-------. 1 root root 825 Aug 16 15:42 /etc/rsyncd.conf -rw-------. 1 root root 13 Aug 16 15:55 /etc/rsync.pass

5.启动rsync服务并加入开机自启

[root@linuxidc ~]# systemctl start rsyncd [root@linuxidc ~]# systemctl enable rsyncd Created symlink from /etc/systemd/system/multi user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service. [root@linuxidc ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :22 : LISTEN 0 100 127.0.0.1:25 : LISTEN 0 5 :873 : LISTEN 0 128 :::22 ::: LISTEN 0 100 ::1:25 ::: LISTEN 0 5 :::873 :::*

在源服务器上做以下操作: 1.关闭防火墙和selinux [root@hejie ~]# systemctl stop firewalld [root@hejie ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@hejie ~]# setenforce 0 [root@hejie ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux

2.配置yum源

[root@hejie yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo [root@hejie yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo [root@hejie yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo [root@hejie ~]# yum -y install epel-release

3.安装rsync服务端软件,只需安装,不要启动,不需要配置 [root@hejie ~]# yum -y install rsync

4.创建认证密码文件,并设置权限,设置文件所有者具有读取、写入权限即可 [root@hejie ~]# echo '123456' > /etc/rsync.pass [root@hejie ~]# cat /etc/rsync.pass 123456 [root@hejie ~]# chmod 600 /etc/rsync.pass [root@hejie ~]# ll /etc/rsync.pass

-rw-------. 1 root root 7 Aug 16 16:44 /etc/rsync.pass

5.在源服务器上创建测试目录,并运行以下命令

[root@hejie ~]# mkdir -pv /root/etc/test mkdir: created directory ‘/root/etc’ mkdir: created directory ‘/root/etc/test’ [root@hejie ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.56.12::etc_from_client --password-file=/etc/rsync.pass

6.运行完成后,在目标服务器上查看,在/heyuanjie目录下有test目录,说明数据同步成功

[root@linuxidc ~]# cd /heyuanjie [root@linuxidc heyuanjie]# ls test

7.安装inotify-tools工具,实时触发rsync进行同步   //查看服务器内核是否支持inotify

[root@hejie ~]# ll /proc/sys/fs/inotify/ total 0 -rw-r--r--. 1 root root 0 Aug 16 16:57 max_queued_events -rw-r--r--. 1 root root 0 Aug 16 16:57 max_user_instances -rw-r--r--. 1 root root 0 Aug 16 16:57 max_user_watches 如果有以上三个max开头的文件则表示服务器内核支持inotify //安装inotify-tools [root@hejie ~]# yum -y install make gcc gcc-c++ [root@hejie ~]# yum -y install inotify-tools

//写同步脚本,让脚本自动去检测我们制定的目录下文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去 [root@hejie ~]# mkdir /scripts [root@hejie ~]# touch /scripts/inotify.sh [root@hejie ~]# chmod 755 /scripts/inotify.sh [root@hejie ~]# vi /scripts/inotify.sh

host=192.168.56.12 //目标服务器的ip(备份服务器) src=/etc //在源服务器上所要监控的备份目录(可自定义,但必须存在) des=etc_from_client //自定义模块名,需要与目标服务器上定义的同步名称一致 password=/etc/rsync.pass //执行数据同步的密码文件 user=admin //执行数据同步的用户名 inotifywait=/usr/bin/inotifywait

$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \ | while read files ; do rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done

//启动脚本 [root@linuxidc ~]# nohup bash /scripts/inotify.sh & [1] 8784 [root@linuxidc ~]# nohup: ignoring input and appending output to ‘nohup.out’ [root@linuxidc ~]# ps -ef|grep inotify root 8784 8528 0 00:46 pts/0 00:00:00 bash /scripts/inotify.sh root 8785 8784 0 00:46 pts/0 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc root 8786 8784 0 00:46 pts/0 00:00:00 bash /scripts/inotify.sh root 8788 8528 0 00:47 pts/0 00:00:00 grep --color=auto inotify

//在源服务器上生成一个新文件 [root@linuxidc ~]# mkdir /etc/aaa 会触发同步到目标服务器上的/heyuanjie/etc中 [root@linuxidc ~]# cd /heyuanjie/ [root@linuxidc heyuanjie]# ls etc test [root@linuxidc heyuanjie]# cd etc/ [root@linuxidc etc]# ls aaa //查看inotify生成的日志,可以看到生成了一个aaa目录文件 [root@linuxidc ~]# tail /tmp/rsync.log 20180817 00:49 /etc/aaaCREATE,ISDIR was rsynced

设置脚本开机自启动: [root@linuxidc ~]# chmod +x /etc/rc.d/rc.local [root@linuxidc ~]# ll /etc/rc.d/rc.local -rwxr-xr-x. 1 root root 473 Aug 5 2017 /etc/rc.d/rc.local [root@linuxidc ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local [root@linuxidc ~]# tail -1 /etc/rc.d/rc.local

nohup /bin/bash /scripts/inotify.sh

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档