rsync 是一个快速增量文件传输工具,它能够用于在同一主机备分内部的备分,咱们还能够把它做为不一样主机网络备份工具之用。本文主要讲述的是如何自架rsync服务器,以实现文件传输、备份和镜像。相对tar和wget来讲,rsync 也有其自身的优势,好比速度快、安全、高效。html
服务器环境:linux
源服务器:192.168.1.10c++
备份服务器:192.168.1.1git
一:在rsync服务端(备份服务器)进行如下操做:
github
一、关闭SELINUX:web
vi /etc/selinux/config #编辑防火墙配置文件 #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINUX=disabled #增长 setenforce 0 #当即生效
二、开启防火墙tcp 873端口(Rsync默认端口)算法
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT service iptables save service iptables restart
2、在备份服务器安装Rsync服务端:shell
# yum install rsync >源码包安装 # tar zxvf rsync-3.1.1.tar.gz # cd rsync-3.1.1 # ./configure --prefix=/usr/local/rsync/ # make &&make install 注:在用源码包编译安装以前,您得安装gcc等编译开具才行;
二、配置文件:安全
rsync的主要有如下三个配置文件rsyncd.conf(主配置文件)、rsyncd.secrets(密码文件)、rsyncd.motd(rysnc服务器信息)服务器
touch /etc/rsyncd.conf #建立rsyncd.conf,这是rsync服务器的配置文件。 touch /etc/rsyncd.secrets #建立rsyncd.secrets ,这是用户密码文件。 touch /etc/rsyncd.motd
# vi /etc/rsyncd.conf #全局参数 uid = root #运行RSYNC守护进程的用户 gid = root #运行RSYNC守护进程的组 use chroot = no #默认为true,修改成no,增长对目录文件软链接的备份 max connections = 10 # 最大链接数为10 timeout = 600 #设置超时时间 strict modes =yes #是否检查口令文件的权限 address = 192.168.1.1 #配置监听ip port = 873 #默认端口873 pid file = /var/run/rsyncd.pid #pid文件的存放位置 lock file = /var/run/rsync.lock #锁文件的存放位置 log file = /var/log/rsyncd.log #日志记录文件的存放位置 motd file = /etc/rsyncd.motd #rsync启动时欢迎信息页面文件位置(文件内容自定义) secrets file = /etc/rsyncd.secrets #密码和用户名对比表,密码文件本身生成 #模块参数 [backup] #这里是认证的模块名,在client端须要指定 path = /data/html/ #须要作镜像的目录,不可缺乏! comment = web file #这个模块的注释信息 ignore errors #能够忽略一些无关的IO错误 read only = no write only = no #hosts allow = * #容许主机 hosts allow=192.168.1.1 192.168.10.0/24 #hosts deny = * #禁止主机 list = false #不显示rsync服务端资源列表 auth users = backup #认证的用户名,若是没有这行则代表是匿名,此用户与系统无关 #transfer logging = yes #使rsync服务器使用ftp格式的文件来记录下载和上载操做在本身单独的日志中。
注:关于auth users是必须在服务器上存在的真实的系统用户,若是你想用多个用户以,号隔开,好比auth users = easylife,root
三、建立密码文件:
echo "backup:123456" > /etc/rsyncd.secrets #建立rsync证文件 chmod 600 /etc/rsyncd.secrets chown root:root /etc/rsyncd.secrets
注:一、将rsyncd.secrets这个密码文件的文件属性设为root拥有, 且权限要设为600, 不然没法备份成功! 出于安全目的,文件的属性必需是只有属主可读。
四、设定rsyncd.motd 文件:
它是定义rysnc服务器信息的,也就是用户登陆信息。好比让用户知道这个服务器是谁提供的等;相似ftp服务器登陆时,咱们所看到的 linuxsir.org ftp ……。 固然这在全局定义变量时,并非必须的,你能够用#号注掉,或删除;我在这里写了一个 rsyncd.motd的内容为:
++++++++++++++++++++++++++++++++++++++++++++++ Welcome to use the mike.org.cn rsync services! ++++++++++++++++++++++++++++++++++++++++++++++
五、 独立运行 rsync 服务:
ln -s /usr/local/rsync/bin/rsync /usr/bin/rsync rsync --daemon #启动服务 echo "/usr/bin/rsync --daemon" >> /etc/rc.local #开机自启动
更改配置文件只要不更改端口就不用重启服务
3、源服务器配置:
一、建立密码文件:
echo "123456" > /etc/rsyncd.secrets #建立rsync证文件 chmod 600 /etc/rsyncd.secrets chown root:root /etc/rsyncd.secrets
二、同步服务器端文件到本地
rsync -avzP --delete --progress --bwlimit=2000 /date/html/ backup@192.168.1.1::backup --password-file=/etc/rsyncd.secrets >/dev/null 2>&1
注:出现error while loading shared libraries解决方法:
一、编辑/etc/ld.so.conf文件,添加一行:
/usr/local/lib
二、保存后运行ldconfig。
三、再启动运行rsyncd服务运行,问题获得解决。
(注:ld.so.conf和ldconfig用于维护系统动态连接库)
4、源服务器安装Inotify-tools工具,实时触发rsync进行同步
一、查看服务器内核是否支持inotify
ll /proc/sys/fs/inotify #列出文件目录,出现下面的内容,说明服务器内核支持inotify -rw-r--r-- 1 root root 0 Mar 7 02:17 max_queued_events -rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_instances -rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_watches
二、安装inotify-tools
yum install make gcc gcc-c++ #安装编译工具 #inotify-tools下载地址 wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz tar zxvf inotify-tools-3.14.tar.gz cd inotify-tools-3.14 ./configure --prefix=/usr/local/inotify make &&make install
三、设置系统环境变量,添加软链接
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
四、修改inotify默认参数(inotify默认内核参数值过小)
查看系统默认参数值
sysctl -a | grep max_queued_events 结果是:fs.inotify.max_queued_events = 16384 sysctl -a | grep max_user_watches 结果是:fs.inotify.max_user_watches = 8192 sysctl -a | grep max_user_instances 结果是:fs.inotify.max_user_instances = 128
修改参数:
sysctl -w fs.inotify.max_queued_events="99999999" sysctl -w fs.inotify.max_user_watches="99999999" sysctl -w fs.inotify.max_user_instances="65535" #内核参数添加如下代码 #vi /etc/sysctl.conf fs.inotify.max_queued_events=99999999 fs.inotify.max_user_watches=99999999 fs.inotify.max_user_instances=65535
参数说明:
max_queued_events:inotify队列最大长度,若是值过小,会出现"** Event Queue Overflow **"错误,致使监控文件不许确
max_user_watches:要同步的文件包含多少目录,能够用:find /home/www.osyunwei.com -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/www.osyunwei.com为同步文件目录)
max_user_instances:每一个用户建立inotify实例最大值
五、建立脚本,实时触发rsync进行同步
vi /usr/local/inotify/rsync.sh #编辑,添加如下代码 #!/bin/sh srcdir=/data/html/ dstdir=backup excludedir=/usr/local/inotify/exclude.list rsyncuser=backup rsyncpassdir=/etc/rsyncd.secrets dstip="192.168.1.1,192.168.1.2" for ip in $dstip do rsync -avH --port=873 --progress --delete --bwlimit=2000 --exclude-from=$excludedir $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir >/dev/null 2>&1 done /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srcdir | while read file do for ip in $dstip do rsync -avH --port=873 --progress --delete --bwlimit=2000 --exclude-from=$excludedir $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir >/dev/null 2>&1 echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1 done done
chmod +x /usr/local/inotify/rsync.sh #添加脚本执行权限
脚本参数说明:
srcdir=/data/html/ #源服务器同步目录
dstdir=backup #目标服务器rsync同步目录模块名称
excludedir=/usr/local/inotify/exclude.list #不须要同步的目录,若是有多个,每一行写一个目录,使用相对于同步模块的路径;
#例如:不须要同步/data/html/目录下的a目录和b目录下面的b1目录,exclude.list文件能够这样写
a/
b/b1/
rsyncuser=backup #目标服务器rsync同步用户名
rsyncpassdir=/etc/rsyncd.secrets #目标服务器rsync同步用户的密码在源服务器的存放路径
dstip="192.168.1.1,192.168.1.2" #目标服务器ip,多个ip用空格分开
/tmp/rsync.log #脚本运行日志记录
六、设置脚本开机自动执行
#vi /etc/rc.d/rc.local #编辑,在最后添加一行 #设置开机自动在后台运行脚本 sh /usr/local/inotify/rsync.sh &
七、测试inotify实时触发rsync同步脚本是否正常运行
在源服务器192.168.21.129上建立文件test.txt
touch /data/html/test.txt
查看两台目标服务器192.168.1.1,192.168.1.2的/data/html/下是否有test文件夹
若是以上测试经过,说明inotify实时触发rsync同步脚本运行正常。
至此,Linux下Rsync+Inotify-tools实现数据实时同步完成。
扩展阅读:
============================================
inotify参数
-m 是保持一直监听 -r 是递归查看目录 -q 是打印出事件 -e create,move,delete,modify,attrib 是指 “监听 建立 移动 删除 写入 权限” 事件
rsync参数
============================================ -v, --verbose 详细模式输出 -q, --quiet 精简输出模式 -c, --checksum 打开校验开关,强制对文件传输进行校验 -a, --archive 归档模式,表示以递归方式传输文件,并保持全部文件属性,等于-rlptgoD -r, --recursive 对子目录以递归模式处理 -R, --relative 使用相对路径信息 -b, --backup 建立备份,也就是对于目的已经存在有一样的文件名时,将老的文件从新命名为~filename。可使用--suffix选项来指定不一样的备份文件前缀。 --backup-dir 将备份文件(如~filename)存放在在目录下。 -suffix=SUFFIX 定义备份文件前缀 -u, --update 仅仅进行更新,也就是跳过全部已经存在于DST,而且文件时间晚于要备份的文件。(不覆盖更新的文件) -l, --links 保留软链结 -L, --copy-links 想对待常规文件同样处理软链结 --copy-unsafe-links 仅仅拷贝指向SRC路径目录树之外的链结 --safe-links 忽略指向SRC路径目录树之外的链结 -H, --hard-links 保留硬链结 -p, --perms 保持文件权限 -o, --owner 保持文件属主信息 -g, --group 保持文件属组信息 -D, --devices 保持设备文件信息 -t, --times 保持文件时间信息 -S, --sparse 对稀疏文件进行特殊处理以节省DST的空间 -n, --dry-run现实哪些文件将被传输 -W, --whole-file 拷贝文件,不进行增量检测 -x, --one-file-system 不要跨越文件系统边界 -B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节 -e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步 --rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息 -C, --cvs-exclude 使用和CVS同样的方法自动忽略文件,用来排除那些不但愿传输的文件 --existing 仅仅更新那些已经存在于DST的文件,而不备份那些新建立的文件 --delete 删除那些DST中SRC没有的文件 --delete-excluded 一样删除接收端那些被该选项指定排除的文件 --delete-after 传输结束之后再删除 --ignore-errors 及时出现IO错误也进行删除 --max-delete=NUM 最多删除NUM个文件 --partial 保留那些因故没有彻底传输的文件,以是加快随后的再次传输 --force 强制删除目录,即便不为空 --numeric-ids 不将数字的用户和组ID匹配为用户名和组名 --timeout=TIME IP超时时间,单位为秒 -I, --ignore-times 不跳过那些有一样的时间和长度的文件 --size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间 --modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0 -T --temp-dir=DIR 在DIR中建立临时文件 --compare-dest=DIR 一样比较DIR中的文件来决定是否须要备份 -P 等同于 --partial --progress 显示备份过程 -z, --compress 对备份的文件在传输时进行压缩处理 --exclude=PATTERN 指定排除不须要传输的文件模式 --include=PATTERN 指定不排除而须要传输的文件模式 --exclude-from=FILE 排除FILE中指定模式的文件,文件里面填写要排除的目录(一行一个文件/目录) --include-from=FILE 不排除FILE指定模式匹配的文件 --version 打印版本信息 --address 绑定到特定的地址 --config=FILE 指定其余的配置文件,不使用默认的rsyncd.conf文件 --port=PORT 指定其余的rsync服务端口 --blocking-io 对远程shell使用阻塞IO -stats 给出某些文件的传输状态 --progress 在传输时现实传输过程 --log-format=formAT 指定日志文件格式 --password-file=FILE 从FILE中获得密码 --bwlimit=KBPS 限制I/O带宽,KBytes per second -h, --help 显示帮助信息
============================================
参考连接:http://www.osyunwei.com/archives/7435.html