18 目录文件变化监控与实时文件同步
#!/bin/bash source /etc/profile DATE="$(date +'%F')" HOST_IP="172.16.1.122" SOURCE_DIR="/opt" # 注意:目录最后面的左斜杠(/) # /opt : 同步时会将整个源目录也同步到备份目录之中 # /opt/ : 同步时只同步源目录中的所有文件 SOURCE_DIR_NAME="opt" AIMS_DIR="/tmp/backup_dir" [ ! -d $AIMS_DIR ] && mkdir -p $AIMS_DIR inotifywait -mrq --timefmt '%Y/%m/%d %H:%M:%S' --format '%T %w %f %e' -e modify,delete,create,attrib $SOURCE_DIR |\ while read files; do # sleep 30 # 同步时间延迟 if ! rsync $SOURCE_DIR --delete -az $AIMS_DIR 2>>${AIMS_DIR}/${SOURCE_DIR_NAME}_dir_${DATE}.error; then echo "Server $HOST_IP ${SOURCE_DIR} DIR $files RSYNC FAILD" |mail -s "Server $HOST_IP ${SOURCE_DIR} DIR RSYNC FAILD" 2504164765@qq.com fi done # 依赖包 # yum install inotify-tools -y
#!/bin/bash source /etc/profile HOST_IP="172.16.1.122" MON_DIR="/opt" inotifywait -mrq --timefmt '%Y/%m/%d %H:%M:%S' --format '%T %f' -e create $MON_DIR |\ while read files; do echo "Server $HOST_IP AT ${MON_DIR} DIR CREATE ${files}" |mail -s "Server $HOST_IP $MON_DIR DIR MONITOR" 2504164765@qq.com done # 依赖包 # yum install inotify-tools -y
#!/bin/bash source /etc/profile SOURCE_DIR="/opt/" AIMS_DIR="/tmp/backup_dir" [ ! -d $AIMS_DIR ] && mkdir -p $AIMS_DIR inotifywait -mrq --format '%w%f' -e modify,create $SOURCE_DIR |\ while read files; do if [ -f "$files" ];then rsync -az $files $AIMS_DIR &>/dev/null & fi done # 依赖包 # yum install inotify-tools -y