应用场景
- 作为命令使用: 临时拉取,推送数据, scp命令
- 定时备份: rsync服务+定时任务
- 实时同步: rsync_sersync/lsyncd
- rsync服务于异地容灾
选项
| rsync选项 | 含义 |
|---|
| -a | -rlptgoD-r递归复制-l复制软连接-p保持权限不变-m保持修改时间不变-o所有者不变-g用户组不变-D devices specials 设备与特殊文件 |
| -v | 显示过程 |
| -z | 传输数据的时候进行 压缩 (公网),与限速选项冲突(极限情况下需要考虑加上-z选项) |
| -P | 显示每rsync -av . root@10.0.0.31:/tmp/ exclude=04 rsync -av . root@10.0.0.31:/mnt exclude={04,05}个文件传输过程 (进度条) 断点续传 partial progress |
| --bwlimit | 限速,注意不要与-z一起使用. |
| --exclude--exclude-from | 排除 |
| --delete | 保持客户端与服务端数据一致(高度保持2者一致,一般用于实时同步,其他场景慎用) |
Rsync使用模式
本地模式: 不推荐使用
rsync -a /etc/ /tmp/
rsync -a /etc /opt/
在rsync中对于目录 /etc/ 和 /etc是有区别的
/etc /etc目录+目录内容
/etc/ /etc/目录下面的内容
远程模式: 数据传输,临时使用scp代替
| rsync -a | 源 | 目标 |
|---|
| 推送 | /etc/hosts | root@10.0.0.31:/tmp/ |
| 拉取 | root@10.0.0.31:/ect/hosts | /tmp/ |
rsync -a /etc/hosts root@10.0.0.31:/tmp/
rsync -av /etc root@10.0.0.31:/tmp/
rsync -av /etc root@10.0.0.31:/tmp/
touch /etc/test.txt
rsync -av /etc root@10.0.0.31:/tmp/
scp -r /etc/ root@10.0.0.31:/opt/
-r 递归传输
rsync守护进程rsyncd
准备环境
| 角色 | 主机名 | ip |
|---|
| rsync服务端 | backup | 10.0.0.41/172.16.1.41 |
| rsync客户端 | nfs01 | 10.0.0.31/172.16.1.31 |
服务端使用流程
- 部署
- 配置 /etc/rsyncd.conf
- 准备环境(用户,目录,目录权限,服务端密码文件,共享目录)
- 启动与检查(端口,进程)测试(传输)
安装
检查安装 更新
yum install -y rsync
检查软件包内容
/etc/rsyncd.conf
/usr/bin/rsync
/usr/lib/systemd/system/rsyncd.service
配置
fake super =yes
uid = rsync
gid = rsync
use chroot = no
max connections = 2000
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.password
[data]
comment = 注释内容
path = /data
后续配置
useradd -s /sbin/nologin -M rsync
密码文件格式: 用户名:密码
echo 'rsync_backup:123' >/etc/rsync.password
chmod 600 /etc/rsync.password
mkdir /data/
chown rsync.rsync /data/
启动服务
systemctl enable rsyncd
systemctl start rsyncd
ps -ef |grep rsync
ss -lntup |grep rsync
backup服务端本地测试
[root@backup ~]
Password:
sending incremental file list
hostname
sent 102 bytes received 43 bytes 41.43 bytes/sec
total size is 7 speedup is 0.05
[root@backup ~]
total 4
-rw-rr 1 rsync rsync 7 Aug 16 16:02 hostname
访问测试(客户端)
[root@nfs01 ~]
Password:
sending incremental file list
hosts
sent 214 bytes received 43 bytes 73.43 bytes/sec
total size is 329 speedup is 1.28
免密码传输数据到服务端

其他
访问控制-安全措施
- hosts allow 只准许指定的ip或网段访问.
- hosts deny 拒绝.
配置只准许172.16.1.0/24 网段访问
备份服务项目案例
项目实施步骤(概述)
| 备份项目的步骤 | 流程 |
|---|
| 1.备份所有机器,定时备份,备份数据放在备份服务器 | |
| 2.线准备rsync服务端,测试rsync客户端 | 专门备份模块backup 目录/backup/ |
| 3. rsync客户端书写备份脚本. | 1. 打包备份/etc/ /var/spool/cron/ 2个目录,年-月-日_周几 2. 存放在/backup/172.16.1.xxx/ 3. 备份发送到rsync服务端 4. 删除本地旧的备份(7,30),写入定时 增加校验功能 |
| 4. rsync服务端书写 检查脚本 | 1. 检查每天的备份结果,mail命令发送即可. 2. 删除旧的备份180天之前的,保留每周一的备份(360天). .每个月第1号(保留720天) 3.每年6月1日数据永久保留不删除. |
项目ip地址规划
| 角色 | 主机名 | ip地址公网 | ip内网 |
|---|
| rsync服务端 | backup | 10.0.0.41 | 172.16.1.41 |
| rsync客户端 | nfs01 | 10.0.0.31 | 172.16.1.31 |
| rsync客户端 | web01 | 10.0.0.7 | 172.16.1.7 |
rsync服务端与客户端
服务端主要步骤修改配置文件.
添加个backup 对应的目录/backup/
[backup]
comment = this is a comment
path = /backup
重启服务
systemctl restart rsyncd.service
客户端测试
rsync -avz /etc/hosts rsync_backup@backup::backup --password-file=/etc/rsync.clent
rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.clent
rsync客户端上的备份脚本
rsync服务端调试
mailx 配置/etc/mail.rc
yum install -y mailx postfix
启动postfix服务,开机自启动
服务端脚本内容要求
- 检查每天的备份结果,mail命令发送即可.
- 检查md5sum结果(一共,成功,失败) md5sum -c xxx.md5
- 简单tree+mail
- 复杂:统计备份大小,数量,然后通过echo/cat +mail邮件
- 删除旧的备份180天之前的,保留每周一的备份(360天).
- 扩展:每个月第1号(保留720天)
- 扩展:每年6月1日数据永久保留不删除.
find /backup/ -type f -name "*.tar.gz" ! -name "*_1.tar.gz" -mtime +180 |xargs rm -f
-name "*.tar.gz" 找出周1到周日的备份.
! -name "*_1.tar.gz" 排除周一的备份.剩下的备份就是周二到周日
发送邮件
tree /backup/ |mailx xxxx
脚本
check_result=/backup/result.txt
date_str=`date +%F+%w`
function log(){
script_name=`basename $0`
log_file=/var/log/${script_name}.log
level=$1
msg=$2
time=`date +%F_%w_%T`
echo "${time} [${level}] ${msg}" >>$log_file
}
md5sum -c /backup/*/md5.txt >$check_result
if [ $? -eq 0 ];then
log NOTICE "md5检查成功"
else
log NOTICE "md5检查失败"
exit 1
fi
du -sh /backup/* >>$check_result
bak_cnt=`find /backup/ -type f -name "*.tar.gz"|wc -l`
bak_today_cnt=`find /backup/ -type f -name "*${date_str}*.tar.gz"|wc -l`
echo "备份文件总数:${bak_cnt},今日备份文件总数:${bak_today_cnt}" >>$check_result
file_cnt=`find /backup/ -type f -name "*.tar.gz" ! -name "*_1.tar.gz" -mtime +180|wc -l`
if [ $file_cnt -ne 0 ];then
find /backup/ -type f -name "*.tar.gz" ! -name "*_1.tar.gz" -mtime +180|xargs rm -rf
echo "删除180天之前的备份成功">>$check_result
else
log WARNING "180天之前的日志数量为0"
fi
file_cnt=`find /backup/ -type f -name "*_1.tar.gz" -mtime +360|wc -l`
if [ $file_cnt -ne 0 ];then
find /backup/ -type f -name "*_1.tar.gz" -mtime +360|xargs rm -rf
echo "删除360天之前的备份成功">>$check_result
else
log WARNING "360天之前的日志数量为0"
fi
cat $check_result |mail -s "备份结果" 182160328@qq.com