Rsync是Linux常用的一个软件,可以在本地计算机与远程计算机之间或者本地两个目录中进行文件的同步。与SCP不同,Rsync只传输文件有变动的部分,而不是每一次都传输全部文件。 Sersync是利用inotify和rsync两种技术来实现数据实时同步的功能。inotify用来监听所在服务器上文件变动,然后利用rsync将数据同步。
Rsync
Rsync安装
Rsync的安装很简单,利用yum就能安装好
yum -y install rsync
Rsync常用方法
与Linux其他命令一样,Rsync有众多的选项,但常用的选项为两个,-a、-v
-a是多个选项的集合,-v是现实传输信息。
实验环境
| 主机 |
|---|
| 192.168.1.15 |
| 192.168.1.20 |
在15中创建两个目录rsync_1、rsync_2来进行本地文件同步实验。
在20中创建rsync_1_1,来进行远程同步实验。
192.168.1.15
drwxr-xr-x 2 root root 6 10月 10 16:04 rsync_1
drwxr-xr-x 2 root root 6 10月 10 16:04 rsync_2
192.168.1.20
drwxrwxr-x 2 dhms dhms 6 10月 10 16:07 rsync_1_1
Rsync本地文件同步
将rsync_1中的1文件同步至rsync_2中
[root@E4-54-E8-DD-88-7D tmp]# rsync -av rsync_1/1 rsync_2/
sending incremental file list
1
sent 83 bytes received 35 bytes 236.00 bytes/sec
total size is 0 speedup is 0.00
[root@E4-54-E8-DD-88-7D tmp]# tree rsync_1 rsync_2
rsync_1
├── 1
├── 2
└── 3
rsync_2
└── 1
Rsync远程同步
将rsync_1中的文件和目录全部同步到rsync_1_1中
192.168.1.15
[root@E4-54-E8-DD-88-7D tmp]# tree rsync_1
rsync_1
├── 1
├── 2
├── 3
├── dir_1
├── dir_2
└── dir_3
192.168.1.20
rsync_1_1/
0 directories, 0 files
[root@E4-54-E8-DD-88-7D tmp]# rsync -av rsync_1/ dhms@192.168.1.20:/tmp/rsync_1_1
The authenticity of host '192.168.1.20 (192.168.1.20)' can't be established.
ECDSA key fingerprint is SHA256:8Sn8fKgE6Ia3SyTT8XYPd2Bm3c89zgw+t/3dXRd/apM.
ECDSA key fingerprint is MD5:de:04:d0:52:b6:56:c1:20:f0:a0:46:c3:6c:55:b9:b7.
Are you sure you want to continue connecting (yes/no)? yes #第一次连接所以需要输入yes确认
Warning: Permanently added '192.168.1.20' (ECDSA) to the list of known hosts.
dhms@192.168.1.20's password: #输入远程机用户密码
sending incremental file list
./
1
2
3
dir_1/
dir_2/
dir_3/
sent 287 bytes received 92 bytes 13.30 bytes/sec
total size is 0 speedup is 0.00
rsync -av rsync_1/ dhms@192.168.1.20:/tmp/rsync_1_1,其中rsync_1是本地要同步的文件,dhms为登录远程机所用的用户,192.168.1.20为远程机IP,/tmp/rsync_1_1是同步到远程机的目标文件地址。
[dhms@kvm1-20 tmp]$ tree rsync_1_1/
rsync_1_1/
├── 1
├── 2
├── 3
├── dir_1
├── dir_2
└── dir_3
如此,rsync_1中的所有文件和目录都被同步到rsync_1_1中。
rsync -av rsync_1 dhms@192.168.1.20:/tmp/rsync_1_1
注意此条命令与上一条命令的区别,源文件rsync_1后不带/,同步文件时,会将rsync_1也同步到rsync_1_1中。
[dhms@kvm1-20 tmp]$ tree rsync_1_1/
rsync_1_1/
└── rsync_1
├── 1
├── 2
├── 3
├── dir_1
├── dir_2
└── dir_3
4 directories, 3 files
同步rsync_1中除了1以外的文件
rsync -av rsync_1/ --exclude=1 dhms@192.168.1.20:/tmp/rsync_1_1
[dhms@kvm1-20 tmp]$ tree rsync_1_1/
rsync_1_1/
├── 2
├── 3
├── dir_1
├── dir_2
└── dir_3
3 directories, 2 files
rsync同步脚本,引用自linux.cn/article-569…
#!/bin/bash
# 将备份列表文件的路径保存到变量中
backupf='./bckup.txt'
# 输入一个提示信息
echo "Shell Script Backup Your Files / Directories Using rsync"
# 检查是否输入了目标服务器,如果为空就再次提示用户输入
while [ x$desthost = "x" ]; do
# 提示用户输入目标服务器地址并保存到变量
read -p "Destination backup Server : " desthost
# 结束循环
done
# 检查是否输入了目标文件夹,如果为空就再次提示用户输入
while [ x$destpath = "x" ]; do
# 提示用户输入目标文件夹并保存到变量
read -p "Destination Folder : " destpath
# 结束循环
done
# 逐行读取备份列表文件
for line in `cat $backupf`
# 对每一行都进行处理
do
# 显示要被复制的文件/文件夹名称
echo "Copying $line ... "
# 通过 rsync 复制文件/文件夹到目标位置
rsync -ar "$line" dhms@"$desthost":"$destpath"
# 显示完成
echo "DONE"
# 结束
done
Sersyn
环境
| 主机 | 角色 |
|---|---|
| 10.4.7.41 | 备份服务器 |
| 10.4.7.31 | 被备份服务器 |
Sersync安装
安装sersync首先下载sersync软件github.com/wsgzao/sers… 并移动到所需监视的服务器(10.4.7.31)上,并解压。
[root@localhost tools]# tree sersync-master
sersync-master
├── inotify-tools-3.14.tar.gz
├── README.md
├── rsync-3.1.1.tar.gz
└── sersync2.5.4_64bit_binary_stable_final.tar.gz
0 directories, 4 files
如上所示,解压sersync的压缩包
mv GNU-Linux-x86 /usr/local/sersync
mv confxml.xml ./conf
mv sersync2 ./bin/sersync
修改sersync配置
<localpath watch="/data/sersync"> 监视的文件
<remote ip="172.16.1.41" name="backup"/> rsync的服务目标以及模块
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> 密码文件以及rsync用户
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
将sersync加入环境变量
echo "export PATH=$PATH:/usr/local/sersync/bin" >> /etc/profile
开启sersync
sersync -d -r -o /usr/local/sersync/conf/confxml.xml
-o 指定sersync配置文件
验证
在10.4.7.31的/data/sersync文件中创建个修改目录,会自动同步到10.4.7.41的backup目录中。
10.4.7.31
10.4.7.41