rsync同步命令
同步操作
rsync [选项..] 源目录 目标目录
同步与复制的差异
复制:完成拷贝源到目标
同步:增量拷贝,只传输变化过的数据
本地同步
rsync [选项...] 本地目录1 本地目录2 #同步整个文件夹
rsync [选项...] 本地目录1/ 本地目录2 #只同步目录的数据
操作选项
-n : 测试同步过程,不做实际修改
--delete:删除目标文件夹多余的文档
-a :归档模式,相当于-rlptgoD
-v :显示详细操作信息
-z :传输过程中启动压缩/解压
[root@test ]# mkdir /abc
[root@test ]# mkdir /test
[root@test ]# cp /etc/passwd /etc/fstab /etc/shadow /etc/group /abc
[root@test ]# rsync -avz /abc /test
sending incremental file list
abc/
abc/fstab
abc/group
abc/passwd
abc/shadow
sent 2596 bytes received 92 bytes 5376.00 bytes/sec
total size is 5551 speedup is 2.07
[root@test ]# ls /test
abc
[root@test ]# rsync -avz /abc/ /test
sending incremental file list
./
fstab
group
passwd
shadow
sent 2581 bytes received 91 bytes 5344.00 bytes/sec
total size is 5551 speedup is 2.08
[root@test ]# ls /test
abc fstab group passwd shadow
[root@test ]# echo 123 >> /abc/group
[root@test ]# rsync -avz /abc/ /test
sending incremental file list
group
sent 668 bytes received 31 bytes 1398.00 bytes/sec
total size is 5555 speedup is 7.95
[root@test ]# touch /test/test.txt
[root@test ]# rsync -avz --delete /abc/ /test
sending incremental file list
./
deleting abc/shadow
deleting abc/passwd
deleting abc/group
deleting abc/fstab
deleting abc/
deleting test.txt
sent 87 bytes received 15 bytes 204.00 bytes/sec
total size is 5555 speedup is 54.46
rsync+ssh同步
下行:rsync [...] user@host:远程目录
上行:rsync [...] 本地目录 user@host:远程目录
[root@test ~]# ls /abc/
fstab group passwd shadow
[root@test ~]# rsync -avz --delete /abc/ root@192.168.4.207:/opt/
root@192.168.4.207's password:
sending incremental file list
./
deleting rh/
fstab
group
passwd
shadow
sent 2590 bytes received 91 bytes 766.00 bytes/sec
[root@pc207 ~]# ls /opt/
fstab group passwd shadow
实时同步
1.密码验证取消,采用公钥 私钥 验证
[root@test ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
45:3d:84:d0:ac:08:4b:9b:84:a5:b4:fc:47:eb:7f:98 root@svr7.tedu.cn
The key's randomart image is:
+--[ RSA 2048]----+
| .o. .+.+. |
| o.o+ .+ o |
| +o =.. .. . |
| .+..... |
| . o S |
| o |
| . o |
| .E . |
| .. |
+-----------------+
[root@test ~]# ls /root/.ssh #公私钥存放地址
id_rsa id_rsa.pub known_hosts
[root@test ~]# ssh-copy-id root@192.168.4.207
[root@pc207 ~]# ls /root/.ssh
authorized_keys
2.验证
[root@test ~]# ssh root@192.168.4.207
Last login: Thu Nov 16 20:07:55 2017 from 192.168.4.254
[root@pc207 ~]# exit
3.inotify实时监控目录,内容是否变化
inotify-tools-3.13.tar.gz
[root@test ~]# rm -rf /opt/*
[root@test ~]# tar -xf /tools/inotify-tools-3.13.tar.gz -C /opt/
[root@test ~]# ls /opt/
inotify-tools-3.13
[root@test ~]# cd /opt/inotify-tools-3.13/
[root@test inotify-tools-3.13]# ./configure
[root@test inotify-tools-3.13]# make
[root@test inotify-tools-3.13]# make install
[root@test inotify-tools-3.13]# inotifywait
No files specified to watch!
inotify基本用法
inotifywait [选项 ] 目标文件
常用选项
-m:
-r
-q
-e
[root@test /]# inotifywait -mrq /opt/
/opt/ CREATE 1.txt
/opt/ OPEN 1.txt
/opt/ ATTRIB 1.txt
/opt/ CLOSE_WRITE,CLOSE 1.txt
/opt/ CREATE 2.txt
/opt/ OPEN 2.txt
/opt/ ATTRIB 2.txt
/opt/ CLOSE_WRITE,CLOSE 2.txt
^C
[root@test /]#
Shell脚本
[root@test ~]# vim /root/look.sh
#!/bin/bash
while inotifywait -rqq /abc
do
rsync -az --delete /abc/ root@192.168.4.207:/opt/
done
[root@test ~]# chmod +x /root/look.sh
[root@test ~]# /root/look.sh &
[1] 19200