定义: Server代表需要将文件夹同步到其它电脑的 主动发送方
Client 代表被动接收进行同步的电脑
1.在需要同步的两台linux电脑分别安装rsync包---安装文件见rsync.
- 通过ssh工具将deb包上传到server和client服务器
- 通过命令进行安装:apt-get install -y ./rsync_3.2.3-2ubuntu1_amd64.deb
(提供了两个安装包 如果一个安装不了 就换另一个,实在安装不了就根据系统版本到https://pkgs.org/download/rsync去下载)
2.配置Server服务器无密码访问
(1)打开client电脑 输入ssh-keygen 并且一直回车 直到结束,此时在~/.ssh/路径生成了id_rsa.pub公匙文件。
(2) 打开client电脑输入ssh-copy-id将公匙拷贝至远程主机,例如client的ip是192.168.66.22:
root@Ubuntu16-04:~/.ssh# ssh-copy-id -i id_rsa.pub 192.168.66.22
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
The authenticity of host '192.168.66.22 (192.168.66.22)' can't be established.
ECDSA key fingerprint is SHA256:kWE1tde6F6JMJYQRN0fZGvDxciWwy+kWi0TG3CyiS60.
Are you sure you want to continue connecting (yes/no)? yes
/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.66.22's password: (输入密码)
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.66.22'"
and check to make sure that only the key(s) you wanted were added.
3.在Server电脑中开启同步
格式:
rsync -avz -e ssh server_文件夹 client_用户名@client_ip:client_文件夹
例如同步本机的/home/rocktech文件夹到远程的/home/rrzz文件夹
则命令维:
rsync -avz -e ssh /home/rocktech/ root@192.168.66.22:/home/rrzz/
4.将以上命令写入server开机运行
编写脚本文件
vi rsync.sh
#!/bin/bash
while true
do
rsync -avz -e ssh /home/test/ root@192.168.66.22:/home/rrzz/
sleep 1s
done
~
将脚本文件加入开机运行
vi /etc/rc.local
sh /etc/init.d/rsync.sh
请记住:该语句应该放到exit(0)之前 否则不能开机运行