rsync数据镜像备份工具,能将本地系统数据通过网络备份到远程主机 特点:可以镜像保存目录树和文件系统、增量同步 、保持原有属性、加密传输 模式:c/s模式,c/c模式 并且两端都需要安装rsync否则无法使用
client/server模式 要在server端启动一个服务端口,通过客户端来连接端口
服务器端有两种方式来连接客户端,一种是使用守护进程(模块),在rsyncd.conf文件中定义secrets file,然后输入密码;一种是做ssh互信不需要输入密码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 /etc/rsyncd.conf # 系统配置 uid 指定守护进程的用户id 默认nobody gid 指定守护进程的用户组id 默认nobody max connections 最大客户端连接数 pid file 服务端的pid文件路径 lock file 服务端的锁文件路径 log file 服务器端的日志文件位置 fake super 虚拟用户伪装管理员 use chroot 是否修改根目录 timeout 超时时间 [ixdba] # 模块名 path 服务端目录(备份的目录) comment 描述 list 查看时,是否列出该模块true/false auth users 定义可连接的用户名 如backup secrets file 需要一个k/v的文件,用户名使用auth users;需要和client的secrets file对应 比如: echo 'backup:123456' >> /etc/server.pass #客户端拉取 rsync -vzrtopg --delete --progress backup@ip::ixdba /data --password-file=/etc/server.pass -v 详细输出 -z 压缩处理 -p 保持文件权限 -r 递归复制 -t 保持文件时间信息 -o 保持文件属主信息 --delete 保证完全一致 -progress 显示同步过程 :: 表示指定模块 ixdba 前面的模块名 /data 存放路径 backup 前面的auth users的用户名 --password-file客户端的密码文件,内容应与服务端一致
使用client/client模式 默认使用ssh 22端口rsync -vzrtopg --delete --progress filename root@xxx:/opt
推送模式rsync -vzrtopg --delete --progress root@xxx:/opt/filename /opt
拉取模式 如果ssh非默认22端口:rsync -vzrtopg --delete --progress -e 'ssh -p 9090' root@xxx:/opt/filename /opt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 参数含义: 如果只是简单的复制,这些参数可以一个都不加 rsync test.txt root@192.168.10.100:~/test -v verbose详细信息 -z zip压缩 -r 递归复制 -D 保持设备文件的信息 -t time保持修改时间 -o owner保持属主信息 -p perm保持权限 -g group保持组信息
使用client/server模式 1.服务器端编辑配置文件/etc/rsync.conf与server.pass文件
1 2 3 4 5 6 7 8 9 10 11 [ixdba] path = /opt/test commit = test read only = true list = false auth users = backup uid = root gid = root secrets file = /etc/server.pass —————————— backup:123456
2.rsync作为进程运行rsync --daemon
查看rsync的端口ss -tunlp | grep rsync
3.客户端创建一个server.passrsync -vzrtopg --delete --progress backup@ip::ixdba /data --password-file=/etc/server.pass
企业中的Rsync备份案例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 SSH互信 ssh-keygen ssh-copy-id -i 192.168.10.102 服务端配置文件 uid=rsync gid=rsync fake super=yes max connections = 100 timeout =300pid file=/var/run/rsyncd.pid lock file=/var/run/rsync.lock log file=/var/log/rsyncd.logignore errors read only=false hosts allow=192.168.10.0/24 auth users =backup secrets file=/var/rsync.password [mysql] path=/backup 服务器端准备用户与目录、权限相关 useradd rsync -M -s /sbin/nologin echo backup:123456 > /var/rsync.passwordchmod 600 /var/rsync.passwordchmod 600 /etc/rsyncd.confmkdir /backupchown -R rsync:rsync /backup/安全相关设置,或者全关了也行 sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config iptables -I INPUT -p tcp --dport 873 -j ACCEPT firewall-cmd --zone=public --add-port=873/tcp --permanent firewall-cmd --reload systemctl enable rsyncd --now systemctl restart rsyncd 客户端配置 echo "123456" > /var/rsync.passwordchmod 600 /var/rsync.password工作方式1(守护模式): 拉取并输入密码 rsync -avz backup@192.168.10.125::wangsheng 拉取并指定密码文件 rsync -avz backup@192.168.10.125::wangsheng --password-file=/var/rsync.password 推送并输入密码 rsync -avz ~/backup/mysql20240726 backup@192.168.10.125::wangsheng 推送并指定密码文件 rsync -avz ~/backup/mysql20240726 backup@192.168.10.125::wangsheng --password-file=/var/rsync.password 工作方式2(SSH模式) 需要做SSH互信,不需要虚拟账户的密码 与scp相似 rsync -avz /backup 192.168.10.102:/root
rsync+inotify实现数据实时备份 先做SSH互信
inotify-tools是用来监控文件和文件夹变动的工具
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 下载inotify-tools插件与编译安装 wget https://github.com/inotify-tools/inotify-tools/archive/refs/tags/3.22.6.0.tar.gz tar -xf 3.22.6.0.tar.gz cd inotify-tools-3.22.6.0/yum -y install autoconf automake libtool ./autogen.sh ./configure --prefix=/usr/local/inotify make make install ls /usr/local/inotify/bininotifywait 能监控文件和目录设置,并设置触发器 inotifywatch 收集被监控的文件系统的统计数据 inotifywait参数 -m monitor保持监听状态 -r recursive递归 -q quiet安静模式 -e event 指定要监控的事件、包括modify修改、delete删除、create创建、attrib修改属性 脚本 cp -f /usr/local/inotify/bin/* /bin/cat >> rsync.sh << EOF #!/bin/bash inotifywait -mrq --format '%w%f' -e create,delete,close_write /usr/local/mysql/data|while read file # 监控,并且将有变化的文件作为变量输入给file do rsync -az /usr/local/mysql/data --delete backup@192.168.10.102::wangsheng --password-file=/var/rsync.password #或使用ssh模式rsync -az /uar/local/mysql/data --delete root@192.168.10.102:/backup echo "${file} is rsynced" >> /var/log/rsyncd.log 2>&1 done #如果有变化(create,delete,close_write),使用rsync将 EOF chmod +x rsync.sh./rsync.sh 脚本测试 修改一下数据库内容 create database ws02; use ws02; create table wst1 (id int, name varchar(20)); insert into wst1 value (1,"ws" ),(2,"xhy" ); commit; tail -f /var/log/rsyncd.log