RSYNC概述
Sync同步:刷新文件系统缓存,强制将修改过的数据块写入磁盘,并且更新超级快
Async异步:将数据先放到缓冲区,再周期性(一般是30s)的去同步到磁盘
Rsync远程同步:remote syncnronous
数据同步过程:sync数据同步 => 保存文件(目标) => 强制把缓存的数据写入磁盘(立即保存),实时性要求比较高的场景
Async异步 => 保存文件(目标) =>
在加目录中创建一些文件,将文件同步到opt下
repolist: 9,911
[root@localhost ~]# touch folder/f1/file{0..4}
[root@localhost ~]# which rsync
/usr/bin/rsync
[root@localhost ~]# # folder目录下的文件传到opt
[root@localhost ~]# rsync -av folder/ /opt/
sending incremental file list
./
f1/
f1/file0
f1/file1
f1/file2
f1/file3
f1/file4
f2/
f3/
sent 401 bytes received 130 bytes 1,062.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls /opt/
f1 f2 f3
[root@localhost ~]# rsync -av folder /opt/
sending incremental file list
folder/
folder/f1/
folder/f1/file0
folder/f1/file1
folder/f1/file2
folder/f1/file3
folder/f1/file4
folder/f2/
folder/f3/
sent 412 bytes received 131 bytes 1,086.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls /opt/
f1 f2 f3 folder
[root@localhost ~]# # 不加/,就会将folder整个目录同步到/opt下
[root@localhost ~]# rm -rf /opt/*
[root@localhost ~]# ls /opt/
[root@localhost ~]# rsync -av folder/ /opt/
sending incremental file list
./
f1/
f1/file0
f1/file1
f1/file2
f1/file3
f1/file4
f2/
f3/
sent 401 bytes received 130 bytes 1,062.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# rm -rf /opt/*
[root@localhost ~]# rsync -av folder/ /opt/
sending incremental file list
./
f1/
f1/file0
f1/file1
f1/file2
f1/file3
f1/file4
f2/
f3/
sent 401 bytes received 130 bytes 1,062.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# rsync -avR folder/ /opt/
sending incremental file list
folder/
folder/f1/
folder/f1/file0
folder/f1/file1
folder/f1/file2
folder/f1/file3
folder/f1/file4
folder/f2/
folder/f3/
sent 412 bytes received 131 bytes 1,086.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls /opt/
f1 f2 f3 folder
[root@localhost ~]# # 现在不传到/opt
[root@localhost ~]# rsync -av folder/f1/ folder/f2/
sending incremental file list
./
file0
file1
file2
file3
file4
sent 314 bytes received 114 bytes 856.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# touch folder/f1/file5
[root@localhost ~]# ls folder/f1
file0 file1 file2 file3 file4 file5
[root@localhost ~]# ls folder/f2
file0 file1 file2 file3 file4
[root@localhost ~]# rsync -av folder/f1/ folder/f2/
sending incremental file list
./
file5
sent 179 bytes received 38 bytes 434.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls folder/f2
file0 file1 file2 file3 file4 file5
[root@localhost ~]# ls folder/f1
file0 file1 file2 file3 file4 file5
[root@localhost ~]# rm -rf folder/f1/file0
[root@localhost ~]# ls folder/f1
file1 file2 file3 file4 file5
[root@localhost ~]# ls folder/f2
file0 file1 file2 file3 file4 file5
[root@localhost ~]# rsync -av folder/f1/ folder/f2/
sending incremental file list
./
sent 127 bytes received 19 bytes 292.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls folder/f2
file0 file1 file2 file3 file4 file5
[root@localhost ~]# rsync -av -delete folder/f1/ folder/f2/
sending incremental file list
sent 120 bytes received 12 bytes 264.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls folder/f2
file0 file1 file2 file3 file4 file5
[root@localhost ~]# # rsync语法:
[root@localhost ~]# #rsync [选项] 原数据位置 目标位置
[root@localhost ~]# # 文件的增加会同步
[root@localhost ~]# vim folder/f1/file1
[root@localhost ~]# cat folder/f1/file1
大家好我是渣渣辉
[root@localhost ~]# # 修改了f1中的内容,f2目录中没有发生改变
[root@localhost ~]# rsync -av --delete folder/f1/ folder/f2/
sending incremental file list
deleting file0
./
file1
sent 199 bytes received 47 bytes 492.00 bytes/sec
total size is 25 speedup is 0.10
[root@localhost ~]# cat folder/f2/file1
大家好我是渣渣辉
[root@localhost ~]# # 文件的修改会被rsync同步
[root@localhost ~]# touch folder/f1/file0 -m -d "2014-7-14 00:00"
[root@localhost ~]# rsync -av --delete folder/f1/ folder/f2/
sending incremental file list
./
file0
sent 181 bytes received 38 bytes 438.00 bytes/sec
total size is 25 speedup is 0.11
[root@localhost ~]# ls -l folder/f1/file0
-rw-r--r--. 1 root root 0 7月 14 2014 folder/f1/file0
[root@localhost ~]# chmod g+w folder/f1/file0
[root@localhost ~]# ls -l folder/f1/file0
-rw-rw-r--. 1 root root 0 7月 14 2014 folder/f1/file0
[root@localhost ~]# rsync -av --delete folder/f1/ folder/f2/
sending incremental file list
sent 146 bytes received 19 bytes 330.00 bytes/sec
total size is 25 speedup is 0.15
[root@localhost ~]# ls -l folder/f2/file0
-rw-rw-r--. 1 root root 0 7月 14 2014 folder/f2/file0
[root@localhost ~]# # 同步文件内容的修改,文件的删除,以及文件的属性的修改
[root@localhost ~]# # 向web1主机的/tmp目录同步数据、
[root@localhost ~]# rsync -av folder/ root@192.168.17.99 :/tmp/
[root@localhost ~]# # 要实现远程同步,对面主机也要安装rsync
[root@localhost ~]# rsync -av root@192.168.10.47:/tmp/lajiwenjian /tmp/
[root@localhost ~]# # 本地同步
[root@localhost ~]# # 远程同步
[root@localhost ~]# systemctl status rsyncd.s
Unit rsyncd.s.service could not be found.
[root@localhost ~]# netstat -lntup | grep rsync
[root@localhost ~]# systemctl start rsyncd.s
Failed to start rsyncd.s.service: Unit not found.
[root@localhost ~]# systemctl start rsyncd
[root@localhost ~]# find / -name "rsync"
/etc/selinux/targeted/active/modules/100/rsync
/usr/bin/rsync
[root@localhost ~]# find / -name "rsync*conf"
/etc/rsyncd.conf
[root@localhost ~]# vim /etc/rsyncd.conf
[root@localhost ~]# mkdir -p /app/studentsweb/src/main/java/co/yuanyu.studebtweb/
[root@localhost ~]# yum -y install tree.x86_64
[root@localhost ~]# tree /app/
/app/
└── studentsweb
└── src
└── main
└── java
└── co
└── yuanyu.studebtweb
6 directories, 0 files
[root@localhost ~]# touch /app/studentsweb/src/main/java/co/yuanyu.studebtweb/file{0..9}.java
[root@localhost ~]# tree /app/
/app/
└── studentsweb
└── src
└── main
└── java
└── co
└── yuanyu.studebtweb
├── file0.java
├── file1.java
├── file2.java
├── file3.java
├── file4.java
├── file5.java
├── file6.java
├── file7.java
├── file8.java
└── file9.java
6 directories, 10 files
[root@localhost ~]# vim /etc/rsyncd.conf
[root@localhost ~]# cat /etc/rsyncd.conf | tail -2
path=/app/studentsweb/
log file=/var/log/rsync.log
[root@localhost ~]# systemctl restart rsyncd
[root@localhost ~]# # 在桥主机提供了一个针对app/下的项目rsync服务
[root@localhost ~]# # 下次服务直接找服务
[root@localhost ~]# tree /app/
/app/
└── studentsweb
└── src
└── main
└── java
└── co
└── yuanyu.studebtweb
├── file0.java
├── file1.java
├── file2.java
├── file3.java
├── file4.java
├── file5.java
├── file6.java
├── file7.java
├── file8.java
└── file9.java
6 directories, 10 files