rsync工具介绍以及经常使用选项

​10月31日任务linux

10.28 rsync工具介绍bash

10.29/10.30 rsync经常使用选项ssh

10.31 rsync经过ssh同步tcp

 

10.2八、linux文件同步工具-rsync工具

  • rsync -av /etc/passwd  /tmp/q.txt
  • rsync -av /tmp//1.txt 192.168.188.128:/tmp/2.txt
  • rsync格式
  • rsync [OPTION] ... SRC DEST
  • rsync [OPTION] ... SRC [user@]host:DEST
  • rsync [OPTION] ...[user@]host:SRC  DEST
  • rsync [OPTION] ... SRC [user@]host::DEST
  • rsync [OPTION] ...[user@]host::SRC  DEST

 

 

10.29 /10.30、rsync经常使用选项ui

  • -a  包含 -rtplgoDrem

  • -r  同步目录时要加上,相似cp时的-r选项同步

  • -v  同步时显示一些信息,让咱们知道同步的过程test

  • -l  保留软链接module

  • -L 加上该选项后,同步软链接时会把源文件给同步

  • -p 保持文件的权限属性

  • -o  保持文件的属主

  • -g  保持文件的属组

  • -D 保持设备文件信息

  • -t  保持文件的时间属性

  • --delte  删除DEST中SRC没有的文件

  • --exclude 过滤指定文件,如--exclude "logs"会把文件名包含logs的文件或者目录过滤掉,不一样步

  • -P 显示同步过程,好比速率,比-v更加详细

  • -u 加上该选项后,若是DEST中的文件比SRC新,则不一样步

  • -z  传输时压缩

 

#运用,把目录/root/目录下111目录同步到/tmp/下并改名为111_dest

[root@zgxlinux-aliyun 111]# rsync -av /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_test
./
1.txt
2.txt
passwd.txt
zgx -> /etc/passwd
222/
sent 1,264 bytes  received 119 bytes  2,766.00 bytes/sec
total size is 967  speedup is 0.70
[root@zgxlinux-aliyun 111]# ls /tmp/
111_dest  Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>
1.txt     systemd-private-2c5b8db77cfb4ec8b50aeac4c31538bb-ntpd.service-EadYZI
[root@zgxlinux-aliyun ~]# ll 111/
total 8
-rw-r--r-- 1 root root    0 Oct 31 14:26 1.txt
drwxr-xr-x 2 root root 4096 Oct 31 14:29 222
-rw-r--r-- 1 root root    0 Oct 31 14:28 2.txt
-rw-r--r-- 1 root root  956 Oct 31 14:27 passwd.txt
lrwxrwxrwx 1 root root   11 Oct 31 14:32 zgx -> /etc/passwd
[root@zgxlinux-aliyun ~]# ll /tmp/111_dest/
total 8
-rw-r--r-- 1 root root    0 Oct 31 14:26 1.txt
drwxr-xr-x 2 root root 4096 Oct 31 14:29 222
-rw-r--r-- 1 root root    0 Oct 31 14:28 2.txt
-rw-r--r-- 1 root root  956 Oct 31 14:27 passwd.txt
lrwxrwxrwx 1 root root   11 Oct 31 14:32 zgx -> /etc/passwd

#加上L选项会把软连接的源文件给拷贝到目标目录

 

[root@zgxlinux-aliyun ~]# tail -n2 /etc/passwd
tcpdump:x:72:72::/:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
[root@zgxlinux-aliyun ~]# useradd zhangguoxiang
[root@zgxlinux-aliyun ~]# tail -n2 /etc/passwd
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
zhangguoxiang:x:1000:1000::/home/zhangguoxiang:/bin/bash
[root@zgxlinux-aliyun ~]# rsync -avL /root/111/ /tmp/111.dest/
sending incremental file list
./
1.txt
2.txt
passwd.txt
zgx
222/
sent 2,312 bytes  received 99 bytes  4,822.00 bytes/sec
total size is 1,979  speedup is 0.82
[root@zgxlinux-aliyun ~]# tail -n2 /tmp/111.dest/zgx
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
zhangguoxiang:x:1000:1000::/home/zhangguoxiang:/bin/bash

 

#添加delete选项后会删除源文件中没有的文件

[root@zgxlinux-aliyun ~]# ls /root/111/
1.txt  222  2.txt  passwd.txt  zgx
[root@zgxlinux-aliyun ~]# ls /tmp/111_dest/
1.txt  222  2.txt  passwd.txt  zgx
[root@zgxlinux-aliyun ~]# touch /tmp/111_dest/new.txt
[root@zgxlinux-aliyun ~]# ls /tmp/111_dest/
1.txt  222  2.txt  new.txt  passwd.txt  zgx
[root@zgxlinux-aliyun ~]# rsync -avL --delete /root/111/ /tmp/111_dest/
sending incremental file list
deleting new.txt
./
sent 162 bytes  received 31 bytes  386.00 bytes/sec
total size is 2,020  speedup is 10.47
[root@zgxlinux-aliyun ~]# ls /tmp/111_dest/
1.txt  222  2.txt  passwd.txt  zgx

#加上exclude表示过滤

[root@zgxlinux-aliyun ~]# ls /tmp/111_dest/
1.txt  222  2.txt  passwd.txt  zgx
[root@zgxlinux-aliyun ~]# rm -rf /tmp/111_dest/*
[root@zgxlinux-aliyun ~]# rsync -avL --exclude "*.txt" /root/111/ /tmp/111_dest/
sending incremental file list
./
zgx
222/
sent 1,197 bytes  received 42 bytes  2,478.00 bytes/sec
total size is 1,054  speedup is 0.85
[root@zgxlinux-aliyun ~]# ls /tmp/111_dest/
222  zgx

10.31 rsync经过ssh同步

  • rsync经过ssh方式同步

  • rsync -av test1/ 192.168.133.132:/tmp/test2/

  • rsync 经过服务的方式同步

  • 要编辑配置文件 /etc/rsyncd.conf

  • 启动服务 rsync --daemon

  • 格式 : rsync -av test1/192.168.133.130::module/dir/

相关文章
相关标签/搜索