1 ssh协议数据同步:将NFS服务器数据同步备份到 rsync服务器
实验环境:一台NFS服务器,一台rsync服务器
在两台服务器上分别创建目录(/filesrc、/filedst)
mkdir -pv /filesrc /filedst
下行同步(下载)
格式: rsync -avz服务器地址:/服务器目录/*/本地目录
示例: rsync -avz root@192.168.88.10:/filesrc/* /filedst -a :归档模式,递归并保留对象属性 -v :显示同步过程 -z :在传输文件时进行压缩
范例:
40~ touch /filesrc/{1..5}.txt41~ rsync -avz root@10.0.0.40:/filesrc/* /filedstThe authenticity of host '10.0.0.40 (10.0.0.40)' can't be established.RSA key fingerprint is a9:66:14:15:29:9a:53:57:67:72:74:ae:36:ea:72:be.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '10.0.0.40' (RSA) to the list of known hosts.root@10.0.0.40's password:receiving incremental file list1.txt2.txt3.txt4.txt5.txt41~ ls /filedst1.txt 2.txt 3.txt 4.txt 5.txt
上行同步(上传)
格式: rsync -avz/本地目录/*服务器地址:/服务器目录
示例: rsync -avz /filedst/* root@192.168.88.10:/filesrc
注意:使用root用户进行实验可以,但生产环境中尽量使用单独创建的普通用户,减少权限溢出
创建用来做数据同步的用户,并给予用户对目录的相应权限,一般使用ACL设置权限。
useradd zhangsanpasswd zhangsansetfacl -m u:zhangsan:rwx /filesrc
范例:
40~ rm -rf /filesrc/*41~ rsync -avz /filedst/* root@10.0.0.40:/filesrcroot@10.0.0.40's' password:sending incremental file list1.txt2.txt3.txt4.txt5.txtsent 250 bytes received 107 bytes 47.60 bytes/sectotal size is 0 speedup is 0.0040~ ls /filesrc/1.txt 2.txt 3.txt 4.txt 5.txt
拓展:若要实现免密码数据同步,只需要做好ssh密钥对登录即可
40~ ssh-keygen -t rsa -b 204840~ ssh-copy-id root@10.0.0.4141~ ssh-keygen -t rsa -b 204841~ ssh-copy-id root@10.0.0.40
40~ rm -rf /filesrc/*#无需输入密码41~ rsync -avz /filedst/* root@10.0.0.40:/filesrc#验证40~ ls1.txt 2.txt 3.txt 4.txt 5.txt
2 rsync协议数据同步:将NFS服务器数据同步备份到rsync服务器
实验环境:一台服务器,一台客户端。
- 在两台服务器上分别创建目录(/filesrc、/filedst)
- 搭建rsync服务(仅需要在NFS服务器上搭建即可)
a.创建主配置文件(/etc/rsyncd.conf)
~ vim /etc/rsyncd.confaddress = 10.0.0.40#rsync服务绑定IPport 873#默认服务端口873log file = /var/log/rsyncd.log#日志文件位置pid file = /var/run/rsyncd.pid#进程号文件位置[web]#共享名:用来连接是写在url上的,切记comment = web directory backup#共享描述话语path = /filesrc#实际共享目录read only = no#是否仅允许读取dont compress = *.gz *.bz2 *.xz *.zip#哪些文件类型不进行压缩auth users = user1#登录用户名(非系统用户,需要自行创建)secrets file = /etc/rsyncd_users.db#认证所需账户密码文件(需自行创建-同上)
b.创建认证所需账户密码文件
~ vim /etc/rsyncd_users.dbuser1:123456~ chmod 600 /etc/rsyncd_users.db#必须修改权限,否则登录报错
c.启动服务
~ rsync --daemon#默认rsync开启端口为 873~ netstat -antlp | grep :873
d.设置映射用户对共享目录有权限(r)
setfacl -m u:nobody:rwx /filesrc
注意:关闭服务可使用kill命令,但偶尔会造成服务被结束,但进程号配置文件不被删除的问题,若遇到此类问题可自己手动删除,再启动则正常(建议自己写一个rsync的服务管理脚本)
下行同步(下载)
- 格式: rsync -avz rsync://用户名@服务器地址/共享模块名 /本地目录
- 示例: rsync -avz rsync: //user1@192.168.88.10/web /filedst
- 拓展:—delete:删除本地比服务器多出来的文件(源地址没有,目标地址有的删掉)。其—delete参数只能用于 rsync 协议,ssh 协议无法使用。
rsync -avz —delete rsync://user1@192.168.88.10/web /filedst
范例:将NFS服务器数据同步备份到rsync服务器
40~ cp /etc/passwd /filesrc40~ cp /etc/issue /filesrc41~ rsync -avz rsync://user1@10.0.0.40:/web /filedstPassword:receiving incremental file list./issuepasswdsent 95 bytes received 907 bytes 222.67 bytes/sectotal size is 1660 speedup is 1.6641~ ls1.txt 2.txt 3.txt 4.txt 5.txt issue passwd
范例:使用 —delete 参数
40~ ls /filesrcissue passwd41~ ls /filedst/1.txt 2.txt 3.txt 4.txt 5.txt issue passwd#--delete会严格同步远程服务器目录的内容,本地同步目录有的文件将会删除41~ rsync -avz --delete rsync://user1@10.0.0.40:/web /filedstPassword:receiving incremental file listdeleting 5.txtdeleting 4.txtdeleting 3.txtdeleting 2.txtdeleting 1.txt./sent 57 bytes received 130 bytes 74.80 bytes/sectotal size is 1660 speedup is 8.8841~ ls /filedst/issue passwd
上行同步(上传)
- 格式:rsync -avz/本地目录/*rsync://用户名@服务器地址/共享模块名
- 示例: rsync -avz /filedst/* rsync://user1@192.168.88.10/web
拓展: rsync协议的免密码可以借助一个环境变量实现 export RSYNC_PASSWORD=虚拟用户密码(客户端生成)
40~ rm -rf /filesrc/*41~ rsync -avz /filedst/* rsync://user1@10.0.0.40:/webPassword:sending incremental file listissuepasswdsent 818 bytes received 46 bytes 345.60 bytes/sectotal size is 1660 speedup is 1.92#验证40~ ls /filesrcissue passwd41~ export RSYNC_PASSWORD=12345641~ rm -rf /filedst/*41~ touch /filedst/{a..f}.txt41~ rsync -avz --delete rsync://user1@10.0.0.40:/web /filedst/receiving incremental file listdeleting f.txtdeleting e.txtdeleting d.txtdeleting c.txtdeleting b.txtdeleting a.txt./issuepasswdsent 95 bytes received 909 bytes 2008.00 bytes/sectotal size is 1660 speedup is 1.6541~ ls /filedst/issue passwd
