linux文件同步工具- rsync

rsync 文件同步工具

rsync命令是一个远程数据同步工具,可经过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不一样部分,而不是每次都整份传送,所以速度至关快。 rsync是一个功能很是强大的工具,其命令也有不少功能特点选项,咱们下面就对它的选项一一进行分析说明。python

格式

  • rsync [OPTION]... SRC DEST
  • rsync [OPTION]... SRC [USER@]host:DEST
  • rsync [OPTION]... [USER@]HOST:SRC DEST
  • rsync [OPTION]... [USER@]HOST::SRC DEST
  • rsync [OPTION]... SRC [USER@]HOST::DEST
  • rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

选项

  • -a 包含-rtplgoD
  • -r 同步目录时要加上,相似cp时的-r选项
  • -v 同步时显示一些信息,让咱们知道同步的过程
  • -l 保留软链接
  • -L 加上该选项后,同步软链接时会把源文件给同步
  • -p 保持文件的权限属性
  • -o 保持文件的属主
  • -g 保持文件的属组
  • -D 保持设备文件的信息
  • -t 保持文件的时间属性
  • --delete 删除SEDT中SRC没有的文件
  • --exclude过滤指定文件,如--exclude "logs"会把文件名包含logs的文件或者目录过滤掉,不一样步
  • -P 显示同步过程,好比速率,比-v更加详细
  • -u 加上该选项后,若是DEST中的文件比SRC新,则不一样步
  • -z 传输时压缩

举例说明

  • 建立目录test
  • 在目录test中建立文件1.txt>内容111,222,33三、 软链接文件xuexi.sorft
[root@xuexi-001 ~]# mkdir test
[root@xuexi-001 test]# vi 1.txt 
[root@xuexi-001 test]# cat 1.txt 
111


222



333
[root@xuexi-001 test]# touch xuexi.txt 
[root@xuexi-001 test]# ln -s xuexi.txt xuexi.sorft
[root@xuexi-001 test]# ls
1.txt  xuexi.sorft  xuexi.txt
[root@xuexi-001 test]# echo "aaa" > xuexi.txt 
[root@xuexi-001 test]# cat xuexi.txt 
aaa
  • 将test目录 同步到/tmp/下并更名test2
[root@xuexi-001 test]# rsync -av /root/test/  /tmp/test2
sending incremental file list
created directory /tmp/test2
./
1.txt
xuexi.sorft -> xuexi.txt
xuexi.txt

sent 241 bytes  received 93 bytes  668.00 bytes/sec
total size is 31  speedup is 0.09
  • 加上L 后同步时 选项a 里面包含的小写l做用将失效
[root@xuexi-001 test]# rm -rf /tmp/test2/*
[root@xuexi-001 test]# rsync -avL /root/test/  /tmp/test2
sending incremental file list
./
1.txt
xuexi.sorft // 此时这边同步的是一个文件并不是时软链接文件。
xuexi.txt

sent 267 bytes  received 76 bytes  686.00 bytes/sec
total size is 26  speedup is 0.08
[root@xuexi-001 test]# ls -l /tmp/test2/
总用量 12
-rw-r--r-- 1 root root 18 6月  18 23:19 1.txt
-rw-r--r-- 1 root root  4 6月  18 23:21 xuexi.sorft
-rw-r--r-- 1 root root  4 6月  18 23:21 xuexi.txt
  • 在/tmp/test2/目录中新增一个文件new.txt ,在同步的时候加上 --delete 删除test2中test没有的文件
[root@xuexi-001 test]# touch /tmp/test2/new.txt
[root@xuexi-001 test]# rsync -av --delete  /root/test/ /tmp/test2/
sending incremental file list
deleting new.txt
./
1.txt
xuexi.sorft -> xuexi.txt
xuexi.txt

sent 241 bytes  received 71 bytes  624.00 bytes/sec
total size is 31  speedup is 0.10
  • 在同步目录test2时,不想同步包含“.txt”的文件时可使用 --exclude过滤指定文件
[root@xuexi-001 ~]# ls -l /root/test
总用量 8
-rw-r--r-- 1 root root 18 6月  18 23:19 1.txt
lrwxrwxrwx 1 root root  9 6月  18 23:20 xuexi.sorft -> xuexi.txt
-rw-r--r-- 1 root root  4 6月  18 23:21 xuexi.txt
[root@xuexi-001 ~]# rsync -av --exclude "*.txt" /root/test/ /tmp/test2/ 
sending incremental file list
./
xuexi.sorft -> xuexi.txt

sent 86 bytes  received 22 bytes  216.00 bytes/sec
total size is 9  speedup is 0.08
[root@xuexi-001 ~]# ls -l /tmp/test2/
总用量 0
lrwxrwxrwx 1 root root 9 6月  18 23:20 xuexi.sorft -> xuexi.txt // 由于软链接的源文件没有同步过来,因此这边显示的文件为错误的软链接。
  • -P 显示同步过程,好比速率,比-v更加详细
[root@xuexi-001 ~]# rsync -avP /root/test/ /tmp/test2/
sending incremental file list
./
1.txt
             18 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=2/4)
xuexi.sorft -> xuexi.txt
xuexi.txt
              4 100%    3.91kB/s    0:00:00 (xfr#2, to-chk=0/4)

sent 241 bytes  received 60 bytes  602.00 bytes/sec
total size is 31  speedup is 0.10
  • 若是目标test2目录的文件内容发生改变,不想被源文件同步时覆盖须要加上-u选项: 加上该选项后,若是test2目录中的文件比test新,则不一样步
[root@xuexi-001 ~]# vi /tmp/test2/1.txt 
111
aaa
bbb
ccc


222



333
[root@xuexi-001 ~]# cat !$
cat /tmp/test2/1.txt
111
aaa
bbb
ccc

 
222



333
[root@xuexi-001 ~]# cat /root/test/1.txt 
111

 
222



333
[root@xuexi-001 ~]# rsync -avPu /root/test/ /tmp/test2/
sending incremental file list
./

sent 130 bytes  received 19 bytes  298.00 bytes/sec
total size is 31  speedup is 0.21
[root@xuexi-001 ~]# cat /tmp/test2/1.txt 
111
aaa
bbb
ccc

 
222



333
[root@xuexi-001 ~]# cat /root/test/1.txt 
111

 
222



333

rsync 经过ssh方式同步

  • rsync -av /root/test/ 192.168.5.132:/root/test2

192.168.5.130机器算法

[root@xuexi-001 ~]# rsync -av /root/test/ 192.168.5.132:/root/test2/
root@192.168.5.132's password: 
sending incremental file list
created directory /root/test2
./
1.txt
xuexi.sorft -> xuexi.txt
xuexi.txt

sent 241 bytes  received 94 bytes  95.71 bytes/sec
total size is 31  speedup is 0.09

192.168.5.132机器安全

[root@localhost ~]# ls 
anaconda-ks.cfg  test1  test2
[root@localhost ~]# ls -l test2
总用量 8
-rw-r--r-- 1 root root 18 6月  18 23:19 1.txt
lrwxrwxrwx 1 root root  9 6月  18 23:20 xuexi.sorft -> xuexi.txt
-rw-r--r-- 1 root root  4 6月  18 23:21 xuexi.txt
  • rsync -av -e "ssh -p 22" /root/test/ 192.168.5.132:/root/test3/······ 指定端口同步

192.168.5.130机器服务器

[root@xuexi-001 ~]# rsync -av -e "ssh -p 22" /root/test/ 192.168.5.132:/root/test3/
root@192.168.5.132's password: 
sending incremental file list
created directory /root/test3
./
1.txt
xuexi.sorft -> xuexi.txt
xuexi.txt

sent 241 bytes  received 94 bytes  74.44 bytes/sec
total size is 31  speedup is 0.09

192.168.5.132机器ssh

[root@localhost ~]# ls -l test3
总用量 8
-rw-r--r-- 1 root root 18 6月  18 23:19 1.txt
lrwxrwxrwx 1 root root  9 6月  18 23:20 xuexi.sorft -> xuexi.txt
-rw-r--r-- 1 root root  4 6月  18 23:21 xuexi.txt
  • rsync 经过服务的方式同步
  • 要编辑配置文件/etc/rsyncd.conf
  • 启动服务rsync --daemon
  • 格式:rsync -av /root/test/ 192.168.5.130::module/dir/

  • rsyncd.conf样例

port=873tcp

log file=/var/log/rsync.log工具

pid file=/var/run/rsyncd.pid测试

address=192.168.133.130ui

[test]日志

path=/root/rsync

use chroot=true

max connections=4

read only=no

list=true

uid=root

gid=root

auth users=test

secrets file=/etc/rsyncd.passwd

hosts allow=192.168.133.132


  • rsyncd.conf配置文件详解
  • port:指定在哪一个端口启动rsyncd服务,默认是873端口。
  • log file:指定日志文件。
  • pid file:指定pid文件,这个文件的做用涉及服务的启动、中止等进程管理操做。
  • address:指定启动rsyncd服务的IP。假如你的机器有多个IP,就能够指定由其中一个启动rsyncd服务,若是不指定该参数,默认是在所有IP上启动。
  • []:指定模块名,里面内容自定义。
  • path:指定数据存放的路径。
  • use chroot true|false:表示在传输文件前首先chroot到path参数所指定的目录下。这样作的缘由是实现额外的安全防御,但缺点是须要以roots权限,而且不能备份指向外部的符号链接所指向的目录文件。默认状况下chroot值为true,若是你的数据当中有软链接文件,阿铭建议你设置成false。

将机器B的文件同步到A机器上面(不使用密码的状况下)

A机器

1.将配置文件的样例粘贴到配置文件/etc/rsync.conf中

[root@xuexi-001 ~]# vi /etc/rsyncd.conf 

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.5.130
[test]
path=/tmp/rsync
use chroot=true
max connections=4
read only=no
list=true
uid=root
gid=root
#auth users=test
#secrets file=/etc/rsyncd.passwd
"/etc/rsyncd.conf" 35L, 721C                                  34,1          85%

2.启动rsync 服务 3.检查rsync服务是否启动 4.检查873端口 5.建立模块test的路径目录 `````mkdir /tmp/rsync

[root@xuexi-001 ~]# rsync --daemon
[root@xuexi-001 ~]# ps aux |grep rsync
root       2074  0.0  0.0 114696   540 ?        Ss   22:21   0:00 rsync --daemon
root       2584  0.0  0.0 112676   984 pts/0    R+   23:45   0:00 grep --color=auto rsync
[root@xuexi-001 ~]# netstat -lntp 
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      919/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1119/master         
tcp        0      0 192.168.5.130:873       0.0.0.0:*               LISTEN      2074/rsync          
tcp6       0      0 :::22                   :::*                    LISTEN      919/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1119/master         
[root@xuexi-001 ~]# cat /etc/rsyncd.conf 
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.5.130
[test]
path=/tmp/rsync
use chroot=true
max connections=4
read only=no
list=true
uid=root
gid=root
#auth users=test
#secrets file=/etc/rsyncd.passwd
hosts allow=192.168.5.132 
[root@xuexi-001 ~]# mkdir /tmp/rsync

B机器

1.将B机器上的一个文件同步到A机器上

将B机器上的root目录下面的test3目录 同步到A机器上的/tmp/rsync目录下并更名为test110

[root@localhost ~]# rsync -avP /root/test3/ 192.168.5.130::test/test110
sending incremental file list
created directory /test110
./
1.txt
             18 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=2/4)
xuexi.sorft -> xuexi.txt
xuexi.txt
              4 100%    3.91kB/s    0:00:00 (xfr#2, to-chk=0/4)

sent 242 bytes  received 91 bytes  31.71 bytes/sec
total size is 31  speedup is 0.09

2.查看A机器上的文件

[root@xuexi-001 ~]# ls /tmp/rsync/
test110

3.将A机器上的文件同步到B机器上

[root@localhost ~]# rsync -avP 192.168.5.130::test/test110  /tmp/test111
receiving incremental file list
created directory /tmp/test111
test110/
test110/1.txt
             18 100%   17.58kB/s    0:00:00 (xfr#1, to-chk=2/4)
test110/xuexi.sorft -> xuexi.txt
test110/xuexi.txt
              4 100%    3.91kB/s    0:00:00 (xfr#2, to-chk=0/4)

sent 69 bytes  received 260 bytes  31.33 bytes/sec
total size is 31  speedup is 0.09
[root@localhost ~]# ls /tmp/
test111

在同步的过程当中须要将firewalld服务关闭。

systemctl stop firewalld

在同步过程当中能够指定端口 使用--port 873

[root@localhost ~]# rsync -avP --port 873 192.168.5.130::test/test110  /tmp/test111

列出模块名,在服务器上的配置文件/etc/rsync.conf中将list=true 改成false 则不列出模块名

[root@localhost ~]# rsync --port=873 192.168.5.130::
test

加入密码同步

[root@xuexi-001 ~]# vi /etc/rsyncd.conf 

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.5.130
[test]
path=/tmp/rsync
use chroot=true
max connections=4
read only=no
list=true
uid=root
gid=root
auth users=test·····使用的用户名
secrets file=/etc/rsyncd.passwd ····密码文件名
"/etc/rsyncd.conf" 35L, 719C 
[root@xuexi-001 ~]# vi /etc/rsyncd.passwd

test:111111
[root@xuexi-001 ~]# chmod 600 /etc/rsyncd.passwd ·····须要设置文件的权限 600

B机器上测试同步效果

[root@localhost ~]# rsync -avP /root/test3/ test@192.168.5.130::test/test110 //·····这时候就须要输入用户名 test@192.168.5.130···
Password: ····输入密码
sending incremental file list
sent 124 bytes  received 12 bytes  10.07 bytes/sec
total size is 31  speedup is 0.23

在客户端B机器上定义一个密码文件 vi /etc/rsync_pass.txt

而后在同步的时候就不须要输入密码了,须要加 --password-file=/etc/rsync_pass.txt

[root@localhost ~]# vi /etc/rsync_pass.txt ······将密码文件写入 只写密码

111111
[root@localhost ~]# chmod 600 /etc/rsync_pass.txt
[root@localhost ~]# rsync -avP /root/test3/ --password-file=/etc/rsync_pass.txt test@192.168.5.130::test/test110
sending incremental file list

sent 124 bytes  received 12 bytes  12.95 bytes/sec
total size is 31  speedup is 0.23
相关文章
相关标签/搜索