1. rsync补充
1.1 访问控制功能
# 白名单-允许连接rsync服务的网段,一般是内网
hosts allow = 172.16.1.0/24
# 黑名单-拒绝访问的网段
hosts deny = 0.0.0.0/32
1.2 rsync命令
- -P:显示备份过程
- –bwlimit=500:设置传输速率,与-z冲突。默认是KB,也可用M
- –delete:极其危险。保证客户端与服务端内容一模一样(服务端多余的内容会被删除),一般用于实时同步场景。
2. rsync本地备份
2.1 本地rsync客户端脚本
[root@nfs01 /server/scripts]# cat rsync_backup2.sh
#!/bin/bash
##############################################################
# File Name:rsync_backup2.sh
# Version:V1.0
# Author:SunKexu
# Organization:www.oldboyedu.com
# Desc:本地rsync同步备份脚本
##############################################################
export LANG=en_US.UTF-8
# vars
# rsync用户名
username="rsync_backup"
# rsync服务端ip或主机名
server_ip="backup"
# 模块名
bak_module="backup"
# 客户端备份目录
bak_dir="/backup/"
# 客户端ip,该ip需要被服务端成功解析
ip=`hostname -I |awk '{print $1}'`
# 日期,作为文件名
date=`date +%F_%w`
# 备份的文件名
file_name=${date}.tar.gz
# 客户端密码文件
client_password="/etc/rsync-client.password"
# command
# tar
backup_files(){
[ ! -d "${bak_dir}${ip}" ] && mkdir -p "${bak_dir}${ip}"
tar zcf "${bak_dir}${ip}/${file_name}" /etc/
[ $? -eq 0 ] || {
echo "tar failed"
exit 1
}
}
# md5sum校验信息
create_md5(){
md5sum "${bak_dir}${ip}/${file_name}" >"${bak_dir}${ip}/${file_name}.md5"
[ $? -eq 0 ] || {
echo "md5sum failed"
exit 2
}
}
# rsync同步备份
send_backup_files(){
rsync -avz "${bak_dir}" "${username}"@"${server_ip}"::"${bak_module}" --password-file="${client_password}"
[ $? -eq 0 ] || {
echo "rsync failed"
exit 3
}
}
# 删除旧的备份文件
delete_files(){
find "${bak_dir}" -type f -name "*.tar.gz" -mtime +7 -o -type f -name "*.md5" -mtime +7 | xargs rm -f
[ $? -eq 0 ] || {
echo "delete old files failed"
exit 4
}
}
# main
main(){
backup_files
create_md5
send_backup_files
delete_files
}
main
[root@nfs01 /backup]# bash /server/scripts/rsync_backup2.sh
tar: Removing leading `/' from member names
sending incremental file list
./
10.0.0.31/
10.0.0.31/2025-06-13_5.tar.gz
10.0.0.31/2025-06-13_5.tar.gz.md5
sent 5,300,795 bytes received 73 bytes 10,601,736.00 bytes/sec
total size is 5,349,188 speedup is 1.01
[root@nfs01 /backup]# ll 10.0.0.31/
总用量 5228
-rw-r--r-- 1 root root 5349116 6月 13 17:52 2025-06-13_5.tar.gz
-rw-r--r-- 1 root root 72 6月 13 17:52 2025-06-13_5.tar.gz.md5
# 查看下服务端文件
[root@backup /backup]# ll 10.0.0.31/
总用量 5228
-rw-r--r-- 1 rsync rsync 5349116 6月 13 17:52 2025-06-13_5.tar.gz
-rw-r--r-- 1 rsync rsync 72 6月 13 17:52 2025-06-13_5.tar.gz.md5
2.2 本地定时任务
- 定时任务是客户端执行
0 0 * * * bash /server/scripts/rsync_backup2.sh >/dev/null 2>&1
2.3 本地发送检查邮件
- 服务端定时发送邮件
- 下载邮件服务:
yum install -y mailx sendmail
- 添加邮箱配置信息,文件:
/etc/mail.rc
- 下载邮件服务:
set from=skx2554798585@163.com
set smtp=smtp.163.com
set smtp-auth-user=skx2554798585@163.com
set smtp-auth-password=CPVZ6355KsCtEUik
set smtp-auth=login
- 编写检查脚本并发送邮件
[root@backup /backup/10.0.0.31]# cat /server/scripts/check_rsync_backup.sh
#!/bin/bash
##############################################################
# File Name:check_rsync_backup.sh
# Version:V1.0
# Author:SunKexu
# Organization:www.oldboyedu.com
# Desc:检查备份的压缩包,并发送邮件
##############################################################
export LANG=en_US.UTF-8
# vars
tmp_file=`mktemp`
bak_dir="/backup/"
mail_list="skx2554798585@qq.com"
date=`date +%F_%w`
title="每日备份检查---${date}"
# command
# md5sum check
echo "md5校验:" > ${tmp_file}
find "${bak_dir}" -type f -name "${date}.tar.gz.md5" |xargs md5sum -c >> ${tmp_file}
# 文件数量
file_count=`find ${bak_dir} -type f -name "${date}.tar.gz.md5" |wc -l`
echo "备份文件总数:${file_count}" >>${tmp_file}
# 备份大小
bak_size=`du -sh ${bak_dir} |cut -f1`
echo "备份目录:${bak_dir},占用空间:${bak_size}" >> ${tmp_file}
# 发送邮件
cat ${tmp_file} |mail -s "${title}" "${mail_list}"
# 清理临时文件
\rm -f ${tmp_file}
- 添加定时任务
[root@backup /backup/10.0.0.31]# crontab -l
#1. sync time by lidao996 at 20230101
*/3 * * * * /sbin/ntpdate ntp.aliyun.com >/dev/null 2>&1
#2. 定时检查备份目录并发送邮件
0 8 * * * bash /server/scripts/check_rsync_backup.sh >/dev/null 2>&1
3. rsync异地容灾
- curl ifconfig.io:查看本机公网ip
- systemctl cat rsync:查看服务的配置文件
3.1 环境准备
角色 | 机器 | 主机名 | ip |
---|---|---|---|
本地rsync服务端 | 本地虚拟机-backup | backup | 10.0.0.41/172.16.1.41 |
本地rsync客户端 | 本地虚拟机-nfs01 | nfs01 | 10.0.0.31/172.16.2.31 |
本地rsync客户端 | 本地虚拟机-web01 | web01 | 10.0.0.7/172.16.1.7 |
异地容灾rsync服务端 | 阿里云服务器 | aliyun-backup |
3.2 开放云服务器安全组和关闭防火墙
- 开放tcp的873端口,正是rsync服务
- 源ip是自己的公网ip
- 关闭云服务器的防火墙
[root@aliyun-ubuntu ~]# systemctl disable --now ufw.service
Synchronizing state of ufw.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable ufw
Removed /etc/systemd/system/multi-user.target.wants/ufw.service.
[root@aliyun-ubuntu ~]# systemctl is-active ufw.service
inactive
[root@aliyun-ubuntu ~]# systemctl is-enabled ufw.service
disabled
3.3 rsync服务端配置-云服务器
- 编辑rsync服务端配置文件
[root@aliyun-ubuntu ~]# cat /etc/rsyncd.conf
#created by oldboy 15:01 2009-6-5
##rsyncd.conf start##
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
#hosts allow = 172.16.1.0/24
#hosts deny = 0.0.0.0/32
auth users = aliyun_rsync_backup
secrets file = /etc/rsync.password
#####################################
[data]
comment = www by old0boy 14:18 2012-1-13
path = /data
[backup]
comment = www by old0boy 14:18 2012-1-13
path = /backup
- 创建rsync虚拟用户
[root@aliyun-ubuntu ~]# id rsync
id: 'rsync': no such user
[root@aliyun-ubuntu ~]# useradd -s /sbin/nologin -M rsync
[root@aliyun-ubuntu ~]# id rsync
uid=1003(rsync) gid=1003(rsync) groups=1003(rsync)
- 创建rsync认证用户的密码文件
echo "aliyun_rsync_backup:SKX2554." > /etc/rsync.password
[root@aliyun-ubuntu ~]# chmod 600 /etc/rsync.password
- 创建模块的目标目录
[root@aliyun-ubuntu ~]# mkdir /backup/
[root@aliyun-ubuntu ~]# chown -R rsync:rsync /backup/
- 启动服务并设置为开机自启动,检查程序和端口
[root@aliyun-ubuntu ~]# systemctl enable --now rsync
Synchronizing state of rsync.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable rsync
[root@aliyun-ubuntu ~]# systemctl is-active rsync
active
[root@aliyun-ubuntu ~]# systemctl is-enabled rsync
enabled
[root@aliyun-ubuntu ~]# ps -ef |grep [r]sync
root 64205 1 0 12:12 ? 00:00:00 /usr/bin/rsync --daemon --no-detach
[root@aliyun-ubuntu ~]# ss -lntup |grep [r]sync
tcp LISTEN 0 5 0.0.0.0:873 0.0.0.0:* users:(("rsync",pid=64205,fd=5))
tcp LISTEN 0 5 [::]:873 [::]:* users:(("rsync",pid=64205,fd=6))
3.4 客户端配置
- 异地容灾客户端就是本地备份的服务端
- 创建rsync用户的密码文件
[root@backup ~]# echo "SKX2554." > /etc/rsync-client.password
3.4.1 备份脚本
[root@backup /server/scripts]# cat rsync_backup.sh
#!/bin/bash
##############################################################
# File Name:rsync_backup.sh
# Version:V1.0
# Author:SunKexu
# Organization:www.oldboyedu.com
# Desc:异地rsync同步备份脚本
##############################################################
export LANG=en_US.UTF-8
# vars
username="aliyun_rsync_backup"
server_ip="520skx.com"
bak_module="backup"
bak_dir="/backup/"
client_password="/etc/rsync-client.password"
# command
# rsync同步备份
send_backup_files(){
rsync -avz "${bak_dir}" "${username}"@"${server_ip}"::"${bak_module}" --password-file="${client_password}"
[ $? -eq 0 ] || {
echo "rsync failed"
exit 1
}
}
send_backup_files
3.4.2 定时任务
[root@backup /server/scripts]# crontab -l
#1. sync time by lidao996 at 20230101
*/3 * * * * /sbin/ntpdate ntp.aliyun.com >/dev/null 2>&1
#2. 定时检查备份目录并发送邮件
0 8 * * * bash /server/scripts/check_rsync_backup.sh >/dev/null 2>&1
#3. 同步备份文件到云服务器
0 1 * * * bash /server/scripts/rsync_backup.sh >/dev/null 2>&1
3.5 云服务检查脚本与发送邮件
3.5.1 云服务器配置邮件服务
- 下载邮件服务:
apt install -y s-nail
- 邮件服务的配置文件:
/etc/s-nail.rc
- 云服务器的SMTP使用的是加密协议,端口是465
set from=skx2554798585@163.com
set smtp=smtps://smtp.163.com:465
set smtp-auth-user=skx2554798585@163.com
set smtp-auth-password=CPVZ6355KsCtEUik
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
3.5.2 检查脚本
[root@aliyun-ubuntu /server/scripts/rsync]# cat check_rsync_backup.sh
#!/bin/bash
##############################################################
# File Name: check_rsync_backup.sh
# Version: V1.0
# Author: SunKexu
# Organization: www.oldboyedu.com
# Description:check rsync backup files,and send mail
##############################################################
export LANG=en_US.UTF-8
# vars
date=`date +$F_%w`
bak_dir="/backup/"
file=`mktemp`
title="aliyun-backup_check-${date}"
mail="skx2554798585@qq.com"
# command
# md5 check
echo "md5 check:" >${file}
find ${bak_dir} -type f -name "*.tar.gz.md5" |xargs md5sum -c >>${file}
# file nums
echo "today backup file nums:" >>${file}
find ${bak_dir} -type f -name "*.tar.gz.md5" | wc -l >> ${file}
# backup size
echo "backup dir size:" >>${file}
du -sh ${bak_dir} |cut -f1 >>${file}
# send mail
cat ${file} |s-nail -s "${title}" ${mail}
3.5.3 定时任务
[root@aliyun-ubuntu /server/scripts/rsync]# crontab -l
# rsync backup check
0 8 * * * bash /server/scripts/rsync/check_rsync_backup.sh >/dev/null 2>&1
4. 踩坑记录
1. rsync: --password-file:/etc/rsync.password: unknown option
- 选项错误
- 模块前应该有两个冒号
2. The --password-file option may only be used when accessing an rsync daemon.
- only be used when accessing an rsync daemon:仅在访问rsync守护进程时使用
- 修改密码文件后要重启rsync服务
3. @ERROR: auth failed on module backup
- 查看rsync的日志:tail -f /var/log/rsyncd.log
- password mismatch:密码不匹配
- 原因是密码文件错误,命令行中的密码文件是用户认证的密码文件;而客户端使用时需自建密码文件,并修改密码文件的权限
4. s-nail: Unexpected EOF on SMTP connection
- 原因:s-nail的配置文件中应该是smtps协议,不是smtp
5. 思维导图
【金山文档】 思维导图 https://www.kdocs.cn/l/co3I7PtpTYQX