关于rsyncd镜像服务器同步及web页面挂载(浅解)
参考:rsync 同步及服务:
1.http://www.howtocn.org/rsync (命令等详解)
2.http://www.centos.bz/2011/06/rsync-server-setup/
3.http://blog.csdn.net/21aspnet/article/details/6798179 (Crontab用法详解)
4.http://www.rjkfw.com/s_1027.html (纠错)
rsync (remote synchronize)
是一个远程数据同步工具,可通过 LAN/WAN 快速同步多台主机之间的文件。也可以使用 rsync 同步本地硬盘中的不同目录。
rsync 是用于替代 rcp 的一个工具,rsync 使用所谓的 rsync算法 进行数据同步,这种算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。 您可以参考 How Rsync Works A Practical Overview 进一步了解 rsync 的运作机制。
rsync 的初始作者是 Andrew Tridgell 和 Paul Mackerras,目前由 http://rsync.samba.org 维护。
rsync 支持大多数的类 Unix 系统,无论是 Linux、Solaris 还是 BSD上 都经过了良好的测试。 CentOS系统默认就安装了 rsync 软件包。 此外,在 windows 平台下也有相应的版本,如 cwrsync 和DeltaCopy 等。
rsync服务器,以实现文件传输、备份和镜像。相对tar和wget来说,rsync 也有其自身的优点,比如速度快、安全、高效。
###########
服务器配置 #
###########
debian 下安装包:
apt-get install rsyncd
_____________________________________
配置文件:( 默认文件不存在 ) ps:对于服务器共享来说无须创建 密码文件
rsyncd.conf (rsync服务器主要配置文件)
rsyncd.secrets (登录rsync服务器的密码文件)
rsyncd.motd (定义rysnc 服务器信息的,也就是用户登录信息)
:/etc$ touch rsyncd.conf rsyncd.motd
_______________________________________________________________________
rsyncd.conf
# Minimal configuration file for rsync daemon
# See rsync(1) and rsyncd.conf(5) man pages for help
# This line is required by the /etc/init.d/rsyncd script
pid file = /var/run/rsyncd.pid 注:rsync的pid文件
motd file = /etc/rsyncd.motd 注:消息文件,当客户连接服务器时该文件的内容显示给客户
port = 注:运行端口,默认是873
uid = nobody 注:当该模块传输文件时守护进程具有的uid,配合gid选项使用可以确定访问件权限,默认:nobody
gid = nobody 注:当该模块传输文件时守护进程应该具有的gid,默认:nobody
use chroot = yes 注:如果”use chroot”指定为yes,那么rsync在传输文件以前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但是缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为yes。
read only = 注:设定是否允许客户上载文件。如果为yes那么任何上载请求都会失败,如果为no并且服务器目录读写权限允许那么上载是允许的。默认值为yes
max connections = 注:指定该模块的最大并发连接数量以保护服务器,超过限制的连接请求将被告知随后再试
log file = /var/log/rsyncd.log 注:rsync的日志文件,而不将日志发送给syslog
log format = %t %a %m %f %b
注:用户在使用transfer logging可以自己定制日志文件的字段。其格式是一个包含格式定义符的字符串,可以使用的格式定义符如下所示:
%h 远程主机名 %a 远程IP地址 %l 文件长度字符数 %p 该次rsync会话的进程id
%o 操作类型:”send”或”recv” %f 文件名 %P 模块路径 %m 模块名 %t 当前时间
%u 认证的用户名(匿名时是null) %b 实际传输的字节数
%c 当发送文件时,该字段记录该文件的校验码
默认log格式为:”%o %h [%a] %m (%u) %f %l”,一般来说,在每行的头上会添加”%t [%p] “。在源代码中同时发布有一个叫rsyncstats的perl脚本程序来统计这种格式的日志文件
timeout = 300 注:可以覆盖客户指定的IP超时时间。通过该选项可以确保rsync服务器不会永远等待一个崩溃的客户。超时单位为秒钟,0表示没有超时定义,这也是默认值
[modlue]
path = 注:该模块的供备份的目录树路径,该参数是必须指定的
comment = 给模块指定一个描述,该描述连同模块名在客户连接得到模块列表时显示给客户
________________________________________________________________________________
#########
服务设置 #
#########
启动rsync 服务器相当简单,–daemon 是让rsync 以服务器模式运行:
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf --log-file=/var/log/rsyncd.log
——————————————————————————————————————
开启防火墙:(默认端口:873)
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
如果需要开机自动加载可编辑入/etc/network/interface:
iptables-save > /etc/iptables.up.rules
在/etc/network/interfaces加入:
pre-up iptables-restore < /etc/iptables.up.rules
——————————————————————————————————————
同步数据:(由于需要定时同步数据,为此制作了运行脚本。通过crontab来自动同步)
命令详情请见参考1
mirror同步脚本:
#!/bin/bash
LOG="/home/loading/ubuntulog/ubuntu_mirror.`date +%m%d`.log"
OPTIONS="-avzSPL --timeout=300 --delete --delete-excluded"
EXCLUDE="--exclude=ubuntu/ --exclude=.~tmp~"
SRC="rsync://tw.archive.ubuntu.com/ubuntu/"
DST="/home/loading/ubuntu/"
rsync $OPTIONS $EXCLUDE $SRC $DST > $LOG
echo -e "\n" >> $LOG
date >> $LOG
df -h | grep /home >> $LOG
find /home/loading/ubuntulog/ -mtime +5 | xargs rm -rf ""
iso同步脚本:(硬盘空间有限去掉有关同步网站循环和相同文件..这需要因需要设置)
LOG="/home/loading/ubuntulog/ubuntuiso.`date +%m%d`.log"
OPTIONS="-avzSPL --timeout=300 --delete --delete-excluded"
OPT2="--exclude="
EX="releases/" EX1="8.04.4/" EX2="10.04.4/" EX3="edubuntu/" EX4="hardy/" EX5="kubuntu/"
EX6="lucid/" EX7="maverick" EX8="natty/" EX9="oneiric/" EX10="precise/" EX11="quantal/"
SRC="rsync://mirrors.xmu.edu.cn/ubuntu-releases/"
DEST="/home/loading/iso"
rsync $OPTIONS $OPT2$EX $OPT2$EX1 $OPT2$EX2 $OPT2$EX3 $OPT2$EX4 $OPT2$EX5 $OPT2$EX6 $OPT2$EX7 $OPT2$EX8 $OPT2$EX9 $OPT2$EX10 $OPT$EX11 $SRC $DEST > $LOG
echo -e "\n" >> $LOG
date >> $LOG
find /home/loading/ubuntulog/ -mtime +3 | xargs rm -rf ""
________________________________________________________________________________
Crontab设置: 根据《Cron Help Guide》
crontab -e
# m h dom mon dow command
0 2,12,20 * * * sh /home/loading/ubuntu_mirror.sh > /dev/null 2>&1
0 4,12,20 17,18,19 10 * sh /home/loading/isoubuntu.sh > /dev/null 2>&1
######
纠错 #
######
# /etc/init.d/rsync status
could not access PID file for rsync ... failed!
要修改/etc/default/rsync文件,允许 --daemon 启动:
RSYNC_ENABLE=false
改为
RSYNC_ENABLE=true
#############
关于web页面 #
#############
安装apache:
debian 下安装包
apt-get install apache2
略写启动及配置
配置文件:httpd.conf
www路径:/var/www
为同步地址添加一个链接指向,通过web查看:
ln -s XXX
1.http://www.howtocn.org/rsync (命令等详解)
2.http://www.centos.bz/2011/06/rsync-server-setup/
3.http://blog.csdn.net/21aspnet/article/details/6798179 (Crontab用法详解)
4.http://www.rjkfw.com/s_1027.html (纠错)
rsync (remote synchronize)
是一个远程数据同步工具,可通过 LAN/WAN 快速同步多台主机之间的文件。也可以使用 rsync 同步本地硬盘中的不同目录。
rsync 是用于替代 rcp 的一个工具,rsync 使用所谓的 rsync算法 进行数据同步,这种算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。 您可以参考 How Rsync Works A Practical Overview 进一步了解 rsync 的运作机制。
rsync 的初始作者是 Andrew Tridgell 和 Paul Mackerras,目前由 http://rsync.samba.org 维护。
rsync 支持大多数的类 Unix 系统,无论是 Linux、Solaris 还是 BSD上 都经过了良好的测试。 CentOS系统默认就安装了 rsync 软件包。 此外,在 windows 平台下也有相应的版本,如 cwrsync 和DeltaCopy 等。
rsync服务器,以实现文件传输、备份和镜像。相对tar和wget来说,rsync 也有其自身的优点,比如速度快、安全、高效。
###########
服务器配置 #
###########
debian 下安装包:
apt-get install rsyncd
_____________________________________
配置文件:( 默认文件不存在 ) ps:对于服务器共享来说无须创建 密码文件
rsyncd.conf (rsync服务器主要配置文件)
rsyncd.secrets (登录rsync服务器的密码文件)
rsyncd.motd (定义rysnc 服务器信息的,也就是用户登录信息)
:/etc$ touch rsyncd.conf rsyncd.motd
_______________________________________________________________________
rsyncd.conf
# Minimal configuration file for rsync daemon
# See rsync(1) and rsyncd.conf(5) man pages for help
# This line is required by the /etc/init.d/rsyncd script
pid file = /var/run/rsyncd.pid 注:rsync的pid文件
motd file = /etc/rsyncd.motd 注:消息文件,当客户连接服务器时该文件的内容显示给客户
port = 注:运行端口,默认是873
uid = nobody 注:当该模块传输文件时守护进程具有的uid,配合gid选项使用可以确定访问件权限,默认:nobody
gid = nobody 注:当该模块传输文件时守护进程应该具有的gid,默认:nobody
use chroot = yes 注:如果”use chroot”指定为yes,那么rsync在传输文件以前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但是缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为yes。
read only = 注:设定是否允许客户上载文件。如果为yes那么任何上载请求都会失败,如果为no并且服务器目录读写权限允许那么上载是允许的。默认值为yes
max connections = 注:指定该模块的最大并发连接数量以保护服务器,超过限制的连接请求将被告知随后再试
log file = /var/log/rsyncd.log 注:rsync的日志文件,而不将日志发送给syslog
log format = %t %a %m %f %b
注:用户在使用transfer logging可以自己定制日志文件的字段。其格式是一个包含格式定义符的字符串,可以使用的格式定义符如下所示:
%h 远程主机名 %a 远程IP地址 %l 文件长度字符数 %p 该次rsync会话的进程id
%o 操作类型:”send”或”recv” %f 文件名 %P 模块路径 %m 模块名 %t 当前时间
%u 认证的用户名(匿名时是null) %b 实际传输的字节数
%c 当发送文件时,该字段记录该文件的校验码
默认log格式为:”%o %h [%a] %m (%u) %f %l”,一般来说,在每行的头上会添加”%t [%p] “。在源代码中同时发布有一个叫rsyncstats的perl脚本程序来统计这种格式的日志文件
timeout = 300 注:可以覆盖客户指定的IP超时时间。通过该选项可以确保rsync服务器不会永远等待一个崩溃的客户。超时单位为秒钟,0表示没有超时定义,这也是默认值
[modlue]
path = 注:该模块的供备份的目录树路径,该参数是必须指定的
comment = 给模块指定一个描述,该描述连同模块名在客户连接得到模块列表时显示给客户
________________________________________________________________________________
#########
服务设置 #
#########
启动rsync 服务器相当简单,–daemon 是让rsync 以服务器模式运行:
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf --log-file=/var/log/rsyncd.log
——————————————————————————————————————
开启防火墙:(默认端口:873)
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
如果需要开机自动加载可编辑入/etc/network/interface:
iptables-save > /etc/iptables.up.rules
在/etc/network/interfaces加入:
pre-up iptables-restore < /etc/iptables.up.rules
——————————————————————————————————————
同步数据:(由于需要定时同步数据,为此制作了运行脚本。通过crontab来自动同步)
命令详情请见参考1
mirror同步脚本:
#!/bin/bash
LOG="/home/loading/ubuntulog/ubuntu_mirror.`date +%m%d`.log"
OPTIONS="-avzSPL --timeout=300 --delete --delete-excluded"
EXCLUDE="--exclude=ubuntu/ --exclude=.~tmp~"
SRC="rsync://tw.archive.ubuntu.com/ubuntu/"
DST="/home/loading/ubuntu/"
rsync $OPTIONS $EXCLUDE $SRC $DST > $LOG
echo -e "\n" >> $LOG
date >> $LOG
df -h | grep /home >> $LOG
find /home/loading/ubuntulog/ -mtime +5 | xargs rm -rf ""
iso同步脚本:(硬盘空间有限去掉有关同步网站循环和相同文件..这需要因需要设置)
LOG="/home/loading/ubuntulog/ubuntuiso.`date +%m%d`.log"
OPTIONS="-avzSPL --timeout=300 --delete --delete-excluded"
OPT2="--exclude="
EX="releases/" EX1="8.04.4/" EX2="10.04.4/" EX3="edubuntu/" EX4="hardy/" EX5="kubuntu/"
EX6="lucid/" EX7="maverick" EX8="natty/" EX9="oneiric/" EX10="precise/" EX11="quantal/"
SRC="rsync://mirrors.xmu.edu.cn/ubuntu-releases/"
DEST="/home/loading/iso"
rsync $OPTIONS $OPT2$EX $OPT2$EX1 $OPT2$EX2 $OPT2$EX3 $OPT2$EX4 $OPT2$EX5 $OPT2$EX6 $OPT2$EX7 $OPT2$EX8 $OPT2$EX9 $OPT2$EX10 $OPT$EX11 $SRC $DEST > $LOG
echo -e "\n" >> $LOG
date >> $LOG
find /home/loading/ubuntulog/ -mtime +3 | xargs rm -rf ""
________________________________________________________________________________
Crontab设置: 根据《Cron Help Guide》
crontab -e
# m h dom mon dow command
0 2,12,20 * * * sh /home/loading/ubuntu_mirror.sh > /dev/null 2>&1
0 4,12,20 17,18,19 10 * sh /home/loading/isoubuntu.sh > /dev/null 2>&1
######
纠错 #
######
# /etc/init.d/rsync status
could not access PID file for rsync ... failed!
要修改/etc/default/rsync文件,允许 --daemon 启动:
RSYNC_ENABLE=false
改为
RSYNC_ENABLE=true
#############
关于web页面 #
#############
安装apache:
debian 下安装包
apt-get install apache2
略写启动及配置
配置文件:httpd.conf
www路径:/var/www
为同步地址添加一个链接指向,通过web查看:
ln -s XXX