介绍
《
点击打开链接》
第一部分,rsync
《
点击打开链接》
下载
源服务器
网址https://github.com/rvoicilas/inotify-tools/wiki
在github上获取download地址
wget https://github.s3.amazonaws.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
安装
需要安装gcc
yum install -y gcc
解压
tar zxvf inotify-tools-3.14.tar.gz
安装
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make
make install
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make
make install
配置脚本
跳转路径
cd /usr/local/inotify
vi /usr/local/inotify/rsync.sh
----------------------------------------
host=192.168.159.101
srcdir=/home/rsync_file/
module=rsync_file
excludedir=/usr/local/inotify/exclude.list
rsyncuser=user1
rsyncpassdir=/etc/passwd.txt
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%y/%m/%d %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srcdir | while read files
do
rsync -avH --port=873 --progress --delete $srcdir $rsyncuser@$host::$module --password-file=$rsyncpassdir --exclude-from=$excludedir
srcdir=/home/rsync_file/
module=rsync_file
excludedir=/usr/local/inotify/exclude.list
rsyncuser=user1
rsyncpassdir=/etc/passwd.txt
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%y/%m/%d %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srcdir | while read files
do
rsync -avH --port=873 --progress --delete $srcdir $rsyncuser@$host::$module --password-file=$rsyncpassdir --exclude-from=$excludedir
echo " ${files} was rsynced" >> /tmp/rsync.log 2>&1
done
done
----------------------------------------
:wq
保存退出
字段说明在备注
配置排除的文件夹(可为空)
touch /usr/local/inotify/exclude.list
设置脚本开机自动执行
vi /etc/rc.d/rc.local #编辑,在最后添加一行
sh /usr/local/inotify/rsync.sh & #设置开机自动在后台运行脚本
:wq! #保存退出
vi /etc/rc.d/rc.local #编辑,在最后添加一行
sh /usr/local/inotify/rsync.sh & #设置开机自动在后台运行脚本
:wq! #保存退出
获取权限
chmod 764 rsync.sh
chmod +x /usr/local/inotify/rsync.sh
chmod +x /usr/local/inotify/rsync.sh
执行脚本
[root@bogon inotify]# ./rsync.sh
在源服务器的/home/rsync_file/中新增文件夹
观察脚本信息
sending incremental file list
./
ceshi/
sent 121 bytes received 27 bytes 14.10 bytes/sec
total size is 40 speedup is 0.00
./
ceshi/
sent 121 bytes received 27 bytes 14.10 bytes/sec
total size is 40 speedup is 0.00
出现以上信息
在目标服务中也同样出现ceshi文件夹
说明同步备份
成功!!
备注
设置系统环境变量,添加软连接(此步可不做)
echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh
source /etc/profile.d/inotify.sh #使设置立即生效
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
ln -s /usr/local/inotify/include /usr/include/inotify
echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh
source /etc/profile.d/inotify.sh #使设置立即生效
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
ln -s /usr/local/inotify/include /usr/include/inotify
配置脚本的字段说明
host
:备份到目标服务器的ip地址,如果是多个目标则用空格分隔,外加双引号,并for in循环
例:
host="192.168.159.101 192.168.159.102"
for ip in $dstip
do
do
statement...$ip...
done
srcdir:原服务器需要同步的文件夹路径
module:模块名
excludedir:
#不需要同步的目录,如果有多个,每一行写一个目录,使用相对于同步模块的路径;
例:不需要同步/home/rsync_file/目录下的a目录和b目录下面的b1目录,exclude.list文件可以这样写
a/
b/b1/
例:不需要同步/home/rsync_file/目录下的a目录和b目录下面的b1目录,exclude.list文件可以这样写
a/
b/b1/
rsyncuser:源服务器同步用户名
rsyncpassdir:源服务器同步密码文件路径
/tmp/rsync.log
:脚本运行日志记录