1. 服务器情况的说明:
| 服务器的IP | 服务器的系统版本 | 服务器的功能 |
|---|---|---|
| 192.168.88.100 | Ubuntu 18.04 | 主服务器 |
| 192.168.88.101 | Ubuntu 18.04 | 备服务器 |
说明:
- rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。
- rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!
- Inotify 是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加 入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这 样的一个第三方软件。
2. Rsync安装和配置
2.1 备份服务器的配置[192.168.88.101]
# 1. 安装rsync的应用sudo apt install -y rsync# 2. 修改 /etc/default/rsyncsudo vim /etc/default/rsync### 修改的文件中的内容 -- 默认config文件在/etc目录下,如果修改目录,需要修改rysncd.serviceRSYNC_ENABLE=trueRSYNC_CONFIG_FILE = '/etc/rsync/rsyncd.conf'# 3. 方便管理,添加rsync同步的目录(目录位置自定义)sudo mkdir -p /etc/rsync/# 4. 建立同步的密码文件,rsync.secrets, 必须设置为600权限(必须),在里面放用户名和密码cat > /etc/rsync/rsyncd.secrets << EOF#/etc/rsync/rsyncd.secretsfred:123456EOF# 5. 设置rsyncd.secrets的权限为600sudo chmod 600 /etc/rsync/rsync.secrets
# 6.拷贝并修改配置文件 /etc/rsync/rsyncd.conf --需要修改rysncd.service###-- 配置内容后面不能添加注释内容 --###cp /usr/share/doc/rsync/examples/rsyncd.conf /etc/rsync/vim /etc/rsync/rsyncd.conf### 修改内容 #### sample rsyncd.conf configuration file# GLOBAL OPTIONS#motd file=/etc/motdlog file=/var/log/rsyncdfake super = yes# for pid file, do not use /var/run/rsync.pid if# you are going to run rsync out of the init.d script.# The init.d script does its own pid file handling,# so omit the "pid file" line completely in that case.pid file=/var/run/rsyncd.pid#syslog facility=daemon#socket options=# MODULE OPTIONS## 1. 每一个同步的目录都需要单独创建[fred]comment = public archive# 指定同步的目录,确保目录必须存在path = /home/fred/ftp/rootuse chroot = yes# max connections=10lock file = /var/lock/rsyncd# the default for read only is yes...# 不能只读,还要有写的权限read only = nolist = yes# 文件的所属用户及组uid = fredgid = fred# exclude =# exclude from =# include =# include from =# 指定同步校验用户(与主服务器中用于同步的用户相同)auth users = fredsecrets file = /etc/rsync/rsyncd.secretsstrict modes = yes# 指定host(主服务器IP)hosts allow = 192.168.88.100# hosts deny =ignore errors = noignore nonreadable = yestransfer logging = yes# log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.timeout = 600refuse options = checksum dry-rundont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
# 7. 服务启动/etc/init.d/rsync restart/etc/init.d/rsync status
特别注意: 在默认的情况下,rsyncd.conf文件是在’/etc/‘目录下的;但是,如果改变文件的位置需要增加的步骤
# 1. 修改配置文件'/lib/systemd/system/rsync.service'sudo vim /lib/systemd/system/rsync.service### 修改为:主要是启动指定config[Unit]Description=fast remote file copy program daemonConditionPathExists=/etc/rsync/rsyncd.conf[Service]ExecStart=/usr/bin/rsync --config=/etc/rsync/rsyncd.conf --daemon --no-detach[Install]WantedBy=multi-user.target# 2. 需要重新加载sudo systemctl daemon-reload/etc/init.d/rsync restart ## 同样能够,启动rsync
2.2 主服务器【192.168.88.100】—主要的文件内容都在该服务器上面
# 1.安装rsyncapt-get install rsync# 2.修改 /etc/default/rsyncsudo vim /etc/default/rsync### 修改的文件中的内容RSYNC_ENABLE=true# 3. 方便管理,添加rsync文件同步的配置目录(目录可以自定义)sudo mkdir -p /etc/rsync/# 4. 建立密码文件 rsyncd.secrets1,并设置为600权限(考虑主备环境需要互相拷)cat > /etc/rsync/rsyncd.secrets1 << EOF123456EOF### 设置600权限sudo chmod 600 /etc/rsync/rsyncd.secrets1
2.3 验证主备连接(在192.168.1.100 主服务器上运行)
# 1.在主服务器上测试连接rsync -vzrtopg --progress fred@192.168.88.101::fred --password-file=/etc/rsync/rsyncd.secrets1# 2. 主服务器推向备服务器rsync -avzP --progress /home/fred/ftp/root --password-file=/etc/rsync/rsyncd.secrets1 fred@192.168.88.101::fred# 3. 主服务器 拉取 备用服务器rsync -avzPt --password-file=/etc/rsync/rsyncd.secrets1 fred@192.168.1.101::fred /home/qif/Documents
3. inotify
inotify 将安装在主服务器(192.168.88.100)上,需要与rsync配合使用!
# 1. 查看是否安装有inotifyls /proc/sys/fs/inotify## 能否看到max_queued_events max_user_instances max_user_watches,三个值# 2. 安装inotifyapt-get updateapt-get install -y inotify-tools# 3. 安装完成后,inotifywait默认路劲为 /usr/bin/inotifywait# 4./etc/rsync下配置rsync.sh脚本
# 1. rsync-minio.sh#!/bin/shsrcdir=/home/fred/docker/minio/data/dstdir=rsync-miniorsyncuser=fredrsyncpassdir=/etc/rsync/rsyncd.secrets1host=192.168.88.101dstip=`echo $host|tr ',' ' '`for ip in $dstipdoresult1=$(service keepalived status | tail -1 |grep MASTER | wc -l)echo $result1if [ $result1 -eq 1 ]; thenrsync -avH --port=873 --progress --delete $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdirecho ' ${file} was rsynced' >> /tmp/rsync.log 2>&1elseecho 'BACKUP NOT NEED RSYNC'fidone/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move /home/fred/docker/minio/data/ | while read filedofor ip in $dstipdoresult2=$(service keepalived status | tail -1 |grep MASTER | wc -l)if [ $result2 -eq 1 ]; thenrsync -avH --port=873 --progress --delete /usr/local/qif/docker/minio/data/ $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdirecho ' ${file} was rsynced' >> /tmp/rsync.log 2>&1elseecho 'BACKUP NOT NEED RSYNC'fidonedone
# 2. rsync-ftp.sh#!/bin/shsrcdir=/home/fred/ftp/root/dstdir=rsync-ftprsyncuser=fredrsyncpassdir=/etc/rsync/rsyncd.secrets1host=192.168.88.101dstip=`echo $host|tr ',' ' '`for ip in $dstipdoresult1=$(service keepalived status | tail -1 |grep MASTER | wc -l)echo $result1if [ $result1 -eq 1 ]; thenrsync -avH --port=873 --progress --delete $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdirecho ' ${file} was rsynced' >> /tmp/rsync.log 2>&1elseecho 'BACKUP NOT NEED RSYNC'fidone/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move /home/fred/ftp/root/ | while read filedofor ip in $dstipdoresult2=$(service keepalived status | tail -1 |grep MASTER | wc -l)if [ $result2 -eq 1 ]; thenrsync -avH --port=873 --progress --delete /home/fred/ftp/root/ $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdirecho ' ${file} was rsynced' >> /tmp/rsync.log 2>&1elseecho 'BACKUP NOT NEED RSYNC'fidonedone
总结: 在备用服务器上,需要创建rsync.conf配置信息; 在主服务器上需要配置inotify的脚本 【主->备拷贝】
4. 修改rsync脚本的权限 并自动启动
# 1. 修改脚本权限chmod a+x /etc/rsync/rsync-*.sh# 2. 执行脚本sh /etc/rsync/rsync-*.sh &
# 3. 创建软连接ln -sf /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service# 4. 添加命令cat >>/etc/systemd/system/rc-local.service<<EOF[Install]WantedBy=multi-user.targetAlias=rc-local.serviceEOF# 5. 创建rc.local文件并赋权touch /etc/rc.local && chmod 755 /etc/rc.local# 6. 导入必要的语句cat >>/etc/rc.local<<EOF#!/bin/bash/bin/bash /etc/rsync/rsync.sh > /dev/null 2>&1 &exit 0EOF# 7.重新加载服务systemctl daemon-reload
5. rsync命令说明
-v:显示rsync过程中详细信息。可以使用”-vvvv”获取更详细信息。 -P:显示文件传输的进度信息。(实际上”-P”=”–partial —progress”,其中的”–progress”才 是显示进度信息的)。 -n —dry-run :仅测试传输,而不实际传输。常和”-vvvv”配合使用来查看rsync是如何工作的。 -a —archive :归档模式,表示递归传输并保持文件属性。等同于”-rtopgDl”。 -r —recursive:递归到目录中去。 -t —times:保持mtime属性。强烈建议任何时候都加上”-t”,否则目标文件mtime会设置为系 统时间,导致下次更新 :检查出mtime不同从而导致增量传输无效。 -o —owner:保持owner属性(属主)。 -g —group:保持group属性(属组)。 -p —perms:保持perms属性(权限,不包括特殊权限)。 -D :是”–device —specials”选项的组合,即也拷贝设备文件和特殊文件。 -l —links:如果文件是软链接文件,则拷贝软链接本身而非软链接所指向的对象。 -z :传输时进行压缩提高效率。 -R —relative:使用相对路径。意味着将命令行中指定的全路径而非路径最尾部的文件名发送 给服务端,包括它们的属性。用法见下文示例。 –size-only :默认算法是检查文件大小和mtime不同的文件,使用此选项将只检查文件大小。 -u —update :仅在源mtime比目标已存在文件的mtime新时才拷贝。注意,该选项是接收端判 断的,不会影响删除行为。 -d —dirs :以不递归的方式拷贝目录本身。默认递归时,如果源为”dir1/file1”,则不会拷贝 dir1目录,使用该选项将拷贝dir1但不拷贝file1。 –max-size :限制rsync传输的最大文件大小。可以使用单位后缀,还可以是一个小数值(例如: “–max-size=1.5m”) –min-size :限制rsync传输的最小文件大小。这可以用于禁止传输小文件或那些垃圾文件。 –exclude :指定排除规则来排除不需要传输的文件。 –delete :以SRC为主,对DEST进行同步。多则删之,少则补之。注意”–delete”是在接收端 执行的,所以它是在 :exclude/include规则生效之后才执行的。 -b —backup :对目标上已存在的文件做一个备份,备份的文件名后默认使用”~“做后缀。 –backup-dir:指定备份文件的保存路径。不指定时默认和待备份文件保存在同一目录下。 -e :指定所要使用的远程shell程序,默认为ssh。 –port :连接daemon时使用的端口号,默认为873端口。 –password-file:daemon模式时的密码文件,可以从中读取密码实现非交互式。注意,这不是 远程shell认证的密码,而是rsync模块认证的密码。 -W —whole-file:rsync将不再使用增量传输,而是全量传输。在网络带宽高于磁盘带宽时,该 选项比增量传输更高效。 –existing :要求只更新目标端已存在的文件,目标端还不存在的文件不传输。注意,使用相 对路径时如果上层目录不存在也不会传输。 –ignore-existing:要求只更新目标端不存在的文件。和”–existing”结合使用有特殊功能, 见下文示例。 –remove-source-files:要求删除源端已经成功传输的文件。 rsync的选项非常多,能够实现非常具有弹性的功能,以上选项仅仅只是很小一部分常用的选项, 关于更完整更详细的选项说明,见我的rsync man手册翻译。 虽然选项非常多,但最常用的选项组合是”avz“,即压缩和显示部分信息,并以归档模式传输。
参考文档:
