1、使用rsync进行数据同步
格式:rsync 【选项】 源文件 目标文件
常用选项:
-a,--archive(存档)归档模式,表示以递归的方式传输文件,并且保持文件属性
-z,表示压缩传输
-P,显示传输进度
--delete,删除那些目标文件有而原始位置没有的文件
例:在niexj21上创建/mnt/test1,把文件/mnt/,备份到niexj22的/backup文件中
[root@niexj21 ~]# touch /mnt/test1
[root@niexj22 ~]# mkdir /backup
[root@niexj21 ~]# rsync -azP /mnt 192.168.1.22:/backup
[root@niexj22 ~]# ls /backup
mnt
[root@niexj22 ~]# ls /backup/mnt/
test1
两台主机之间实现无交付连接
[root@niexj21 ~]# 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:直接回车
SHA256:uwYD8uqisuJTX5miaIZTTf+EIQ18qzhhHe2NNz0QRYo root@niexj21
The key's randomart image is:
+---[RSA 2048]----+
| . . .+o |
| + o... |
| . *E+.o |
| + = * + o |
| . B = +S. . |
| +.+.=+.. |
|..ooo o=. |
|*=.. . o. |
|X*o .. |
+----[SHA256]-----+
[root@niexj21 ~]# ssh-copy-id 192.168.1.22
/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
root@192.168.1.22's password:输入密码
测试
[root@niexj21 ~]# touch /mnt/test2
[root@niexj21 ~]# rsync -azP --delete /mnt 192.168.1.22:/backup #直接回车不需要输入密码
[root@niexj22 ~]# ls /backup/mnt/
test1 test2