Linux日常运维4 rsync同步,系统日志,screen工具

1、Linux文件同步工具-rsync安装:yum install -y rsyncrsync -av /etc/passwd /tmp/1.txt 本地同步rsync -av /tmp/1.txt 192.168.188.128:/tmp/2.txt 远程同步rsync格式rsync [OPTION] … SRC(源目录) DEST(目的目录)rsync [OPTION] … SRC [user...
摘要由CSDN通过智能技术生成

1、Linux文件同步工具-rsync

安装:yum install -y rsync

  • rsync -av /etc/passwd /tmp/1.txt 本地同步
  • rsync -av /tmp/1.txt 192.168.188.128:/tmp/2.txt 远程同步

rsync格式

  • rsync [OPTION] … SRC(源目录) DEST(目的目录)
  • rsync [OPTION] … SRC [user@]host:DEST (到远程的服务器;user@可省略,会默认为当前终端的用户)
  • rsync [OPTION] … [user@]host:SRC DEST (先写远程的机器/目录,然后同步到本地的目录下)
  • rsync [OPTION] … SRC [user@]host::DEST (两个冒号,可以是目标,可以是源)
  • rsync [OPTION] … [user@]host::SRC DEST

1.2、rsync常用选项

  • -a 归档模式,以递归方式传输文件,并保持文件所有属性;包含-rtplgoD参数选项
  • -r 同步目录时要加上,类似cp时的-r选项
  • -v 同步时显示一些信息,让我们知道同步的过程
  • -l 保留软连接(若是拷贝的原目录里面有一个软链接文件,那这个软链接文件指向到了另外一个目录下;在加上-l,它会把软链接文件本身拷贝到目标目录里面去)
  • -L 加上该选项后,同步软链接时会把源文件给同步
  • -p 保持文件的权限属性
  • -o 保持文件的属主
  • -g 保持文件的属组
  • -D 保持设备文件信息(/dev/sdb1 这样的设备文件有它的特殊性,如果不加-D 可能拷贝过去就是一个非常普通的文件,不能当设备来用)
  • -t 保持文件的时间属性
  • --delete 删除DEST中SRC没有的文件
  • --exclude 过滤指定文件,也可这样使用--exclude=“过滤文件”;如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步
  • -P 显示同步过程,比如速率,比-v更加详细
  • -u 加上该选项后,如果DEST中的文件比SRC新,则不同步
  • update
  • -z 传输时压缩

常用选项:-a、-v、-z、--delete、--exclude

实验:

  • 创建目录和文件
[root@localhost ~]# mkdir rsync
[root@localhost ~]# cd rsync
[root@localhost rsync]# mkdir test1
[root@localhost rsync]# touch 1 2 3 /root/123.txt
[root@localhost rsync]# ln -s /root/123.txt ./123.txt
[root@localhost rsync]# ls -l
总用量 0
-rw-r--r--. 1 root root  0 616 13:16 1
lrwxrwxrwx. 1 root root 13 616 13:17 123.txt -> /root/123.txt
-rw-r--r--. 1 root root  0 616 13:16 2
-rw-r--r--. 1 root root  0 616 13:16 3
drwxr-xr-x. 2 root root  6 616 13:15 test1

1.2.1、-a选项

[root@aminglinux rsync]# rsync -a test1 test2
[root@aminglinux rsync]# ls test2
test1
[root@aminglinux rsync]# ls test2/test1
1  123.txt  2  3

想法:把test1复制到test2;目录不加“/”,则变成创建了test2目录,再把test1放到test2目录中;与想法不符;需在目录加“/”;

[root@aminglinux rsync]# rm -rf test2
[root@aminglinux rsync]# rsync -a test1/ test2/
[root@aminglinux rsync]# ls -l test2/
总用量 0
-rw-r--r-- 1 root root  0 616 13:28 1
lrwxrwxrwx 1 root root 13 616 13:28 123.txt -> /root/123.txt
-rw-r--r-- 1 root root  0 616 13:28 2
-rw-r--r-- 1 root root  0 616 13:28 3

-a和--no-OPTIN一起使用(-a选项等同于-rlptgoD;--no-OPTIN等于去掉-rlptgoD中的选项)

[root@aminglinux rsync]# rm -rf test2
[root@aminglinux rsync]# rsync -av --no-l test1/ test2/
sending incremental file list
created directory test2
skipping non-regular file "123.txt"
./
1
2
3

sent 234 bytes  received 144 bytes  756.00 bytes/sec
total size is 13  speedup is 0.03

1.2.2、-L选项:把SRC中软链接的源文件复制到DEST;

[root@aminglinux rsync]# rm -rf test2
[root@aminglinux rsync]# rsync -avL test1/ test2/
sending incremental file list
created directory test2
./
1
123.txt
2
3

sent 265 bytes  received 123 bytes  776.00 bytes/sec
total size is 0  speedup is 0.00

1.2.3、--delete选项:删除DEST中SRC没有的文件

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值