Linux-rsync 服务器客户端模式同步

rsync(Remote Sync,远程同步)

rsync 两种认证协议:

① SSH 协议(默认)

② rsync 协议。C/S架构(客户端-服务器模式),默认端口 873。

安装

bash 复制代码
rpm -qa |grep rsync
yum -y install rsync
rsync --version

① 使用方式1:SSH 协议

bash 复制代码
### 本地目录同步到远程
rsync -avz source/ username@remote_host:destination
### 远程目录同步到本地
rsync -av username@remote_host:source/ destination

### `-e` 指定 SSH 端口
rsync -avz -e 'ssh -p 22' source/ user@remote_host:/destination

② 使用方式2:rsync协议(rsync-daemon模式)

### rsync 协议服务端配置

bash 复制代码
vim /etc/rsyncd.conf

```
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass

uid = root
gid = root
port = 873
ignore errors
use chroot = no
read only = no
list = no
max connections = 300
timeout = 600
auth users = backup
hosts allow = 192.168.10.0/24,192.168.20.0/24
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[logSync]
path=/opt/backup/logs
comment=sync logs from client
```

echo 'backup:123456' > /etc/rsync.pass
chmod 600 /etc/rsync.pass


systemctl start rsyncd
systemctl restart rsyncd
systemctl enable rsyncd

开启 rsync 服务端端口

bash 复制代码
# firewall-cmd --zone=public --add-port=873/tcp --permanent && firewall-cmd --reload
iptables -I INPUT -p tcp --dport 873 -j ACCEPT
service iptables save

查看模块列表(需要服务端配置:list = yes)

bash 复制代码
rsync rsync://192.168.110.100

### rsync协议客户端配置

bash 复制代码
echo '123456' > /etc/rsync.pass
chmod 600 /etc/rsync.pass

rsync 协议客户端命令格式1:

bash 复制代码
sudo rsync -avH --port 873 --progress --delete /opt/app/logs/xxx/ backup@192.168.110.100::logSync/xxx/192.168.10.11 --password-file=/etc/rsync.pass

rsync 协议客户端命令格式2:

bash 复制代码
sudo rsync -avH --port 873 --progress --delete /opt/app/logs/xxx/ rsync://backup@192.168.110.100/logSync/xxx/192.168.10.11 --password-file=/etc/rsync.pass

拓展

① 定时同步(与crontab结合)

bash 复制代码
crontab -e
```
*/5 * * * * rsync -aq --port 873 --progress --delete /opt/app/logs/{xxx}/ backup@192.168.110.100::logSync/{xxx}/192.168.10.11 --password-file=/etc/rsync.pass
```

② 实时备份(inotify)

bash 复制代码
rpm -qa inotify-tools
yum install -y inotify-tools

vim /opt/inotify.sh
```
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /opt/app/logs/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/rsync.pass /opt/app/logs/ backup@192.168.110.100::logSync/"
#使用while、read持续获取监控结果,根据结果可以作进一步判断是否读取到输出的监控记录
$INOTIFY_CMD | while read DIRECTORY FILE EVENT
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
    #如果rsync未在执行,则立即启动
        $RSYNC_CMD
    fi
done
```

③ Nginx 配置日志目录示例

bash 复制代码
location /{xxx} {
    root /opt/backup/logs;
    autoindex on;                # 显示目录
    autoindex_exact_size off;    # 显示文件大小
    autoindex_localtime on;        # 显示文件时间为服务器时间格式
}
相关推荐
其实防守也摸鱼几秒前
无线网络安全---WLAN相关安全工具--kali(理论附题目)
linux·安全·web安全·学习笔记·kali·命令模式·wlan
uesowys1 小时前
CentOS Linux安装部署OpenClaw
linux·centos·安装部署openclaw
IMPYLH2 小时前
Linux 的 rm 命令
linux·运维·服务器·网络·bash
YIN_尹2 小时前
【Linux系统编程】进程地址空间
linux·c++
代码中介商3 小时前
手把手教你Linux 打包压缩与 gcc 编译详解
linux·运维·服务器·编译·打包·压缩
longerxin20203 小时前
阿里云AlmaLinux操作系统允许root登录配置步骤
linux·服务器·阿里云
独小乐3 小时前
019.ADC转换和子中断|千篇笔记实现嵌入式全栈/裸机篇
linux·c语言·驱动开发·笔记·嵌入式硬件·mcu·arm
GottdesKrieges4 小时前
OceanBase租户级物理恢复
linux·oceanbase
2601_949817724 小时前
基础篇:Linux安装redis教程(详细)
linux·运维·redis
CQU_JIAKE4 小时前
4.17[Q]
java·linux·服务器