简介
rsync 是 Linux 系统下的数据镜像备份系统. 可以远程同步, 支持本地复制, 或者与其他 SSH,rsync 主机同步.
特性
同步小文件
可以镜像保存整个目录树和文件系统
无需特殊权限即可安装
快速: 第一次同步会复制全部内容, 但下一次只传输修改过的文件; 传输过程可以实行压缩解压缩, 占用带宽少
安全: 可以使用 scp,SSH. 也可以直接 socket
支持匿名传输 (已做好互信), 方便网站同步
- // rsync 的命令格式常用的有一下三种:
- rsync [OPTION]... SRC DEST(源地址)
- // 拷贝本地文件至本地
- [root@system1 ~]# ls
- anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
- Desktop Downloads Music Public Videos
- [root@system1 ~]# rsync -avz anaconda-ks.cfg rsync-copy
- sending incremental file list
- anaconda-ks.cfg
- sent 995 bytes received 31 bytes 2052.00 bytes/sec
- total size is 1614 speedup is 1.57
- [root@system1 ~]# ls
- anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures rsync-copy
- rsync [OPTION]... SRC [USER@]HOST:DEST
- // 拷贝本地文件至远程主机
- [root@system1 etc]# rsync -avz passwd root@172.16.30.254:/tmp/
- root@172.16.30.254's password:
- sending incremental file list
- passwd
- sent 830 bytes received 31 bytes 114.80 bytes/sec
- total size is 1968 speedup is 2.29
- rsync [OPTION]... [USER@]HOST:SRC DEST
- // 拷贝远程主机文件至本地
- [root@system1 ~]# rsync -avz root@172.16.30.254:/etc/yum.repos.d /root
- root@172.16.30.254's password:
- receiving incremental file list
- yum.repos.d/
- yum.repos.d/iso.repo
- sent 34 bytes received 167 bytes 44.67 bytes/sec
- total size is 61 speedup is 0.30
- [root@system1 ~]# ls
- anaconda-ks.cfg Downloads Pictures Templates
- Desktop initial-setup-ks.cfg Public Videos
- Documents Music rsync-copy yum.repos.d
- //rsync 常用选项:
- -a, --archive // 归档
- -v, --verbose // 啰嗦模式
- -q, --quiet // 静默模式
- -r, --recursive // 递归
- -p, --perms // 保持原有的权限属性
- -z, --compress // 在传输时压缩, 节省带宽, 加快传输速度
- --delete // 在源服务器上做的删除操作也会在目标服务器上同步
- rsync+inotify
在目标服务器上做以下操作:
- // 关闭防火墙与 SELINUX
- [root@ldap ~]# systemctl stop firewalld
- [root@ldap ~]# systemctl disable firewalld
- [root@ldap ~]# getenforce
- Enforcing
- [root@ldap ~]# setenforce 0
- [root@ldap ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
- // 安装 rsync 服务端软件
- [root@ldap ~]# yum -y install rsync
- Loaded plugins: product-id, search-disabled-repos, subscription-manager
- This system is not registered with an entitlement server. You can use subscription-manager to register.
- myrepo | 4.1 kB 00:00
- (1/2): myrepo/group_gz | 137 kB 00:00
- (2/2): myrepo/primary_db | 4.0 MB 00:00
- Resolving Dependencies
- --> Running transaction check
- ---> Package rsync.x86_64 0:3.0.9-18.el7 will be installed
- ......
- myrepo/productid | 1.6 kB 00:00
- Verifying : rsync-3.0.9-18.el7.x86_64 1/1
- Installed:
- rsync.x86_64 0:3.0.9-18.el7
- Complete!
- // 设置 rsyncd.conf 配置文件
- root@ldap ~]# 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 // 用户认证配置文件, 里面保存用户名称和密码, 必须手动创建这个文件
- [etcfromclient] // 自定义同步名称
- path = /tmp/ //rsync 服务端数据存放路径, 客户端的数据将同步至此目录
- comment = sync etc from client
- 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 connections = 200 // 最大连接数
- timeout = 600 // 设置超时时间
- auth users = admin // 执行数据同步的用户名, 可以设置多个, 用英文状态下逗号隔开
- hosts allow = 172.16.30.254 // 允许进行数据同步的客户端 IP 地址, 可以设置多个, 用英文状态下逗号隔开
- hosts deny = 192.168.1.1 // 禁止数据同步的客户端 IP 地址, 可以设置多个, 用英文状态下逗号隔开
- EOF
- // 创建用户认证文件
- [root@ldap ~]# echo 'admin:123456' > /etc/rsync.pass
- [root@ldap ~]# cat /etc/rsync.pass
- admin:123456
- // 设置文件权限
- [root@ldap ~]# chmod 600 /etc/rsync[root@ldap ~]# ll /etc/rsync
- -rw-------. 1 root root 802 Aug 9 22:45 /etc/rsyncd.conf
- -rw-------. 1 root root 13 Aug 9 22:47 /etc/rsync.pass
- // 启动 rsync 服务并设置开机自启动
- [root@ldap ~]# systemctl start rsyncd
- [root@ldap ~]# systemctl enable rsyncd
- Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
- [root@ldap ~]# 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 :::*
- ![](https://s1.51cto.com/images/blog/201902/19/2a97b5ca61ccff2d055d084474f70815.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
** 在原服务器上 **
- // 关闭防火墙和 SELINUX
- [root@system1 ~]# systemctl stop firewalld
- [root@system1 ~]# systemctl disable firewalld
- rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
- rm '/etc/systemd/system/basic.target.wants/firewalld.service'
- [root@system1 ~]# getenforce
- Permissive
- [root@system1 ~]# yum -y install rsync// 安装 rsync
- // 创建认证密码文件
- [root@system1 ~]# echo '123456'> /etc/rsync.pass
- // 设置文件权限, 只设置文件所有者具有读取, 写入权限
- [root@system1 ~]# chmod 600 /etc/rsync.pass
- [root@system1 ~]# ll /etc/rsync.pass
- -rw-------. 1 root root 7 Feb 19 19:59 /etc/rsync.pass
- // 在源服务器上创建测试目录, 然后在源服务器运行
- [root@system1 ~]# mkdir -pv /root/etc/test
- mkdir: created directory '/root/etc'
- mkdir: created directory '/root/etc/test'
- [root@system1 ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@172.16.30.254::etc_from_client --password-file=/etc/rsync.pass
- sending incremental file list
- ./
- deleting systemd-private-ySPPll/tmp/
- deleting systemd-private-ySPPll/
- ...
- ![](https://s1.51cto.com/images/blog/201902/20/951ffb3acbe823abb1d5f92ce2ca8f2d.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
- // 安装 inotify-tools 工具, 实时触发 rsync 进行同步
- // 查看服务器内核是否支持 inotify
- [root@system1 ~]# ll /proc/sys/fs/inotify/
- total 0
- -rw-r--r-- 1 root root 0 Aug 10 11:19 max_queued_events
- -rw-r--r-- 1 root root 0 Aug 10 11:19 max_user_instances
- -rw-r--r-- 1 root root 0 Aug 10 11:19 max_user_watches
- // 如果有这三个 max 开头的文件则表示服务器内核支持 inotify
- // 安装 inotify-tools
- [root@system1 ~]# yum -y install make gcc gcc-c++
- ....
- [root@system1 ~]# yum -y install inotify-tools
- ....
- // 写同步脚本, 此步乃最最重要的一步, 请慎之又慎. 让脚本自动检测我们制定的目录下文件发生的变化, 然后再执行 rsync 的命令把它同步到我们的服务器端去
- [root@system1 ~]# mkdir /scripts
- [root@system1 ~]# touch /scripts/inotify.sh
- [root@system1 ~]# chmod 755 /scripts/inotify.sh
- [root@system1 ~]# ll /scripts/inotify.sh
- -rwxr-xr-x 1 root root 0 Aug 10 13:02 /scripts/inotify.sh
- [root@system1 ~]# VIM /scripts/inotify.sh
- host=172.16.30.254 // 目标服务器的 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@system1 ~]# nohup bash /scripts/inotify.sh &
- [1] 86871
- [root@system1 ~]# nohup: ignoring input and appending output to 'nohup.out'
- [root@system1 ~]# ps -ef|grep inotify
- root 86871 2143 0 14:52 pts/0 00:00:00 bash /scripts/inotify.sh
- root 86872 86871 0 14:52 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 86873 86871 0 14:52 pts/0 00:00:00 bash /scripts/inotify.sh
- root 86875 2143 0 14:52 pts/0 00:00:00 grep --color=auto inotify
- rsync
来源: http://www.bubuko.com/infodetail-2961192.html