2021-11-14 15:52:46 +08:00

102 lines
3.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "rsync 命令"
date: 2019-10-29T21:21:14+08:00
lastmod: 2019-10-29T21:21:14+08:00
keywords: []
tags: ["rsync", "同步"]
categories: ["shell"]
---
## 简介
- rsyncRemote Sync远程同步是一个开源的快速备份工具可以在不同主机之间镜像同步整个目录树支持增量备份保持链接和权限且采用优化的同步算法传输前执行压缩因此非常适用于异地备份、镜像服务器等应用。
- 支持:
- 本地复制
- 与其他SSH同步
- 与rsync主机同步
## rsyncd 源服务
- 创建 rsync 配置文件 /etc/rsyncd.conf内容如下
```ini
# 限制客户端登陆身份为 nobody:nobody
uid = nobody
gid = nobody
# 禁锢在源目录下
use chroot = yes
# 指定监听端口,默认监听 tcp 873 端口
port = 873
# 指定监听地址,默认监听全部网卡
address = 192.168.1.101
# 允许访问的客户端地址
hosts allow = 192.168.1.0/24
max connections = 4
# 日志位置
log file = /var/log/rsyncd.log
# PID 文件位置
pid file = /var/run/rsyncd.pid
exclude = lost+found/
transfer logging = yes
timeout = 900
ignore nonreadable = yes
# 同步时不压缩的文件类型
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
# 账户文件,每行格式 user:password权限 600
secrets file = /etc/rsyncd_users.db
[data1]
# 源目录实际路径,注意该目录需要对 nobody 用户可读
path = /path/to/data1
# 描述
comment = sth about dir data1
# 只读
read only = yes
# 授权账户
auth users = backuper
```
- 创建备份帐号信息
```bash
echo 'backuper:123456' > /etc/rsyncd_users.db
chmod 0600 /etc/rsyncd_users.db
```
- 启动 rsyncd 服务
```bash
systemctl start rsyncd
```
## rsync 命令选项
- -r: 递归模式,包含目录及子目录中所有文件
- -l: 对于符号链接文件仍然复制为符号链接文件
- -p: 保留文件的权限标记
- -t: 保留文件的时间标记
- -g: 保留文件的属组标记(仅超级用户使用)
- -o: 保留文件的属主标记(仅超级用户使用)
- -D: 保留设备文件及其他特殊文件
- -a: 归档模式,递归并保留对象属性,等同于 -rlptgoD
- -v: 显示同步过程的详细(verbose)信息
- -z: 在传输文件时进行压缩(compress)
- -H: 保留硬连接文件
- -A: 保留ACL属性信息
- --delete: 删除目标位置有而原始位置没有的文件
- --checksum: 根据对象的校验和来决定是否跳过文件
- --password-file: 指定包含密码的文件
## 简单使用
- 本地复制,类似 cp 命令
```bash
rsync -a /etc/passwd 123.txt
```
- 与其他 ssh 同步
```bash
rsync -av root@192.168.1.100:/root/123.txt .
```
- 与 rsync 主机同步
```bash
rsync -avz backuper@192.168.1.100::data1 /root
# 或者
rsync -avz rsync://backuper@192.168.1.100/data1 /root
```
- 向同步源服务上传文件时,直接调换源参数和目的参数,注意客户端可写上传目录
## 参考
- [https://www.linuxidc.com/Linux/2019-08/160108.htm](https://www.linuxidc.com/Linux/2019-08/160108.htm)