Rsync:数据同步软件使用方法教程LinuxServer

印迹发布于:2019-12-3 1015

简介

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。

Rsync数据同步工具,它的特点是开源的,速度快,功能多;可以实现全量和增量本地或者远程数据同步功能

Rsync适用于 : unix , linux ,windows等多平台操作系统

常在定时任务中备份数据时使用


三种方法:

1. 本地间数据同步,类似cp命令

2. 网络间两台不同IP服务器间数据同步

3. 以socket进程监听方式进行数据同步

第一:本地间传送格式

rsync  -avz 源目录  目标目录

这是增量同步方式速度快只传对方没有的

rsync -avz --delete  A目录    B目录

还可以实现删除 ,A有的B没有的把A的传给B,如果B有的A没有,那么就把B有的删除

第二:网络间传送

需要先安装个远程传输插件

yum -y install openssh-clients

格式是

rsync -avz 源服务器目录  root@对方服务器IP:对方服务器目标路径

第三:socket进程监听方式传送

服务端的准备工作

1 .创建配置文件

2 .创建共享目录,并创建执行用户账号rsync

3. 将共享目录也就是模块名改属主和属组都为执行账号rsync

4. 创建虚拟账号密码文件

echo "虚拟账号名(在配置文件的模块内有):密码">/etc/rsync.password

5. 将账号密码文件改为600权限

6. 启动服务

rsync --daemon

7. 加入开机启动

echo "rsync --daemon"> /etc/rc.local


客户端准备工作:

1. 创建密码文件

2. 将密码文件改为600权限

推送格式 (免交互推送)定时任务中常用

rsync -avz 客户端路径 虚拟用户名@服务端IP::模块名 --password -file=/etc/rsync.password

示例1(通过rsync结合crontab(任务计划)实现按时自动同步备份)

通过rsync可以结合crontab(任务计划)实现按时自动备份,将服务器server的某目录文件数据自动按时备份到客户端client主机下指定的目录中,实现文件主从同步备份,客户端和服务端都要安装rsync,安装完后有些系统不会生成rsyncd.conf,需要自己创建在/etc/rsync.d/rsyncd.conf

【在centos7.4上安装rsync会默认生成/etc/rsyncd.conf文件,但是在centos6.9上安装后则不会生成,并且如果在要自己定义文件位置,以守护进程方式启动,那么仍然要在/etc/下新建一个rsyncd.conf的文件,否则无法启动。】


     部署环境:
        服务器server:xx.xx.202.94  centos 6
        客户端client:  xx.xx.218.115 centos7
  • 服务器server安装rsync并配置
[root@xxx~]# yum install -y rsync

创建配置文件
[root@xxx ~]# mkdir -p /etc/rsync.d
[root@xxx ~]# touch /etc/rsync.d/rsyncd.conf
[root@xxx ~]# chmod 600 /etc/rsync.d/rsync.conf

编辑rsync.conf配置文件的内容
[root@xxx ~]#  vim /etc/rsync.d/rsync.conf

log file =/var/log/rsyncd.log     
pid file =/tmp/rsyncd.pid
lock file =/tmp/rsync.lock

# port = 8873                # 取消注释后则指定rsync服务的端口为8873,默认是873,指定了端口的在客户端执行同步时命令中需要加上“--port=8873”参数
# address = xx.xx.202.94   #取消注释后则指定rsync服务器的IP为xx.xx.202.94 

[bakhome]         #定义同步的模块名称
path=/home/wwwroot      #要备份的目录
uid=root
gid=root

ignore errors = yes            # 忽略IO错误
read only = no           #  指定是否允许客户端上传文件,若为 yes 则不允许上传;若为 no 并且服务器目录也具有读写权限则允许上传.
write only = no          #  指定是否允许客户端下载文件.若为 yes 则不允许下载;   若为 no 并且服务器目录也具有读权限则允许下载.
hosts allow = xx.xx.218.115            #允许哪个客户端来同步
hosts deny = *          #拒绝的客户端
max connections = 5    #最大客户端连接数
list = no                        #  list 意思是把rsync 服务器上提供同步数据的目录在服务器上模块是否显示列出来。默认是yes 。如果你不想列出来,就no ;如果是no是比较安全的,至少别人不知道你的服务器上提供了哪些目录
auth users = bakfen      #定义同步认证用户可以随便取,但必须与server.pass中的用户名一致
secrets file = /etc/rsync.d/server.pass       #认证用户密码文件


创建服务器认主用户密码文件并修改权限

[root@xxx ~]# echo 'bakfen:bakfile951' > /etc/rsync.d/server.pass    #bakfen 为认证用户名,bakfile951为密码
[root@xxx ~]# chmod 600  /etc/rsync.d/server.pass

服务器端启动rsync

[root@xxx ~]# rsync --daemon --config=/etc/rsync.d/rsyncd.conf      # –-daemon是让rsync 以服务器模式运行,并指向配置文件路径
[root@xxx ~]# ps -aux | grep rsync       #检测rsync是否启动成功
root      7043  0.0  0.0 107636   688 ?        Ss   15:18   0:00 rsync --daemon --config=/etc/rsync.d/rsyncd.conf
root      7086  0.0  0.0 103340   900 pts/2    S+   16:00   0:00 grep rsync

服务器端停止rsync服务
[root@xxxrsync.d]# killall rsync

  •  客户端client的安装与配置
[root@localhost~]# yum install -y rsync

创建同步用户的密码文件及修改权限

[root@localhost~]# echo 'bakfile951' > /etc/rsync_client.pass    #创建客户端同步时认证密码文件该密码必须与server.pass中密码相同,让rsync同步读取该密码文件而不用手动输入密码
[root@localhost ~]# chmod 600  /etc/rsync_client.pass

在客户端执行同步将服务器端的文件备份过来

[root@localhost~]# rsync  -arzvtopg  --delete  --password-file=/etc/rsync_client.pass   bakfen@xx.xx.202.94::bakhome  /home/rsyncbak    
                 # -vzrtopg里的
                          v是verbose,
                          z 是压缩,
                          r 是recursive,
                          topg 都是保持文件原有属性如属主、时间的参数
                    -a参数,相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;
                    --delete :表示客户端上的数据要与服务器端完全一致,如果/home/rsyncbak中有服务器上不存在的文件,则删除。最终目的是让 /home/rsyncbak目录上的数据完全与服务器(/home/wwwroot )上保持一致.
                    --password-file=/etc/rsync_client.pass  同步时读取密码文件
                    bakfen@xx.xx.202.94::bakhome   bakfen 服务器server.pass中认证用户;xx.xx.202.94::bakhome  服务器IP及服务器中rsync.conf中模块名称bakhome
                    /home/rsyncbak  文件同步后保存在本客户端的目录路径,可以自定为其他目录

若提示:

rsync:failed to connect to xx.xx.214.190: No route to host(113)
rsyncerror: error in IO(code 10) at clientserver.c(124) [receiver=3.0.6]

原因是服务端的防火墙没有放行策略,关闭防火墙;

[root@localhost~]# systemctl stop
firewalld 或  service iptables stop

然后再次执行:

[root@localhost~]# rsync  -arzvtopg  --delete  --password-file=/etc/rsync_client.pass   bakfen@xx.xx.202.94::bakhome  /home/rsyncbak
receiving incremental file list
./
aa.log
aaa/
aaa/public_html/
aaa/public_html/
aaa/public_html/index.html
aaa/public_html/errpage/
aaa/public_html/errpage/400.html
aaa/public_html/errpage/401.html
aaa/public_html/errpage/403.html
aaa/public_html/errpage/404.html
aaa/public_html/errpage/405.html
aaa/public_html/errpage/500.html
aaa/public_html/errpage/503.html
sent 234 bytes  received 7,531 bytes  1,411.82 bytes/sec

同步成功了,文件保存到了/home/rsyncbak目录下。

客户端设置定时备份:添加crontab任务计划,每天定时同步文件

[root@localhost ~]# crontab-e     # 编辑任务计划
* * * * * rsync  -arzvtopg  --delete  --password-file=/etc/rsync_client.pass   bakfen@xx.xx.202.94::bakhome  /home/rsyncbak    # 设定每分钟同步
0 3 * * * rsync  -arzvtopg  --delete  --password-file=/etc/rsync_client.pass   bakfen@xx.xx.202.94::bakhome  /home/rsyncbak    # 设定在每天3点时同步


相关资料

rclone使用介绍

Gclone:Rclone开源修改版



http://www.virplus.com/thread-379.htm

转载请注明:2019-12-3 于 VirPlus 发表

推荐阅读
最新回复 (0)

    ( 登录 ) 后,可以发表评论!

    返回