1、基于SSH的同步源
可以通过密钥对来实现
客户端:192.168.20.11
[root@server11 /client/rsync]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:
SHA256:iCvgxpzX0q3f/5zANIrfLtIy1SIdO8CuNFZWRZv4egs root@server11
The key's randomart image is:
+---[RSA 2048]----+
| oo |
| .. o |
| . .. o |
| . * .. |
|. . = S ++ |
|+.. o=.o.*=.. |
| *.o+ooo+Eo+ |
|. .....+oo+ + . |
| ...+o+=.+ |
+----[SHA256]-----+
[root@server11 /client/rsync]$ ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
[root@server11 /client/rsync]$ ssh '[email protected]'
[gang@server104 ~]$ ifconfig
ens33: flags=4163
inet 192.168.20.104 netmask 255.255.255.0 broadcast 192.168.20.255
inet6 fe80::250:56ff:fe21:eabd prefixlen 64 scopeid 0x20
ether 00:50:56:21:ea:bd txqueuelen 1000 (Ethernet)
RX packets 8938 bytes 623153 (608.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4053 bytes 354192 (345.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1 (Local Loopback)
RX packets 140 bytes 8400 (8.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 140 bytes 8400 (8.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[gang@server104 ~]$ exit
logout
Connection to 192.168.20.104 closed.
[root@server11 /client/ssh]$ rm -rf *
#下行同步
[root@server11 /client/ssh]$ rsync -avz [email protected]:/var/www/html/* /client/ssh/
receiving incremental file list
e.txt
index.html
q.txt
w.txt
sent 87 bytes received 247 bytes 668.00 bytes/sec
total size is 8 speedup is 0.02
[root@server11 /client/ssh]$ ll
total 4
-rw-r--r-- 1 gang gang 0 Jan 16 16:34 e.txt
-rw-r--r-- 1 gang gang 8 Jan 16 16:34 index.html
-rw-r--r-- 1 gang gang 0 Jan 16 16:15 q.txt
-rw-r--r-- 1 gang gang 0 Jan 16 16:15 w.txt
2、基于rsync的同步源
#通过系统变量实现 RSYNC_PASSWORD
客户端:192.168.20.11
[root@server11 /client/rsync]$ export RSYNC_PASSWORD=123456
[root@server11 /client/rsync]$ ll
total 8
-rw-r--r-- 1 root root 6 Jan 16 23:15 rsync.txt
-rw-r--r-- 1 root root 0 Jan 16 23:54 zws.txt
-rw-r--r-- 1 root root 3 Jan 16 23:50 zz.txt
[root@server11 /client/rsync]$ rm -rf *
[root@server11 /client/rsync]$ ls
#下行同步
[root@server11 /client/rsync]$ rsync -avz [email protected]::share /client/rsync/
receiving incremental file list
./
rsync.txt
zws.txt
zz.txt
sent 115 bytes received 277 bytes 784.00 bytes/sec
total size is 9 speedup is 0.02
#现在执行同步操作不需要输入密码
[root@server11 /client/rsync]$ ll
total 8
-rw-r--r-- 1 root root 6 Jan 16 23:15 rsync.txt
-rw-r--r-- 1 nobody nobody 0 Jan 16 23:54 zws.txt
-rw-r--r-- 1 nobody nobody 3 Jan 16 23:50 zz.txt
#被删除的文件已经从服务端被同步过来了