付费节点推荐
免费节点
节点使用教程
Clash 小火箭 v2ray节点购买
好用的梯子,海外网络,快速稳定
Google以下是A服务器的配置
首先从主服务器A开始,需要确定你的系统是否支持inotify:ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Jan 4 17:56 max_queued_events
-rw-r--r-- 1 root root 0 Jan 4 17:56 max_user_instances
-rw-r--r-- 1 root root 0 Jan 4 17:56 max_user_watches
能输出这样的结果表示支持。
下载并安装inotify-tools:
完成inotify-tools的安装。 接下来需要为A服务器写两个SH脚本,inotify_init.sh 和 inotify_monitor.sh inotify_init.sh 用于第一次初始化,也就是运行一次完整的RSYNC同步.wget --no-check-certificate http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz tar xzvf inotify-tools-3.14.tar.gz cd inotify-tools-3.14 ./configure --prefix=/usr make make install
vi /root/inotify_init.sh
保存退出. 设置可执行权限:#!/bin/sh SRC=/主服务器A需要同步的目录/ #记得在最后面加/不然RYNC会自动增加一层目录 DES=bbsatt IP= 服务器B的IP USER= rsync #DST=/etc/rsyncd 远程rsync模块下的目录(还不清楚,好像没用) INWT=/usr/bin/inotifywait RSYNC=/usr/bin/rsync $RSYNC -zahqt --password-file=/root/rsync.pwd $SRC $USER@$IP::$DES
chmod +x /root/inotify_init.sh接下是inotify_monitor.sh,用于订阅文件修改事件.注意,因为特别原因,我在这里做的是增量备份+实时同步,也就是说,当主服务器A上的图片被删除是,从服务器B是不会删除图片的. vi /root/inotify_monitor.sh
#!/bin/bash #sync[0]='/主服务器需要同步的目录/,从服务器B的IP,bbsatt,rsync' # localdir,ho st,rsync_module,auth_user#只需要修改 sync[0] 的目录地址,和ip地址即可。逗号分开 sync[0]='/home/wwwroot/****/wp-content/uploads/,45.43.23.12,bbsatt,rsync' INWT=/usr/bin/inotifywait RSYNC=/usr/bin/rsync PASS=/root/rsync.pwd ########################### for item in ${sync[@]}; do dir=`echo $item | awk -F"," '{print $1}'` host=`echo $item | awk -F"," '{print $2}'` module=`echo $item | awk -F"," '{print $3}'` user=`echo $item | awk -F"," '{print $4}'` $INWT -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' --event CLOSE_WRITE, create,move $dir | while read date time file event do echo $event'-'$file case $event in MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR) if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then cmd="$RSYNC -zahqzt --password-file=$PASS --include=$file $dir $user@$host::$mod ule" echo $cmd >> a.log $cmd fi ;; MOVED_FROM|MOVED_FROM,ISDIR|DELETE,ISDIR) if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then cmd="$RSYNC -zahqzt --password-file=$PASS --exclude=$file $dir $user@$host::$mod ule" echo $cmd >>a.log $cmd fi ;; esac done & done
加执行权限
chmod +x /root/inotify_monitor.sh设置RSYNC自动登录验证密码
vi /root/rsync.pwd输入服务器密码到rsync.pwd
123456保存,退出 设置只有ROOT才可以查看的权限.
chmod 0600 /root/rsync.pwd
以下是备从务器B的配置:
安装RSYNCyum install rsync -y#----配置RSNYD服务
vi /etc/rsyncd.conf内容如下,uid,gid修改成你运行网站的用户名,用户组
服务器的密码uid = root gid = root use chroot = no max connections = 4 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log[bbsatt] path = /home/wwwroot/cdn.bcoderss.com/wp-content/uploads/ #服务器B接收的目录 ignore errors read only = no list = false hosts allow = 149.129.61.205 #服务器A的ip地址 auth users = rsync secrets file = /etc/rsync.pas
vi /etc/rsync.pasrsync:123456
chmod 0600 /etc/rsync.pas启动RSYNCD
rsync --daemon

vi /etc/rc.local输入 rsync --daemon

回到主服务器A,
vi /etc/rc.local添加以下内容,实时开机自动同步:
保存退出 运行:/root/inotify_init.sh /root/inotify_monitor.sh
这样就能实现实时同步图片文件了,在主服务器A的同步目录下新建一个文件测试下吧。 问题:发现必须执行/root/inotify_init.sh 才能同步,所以最后只能用crontab 每隔1分钟同步 安装crontab/root/inotify_init.sh /root/inotify_monitor.sh
yum -y install vixie-cron crontabs使用方法:
# 查看cond 状态 service crond status # 启动cron service crond start # 关闭cron service crond stop # 重启cron service crond restart crontab –e : 修改 crontab 文件。 crontab –l : 显示 crontab 文件。 crontab -r : 删除 crontab 文件。 crontab -ir : 删除 crontab 文件前提醒用户。添加任务,每隔一分钟执行一次
* * * * * /root/inotify_init.sh(好像最小间隔时间是1分钟,如果想设置更低,比如10秒钟)
* * * * * sleep 10; /root/inotify_init.sh
* * * * * sleep 20; /root/inotify_init.sh
* * * * * sleep 30; /root/inotify_init.sh
* * * * * sleep 40; /root/inotify_init.sh
* * * * * sleep 50; /root/inotify_init.sh
或者写个shell脚本,循环下。