iis服务器助手广告
返回顶部
首页 > 资讯 > 精选 >rsync基本使用方法有哪些
  • 190
分享到

rsync基本使用方法有哪些

2023-06-27 23:06:04 190人浏览 八月长安
摘要

这篇文章主要讲解了“rsync基本使用方法有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“rsync基本使用方法有哪些”吧!rsync是可以实现增量备份的工具。配合任务计划,rsync能

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

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

rsync基本使用方法有哪些

一、rsync命令的用法:

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

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

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

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

-H 保留硬链接文件 

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

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

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

-p 保留文件的权限标记 

-t 保留文件的时间标记 

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

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

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

二、配置rsync

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

服务端

#在服务端网站首页写入一些内容[root@localhost Desktop]# cd /var/www/html[root@localhost html]# vim index.html[root@localhost html]# cat index.htmlHello World!Hello Jaking![root@localhost html]# ifconfigeth0      Link encap:Ethernet  HWaddr 00:0C:29:BE:68:3F           inet addr:192.168.142.132  Bcast:192.168.142.255  Mask:255.255.255.0         inet6 addr: fe80::20c:29ff:febe:683f/64 Scope:Link         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1         RX packets:580 errors:0 dropped:0 overruns:0 frame:0         TX packets:390 errors:0 dropped:0 overruns:0 carrier:0         collisions:0 txqueuelen:1000         RX bytes:57739 (56.3 KiB)  TX bytes:41856 (40.8 KiB)lo        Link encap:Local Loopback           inet addr:127.0.0.1  Mask:255.0.0.0         inet6 addr: ::1/128 Scope:Host         UP LOOPBACK RUNNING  MTU:16436  Metric:1         RX packets:16 errors:0 dropped:0 overruns:0 frame:0         TX packets:16 errors:0 dropped:0 overruns:0 carrier:0         collisions:0 txqueuelen:0         RX bytes:960 (960.0 b)  TX bytes:960 (960.0 b)[root@localhost rsync]# service Httpd restartStopping httpd:                                            [  OK  ]Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName                                                          [  OK  ]

客户端

#客户端能成功访问服务端网站首页的内容[root@localhost Desktop]# curl 192.168.142.132Hello World!Hello Jaking!

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

三、基于ssh的同步源

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

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

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

[root@localhost html]# useradd server[root@localhost html]# passwd serverChanging passWord for user server.New password:BAD PASSWORD: The password is shorter than 8 charactersRetype new password:passwd: all authentication tokens updated successfully.

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

[root@localhost Desktop]# mkdir /client[root@localhost Desktop]# cd /client/[root@localhost client]# mkdir ssh[root@localhost client]# rsync -avz server@192.168.142.132:/var/www/html/* /client/sshserver@192.168.142.132's password:receiving incremental file listindex.htmlsent 68 bytes  received 219 bytes  114.80 bytes/sectotal size is 27  speedup is 0.0930 bytes  received 104 bytes  15.76 bytes/sectotal size is 27  speedup is 0.20[root@localhost client]# cd ssh[root@localhost ssh]# lsindex.html[root@localhost ssh]# cat index.htmlHello World!Hello Jaking!#客户端已成功同步服务端数据

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

#在客户端创建新文件,准备同步到服务端。[root@localhost ssh]# touch a.txt b.txt[root@localhost ssh]# lsa.txt  b.txt  index.html[root@localhost ssh]# rsync -avz /client/ssh/* server@192.168.142.132:/var/www/htmlserver@192.168.142.132's password:sending incremental file lista.txtb.txtrsync: mkstemp "/var/www/html/.a.txt.6JDDzO" failed: Permission denied (13)rsync: mkstemp "/var/www/html/.b.txt.p7hCLz" failed: Permission denied (13)sent 131 bytes  received 50 bytes  40.22 bytes/sectotal size is 27  speedup is 0.15rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]#同步失败,从报错结果可以server用户权限不足,server用户对/var/www/html目录没有写权限。

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

[root@localhost html]# setfacl -m user:server:rwx /var/www/html

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

[root@localhost ssh]# rsync -avz /client/ssh/* server@192.168.142.132:/var/www/htmlserver@192.168.142.132's password:sending incremental file lista.txtb.txtsent 131 bytes  received 50 bytes  51.71 bytes/sectotal size is 27  speedup is 0.15#由同步的过程可以看出,index.html没有被上传,由此可知rsync使用的同步机制是增量备份的机制。

在服务端查看:

[root@localhost html]# lsa.txt  b.txt  index.html#客户端数据已成功同步到服务端

四、基于rsync的同步源

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

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

1.修改配置文件:

服务端

[root@localhost html]# vim /etc/rsyncd.conf#若配置文件不存在则直接创建[root@localhost html]# cat /etc/rsyncd.confaddress = 192.168.142.132port 873pid file = /var/run/rsyncd.pidlog file = /var/log/rsyncd.log[share]   comment = soft   path = /server/rsync   read only = yes   dont compress = *.gz *.bz2 *.zip   auth users = wang   secrets file = /etc/rsyncd_users.db[root@localhost html]# vim /etc/rsyncd_users.db[root@localhost html]# cat /etc/rsyncd_users.dbwang:123456 #rsync不支持复杂密码,尽量设简单一点。[root@localhost html]# vim /etc/xinetd.d/rsync[root@localhost html]# cat /etc/xinetd.d/rsynC# default: off# description: The rsync server is a good addition to an ftp server, as it \#   allows crc checksumming etc.service rsync{   disable = yes   flags       = IPv6   Socket_type     = stream   wait            = no   user            = root   server          = /usr/bin/rsync   server_args     = --daemon   log_on_failure  += USERID}[root@localhost html]# rsync --daemon #启动rsync[root@localhost html]# netstat -pantu | grep 873tcp        0      0 192.168.142.132:873     0.0.0.0:*               LISTEN      6779/rsync          [root@localhost html]# mkdir -p /server/rsync[root@localhost html]# cd !$cd /server/rsync[root@localhost rsync]# touch rsync.txt[root@localhost rsync]# lsrsync.txt[root@localhost rsync]# chmod 600 /etc/rsyncd_users.db #一定要给密码文件赋予600权限,否则同步数据将出错!

2.执行同步操作:

客户端

[root@localhost rsync]# rsync -avz wang@192.168.142.132::share /client/rsyncPassword:receiving incremental file list./rsync.txtsent 77 bytes  received 151 bytes  50.67 bytes/sectotal size is 0  speedup is 0.00[root@localhost rsync]# lsrsync.txt#数据同步成功[root@localhost rsync]# pwd/client/rsync

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

服务端

#在执行上行同步前一定要修改模块权限和ACL权限[root@localhost rsync]# vim /etc/rsyncd.conf[root@localhost rsync]# cat /etc/rsyncd.confaddress = 192.168.142.132port 873pid file = /var/run/rsyncd.pidlog file = /var/log/rsyncd.log[share]   comment = soft   path = /server/rsync   read only = no #这里一定要改为no   dont compress = *.gz *.bz2 *.zip   auth users = wang   secrets file = /etc/rsyncd_users.db[root@localhost rsync]# setfacl -m u:nobody:rwx /srver/rsync #设置ACL权限[root@localhost rsync]# pkill rsync #关闭rsync[root@localhost rsync]# rsync --daemon #启动rsync

客户端

[root@localhost rsync]# touch client.txt[root@localhost rsync]# rsync -avz /client/rsync/* wang@192.168.142.132::sharePassword:sending incremental file listclient.txtsent 85 bytes  received 27 bytes  32.00 bytes/sectotal size is 0  speedup is 0.00#上行同步成功

在服务端查看:

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

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

客户端

[root@localhost rsync]# lsclient.txt  rsync.txt[root@localhost rsync]# touch test.txt[root@localhost rsync]# rsync -avz /client/rsync/* rsync://wang@192.168.142.132/sharePassword:sending incremental file listtest.txtsent 102 bytes  received 27 bytes  28.67 bytes/sectotal size is 0  speedup is 0.00

服务端

[root@localhost rsync]# lsclient.txt  rsync.txt  test.txt

五、配置免密码验证

1、基于SSH的同步源

通过秘钥对实现 客户端

[root@localhost ssh]# pwd/client/ssh[root@localhost ssh]# lsa.txt  b.txt  index.html[root@localhost ssh]# rm -rf *[root@localhost ssh]# ssh-keygenGenerating 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:3D:fe:c8:0e:2c:b7:90:b0:f4:0d:31:af:b4:d3:9e:87 root@localhost.localdomainThe key's randomart image is:+--[ RSA 2048]----+|                 ||                 ||      o          ||       + .       ||    o o S o      ||   . = O . .     ||    . O *..      ||       *E=.o     ||        +o+ .    |+-----------------+[root@localhost ssh]#[root@localhost ssh]# ssh-copy-id server@192.168.142.132server@192.168.142.132's password:Now try logging into the Machine, with "ssh 'server@192.168.142.132'", and check in: .ssh/authorized_keysto make sure we haven't added extra keys that you weren't expecting.[root@localhost ssh]# id server #server用户在服务端id: server: No such user[root@localhost ssh]# ssh server@192.168.142.132[server@localhost ~]$ ifconfig#成功登录服务端eth0      Link encap:Ethernet  HWaddr 00:0C:29:BE:68:3F           inet addr:192.168.142.132  Bcast:192.168.142.255  Mask:255.255.255.0         inet6 addr: fe80::20c:29ff:febe:683f/64 Scope:Link         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1         RX packets:935 errors:0 dropped:0 overruns:0 frame:0         TX packets:660 errors:0 dropped:0 overruns:0 carrier:0         collisions:0 txqueuelen:1000         RX bytes:112043 (109.4 KiB)  TX bytes:89842 (87.7 KiB)lo        Link encap:Local Loopback           inet addr:127.0.0.1  Mask:255.0.0.0         inet6 addr: ::1/128 Scope:Host         UP LOOPBACK RUNNING  MTU:16436  Metric:1         RX packets:16 errors:0 dropped:0 overruns:0 frame:0         TX packets:16 errors:0 dropped:0 overruns:0 carrier:0         collisions:0 txqueuelen:0         RX bytes:960 (960.0 b)  TX bytes:960 (960.0 b)[server@localhost ~]$ exitlogoutConnection to 192.168.142.132 closed.[root@localhost ssh]# ls[root@localhost ssh]# pwd/client/ssh[root@localhost ssh]# rsync -avz server@192.168.142.132:/var/www/html/* /client/ssh/receiving incremental file lista.txtb.txtindex.html#现在执行同步操作不需要输入密码sent 68 bytes  received 219 bytes  191.33 bytes/sectotal size is 27  speedup is 0.09[root@localhost ssh]# lsa.txt  b.txt  index.html#被删除的文件又从服务端同步过来了

2、基于rsync的同步源

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

[root@localhost client]# cd rsync/[root@localhost rsync]# lsclient.txt  rsync.txt  test.txt[root@localhost rsync]# rm -rf *[root@localhost rsync]# export RSYNC_PASSWORD=123456 #123456为虚拟用户wang的密码[root@localhost rsync]# rsync -avz wang@192.168.142.132::share /client/rsyncreceiving incremental file list./client.txtrsync.txttest.txt#现在执行同步操作不需要输入密码sent 115 bytes  received 265 bytes  760.00 bytes/sectotal size is 0  speedup is 0.00[root@localhost rsync]# lsclient.txt  rsync.txt  test.txt#被删除的文件又从服务端同步过来了

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

--结束END--

本文标题: rsync基本使用方法有哪些

本文链接: https://www.lsjlt.com/news/314546.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • rsync基本使用方法有哪些
    这篇文章主要讲解了“rsync基本使用方法有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“rsync基本使用方法有哪些”吧!rsync是可以实现增量备份的工具。配合任务计划,rsync能...
    99+
    2023-06-27
  • netstat基本使用方法有哪些
    本文小编为大家详细介绍“netstat基本使用方法有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“netstat基本使用方法有哪些”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。netstat命令是一个监控T...
    99+
    2023-06-27
  • Git基本使用方法有哪些
    这篇文章主要介绍了Git基本使用方法有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。创建版本库$ git clone   ...
    99+
    2023-06-27
  • Dockerfile基本使用方法有哪些
    本篇内容主要讲解“Dockerfile基本使用方法有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Dockerfile基本使用方法有哪些”吧!Dockerfile的组成Dockerfile分...
    99+
    2023-06-28
  • SQLite3基本使用方法有哪些
    本文小编为大家详细介绍“SQLite3基本使用方法有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“SQLite3基本使用方法有哪些”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。一、基本语法1.常用指令.op...
    99+
    2023-07-02
  • Kafka基本使用方法有哪些
    Kafka的基本使用方法包括以下几个方面:1. 安装和配置:首先需要在服务器上安装Kafka,并进行必要的配置,例如指定监听端口、设...
    99+
    2023-09-13
    Kafka
  • vim的基本使用方法有哪些
    本文小编为大家详细介绍“vim的基本使用方法有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“vim的基本使用方法有哪些”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。Vim(Vi[Improved])编辑器是...
    99+
    2023-06-27
  • Hive的基本使用方法有哪些
    这篇文章主要介绍“Hive的基本使用方法有哪些”,在日常操作中,相信很多人在Hive的基本使用方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Hive的基本使用方法有哪些”的疑惑有所帮助!接下来,请跟...
    99+
    2023-06-27
  • Git的基本使用方法有哪些
    这篇文章主要介绍了Git的基本使用方法有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Git是一个开源的分布式版本控制系统,用以有效、高速的处理从很小到非常大的项目版本管...
    99+
    2023-06-27
  • MySQL的基本使用方法有哪些
    今天小编给大家分享一下MySQL的基本使用方法有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。MySQL 是最流行的关系...
    99+
    2023-06-27
  • Typescript的基本使用方法有哪些
    本篇内容主要讲解“Typescript的基本使用方法有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Typescript的基本使用方法有哪些”吧!超集是集合...
    99+
    2024-04-02
  • Python类的基本使用方法有哪些
    1、面向对象类(class):是一种用来描述具有相同属性和方法的对象的集合。类变量:类变量在整个实例化的对象中是公用的。一般定义在类中且在函数体之外。方法:类中的函数数据成员:类变量或者实例变量用于处理类及其实例对象的相关的数据。方法重写:...
    99+
    2023-05-18
    Python
  • linux中awk基本使用方法有哪些
    这篇文章给大家分享的是有关linux中awk基本使用方法有哪些的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显...
    99+
    2023-06-27
  • Angular路由基本使用方法有哪些
    这篇文章主要讲解了“Angular路由基本使用方法有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Angular路由基本使用方法有哪些”吧!1. 摘要简单来说地址栏中,不同的地址(URL...
    99+
    2023-07-04
  • update语句的基本使用方法有哪些
    1. 修改单个列的值:```sqlUPDATE 表名 SET 列名=新值 WHERE 条件;```示例:```sql...
    99+
    2023-06-13
    update语句
  • Python闭包的基本使用方法有哪些
    这篇文章主要介绍“Python闭包的基本使用方法有哪些”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Python闭包的基本使用方法有哪些”文章能帮助大家解决问题。什么是闭包闭包(Closure)是一...
    99+
    2023-07-05
  • Oracle数据库基本使用方法有哪些
    本文小编为大家详细介绍“Oracle数据库基本使用方法有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“Oracle数据库基本使用方法有哪些”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。Oracle数据库是目...
    99+
    2023-06-27
  • insert语句的基本使用方法有哪些
    插入语句的基本使用方法如下:1. 插入单行数据:```INSERT INTO table_name (column1, column...
    99+
    2023-06-14
    insert语句
  • Vue3中Typescript的基本使用方法有哪些
    这篇文章主要介绍“Vue3中Typescript的基本使用方法有哪些”,在日常操作中,相信很多人在Vue3中Typescript的基本使用方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Vue3中Ty...
    99+
    2023-06-27
  • 笔记本电脑的基本使用方法有哪些
    这篇“笔记本电脑的基本使用方法有哪些”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“笔记本电脑的基本使用方法有哪些”文章吧。笔...
    99+
    2023-07-01
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作