Linux文件同步工具-rsync

安装包


yum install -y rsync

rsync常用选项

-a:归档模式,表示递归方式传输文件,并保持所有属性;通-rlptgoD;
-r:同步目录时要加上,类似cp时加R;
-v:同步时显示一些信息,让我们知道同步国创;
-l:保留软链接;
-L:同步软链接时会把源文件给同步;
-p:保持文件权限属性;
-o:保持文件的属主;
-g:保持文件的属组;
-D:保持设备文件信息;
-t:保持文件的时间属性;
--delte:删除DEST中SRC没有的文件;
--exclude:过滤指定文件,如--exclude “logs”会把文件log过滤掉,不同步;
-P:显示同步过程,比如速率,比-v更加详细;
-u:如果DEST中的文件比SRC新,则不同步;
-z:测试时压缩;

备份

rsync -av /etc/passwd /tmp/1.txt
将/etc/passwd备份到tmp/1.txt文件中

[root@shu-test ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd
sent 1229 bytes  received 31 bytes  2520.00 bytes/sec
total size is 1155  speedup is 0.92
[root@shu-test ~]# ls /tmp/
1.txt

备份到远程计算机

rsync -av /etc/passwd root@192.168.188.2:/tmp/2.txt
将/etc/passwd文件同步备份到192.168.188.2机器的/tmp/2.txt

[root@shu-test ~]# rsync -av /etc/passwd root@192.168.188.2:/tmp/2.txt
The authenticity of host '192.168.188.2 (192.168.188.2)' can't be established.
ECDSA key fingerprint is SHA256:vl9780juEAW/PspOqj/J4E4yHiQtIG6ZQ2R9S1OAdb8.
ECDSA key fingerprint is MD5:f8:71:01:1c:c1:71:88:97:d8:ca:25:88:79:45:17:1b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.188.2' (ECDSA) to the list of known hosts.
root@192.168.188.2's password:
sending incremental file list
passwd
sent 1229 bytes  received 31 bytes  168.00 bytes/sec
total size is 1155  speedup is 0.92
[root@shu-test ~]# ls /tmp/
1.txt
2.txt
[root@shu-test ~]#

备份到远程计算机(ssh被修改端口情况下)

rsync -avP -e "ssh -p 22" /root/abc/ root@192.168.188.3:/tmp/test2/

[root@shu-test tmp]# rsync -avP -e "ssh -p 22" /root/abc/ root@192.168.188.3:/tmp/test2/
root@192.168.188.3's password:
sending incremental file list
sent 365 bytes  received 19 bytes  109.71 bytes/sec
total size is 255434  speedup is 665.19
[root@shu-test tmp]#

注意:备份到远程服务器上,双方服务器都必须安装rsync包,才能备份;

同步文件夹

rsync -av /root/abc/ /tmp/111_dest/

[root@shu-test ~]# rsync -av /root/abc/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
1.txt
2.txt
3.txt
ip.txt
a/
a/1.txt
a/2.txt
a/b/
a/b/2.txt
a/b/c/
a/k/
a/k11/
a/s/
test/
test/1.txt
test/2.txt
test/a.txt
sent 256259 bytes  received 233 bytes  512984.00 bytes/sec
total size is 255434  speedup is 1.00
[root@shu-test ~]# cd /tmp/
[root@shu-test tmp]# ls
111_dest  systemd-private-603500798f13486a90f57d6e2dd53912-chronyd.service-UCHrHd
1.txt     systemd-private-603500798f13486a90f57d6e2dd53912-vgauthd.service-UNh9hx
2.txt     systemd-private-603500798f13486a90f57d6e2dd53912-vmtoolsd.service-JJIguS
[root@shu-test tmp]#

删除备份目录中不属于源的文件

rsync -av --delete /root/abc/ /tmp/111_dest/
将/root/abc/ 文件夹中的文件备份到/tmp/111_dest/目录下,
并删除/tmp/111_dest/中不属于/root/abc/的文件;

[root@shu-test 111_dest]# touch new.txt
[root@shu-test 111_dest]# ls
1.txt  2.txt  3.txt  a  ip.txt  new.txt  test
[root@shu-test 111_dest]# rsync -av --delete /root/abc/ /tmp/111_dest/
sending incremental file list
./
deleting new.txt
sent 354 bytes  received 22 bytes  752.00 bytes/sec
total size is 255434  speedup is 679.35
[root@shu-test 111_dest]# ls
1.txt  2.txt  3.txt  a  ip.txt  test
[root@shu-test 111_dest]#

过滤指定文件不同步

rsync -av --exclude ".123" /root/abc/ /tmp/111_dest/
备份/root/abc/到/tmp/111_dest/,但是不把
.123的文件备份过去;

[root@shu-test abc]# ls
1.txt  2.txt  3.txt  a  a.123  ip.txt  test
[root@shu-test abc]# cat a.123
[root@shu-test abc]# rsync -av --exclude "*.123" /root/abc/ /tmp/111_dest/
sending incremental file list
./
sent 354 bytes  received 22 bytes  752.00 bytes/sec
total size is 255434  speedup is 679.35
[root@shu-test abc]# cd /tmp/111_dest/
[root@shu-test 111_dest]# ls
1.txt  2.txt  3.txt  a  ip.txt  test
[root@shu-test 111_dest]#
[root@shu-test 111_dest]# ls /root/abc/
1.txt  2.txt  3.txt  a  a.123  ip.txt  test
[root@shu-test 111_dest]#

保护备份后的文件中的新文件

rsync -avu /root/abc/ /tmp/111_dest/
如果/tmp/111_dest/中有比源文件新的文件,则保护新文件不被同步干掉;
并将/root/abc/ 同步到/tmp/111_dest/;

[root@shu-test abc]# cd /tmp/111_dest/
[root@shu-test 111_dest]# touch new.new
[root@shu-test 111_dest]# rsync -avu /root/abc/ /tmp/111_dest/
sending incremental file list
./
sent 368 bytes  received 22 bytes  780.00 bytes/sec
total size is 255434  speedup is 654.96
[root@shu-test 111_dest]# ls
1.txt  2.txt  3.txt  a  a.123  ip.txt  new.new  test
[root@shu-test 111_dest]# ls /root/abc/
1.txt  2.txt  3.txt  a  a.123  ip.txt  test
[root@shu-test 111_dest]#

同步到其他机器方法

通过ssh的方法

需要输入密码
rsync -avL /root/abc/ root@192.168.188.3:/tmp/test2/
或者
rsync -avL root@192.168.188.3:/tmp/test2/ /root/abc/

通过后台服务的方法

编辑配置文件

/etc/rsyncd.conf

添加文件内容

port=873        //监听端口873,默认为873端口;
log file=/var/log/rsync.log        //指定日志文件
pid file=/var/run/rsyncd.pid        //指定pid文件
address=192.168.188.2        //指定启动rsyncd服务的ip,默认为全部ip上启动;
[test]        //指定模块名,自定义;
path=/root/rsync        //指定数据存放路径
use chroot=true        //安全仿佛,默认为true,如果有软链接文件建议设置为false;
max connections=4        //指定最大连接数,默认为0,没有限制;
read only=no        //如果为true,则不能上传到该模块指定目录
list=true        //
uid=root        //指定传输时使用哪个用户名进行
gid=root        //指定传输时使用哪个用户组
auth users=test        //指定传输时要使用哪个用户名
secrets file=/etc/rsyncd.passwd    //指定密码文件,文件权限为600,格式:用户名:密码
hosts allow=192.168.133.132 1.1.1.1 2.2.2.2  192.168.133.0/24        //

注意:

必须创建目录/tmp/rsync、也就是path=/tmp/rsync
还有同步格式中的::后面的test表示为服务器自定义的模块名;
如果指定使用用户名,必须创建好密码文件、文件权限为600、内容格式为用户名:密码;例如user:user123
hosts allow表示允许访问服务器的ip 或ip段;
使用之前最好关闭firewalld
systemctl stop firewalld
systemctl disable firewalld

启动服务

rsync --daemon

查看本机开启的端口

netstat -lnpt

[root@shu-test ~]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      805/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      890/master          
tcp        0      0 192.168.188.2:873       0.0.0.0:*               LISTEN      1075/rsync          
tcp6       0      0 :::22                   :::*                    LISTEN      805/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      890/master          
[root@shu-test ~]#

备份本机文件到服务器

rsync -avP /tmp/shu.test 192.168.188.2::test/shu-test.txt
在测试机器上将/tmp/shu.test文件备份到192.168.188.2服务器定义的目录下保存为shu-test.txt
定义目录为test模块也就是/tmp/rsync/目录下

[root@shu001 ~]# rsync -avP /tmp/shu.test 192.168.188.2::test/shu-test.txt
sending incremental file list
shu.test
        1155 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)
sent 1227 bytes  received 27 bytes  2508.00 bytes/sec
total size is 1155  speedup is 0.92
[root@shu001 ~]# ssh 192.168.188.2
Last login: Wed Jan 31 22:15:41 2018 from 192.168.188.1
[root@shu-test ~]# ls /tmp/rsync/
shu-test.txt
[root@shu-test ~]# exit
登出

备份服务器文件到本机

rsync -avP 192.168.188.2::test/shu-test.txt /tmp/abcdefg.test
将服务器上的文件备份到本机/tmp/目录下保存为abcdefg.test文件

[root@shu001 ~]# rsync -avP 192.168.188.2::test/shu-test.txt /tmp/abcdefg.test
receiving incremental file list
shu-test.txt
        1155 100%    1.10MB/s    0:00:00 (xfer#1, to-check=0/1)
sent 45 bytes  received 1262 bytes  2614.00 bytes/sec
total size is 1155  speedup is 0.88
[root@shu001 ~]# ls /tmp/
2.txt         systemd-private-e48a1f77672c4be982015a3e9b0c9d7f-chronyd.service-12q8Oj   test2
abcdefg.test  systemd-private-e48a1f77672c4be982015a3e9b0c9d7f-vgauthd.service-zesl7A
shu.test      systemd-private-e48a1f77672c4be982015a3e9b0c9d7f-vmtoolsd.service-9vvBzS
[root@shu001 ~]#

使用指定端口备份文件

rsync服务器指定端口为8730,本机不开放8730端口,那么就需要加上port命令来实现;
(使用rsync命令默认端口为873端口,一旦服务器指定端口改变,就必须加上port来备份)

rsync -avP --port 8730 192.168.188.2::test/shu-test.txt /tmp/abc.test

[root@shu001 ~]# rsync -avP --port 8730 192.168.188.2::test/shu-test.txt /tmp/abc.test
receiving incremental file list
shu-test.txt
        1155 100%    1.10MB/s    0:00:00 (xfer#1, to-check=0/1)
sent 45 bytes  received 1262 bytes  871.33 bytes/sec
total size is 1155  speedup is 0.88
[root@shu001 ~]# ls /tmp/
2.txt         systemd-private-e48a1f77672c4be982015a3e9b0c9d7f-chronyd.service-12q8Oj
abcdefg.test  systemd-private-e48a1f77672c4be982015a3e9b0c9d7f-vgauthd.service-zesl7A
abc.test      systemd-private-e48a1f77672c4be982015a3e9b0c9d7f-vmtoolsd.service-9vvBzS
shu.test      test2
[root@shu001 ~]#