Linux article: RSYNC data synchronization service

Mission background

In order to ensure the security of developers' online codes, a company now needs to back up developers' codes.

img

Mission requirements

  1. The backup machine needs to regularly synchronize all files in the /app/java_project directory of the MIS server at 1:03 a.m. every day.
  2. It is required to record synchronization logs to facilitate analysis of reasons for synchronization failures.

Task breakdown

  1. Choose appropriate backup tools and methods for backup (scp can be implemented, but it is not perfect -> rsync)
  2. Learn how to use the tool of your choice
  3. Write a script (super program) to execute scheduled tasks

Involving knowledge points

  • Use of rsync command (new knowledge point)
  • How to use rsync as a background program (new knowledge point)
  • crontab scheduled tasks (old knowledge points)

Course objectives

  • Able to use rsync command to achieve local file synchronization (similar to cp)
  • Able to use rsync command to achieve remote file synchronization (similar to scp)
  • Ability to use rsync as a background program for data synchronization

Theoretical reserves

1. Introduction to rsync

  • Good sister of rsync

    • sync: Flushes the file system cache, forces modified data blocks to be written to disk, and updates the superblock.
    • async asynchronous: Put the data in the buffer first, and then synchronize it to the disk periodically (usually 30s).
    • rsync remote synchronization: remote synchronous
  • Features of rsync

    • Can mirror the entire directory tree and file system
    • You can retain the original permissions (permission, mode), owner, group, time (modification time, modify time), soft and hard links, file acl, file attribute information, etc.
    • High transmission efficiency, using synchronization algorithm, only comparing changed (incremental backup) file1.txt file2.txt file3.txt (A server) rsync realizes data synchronization => only synchronizes file3.txt => incremental backup file1.txt file2 .txt(B server)
    • Supports anonymous transmission to facilitate website mirroring; it can also be used for verification to enhance security

What is the difference between rsync and scp?

```powershell

Both can achieve remote synchronization, but relatively speaking, rsync has stronger capabilities.

① Support incremental backup ② Maintain the original attributes of files during data synchronization```

2. Syntax of rsync

  • man rsync

```powershell NAME rsync — a fast, versatile, remote (and local) file-copying tool //一种快速、通用、远程(和本地)的文件复制工具

SYNOPSIS Local: rsync [OPTION...] SRC... [DEST]

Access via remote shell:
   //通过远程shell访问(命令)
     Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
     Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

   Access via rsync daemon:
   //通过后台程序访问(作为服务)
     Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
           rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
     Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
           rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

```

  • rsync命令参数

powershell -v 详细模式输出 -a 归档模式,递归的方式传输文件,并保持文件的属性,equals -rlptgoD -r 递归拷贝目录 -l 保留软链接 -p 保留原有权限 -t 保留原有时间(修改) -g 保留属组权限 -o 保留属主权限 -D 等于--devices --specials 表示支持b,c,s,p类型的文件 -R 保留相对路径 -H 保留硬链接 -A 保留ACL策略 -e 指定要执行的远程shell命令 -E 保留可执行权限 -X 保留扩展属性信息 a属性

三、rsync命令使用

1. 本机同步

```powershell 注意: 1. 本地数据同步的时候,源目录后面的“/”会影响同步的结果 # rsync -av /dir1/ /dir3 //只同步目录下面的文件到指定的路径 # rsync -av /dir1 /dir2 //将当前目录dir1和目录下的所有文件一起同步

  1. -R:不管加不加"/",都会将源数据的绝对路径一起同步 # rsync -avR /dir1/ /dir2/
  2. --delete:删除目标目录里多余的文件 # rsync -avR --delete /dir1/ /dir2/ ```

2. 远程同步

```powershell pull: rsync -av user@host:/path local/path

rsync -av [email protected]:/etc/hosts /dir1/

rsync -av [email protected]:/backup /dir1

push: rsync -av local/path user@host:/path

rsync -av /dir1/aa1 [email protected]:/home/code

```

思考:

rsync远程同步数据时,默认情况下为什么需要密码?如果不想要密码同步怎么实现?

```powershell 因为两台Linux服务器在连接时,默认使用的还是SSH协议。由于没有做免密登录,所以还是需要输入对方服务器的密码。 如果不想输入密码,可以使用免密登录来实现。

JumpServer操作

ssh-keygen

ssh-copy-id [email protected]

```

四、rsync作为服务使用

思路:

对外提供服务——>端口监听——>启动服务——>启动脚本(没有)——>配置文件(修改需求)

  1. 启动它(找启动脚本—>没找到)
  2. 寻求帮助

powershell man 1 rsync man 5 rsyncd.conf rsync --help Use "rsync --daemon --help" to see the daemon-mode command-line options. 结论: rsync后台程序去启动,那么rsync --daemon [options]

  1. 尝试启动rsync程序

powershell [root@jumper ~]# rsync --daemon Failed to parse config file: /etc/rsyncd.conf 说明:先尝试以后台程序的方式启动它 原因:没有配置文件(默认没有) 解决:创建配置文件 [root@jumper ~]# touch /etc/rsyncd.conf [root@jumper ~]# rsync --daemon [root@jumper ~]# ps -ef|grep rsync root 3814 1 0 11:43 ? 00:00:00 rsync --daemon root 3817 2826 0 11:44 pts/0 00:00:00 grep rsync [root@jumper ~]# netstat -nltup|grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3814/rsync tcp 0 0 :::873 :::* LISTEN 3814/rsync 结论: 1. rsync启动时必须要读取配置文件,如果没有报错 2. rsync默认情况下,端口是873,协议tcp

  1. 了解rsync的配置文件

powershell man 5 rsyncd.conf 全局的参数 port = xxx pid file = xxx 。。。 局部模块 [模块名1] path = /dir1 uid = xxx gid = xxx log file = xxx max connections =4 [模块名2] path = /dir2 uid =xxx gid = xx

任务解决方案

任务分析

powershell 1. 备份的本质 文件的拷贝,cp scp rsync 2. 选择工具 rsync: 1)作为服务不需要交互 2)可以实现增量拷贝 3. 掌握工具的使用 命令: 服务:编写配置文件/etc/rsyncd.conf——>rsync --daemon——>873

实施步骤

```powershell 环境准备: server:10.1.1.1 backup:10.1.1.250

一、以下操作在server(10.1.1.1)服务端完成 1. 在线上环境(server)创建相应的目录 [root@server ~]# mkdir /app/javaproject -p 以下数据是为了测试用: [root@server ~]# touch /app/javaproject/file{1..10}.java [root@server ~]# mkdir /app/javaproject/aa{1..3} 2. 在线上环境中将rsync作为后台程序运行 1)创建配置文件 [root@server ~]# cat /etc/rsyncd.conf [app] path = /app/javaproject log file = /var/log/rsync.log 2)启动rsync服务 rsync --daemon 3)验证服务成功启动 [root@server ~]# netstat -nltp|grep 873

二、备份机器backup(10.1.1.250)操作 1. 编写脚本来同步文件 [root@backup ~]# mkdir /backup/app1java -p [root@backup ~]# rsync -a [email protected]:: 查看服务端的模块名字 app
[root@backup ~]# cat rsync
java.sh

!/bin/bash

rsync -av [email protected]::app /backup/app1_java/ &>/dev/null

[root@backup ~]# chmod +x rsyncjava.sh 2. 交给计划任务去执行 [root@backup ~]# crontab -e [root@backup ~]# crontab -l 03 01 * * * /root/rsyncjava.sh 3. 测试验证 1)通过修改系统时间验证 [root@backup ~]# date -s "2018-12-29 01:02:50" Sat Dec 29 01:02:50 CST 2018 2)查看计划人任务日志 [root@backup ~]# tail -f /var/log/cron Dec 29 01:03:17 MissHou CROND[4410]: (root) CMD (/root/rsyncjava.sh) 3)查看数据文件是否同步过来 [root@backup ~]# ll /backup/app1java/ total 12 drwxr-xr-x 2 root root 4096 Dec 28 15:14 aa1 drwxr-xr-x 2 root root 4096 Dec 28 15:14 aa2 drwxr-xr-x 2 root root 4096 Dec 28 15:14 aa3 -rw-r--r-- 1 root root 0 Dec 28 15:14 file10.java -rw-r--r-- 1 root root 0 Dec 28 15:14 file1.java -rw-r--r-- 1 root root 0 Dec 28 15:14 file2.java -rw-r--r-- 1 root root 0 Dec 28 15:14 file3.java -rw-r--r-- 1 root root 0 Dec 28 15:14 file4.java -rw-r--r-- 1 root root 0 Dec 28 15:14 file5.java -rw-r--r-- 1 root root 0 Dec 28 15:14 file6.java -rw-r--r-- 1 root root 0 Dec 28 15:14 file7.java -rw-r--r-- 1 root root 0 Dec 28 15:14 file8.java -rw-r--r-- 1 root root 0 Dec 28 15:14 file9.java ```

任务总结

  1. rsync干嘛用的?
  2. rsync特点:增量同步、保留文件的属性信息
  3. rsync服务好处,不需要密码,873/tcp,对服务本身做一些限制
  4. 该任务所选择方法有很多种: 1) 配置免密码登录,然后将命令写到脚本里,交给计划任务完成,缺陷:每次全量拷贝 2) 将rsync做成后台程序(服务),然后将命令写到脚本里,交给计划任务完成

课后扩展

思考:如果为了考虑安全性,既想要使用后台服务运行,又想要密码如何实现?

1. 需要密码同步

环境

backup:10.1.1.250 /backup

app1-server:10.1.1.1 /app/java_project

① 修改主配置文件

powershell vim /etc/rsyncd.conf motd file=/etc/rsyncd.welcome 消息文件 [app1] comment=共享给客户端看到的名字,可以自己定义 path=/app/java_project auth users = user1,user2 secrets file = /etc/rsyncd.secrets

② 创建安全用户,该文件不能被其他人查看

```powershell [root@app1-server ~]# vim /etc/rsyncd.secrets
user1:123 user2:123 [root@app1-server ~]# ll /etc/rsyncd.secrets -rw-r--r-- 1 root root 20 Aug 29 09:57 /etc/rsyncd.secrets [root@app1-server ~]# chmod 600 /etc/rsyncd.secrets

注意:密码文件的名字必须和配置文件里定义的保持一致

创建一个消息文件 [root@app1-server ~]# echo "welcome to itcast 哈哈哈" > /etc/rsyncd.welcome ```

③ 测试验证

```powershell [root@app1-server ~]# ps -ef|grep rsync root 2962 1 0 09:48 ? 00:00:00 rsync --daemon root 3034 2534 0 09:59 pts/0 00:00:00 grep rsync [root@app1-server ~]# pkill -9 rsync [root@app1-server ~]# ps -ef|grep rsync root 3038 2534 0 09:59 pts/0 00:00:00 grep rsync

[root@app1-server ~]# rsync --daemon

在backup服务器上测试: [root@backup ~]# rsync -av [email protected]::app1 /backup/ welcome to itcast 哈哈哈

Password: receiving incremental file list ./ aa1/ aa2/ aa3/

sent 70 bytes received 259 bytes 13.43 bytes/sec total size is 0 speedup is 0.00

注意: user1用户是服务器端app1-server上的密码文件里定义的用户;不是当前操作系统的用户。 ```

说明: 服务端rsync服务是以什么用户运行,则必须保证secrets file文件拥有者必须是同一个;

假设root运行rsync --daemon,则secrets file的owner也必须是root;secrets file权限必须是600

总结:

  • 没有密码有好处也有坏处,好处是不需要密码方便写脚本做远程同步;坏处就是不安全,但你可以使用防火墙等来加强安全。

  • 同步可能出现的问题:

    • 如果同步报permission denied这种,可能是服务端selinux没有关闭
    • 同步时间慢:
  • * * 解决方法:绑定对方主机名

  • 如果你希望有密码,可以用rsyncd本身自带的secrets file来做用户验证

2. rsync+inotify实时同步

需求1:将主机A上的/dir1目录的数据实时同步到A主机(本机)的/dir2目录里

```powershell 1. 安装inotify软件

tar xf inotify-tools-3.13.tar.gz -C /usr/src/

cd /usr/src/inotify-tools-3.13/

./configure

make

make install

安装完后,就会产生下面两个命令 /usr/local/bin/inotifywait 等待 /usr/local/bin/inotifywatch 看守

  1. 编写脚本来同步

    !/bin/bash

/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib,move /dir1 |while read events do rsync -a --delete /dir1 /dir2/ echo "date +%F\ %T出现事件$events" >> /var/log/rsync.log 2>&1 done

chmod +x 1.sh

nohup ./1.sh &

--delete 参数:如果目标目录里的数据比源目标多,那么需要删除无用的相关的数据

  1. 测试验证

如有如下错误: @ERROR: chroot failed //rsyncd.conf文件中path路径不存在导致 rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] ```

需求2:

app1-server服务器上的/app/java_project目录的文件和backup主机上的/backup目录实时同步

```powershell 环境: app1-server 10.1.1.1 backup 10.1.1.250 分析: rsync本身不可以做到数据的实时同步,需要借助第三方工具,intotify工具。实现线上环境目录发生改变立马同步到backup主机,是单向同步。

步骤: 1. 在app1-server上安装inotify工具 将软件包上传到虚拟主机上(app1-server) [root@app1-server ~]# ls /soft/ inotify-tools-3.13.tar.gz [root@app1-server ~]# tar xf inotify-tools-3.13.tar.gz [root@app1-server ~]# ls /soft/ inotify-tools-3.13 inotify-tools-3.13.tar.gz [root@app1-server ~]# cd inotify-tools-3.13

[root@app1-server ~]# ./configure [root@app1-server ~]# make [root@app1-server ~]# make install [root@app1-server ~]# ll /usr/local/bin/ total 80 -rwxr-xr-x 1 root root 38582 Aug 29 10:48 inotifywait -rwxr-xr-x 1 root root 40353 Aug 29 10:48 inotifywatch

  1. 查看命令如何使用,然后编写脚本来实现目录的监控 注意:该脚本应该在app1-server上运行 [root@app1-server ~]# inotifywait --help -m 保持监控状态 -r 递归监控 -q 只打印事件 -e 指定事件

事件: move 移动 delete 删除 create 创建 modify 修改 attrib 属性信息

编写脚本实时监控/app/java_project目录 [root@app1-server ~]# echo hello hello [root@app1-server ~]# echo hello|while read i;do echo $i;done hello 解释:上一条命令执行的结果hello赋值给i变量,然后打印出i变量的值。

vim /root/1.sh

!/bin/bash

/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib,move /app/javaproject |while read events do rsync -a --delete /app/javaproject/ 10.1.1.250:/backup echo "date +%F\ %T出现事件$events" >> /var/log/rsync.log 2>&1 done

chmod +x 1.sh 增加可执行权限

./1.sh & 将脚本放到后台去运行

注意: 如果单纯使用命令去推的话,正常情况下需要密码,不利于脚本编写,在这里提供2中解决方案: 1)设置免密码登录 2)在backup服务器上将rsync作为后台程序运行

  1. 测试验证 app1-server上操作目录: [root@app1-server javaproject]# mkdir bbb{1..3} [root@app1-server javaproject]# rm -f file{1..3}

tail -f /var/log/rsync.log 2018-08-29 11:13:35出现事件/app/javaproject/ CREATE,ISDIR bbb1 2018-08-29 11:13:35出现事件/app/javaproject/ CREATE,ISDIR bbb2 2018-08-29 11:13:35出现事件/app/javaproject/ CREATE,ISDIR bbb3 2018-08-29 11:14:15出现事件/app/javaproject/ DELETE file1 2018-08-29 11:14:15出现事件/app/javaproject/ DELETE file2 2018-08-29 11:14:15出现事件/app/javaproject/ DELETE file3 ```

3. rsync托管xinetd

独立服务:独立启动脚本 ssh ftp nfs dns ...

依赖服务: 没有独立的启动脚本 rsync telnet 依赖xinetd服务(独立服务)

  1. 平时不占用系统的运行资源
  2. xinetd服务管理依赖服务
  3. 一些轻量级服务会托管给xinetd服务

如何将rsync托管给xinetd服务去管理?

```powershell 1. 安装相应的软件 [root@jumper ~]# yum -y install xinetd

  1. 查看软件类别 [root@jumper ~]# rpm -ql xinetd /etc/rc.d/init.d/xinetd /etc/xinetd.conf /etc/xinetd.d xinetd服务管理的所有轻量级服务的目录 /usr/sbin/xinetd /usr/share/man/man5/xinetd.conf.5.gz

  2. 查看rsync软件列表 [root@jumper ~]# rpm -ql rsync /etc/xinetd.d/rsync /usr/bin/rsync

  3. 了解配置文件 1)xinetd服务的配置文件 man 5 xinetd.conf onlyfrom 只允许访问 noaccess 拒绝访问 accesstimes 控制访问服务的时间段 logtype 指定日志类型 interface 并发连接数 per_source 每个IP的最大连接数

2)rsync配置文件 /etc/xinetd.d/rsync

service rsync { disable = no //开关;no表示开启该服务;yes表示关闭服务 flags = IPv6 sockettype = stream wait = no user = root server = /usr/bin/rsync serverargs = --daemon logonfailure += USERID }

  1. 把rsync服务的开关打开
  2. 启动xinetd服务 [root@jumper ~]# service xinetd restart Stopping xinetd: [ OK ] Starting xinetd: [ OK ] [root@jumper ~]# netstat -nltp|grep 873 tcp 0 0 :::873 :::* LISTEN 6209/xinetd

到此,rsync服务就托管给了xinetd服务管理;后续需要有些需求,修改配置文件: /etc/rsyncd.conf /etc/xinetd.d/rsync ```

课后实战

Remotely synchronize the /share/dir1 directory on host A to the /backup/dir2 directory on host B. The requirements are as follows:

1. Record the log to /var/log/rsyncd.log

2. The shared module is required to be hidden (that is, the client cannot see the module name)

3. Only one client can connect to synchronize this module at the same time.

4. Only xxxx (IP you customize) can be allowed to synchronize this module

Guess you like

Origin blog.csdn.net/zhiqi_l163991102/article/details/131244798