这篇文章主要讲解了“rsync基本使用方法有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“rsync基本使用方法有哪些”吧!

rsync是可以实现增量备份的工具。配合任务计划,rsync能实现定时或间隔同步,配合inotify或sersync,可以实现触发式的实时同步。

一、rsync命令的用法:

基本格式:rsync [选项] 原始位置 目标位置 常用选项:

-a 归档模式,递归并保留对象属性,等同于 -rlptgoD

-v 显示同步过程的详细(verbose)信息

-z 在传输文件时进行压缩(compress)

-H 保留硬链接文件

-A 保留ACL属性 –delete 删除目标位置有而原始位置没有的文件

-r 递归模式,包含目录及子目录中所有文件

-l 对于软链接文件仍然复制为软链接文件

-p 保留文件的权限标记

-t 保留文件的时间标记

-g 保留文件的属组标记(仅超级用户使用)

-o 保留文件的属主标记(仅超级用户使用)

-D 保留设备文件及其他特殊文件

二、配置rsync

在配置rsync前,先来做个小测试:

服务端

#在服务端网站首页写入一些内容[root@localhostDesktop]#cd/var/www/html[root@localhosthtml]#vimindex.html[root@localhosthtml]#catindex.htmlHelloWorld!HelloJaking![root@localhosthtml]#ifconfigeth0Linkencap:EthernetHWaddr00:0C:29:BE:68:3Finetaddr:192.168.142.132Bcast:192.168.142.255Mask:255.255.255.0inet6addr:fe80::20c:29ff:febe:683f/64Scope:LinkUPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1RXpackets:580errors:0dropped:0overruns:0frame:0TXpackets:390errors:0dropped:0overruns:0carrier:0collisions:0txqueuelen:1000RXbytes:57739(56.3KiB)TXbytes:41856(40.8KiB)loLinkencap:LocalLoopbackinetaddr:127.0.0.1Mask:255.0.0.0inet6addr:::1/128Scope:HostUPLOOPBACKRUNNINGMTU:16436Metric:1RXpackets:16errors:0dropped:0overruns:0frame:0TXpackets:16errors:0dropped:0overruns:0carrier:0collisions:0txqueuelen:0RXbytes:960(960.0b)TXbytes:960(960.0b)[root@localhostrsync]#servicehttpdrestartStoppinghttpd:[OK]Startinghttpd:httpd:Couldnotreliablydeterminetheserver'sfullyqualifieddomainname,usinglocalhost.localdomainforServerName[OK]

客户端

#客户端能成功访问服务端网站首页的内容[root@localhostDesktop]#curl192.168.142.132HelloWorld!HelloJaking!

刚刚的小测试其实是基于SSH实现的,rsync有两种同步源,一种是基于SSH的同步源,另一种是基于rsync的同步源。

三、基于SSH的同步源

设置ACL权限:setfacl -m user:用户名:rwx /服务器目录 下行同步:rsync -avz 用户名@服务器地址:/服务器目录 /本地目录 上行同步:rsync -avz /本地目录 用户名@服务器地址:/服务器目录

为确保服务端的数据能同步到客户端,接下来,我先从SSH的同步源开始配置: 在配置前,分别在服务端和客户端上执行yum install -y rsync,确保rsync已安装。

1.在服务端授权一个用户,也就是创建一个用户:

[root@localhosthtml]#useraddserver[root@localhosthtml]#passwdserverChangingpasswordforuserserver.Newpassword:BADPASSWORD:Thepasswordisshorterthan8charactersRetypenewpassword:passwd:allauthenticationtokensupdatedsuccessfully.

2.在客户端创建ssh目录,同步服务端数据:

[root@localhostDesktop]#mkdir/client[root@localhostDesktop]#cd/client/[root@localhostclient]#mkdirssh[root@localhostclient]#rsync-avzserver@192.168.142.132:/var/www/html/*/client/sshserver@192.168.142.132'spassword:receivingincrementalfilelistindex.htmlsent68bytesreceived219bytes114.80bytes/sectotalsizeis27speedupis0.0930bytesreceived104bytes15.76bytes/sectotalsizeis27speedupis0.20[root@localhostclient]#cdssh[root@localhostssh]#lsindex.html[root@localhostssh]#catindex.htmlHelloWorld!HelloJaking!#客户端已成功同步服务端数据

3.刚刚的同步是下行同步,即从服务器端把数据同步到客户端。接下来我将演示一遍上行同步,即把客户端的数据同步到服务端:

#在客户端创建新文件,准备同步到服务端。[root@localhostssh]#toucha.txtb.txt[root@localhostssh]#lsa.txtb.txtindex.html[root@localhostssh]#rsync-avz/client/ssh/*server@192.168.142.132:/var/www/htmlserver@192.168.142.132'spassword:sendingincrementalfilelista.txtb.txtrsync:mkstemp"/var/www/html/.a.txt.6JDDzO"failed:Permissiondenied(13)rsync:mkstemp"/var/www/html/.b.txt.p7hCLz"failed:Permissiondenied(13)sent131bytesreceived50bytes40.22bytes/sectotalsizeis27speedupis0.15rsyncerror:somefiles/attrswerenottransferred(seepreviouserrors)(code23)atmain.c(1052)[sender=3.0.9]#同步失败,从报错结果可以server用户权限不足,server用户对/var/www/html目录没有写权限。

4.在服务端设置比较安全的ACL权限:

[root@localhosthtml]#setfacl-muser:server:rwx/var/www/html

5.再次在客户端执行上行同步操作:

[root@localhostssh]#rsync-avz/client/ssh/*server@192.168.142.132:/var/www/htmlserver@192.168.142.132'spassword:sendingincrementalfilelista.txtb.txtsent131bytesreceived50bytes51.71bytes/sectotalsizeis27speedupis0.15#由同步的过程可以看出,index.html没有被上传,由此可知rsync使用的同步机制是增量备份的机制。

在服务端查看:

[root@localhosthtml]#lsa.txtb.txtindex.html#客户端数据已成功同步到服务端四、基于rsync的同步源

/etc/rsyncd_users.db文件权限必须是600 做上行同步时,nobody需要有写入权限。 rsync -avz 用户名@服务器地址::共享模块名 /本地目录 rsync -avz rsync://用户名@服务器地址/共享模块名 /本地目录

使用SSH的同步源需要创建用户,对于服务器来说,存在过多的用户不是一件好事。而用基于rsync的同步源则不需要创建用户,指定的用户只需写在配置文件里即可,这样的用户是虚拟用户。

1.修改配置文件:

服务端

[root@localhosthtml]#vim/etc/rsyncd.conf#若配置文件不存在则直接创建[root@localhosthtml]#cat/etc/rsyncd.confaddress=192.168.142.132port873pidfile=/var/run/rsyncd.pidlogfile=/var/log/rsyncd.log[share]comment=softpath=/server/rsyncreadonly=yesdontcompress=*.gz*.bz2*.zipauthusers=wangsecretsfile=/etc/rsyncd_users.db[root@localhosthtml]#vim/etc/rsyncd_users.db[root@localhosthtml]#cat/etc/rsyncd_users.dbwang:123456#rsync不支持复杂密码,尽量设简单一点。[root@localhosthtml]#vim/etc/xinetd.d/rsync[root@localhosthtml]#cat/etc/xinetd.d/rsync#default:off#description:Thersyncserverisagoodadditiontoanftpserver,asit\#allowscrcchecksummingetc.servicersync{disable=yesflags=IPv6socket_type=streamwait=nouser=rootserver=/usr/bin/rsyncserver_args=--daemonlog_on_failure+=USERID}[root@localhosthtml]#rsync--daemon#启动rsync[root@localhosthtml]#netstat-pantu|grep873tcp00192.168.142.132:8730.0.0.0:*LISTEN6779/rsync[root@localhosthtml]#mkdir-p/server/rsync[root@localhosthtml]#cd!$cd/server/rsync[root@localhostrsync]#touchrsync.txt[root@localhostrsync]#lsrsync.txt[root@localhostrsync]#chmod600/etc/rsyncd_users.db#一定要给密码文件赋予600权限,否则同步数据将出错!

2.执行同步操作:

客户端

[root@localhostrsync]#rsync-avzwang@192.168.142.132::share/client/rsyncPassword:receivingincrementalfilelist./rsync.txtsent77bytesreceived151bytes50.67bytes/sectotalsizeis0speedupis0.00[root@localhostrsync]#lsrsync.txt#数据同步成功[root@localhostrsync]#pwd/client/rsync

下行同步已完成,接下来我将演示上行同步:

服务端

#在执行上行同步前一定要修改模块权限和ACL权限[root@localhostrsync]#vim/etc/rsyncd.conf[root@localhostrsync]#cat/etc/rsyncd.confaddress=192.168.142.132port873pidfile=/var/run/rsyncd.pidlogfile=/var/log/rsyncd.log[share]comment=softpath=/server/rsyncreadonly=no#这里一定要改为nodontcompress=*.gz*.bz2*.zipauthusers=wangsecretsfile=/etc/rsyncd_users.db[root@localhostrsync]#setfacl-mu:nobody:rwx/srver/rsync#设置ACL权限[root@localhostrsync]#pkillrsync#关闭rsync[root@localhostrsync]#rsync--daemon#启动rsync

客户端

[root@localhostrsync]#touchclient.txt[root@localhostrsync]#rsync-avz/client/rsync/*wang@192.168.142.132::sharePassword:sendingincrementalfilelistclient.txtsent85bytesreceived27bytes32.00bytes/sectotalsizeis0speedupis0.00#上行同步成功

在服务端查看:

[root@localhostrsync]#lsclient.txtrsync.txt[root@localhostrsync]#pwd/server/rsync

3.上行同步的另一种格式:

客户端

[root@localhostrsync]#lsclient.txtrsync.txt[root@localhostrsync]#touchtest.txt[root@localhostrsync]#rsync-avz/client/rsync/*rsync://wang@192.168.142.132/sharePassword:sendingincrementalfilelisttest.txtsent102bytesreceived27bytes28.67bytes/sectotalsizeis0speedupis0.00

服务端

[root@localhostrsync]#lsclient.txtrsync.txttest.txt五、配置免密码验证

1、基于SSH的同步源

通过秘钥对实现 客户端

[root@localhostssh]#pwd/client/ssh[root@localhostssh]#lsa.txtb.txtindex.html[root@localhostssh]#rm-rf*[root@localhostssh]#ssh-keygenGeneratingpublic/privatersakeypair.Enterfileinwhichtosavethekey(/root/.ssh/id_rsa):Enterpassphrase(emptyfornopassphrase):Entersamepassphraseagain:Youridentificationhasbeensavedin/root/.ssh/id_rsa.Yourpublickeyhasbeensavedin/root/.ssh/id_rsa.pub.Thekeyfingerprintis:3d:fe:c8:0e:2c:b7:90:b0:f4:0d:31:af:b4:d3:9e:87root@localhost.localdomainThekey'srandomartimageis:+--[RSA2048]----+|||||o||+.||ooSo||.=O..||.O*..||*E=.o||+o+.|+-----------------+[root@localhostssh]#[root@localhostssh]#ssh-copy-idserver@192.168.142.132server@192.168.142.132'spassword:Nowtryloggingintothemachine,with"ssh'server@192.168.142.132'",andcheckin:.ssh/authorized_keystomakesurewehaven'taddedextrakeysthatyouweren'texpecting.[root@localhostssh]#idserver#server用户在服务端id:server:Nosuchuser[root@localhostssh]#sshserver@192.168.142.132[server@localhost~]$ifconfig#成功登录服务端eth0Linkencap:EthernetHWaddr00:0C:29:BE:68:3Finetaddr:192.168.142.132Bcast:192.168.142.255Mask:255.255.255.0inet6addr:fe80::20c:29ff:febe:683f/64Scope:LinkUPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1RXpackets:935errors:0dropped:0overruns:0frame:0TXpackets:660errors:0dropped:0overruns:0carrier:0collisions:0txqueuelen:1000RXbytes:112043(109.4KiB)TXbytes:89842(87.7KiB)loLinkencap:LocalLoopbackinetaddr:127.0.0.1Mask:255.0.0.0inet6addr:::1/128Scope:HostUPLOOPBACKRUNNINGMTU:16436Metric:1RXpackets:16errors:0dropped:0overruns:0frame:0TXpackets:16errors:0dropped:0overruns:0carrier:0collisions:0txqueuelen:0RXbytes:960(960.0b)TXbytes:960(960.0b)[server@localhost~]$exitlogoutConnectionto192.168.142.132closed.[root@localhostssh]#ls[root@localhostssh]#pwd/client/ssh[root@localhostssh]#rsync-avzserver@192.168.142.132:/var/www/html/*/client/ssh/receivingincrementalfilelista.txtb.txtindex.html#现在执行同步操作不需要输入密码sent68bytesreceived219bytes191.33bytes/sectotalsizeis27speedupis0.09[root@localhostssh]#lsa.txtb.txtindex.html#被删除的文件又从服务端同步过来了

2、基于rsync的同步源

通过系统变量实现 RSYNC_PASSWORD 客户端

[root@localhostclient]#cdrsync/[root@localhostrsync]#lsclient.txtrsync.txttest.txt[root@localhostrsync]#rm-rf*[root@localhostrsync]#exportRSYNC_PASSWORD=123456#123456为虚拟用户wang的密码[root@localhostrsync]#rsync-avzwang@192.168.142.132::share/client/rsyncreceivingincrementalfilelist./client.txtrsync.txttest.txt#现在执行同步操作不需要输入密码sent115bytesreceived265bytes760.00bytes/sectotalsizeis0speedupis0.00[root@localhostrsync]#lsclient.txtrsync.txttest.txt#被删除的文件又从服务端同步过来了

感谢各位的阅读,以上就是“rsync基本使用方法有哪些”的内容了,经过本文的学习后,相信大家对rsync基本使用方法有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!