上行 客户端同步到服务端
下行 服务端同步到客户端
rsync开源的快速备份的工具,一般是系统自带的
可以在不同主机之间同步整个目录树(目录)
在远程同步的任务中,负责发起rsync的叫做发起端,也就是服务端,负责响应的同步请求的,就是客户端
rsync的特点:
1、支持拷贝文件,链接文件等等
2、可以同步整个目录
3、可以支持保留源文件或者目录的权限等等
4、可以实现增量同步
同步方式:
1、完整备份
2、增量备份
常用的选项
1、-a 归档模式,保留权限
2、-v 显示同步的详细过程
3、-z 压缩,在传输的过程中对文件进行压缩
4、-H 同步硬链接
5、--delete 同步删除文件
6、-l 同步连接文件
7、-r 递归,所有
实验
192.168.124.10 服务端
192.168.124.20 客户端
服务端同步客户端
进入配置文件
vim /etc/rsyncd.conf
1 uid = root
2 gid = root
3 use chroot = yes
4 #是否禁锢在源目录
5 address = 192.168.124.10
6 #监听地址
7 port 873
8 pid file = /var/run/rsyncd.pid
9 log file = /var/log.rsyncd.log
10 hosts allow = 192.168.124.0/24
11 #允许访问的客户端的ip地址
12 dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z
13 #在传输过程中,不再进行压缩的文件类型
14 [test]
15 #共享模块的名称,后续通过模块的名称来进行同步
16 path = /opt/test
17 #源目录,就是同步的目录
18 comment = test
19 #备注信息
20 read only = no
21 #源目录,客户端可以读,也可以写
22 auth users = backuper
23 #授权登录的账户名称
24 secrets file = /etc/rsyncd_users.db
25 #授权登录用户的密码文件
创建一个密码文件
vim /etc/rsyncd_users.db
1 backuper:123456
给这个文件赋权,只能是600
chmod 600 /etc/rsyncd_users.db
创建源目录
mkdir /opt/test
给这个目录赋权
chmod 777 /opt/test/
启动rsync
systemctl restart rsyncd
查看一下端口
1 [root@test1 ~]# netstat -antp | grep 873
2 tcp 0 0 192.168.124.10:873 0.0.0.0:* LISTEN 20135/rsync
在test目录中写入文件
echo 123 > 123
echo 456 > 456
切换到客户端192.168.124.20
输入命令
1 rsync -avz backuper@192.168.124.10::test /opt
2 Password:
3 receiving incremental file list
4 ./
5 123
6 456
7 sent 65 bytes received 174 bytes 68.29 bytes/sec
8 total size is 8 speedup is 0.03
文件就完成同步了
实现免密同步,在客户端进行配置
1 echo "123456" > /etc/server.pass
2 chmod 600 /etc/server.pass
rsync -avz --password-file=/etc/server.pass
backuper@192.168.124.10::test /opt
这样就可以实现免密登录了
设置定时任务同步到客户端
crontab -e
30 22* * * /usr/bin/rsync -avz --password-file=/etc/server.pass backuper@192.168.124.10::test /opt
每晚的十点半同步
客户端同步到服务端
inotify-tools
inotify实现监控
inotify watch 监控文件系统的变化
inotify wait 监控修改,创建,移动,删除,属性修改(权限修改,所有者,所在
组)如果发生变动,立即输出结果。
安装inotify-tools包
1 [root@localhost inotify-tools-3.14]# ls
2 aclocal.m4 config.h.in COPYING libinotifytools man 3 src
4 AUTHORS config.sub depcomp ltmain.sh missing
5 ChangeLog configure INSTALL Makefile.am NEWS
6 config.guess configure.ac install-sh Makefile.in README
在opt目录下创建一个xy102这个目录
mkdir xy102
给这个目录赋权
chmod 777 xy102
inotifywait -mrg -e modify,create,move,delete /opt/xy102
m 持续监控
r递归整个目录 只要有变化包含子目录的变化全部记录
q 简化输出的信息
e 指定监控的时间
attrib属性修改
在客户端写一个脚本,能够让服务端同步到客户端
vim inotify.sh
1 #!/bin/bash
2 inotify_cmd="inotifywait -mrq -e modify,create,move,delete,attrib /opt/xy102"
3 rsync_cmd="rsync -azH --delete --password-file=/etc/server.pass /opt/xy102 backuper@192.168.124.10::test/"
4 $inotify_cmd | while read DIRECTORY EVENT FILE
5 do
6 if [ $(pgrep rsync | wc -l ) -le 0 ]
7 then
8 $rsync_cmd
9 fi
10 done
启动这个脚本
./inotify.sh
在客户端创建的文件都能同步到服务端了
delete的作用就是不管服务器之前里面有多少数据,都得和客户端得数据一致,不一致的全部删除。