首页 > 其他分享 >rsync部署

rsync部署

时间:2022-09-23 01:44:24浏览次数:43  
标签:rsync 部署 00 etc -- root localhost

目录


rsync部署

什么是rsync

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

rsync特性

rsync支持很多特性:

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

rsync的ssh认证协议

rsync命令来同步系统文件之前要先登录remote主机认证,认证过程中用到的协议有2种:

  • ssh协议
  • rsync协议
rsync server`端不用启动`rsync`的`daemon`进程,只要获取`remote host`的用户名和密码就可以直接`rsync`同步文件
`rsync server`端因为不用启动`daemon`进程,所以也不用配置文件`/etc/rsyncd.conf

ssh认证协议跟scp的原理是一样的,如果在同步过程中不想输入密码就用ssh-keygen -t rsa打通通道

//这种方式默认是省略了 -e ssh 的,与下面等价:
rsync -avz /SRC -e ssh [email protected]:/DEST 
    -a  //文件宿主变化,时间戳不变
    -z  //压缩数据传输
 
//当遇到要修改端口的时候,我们可以:
rsync -avz /SRC -e "ssh -p2222" [email protected]:/DEST  
//修改了ssh 协议的端口,默认是22

rsync命令

//Rsync的命令格式常用的有以下三种:
    rsync [OPTION]... SRC DEST
    rsync [OPTION]... SRC [USER@]HOST:DEST
    rsync [OPTION]... [USER@]HOST:SRC DEST
//rsync常用选项:
    -a, --archive       //归档
    -v, --verbose       //啰嗦模式
    -q, --quiet         //静默模式
    -r, --recursive     //递归
    -p, --perms         //保持原有的权限属性
    -z, --compress      //在传输时压缩,节省带宽,加快传输速度
    --delete            //在源服务器上做的删除操作也会在目标服务器上同步

环境说明:

服务器类型 IP地址 应用 操作系统
源服务器 172.16.12.128 rsync inotify-tools 脚本 centos7/redhat7
目标服务器 172.16.12.129 rsync centos7/redhat7

部署rsync

目标服务器

//关闭防火墙与selinux
[root@node1 ~]# systemctl  stop firewalld
[root@node1 ~]# getenforce 
Enforcing
[root@node1 ~]# setenforce 0
[root@node1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
[root@node1 ~]# cat /etc/sysconfig/selinux 
安装rsync
[root@node1 ~]# dnf -y install rsync
CentOS-8.5.2111 - Base - mirrors.aliyun.com          28 kB/s | 3.9 kB     00:00    
CentOS-8.5.2111 - Extras - mirrors.aliyun.com        12 kB/s | 1.5 kB     00:00    
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com     40 kB/s | 4.3 kB     00:00    
Extra Packages for Enterprise Linux Modular 8 - x86  18 kB/s | 3.0 kB     00:00  
配置rsyncd.conf文件
[root@node1 ~]# 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        # 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.124.138   # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.1.1      # 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
//创建用户认证文件
[root@node1 ~]# echo 'admin:liuyang123!' > /etc/rsyncd.pass
[root@node1 ~]# cat /etc/rsyncd.pass
admin:liuyang123!
//设置文件权限
[root@node1 ~]# ll /etc/rsyncd*
-rw-------. 1 root root 1403 Sep 22 18:32 /etc/rsyncd.conf
-rw-------. 1 root root   18 Sep 22 18:33 /etc/rsyncd.pass
下载所需要的包,重启rsyncd
[root@node1 ~]# dnf -y install rsync-daemon
[root@node1 ~]# systemctl start rsyncd
[root@node1 ~]# systemctl enable rsyncd
[root@node1 ~]# ss -antl
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:22              0.0.0.0:*                
LISTEN   0        5                   [::]:873                [::]:*                
LISTEN   0        128                 [::]:22                 [::]:* 

源服务器

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]#  sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
安装epel源
[root@localhost yum.repos.d]#  yum -y install epel-release
CentOS-8.5.2111 - Base - mirrors.aliyun.com         2.0 MB/s | 4.6 MB     00:02    
CentOS-8.5.2111 - Extras - mirrors.aliyun.com        80 kB/s |  10 kB     00:00    
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com    1.4 MB/s | 8.4 MB     00:05    
创建认证密码文件,设置文件权限
[root@localhost yum.repos.d]# echo 'liuyang123!' > /etc/rsync.pass
[root@localhost yum.repos.d]# chmod 600 /etc/rsync.pass
在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@localhost yum.repos.d]# mkdir -pv /root/etc/abc
mkdir: created directory '/root/etc'
mkdir: created directory '/root/etc/abc'
[root@localhost yum.repos.d]#  rsync -avH --port 873 --progress --delete /root/etc/ [email protected]::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
deleting vmware-root_935-3980298462/
deleting vmware-root_925-3988621690/
deleting vmware-root_922-2722632355/
deleting .font-unix/
deleting .XIM-unix/
deleting .X11-unix/
deleting .Test-unix/
deleting .ICE-unix/
deleting ks-script-pu_kozs8
./
abc/

sent 74 bytes  received 214 bytes  576.00 bytes/sec
total size is 0  speedup is 0.00
目的服务器查看abc文件已经同步过来了
[root@node1 tmp]# ls
abc
安装inotify-tools
[root@localhost yum.repos.d]# yum -y install make gcc gcc-c++ inotify-tools
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# touch /scripts/inotify.sh
[root@localhost ~]# chmod +x /scripts/inotify.sh 
[root@localhost scripts]# cat inotify.sh 
host=192.168.124.135
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@localhost scripts]# nohup bash /scripts/inotify.sh &
[1] 15248
[root@localhost scripts]# nohup: ignoring input and appending output to 'nohup.out'
//启动脚本
[root@localhost scripts]# ps -ef |grep inotify
root       15248   14746  0 21:25 pts/1    00:00:00 bash /scripts/inotify.sh
root       15249   15248  0 21:25 pts/1    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       15250   15248  0 21:25 pts/1    00:00:00 bash /scripts/inotify.sh
root       15252   14746  0 21:25 pts/1    00:00:00 grep --color=auto inotify
//在源服务器上生成一个新文件
[root@localhost etc]# echo '1234' > /etc/liuyang 
//查看inotify生成的日志
[root@localhost etc]# tail /tmp/rsync.log 
20220922 21:41 /etc/liuyangCREATE was rsynced
20220922 21:41 /etc/liuyangCREATE was rsynced
设置脚本开机自动启动:
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local[root@localhost ~]# 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
到目标服务器上去查看是否把新生成的文件自动传上去了:
[root@localhost etc]# echo 'abc' > /etc/liuyang/aa
[root@node1 tmp]# ls etc/liuyang/aa 
etc/liuyang/aa
[root@node1 tmp]# cat etc/liuyang/aa
abc

标签:rsync,部署,00,etc,--,root,localhost
From: https://www.cnblogs.com/TQingS/p/16721388.html

相关文章

  • gitlab部署
    目录gitlab部署gitlab部署安装所需得包[[email protected]]#dnf-yinstallepel-releasegitopenssh-serveropenssh-clientspostfixcronie设置postfi......
  • rsync
    rsync目录rsyncrsync简介rsync特性rsync的ssh认证协议rsync命令rsync+inotifyrsync实时同步安装inotifyrsync简介rsync是linux系统下的数据镜像备份工具。使用快速增量......
  • rsync
    目录1.简介2.rsync特性3.rsync+inotify1.简介rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具RemoteSync可以远程同步,支持本地复制,或者与其他SSH、rs......
  • RSYNC
    rsync1.rsync简介rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具RemoteSync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。2.rsync特性rsy......
  • .NET 部署Https(SSL)通过代码方式
    在上一个文章中,传送门,给大家介绍了怎么在配置文件中使用 Kestrel部署Https,正好今天有小伙伴稳问到:可以通过代码的方式实现 Kestrel的Https的部署吗?答案是肯定的......
  • 【云原生】MySQL on k8s 环境部署
    目录一、概述二、开始部署(一主两从)1)添加源2)修改配置3)开始安装4)测试验证5)Prometheus监控6)卸载一、概述MySQL是一个关系型数据库管理系统,由瑞典MySQLAB公司开发,属于Ora......
  • rsync
    rsyncrsync是什么rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具RemoteSync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。rsync特性1)可以镜......
  • rsync
    rsync目录rsyncrsync是什么rsync特性rsync的ssh认证协议rsync命令rsync实时同步目标服务器上做以下操作:在目标服务器上做以下操作:测试脚本rsync是什么rsync是linux系统......
  • gitlab部署
    Gitlab部署//配置yum源[root@localhost~]#curl-o/etc/yum.repos.d/CentOS-Base.repohttps://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo[root@localhost......
  • minikube部署
    安装minikubecurl-LOhttps://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64sudoinstallminikube-linux-amd64/usr/local/bin/minikube......