Rsync配置与应用

一 rsync介绍

rsync 是一个常用的 Linux 应用程序,用于文件同步。它可以在本地计算机与远程计算机之间,或者两个本地目录之间同步文件(但不支持两台远程计算机之间的同步)。它也可以当作文件复制工具,替代cp和mv命令。里面的r指的是remote,rsync 其实就是"远程同步"(remote sync)的意思。与其他文件传输工具(如 FTP 或 scp)不同,rsync 的最大特点是会检查发送方和接收方已有的文件,仅传输有变动的部分(默认规则是文件大小或修改时间有变动)。

二 rsync特点

可以镜像保存整个目录树和文件系统。可以很容易做到保持原来文件的权限、时间、软硬链接等等无须特殊权限即可安装。

快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽

安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。支持匿名传输,以方便进行网站镜像

三 rsynx ssh认证协议

rsync在ssh认证方式下,可通过系统用户进行认证,即在rsync上通过ssh隧道进行传输,类似于scp工具同步操作不在局限于rsync中定义的同步文件夹rsync server端不用启动rsync的daemon进程,只要获取remote host的用户名和密码就可以直接rsync同步文件rsync server端因为不用启动daemon进程,所以也不用配置文件/etc/rsyncd.conf.

//这种方式默认是省略了 -e ssh 的,与下面等价:
rsync -avz /SRC -e ssh root@192.168.143.106:/DEST 
    -a  //文件宿主变化,时间戳不变
    -V  //显示详细信息的过程
    -z  //压缩数据传输

[root@103 ~]# rsync -avz anaconda-ks.cfg -e ssh root@192.168.143.106:/opt/
[root@106 ~]# ls /opt/
anaconda-ks.cfg  data

//当遇到要修改端口的时候,我们可以:
#修改了ssh 协议的端口,默认是22
rsync -avz /SRC -e "ssh -p2222" root@192.168.143.106:/DEST  

四 inotify+rsync

rsync与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。

环境说明:

服务器类型ip地址应用操作系统
源服务器192.168.143.103rsynch和inotify-toolscentos8
目标服务器192.168.143.106rsynchcentos8

目标服务器操作

[root@106 ~]# yum install -y rsync
。。。。。。。。。。。略
[root@106 ~]# yum install -y rsync-daemon
。。。。。。.。。.。。略
[root@106 ~]# vim /etc/rsyncd.conf 
[root@106 ~]# cat /etc/rsyncd.conf 
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    # 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件

[etc_from_client]     # 自定义同步名称
path = /tmp/          # rsync服务端数据存放路径,客户端的数据将同步至此目录
comment = sync etc from client
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        # 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
[root@106 ~]#  tr -dc A-Za-z0-9 < /dev/urandom | head -c 8 |xargs
MvfcaV2C
[root@106 ~]# echo 'admin:MvfcaV2C' > /etc/rsync.pass
[root@106 ~]# echo 'MvfcaV2C' > .pw_rsy
[root@106 ~]# chmod 600 /etc/rsync*
[root@106 ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@106 ~]# ss -atnl
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process     
LISTEN     0           128                    0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0           5                      0.0.0.0:873                 0.0.0.0:*  

源服务器操作:

配置elerl源
yum -y install epel-release
略。。。。。.。。。...。。.。。.。。。。。。

安装inotify-tools
yum -y install inotify-tools
略。。。。。。。。。。.。。。。....

创建密码文件
[root@103 ~]# echo 'MvfcaV2C' > /etc/rsync.pass
[root@server ~]# chmod 600 /etc/rsync.pass

创建目录
[root@103 ~]# mkdir /opt/txt/ 
root@103 ~]# rsync -avH --port 873 --progress --delete /opt/txt/ admin@192.168.143.106::etc_from_client --password-file=/etc/rsync.pass
[root@server ~]# 

使用脚本

[root@103 /]# mkdir scripts
[root@103 /]# ls
bin   dev  home  lib64  mnt  proc  run   scripts  sys  usr
boot  etc  lib   media  opt  root  sbin  srv      tmp  var
[root@103 /]# cd scripts/
[root@103 scripts]# ls
[root@103 scripts]# vim inotify.sh

host=192.168.168.106      # 目标服务器的ip(备份服务器)
src=/etc        # 在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
des=etc_from_client     # 自定义的模块名,需要与目标服务器上定义的同步名称一致
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@103 scripts]# chmod +x inotify.sh 
[root@103 scripts]# ls
inotify.sh

启动脚本并挂在后台
[root@103 scripts]# nohup bash /scripts/inotify.sh &
[2] 140039

查看进程
[root@103 scripts]# ps -ef|grep inotify
root      141852    5139  0 19:47 pts/0    00:00:00 bash /scripts/inotify.sh
root      141853  141852  0 19:47 pts/0    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
root      141854  141852  0 19:47 pts/0    00:00:00 bash /scripts/inotify.sh
root      144190    5139  0 19:48 pts/0    00:00:00 grep --color=auto inotify

触发查看效果
[root@103 etc]# touch test.txt
[root@103 etc]# echo 'google' > test.txt 
[root@103 etc]# cat /etc/test.txt
google

开机自启
[root@103~ ]# chmod +x /etc/rc.d/rc.local
[root@103~ # cat /etc/rc.d/rc.local 
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# 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.

nohup /scripts/inotify.sh &
touch /var/lock/subsys/local

查看目标主机

[root@106 etc]# ls
adjtime                  GREP_COLORS               motd.d                  sasl2
aliases                  groff                     mtab                    security
alternatives             group                     nanorc                  selinux
anacrontab               group-                    netconfig               services
dconf                    libreport                 protocols                test.txt
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值