rsync

rsync

简介:

  • linux系统下的数据镜像备份工具。
  • rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
  • 目前已支持跨平台,可以在Windows与Linux间进行数据同步。

特性:

  • 可以镜像保存整个目录树和文件系统。
  • 可以很容易做到保持原来文件的权限、时间、软硬链接等等。
  • 无须特殊权限即可安装。
  • 快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
  • 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
  • 支持匿名传输,以方便进行网站镜像。

操作流程

服务器端启动

usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
可能需要root权限运行.
/etc/rsyncd/rsyncd.conf 是你刚才编辑的rsyncd.conf的位置.
也可以在/etc/rc.d/rc.local里加入让系统自动启动等.

客户端同步

rsync -参数 用户名@同步服务器的IP::rsyncd.conf中那个方括号里的内容 本地存放路径 如:
rsync -avzP nemo@192.168.10.1::nemo /backup
说明:
-a 参数,相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;
-z 传输时压缩;
-P 传输进度;
-v 传输时的进度等信息,和-P有点关系,自己试试。可以看文档;

rsync命令

//Rsync的命令格式常用的有以下三种:
    rsync [OPTION]... SRC DEST
    rsync [OPTION]... SRC [USER@]HOST:DEST
    rsync [OPTION]... [USER@]HOST:SRC DEST
  
//对应于以上三种命令格式,rsync有三种不同的工作模式:
1)拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:
[root@Avril ~]# ll
total 4
-rw-r--r--. 1 root root    0 Sep 22  20:31 alg
-rw-------. 1 root root 1084 Jul 22 21:12 anaconda-ks.cfg
[root@Avril ~]# rsync -a alg gla
[root@Avril ~]# ll
total 4
-rw-r--r--. 1 root root    0 Sep 22  20:31 alg
-rw-------. 1 root root 1084 Jul 22 21:12 anaconda-ks.cfg
-rw-r--r--. 1 root root    0 Sep 22  20:31 gla
 
2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包 \
含单个冒号":"分隔符时启动该模式。如:
[root@Avril ~]# echo "hello world" > alg 
[root@Avril ~]# rsync -avz alg root@192.168.48.150:/root/hi
root@192.168.48.150's password: 
sending incremental file list
alg
 
sent 98 bytes  received 35 bytes  29.56 bytes/sec
total size is 12  speedup is 0.09
 
[root@Client ~]# ls
anaconda-ks.cfg  hi
[root@Client ~]# cat hi 
hello world
 
3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径 \
包含单个冒号":"分隔符时启动该模式。如:
[root@Avril ~]# rsync -avz root@192.168.48.150:/root/hi /root/hehe
root@192.168.48.150's password: 
receiving incremental file list
hi
 
sent 43 bytes  received 97 bytes  56.00 bytes/sec
total size is 12  speedup is 0.09
[root@Avril ~]# ls
alg  anaconda-ks.cfg  gla  hehe
[root@Avril ~]# cat hehe 
hello world

参数详解

-v, --verbose 详细模式输出
-q, --quiet 精简输出模式
-c, --checksum 打开校验开关,强制对文件传输进行校验
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive 对子目录以递归模式处理
-R, --relative 使用相对路径信息
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
--backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, --links 保留软链结
-L, --copy-links 像对待常规文件一样处理软链接
--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链接
--safe-links 忽略指向SRC路径目录树以外的链接
-H, --hard-links 保留硬链接
-p, --perms 保持文件权限
-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-D, --devices 保持设备文件信息
-t, --times 保持文件时间信息
-S, --sparse 对稀疏文件进行特殊处理以节省DST的空间
-n, --dry-run显示哪些文件将被传输
-W, --whole-file 拷贝文件,不进行增量检测
-x, --one-file-system 不要跨越文件系统边界
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
--existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
--delete 删除那些DST中SRC没有的文件
--delete-excluded 同样删除接收端那些被该选项指定排除的文件
--delete-after 传输结束以后再删除
--ignore-errors 即使出现IO错误也进行删除
--max-delete=NUM 最多删除NUM个文件
--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
--force 强制删除目录,即使不为空
--numeric-ids 不将数字的用户和组ID匹配为用户名和组名
--timeout=TIME IP超时时间,单位为秒
-I, --ignore-times 不跳过那些有同样的时间和长度的文件
--size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
--modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
-T --temp-dir=DIR 在DIR中创建临时文件
--compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
-P 等同于 --partial
--progress 显示备份过程
-z, --compress 对备份的文件在传输时进行压缩处理
--exclude=PATTERN 指定排除不需要传输的文件模式
--include=PATTERN 指定不排除而需要传输的文件模式
--exclude-from=FILE 排除FILE中指定模式的文件
--include-from=FILE 不排除FILE指定模式匹配的文件
--version 打印版本信息
--address 绑定到特定的地址
--config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
--port=PORT 指定其他的rsync服务端口
--blocking-io 对远程shell使用阻塞IO
-stats 给出某些文件的传输状态
--progress 在传输时显示传输过程
--log-format=formAT 指定日志文件格式
--password-file=FILE 从FILE中得到密码
--bwlimit=KBPS 限制I/O带宽,KBytes per second
-h, --help 显示帮助信息

rsync+inotify

  • rsync与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
  • 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!
  • Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。

部署rsync+inotify

环境说明

服务器类型IP地址应用操作系统
源服务器(Avril)192.168.202.138rsync ;inotify-tools ;脚本centos8
目标服务器(AVril1)192.168.48.150rsynccentos8
  • 需求:
    把源服务器上/alg目录实时同步到目标服务器的/gla/下

目标服务器(AVril1)上做以下操作:

//首先关闭防火墙和selinux
[root@AVril1 ~]# systemctl disable --now firewalld.service 
[root@AVril1 ~]# setenforce 0
[root@AVril1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config 
 
//安装rsync服务端软件
[root@AVril1 ~]# dnf -y install rsync  rsync-daemon
 
//设置rsyncd.conf配置文件
 
[root@AVril1 ~]# cat >> /etc/rsyncd.conf <<EOF
log file = /var/log/rsyncd.log    # 日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid     # pid文件的存放位置
lock file = /var/run/rsync.lock   # 支持max connections参数的锁文件
secrets file = /etc/rsync.pass    # 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件
 
[gla_from_AVril1]     # 自定义同步名称
path = /gla/          # rsync服务端数据存放路径,客户端的数据将同步至此目录
comment = sync gla from AVril1	#注释信息,可写可不写
uid = root            # 设置rsync运行权限为root
gid = root            # 设置rsync运行权限为root
port = 873            # 默认端口
ignore errors         # 表示出现错误忽略错误
use chroot = no       # 默认为true,修改为no,增加对目录文件软连接的备份
read only = no        # 设置rsync服务端为读写权限
list = no             # 不显示rsync服务端资源列表
max connections = 200     # 最大连接数
timeout = 600         # 设置超时时间
auth users = admin        # 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.202.138   # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.202.141      # 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
EOF
 
//存储的位置要事先存在,所以说我们创建一个
[root@AVril1 ~]# mkdir /gla
 
 
//创建用户认证文件,并设置文件权限
[root@AVril1 ~]# echo 'admin:123456' > /etc/rsync.pass
[root@AVril1 ~]# cat /etc/rsync.pass 
admin:123456
[root@AVril1 ~]# chmod 600 /etc/rsync.pass 
[root@AVril1 ~]# ll /etc/rsync.pass
-rw-------. 1 root root 13 Sep 22 18:14 /etc/rsync.pass
 
 
//启动rsync服务并设置开机自启动
[root@AVril1 ~]# systemctl enable --now rsyncd.service 
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@AVril1 ~]# ss -anlt
State               Recv-Q              Send-Q                           Local Address:Port                           Peer Address:Port              Process              
LISTEN              0                   5                                      0.0.0.0:873                                 0.0.0.0:*                                      
LISTEN              0                   128                                    0.0.0.0:111                                 0.0.0.0:*                                      
LISTEN              0                   128                                    0.0.0.0:22                                  0.0.0.0:*                                      
LISTEN              0                   5                                         [::]:873                                    [::]:*                                      
LISTEN              0                   128                                       [::]:111                                    [::]:*                                      
LISTEN              0                   128                                       [::]:22                                     [::]:*    

在源服务器上做以下操作:

//关闭防火墙与SELINUX
[root@Avril ~]# systemctl disable --now firewalld.service 
[root@Avril ~]# setenforce 0
[root@Avril ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config 
 
//安装rsync服务端软件,只需要安装,不要启动,不需要配置
[root@Avril ~]# yum -y install rsync 
 
//创建认证密码文件
[root@Avril ~]# echo '123456' > /etc/rsync.pass
 
//设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@Avril ~]# chmod 600 /etc/rsync.pass 
 
//在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@Avril ~]# mkdir /alg
[root@Avril ~]# touch /alg/{1..3}
[root@Avril ~]# ls /alg/
1  2  3
 
//同步时,同步目录alg/加斜杠是同步目录下的文件;同步目录alg不加斜杠是同步整个目录,这个目录会成为被同步目录的下一级目录
[root@Avril ~]# rsync -avH --port 873 --progress --delete /alg/ admin@192.168.48.150::gla_from_AVril1 --password-file=/etc/rsync.pass
sending incremental file list
./
1
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=2/4)
2
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=1/4)
3
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=0/4)
 
sent 212 bytes  received 84 bytes  592.00 bytes/sec
total size is 0  speedup is 0.00
 
//查看通过过来的文件
[root@AVril1 ~]# ls /gla/
1  2  3
 
//安装inotify-tools工具,实时触发rsync进行同步
//查看服务器内核是否支持inotify
[root@Avril ~]# ll /proc/sys/fs/inotify
total 0
-rw-r--r--. 1 root root 0 Sep 22 18:56 max_queued_events
-rw-r--r--. 1 root root 0 Sep 22 18:56 max_user_instances
-rw-r--r--. 1 root root 0 Sep 22 18:56 max_user_watches
##如果有这三个max开头的文件则表示服务器内核支持inotify
 
 
//安装inotify-tools
//先下载epel
[root@Avril ~]# dnf -y install epel-release
[root@Avril ~]# dnf -y install inotify-tools
 
//写同步脚本
//创建脚本存放目录
[root@Avril ~]# mkdir /scripts
//编辑脚本
[root@Avril ~]# vim /scripts/inotify.sh
host=192.168.48.150      # 目标服务器的ip(备份服务器)
src=/alg       # 在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
des=gla_from_AVril1     # 自定义的模块名,需要与目标服务器上定义的同步名称一致
password=/etc/rsync.pass        # 执行数据同步的密码文件
user=admin          # 执行数据同步的用户名
inotifywait=/usr/bin/inotifywait
 
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files;do
    rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$host::$des
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
 
//启动脚本
[root@Avril ~]# nohup bash /scripts/inotify.sh &
[1] 396192
[root@Avril ~]# nohup: ignoring input and appending output to 'nohup.out'
 
//创建新的文件
[root@Avril ~]# echo 'hello world' > /alg/hehe
//查看同步文件
[root@AVril1 ~]# cat /gla/alg/hehe 
hello world

设置脚本开机自动启动:

[root@Server ~]# chmod +x /etc/rc.d/rc.local 
[root@Server ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@Server ~]# tail /etc/rc.d/rc.local 
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
 
touch /var/lock/subsys/local
 
nohup /bin/bash /scripts/inotify.sh
Docker是一种开源的容器化平台,可以帮助开发人员和系统管理员更轻松地构建、部署和运行应用程序。而rsync是一个用于文件同步和备份的工具,它可以在本地或远程系统之间同步文件和目录。 要在Docker中使用rsync,你可以使用两种不同的镜像:eeacms/rsync和apnar/rsync-server。这两个镜像都可以通过Docker Hub获取。 1. 使用eeacms/rsync镜像: - 首先,你需要准备好rsync的密码文件。可以使用以下命令创建密码文件并设置权限: ```shell echo "pass" >/etc/rsync.password chmod 600 /etc/rsync.password ``` - 然后,你可以使用以下命令从Docker Hub上拉取eeacms/rsync镜像: ```shell docker pull eeacms/rsync ``` - 最后,你可以使用以下命令来构建容器并运行rsync: ```shell docker run -d --name rsync-server -p 873:873 -v /path/to/source:/data -v /etc/rsync.password:/etc/rsync.password eeacms/rsync ``` 其中,`/path/to/source`是你要同步的源文件或目录的路径。 2. 使用apnar/rsync-server镜像: - 首先,你需要准备好rsync的配置文件。可以使用以下命令创建一个简单的配置文件: ```shell echo "uid = root" >/path/to/rsyncd.conf echo "gid = root" >>/path/to/rsyncd.conf echo "[data]" >>/path/to/rsyncd.conf echo "path = /data" >>/path/to/rsyncd.conf echo "read only = false" >>/path/to/rsyncd.conf ``` - 然后,你可以使用以下命令从Docker Hub上拉取apnar/rsync-server镜像: ```shell docker pull apnar/rsync-server ``` - 最后,你可以使用以下命令来构建容器并运行rsync-server: ```shell docker run -d --name rsync-server -p 873:873 -v /path/to/rsyncd.conf:/etc/rsyncd.conf -v /path/to/source:/data apnar/rsync-server ``` 其中,`/path/to/rsyncd.conf`是你准备好的rsync配置文件的路径,`/path/to/source`是你要同步的源文件或目录的路径。 请注意,上述命令中的`-p`参数用于将容器的873端口映射到主机的873端口,以便可以通过rsync客户端访问。`-v`参数用于将主机上的文件或目录挂载到容器中,以便进行同步操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值