rsync是增量备份的工具,不同于命令cp\scp的全量备份
参考文章
环境
- system: centos8
- version: rsync-3.1.3
节点架构
源主机:数据来源的主机
目标主机:备份数据的主机
源主机 | 目标主机 | |
---|---|---|
节点名 | node001 | node002 |
节点地址 | 192.168.1.2 | 192.168.1.3 |
组件安装 | lsyncd(服务端)rsync(客户端,服务端) | rsync(客户端,服务端) |
自定义脚本参数
变量名 | 变量默认值 | 说明 |
---|---|---|
backup_user | rsync_backup | 虚拟用户名称,备份专用 |
backup_passwd | 123456 | 虚拟用户密码,备份专用 |
backup_module | backup | 备份模块名,包含具体备份路径,见/etc/rsyncd.conf配置 |
backup_path | /root/rsyncd_backup | 备份路径 |
安装
yum install rsync
服务端
# 备份原配置文件
mv /etc/rsyncd.conf /etc/rsyncd.conf.bkb
# 参数变量
backup_user=rsync_backup
backup_passwd=123456
backup_passwd_path=/etc/rsyncd.passwd
backup_module=backup
backup_path=/root/rsyncd_backup
# 创建新配置文件
cat > /etc/rsyncd.conf <<EOF
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = $backup_user
secrets file = $backup_passwd_path
log file = /var/log/rsyncd.log
[$backup_module]
comment = welcome to backup!
path = $backup_path
EOF
# 创建组与系统用户
groupadd rsync -g 666
useradd rsync -u 666 -g 666 -M -s /sbin/nologin -r
# 创建虚拟用户
echo "$backup_user:$backup_passwd" > $backup_passwd_path
chmod 600 $backup_passwd_path
# 创建备份目录
mkdir $backup_path
chown rsync.rsync $backup_path
# 防火墙
firewall-cmd --permanent --add-port=873/tcp
firewall-cmd --reload
# 启动服务
systemctl start rsyncd
systemctl enable --now rsyncd
客户端
# 配置密码文件
backup_user=rsync_backup
backup_passwd=123456
echo "$backup_passwd" > /etc/rsync.$backup_user.passwd
chmod 600 /etc/rsync.$backup_user.passwd
简单入门
# 本地备份
rsync -avzP /root/local_workspace/src /root/local_workspace/dst
# 远程备份 rsync方式 - 推荐(需要密码文件)
rsync -avzP /root/local_workspace/lsyncd_workspace rsync_backup@192.168.1.3::backup --password-file=/etc/rsync.rsync_backup.passwd
# 远程备份 ssh方式 (无需密码文件,需ssh免密登录)
rsync -avzP /root/local_workspace/lsyncd_workspace root@192.168.1.3:/root/local_workspace