rsync 的简介:
rsync 包括如下的一些特性:
* can update whole directory trees and filesystems
能更新整个目录和树和文件系统;
* optionally preserves symbolic links, hard links, file ownership, permissions, devices and times
有选择性的保持符号链链、硬链接、文件属于、权限、设备以及时间等;
* requires no special privileges to install
对于安装来说,无任何特殊权限要求;
* internal pipelining reduces latency for multiple files
对于多个文件来说,内部流水线减少文件等待的延时;
* can use rsh, ssh or direct sockets as the transport
能用rsh、ssh 或直接端口做为传输入端口;
* supports anonymous rsync which is ideal for mirroring
支持匿名rsync 同步文件,是理想的镜像工具;
2.0架设rsync服务过程:
#rpm -qa rsync 查看是不是安装、版本信息
2.1 rsync安装:
[root@linuxsir:beinan]$ sudo apt-get install rsync 注:在debian、ubuntu 等在线安装方法;
[root@linuxsir:beinan]# slackpkg install rsync 注:Slackware 软件包在线安装;
[root@linuxsir:beinan]# yum install rsync 注:Fedora、Redhat 等系统安装方法
其它Linux发行版,请用相应的软件包管理方法来安装;如果是源码包,也就是用下面的办法;
[root@linuxsir:/home/beinan]# tar xvf sync-xxxx.tar.gz 或sync-xxx.tar.bz2
[root@linuxsir:/home/beinan]# cd sync-xxx
[root@linuxsir:/home/beinan/sync-xxx]# ./configure --prefix=/usr ;make ;make install 注:在用源码包编译安装之前,您得安装gcc等编译开具才行;
2.2 rsync服务器的配置文件rsyncd.conf ;
#mkdir /etc/rsyncd 注:在/etc目录下创建一个rsyncd的目录,我们用来存放rsyncd.conf 和rsyncd.secrets文件;
#touch /etc/rsyncd/rsyncd.conf 注:创建rsyncd.conf ,这是rsync服务器的配置文件;
#touch /etc/rsyncd/rsyncd.secrets 注:创建rsyncd.secrets ,这是用户密码文件;
#chmod 600 /etc/rsyncd/rsyncd.secrets 注:为了密码的安全性,我们把权限设为600;
#ls -lh /etc/rsyncd/rsyncd.secrets
-rw------- 1 root root 14 2007-07-15 10:21 /etc/rsyncd/rsyncd.secrets
#touch /etc/rsyncd/rsyncd.motd
2.3 rsyncd.conf 是rsync服务器主要配置文件,我们来个简单的示例
# Distributed under the terms of the GNU General Public License v2
# 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 # 注:告诉进程写到 /var/run/rsyncd.pid 文件中;
port = 873 # 默认端口
address = 220.181.85.205 # rsync端口
#uid = nobody # 服务器端传输文件时,要发哪个用户和用户组来执行,默认是nobody
#gid = nobody # 组是nobody
uid = root # 如果用nobody可能遇到权限问题,所以就用了root权限
gid = root
use chroot = yes #用chroot,在传输文件之前,服务器守护程序在将chroot 到文件系统中的目录中,这样做的好处是可能保护系统被安装 #漏洞侵袭的可能。缺点是需要超级用户权限。
read only = yes #read only 是只读选择,也就是说,不让客户端上传文件到服务器上。还有一个 write only选项,自己尝试是做什么用 #的吧;
#limit access to private LANs
hosts allow= 192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0 #在您可以指定单个IP,也可以指定整个网段,能提高安全性。格式是 #ip 与ip 之间、ip和网段之间、网段和网段之间要用空格隔开;
hosts deny=*
max connections = 5 # 客户端最多连接数;
motd file = /etc/rsyncd/rsyncd.motd #motd file 是定义服务器信息的,要自己写 rsyncd.motd 文件内容。
#This will give you a separate log file
#log file = /var/log/rsync.log #rsync 服务器的日志;
#This will log every file transferred - up to 85,000+ per user, per sync
#transfer logging = yes #是传输文件的日志;
log format = %t %a %m %f %b #日志格式
syslog facility = local3
timeout = 300
[squidhome] #它为我们提供了一个链接的名字,链接到哪呢
path = /usr/local/squid/var/logs #那个目录文件进行数据同步
list=yes #意思是把rsync 服务器上提供同步数据的目录在服务器上模块是否显示列出来,no是不现 ignore errors # 注:忽略IO错误,详细的请查文档;
auth users = squid #认证用户,本用户必须是在系统里面存在
secrets file = /etc/rsyncd/rsyncd.secrets #密码存放的位置
comment = squid home date #:注释可以自己定义,写什么都行,写点相关的内容就行;
exclude = beinan/ samba/ #exclude 是排除的意思,也就是说,要把/home目录下的beinan和samba 排除在外; #beinan/和samba/目录之间有空格分开 ;
vi /etc/rsyncd/rsyncd.secerts
squid:222222
vi /etc/rsyncd/rsyncd.motd
+++++++++++++++++++++++++++++++++
+++ squidhome rsync server ++
+++++++++++++++++++++++++++++++++
3.启动rsync 服务器及防火墙的设置;
3.1 启动rsync服务
启动rsync 服务器相当简单,--daemon 是让rsync 以服务器模式运行;
#/usr/bin/rsync --daemon --config=/etc/rsync/rsync.conf
如果你找不到rsync 命令,你应该知道rsync 是安装在哪了。比如rsync 可执行命令可能安装在了 /usr/local/bin目录;也就是如下的命令
#/usr/local/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
当然您也可以写一个脚本来开机自动启动rysnc 服务器,你自己查查文档试试,这个简单。因为我用slackware 也有一个类似的脚本。我感觉不如直接手工运行方面,或者把这个命令写入/etc/rc.local文件中,这样也一样能自动运行;
3.2 rsync服务器和防火墙;
#vi /etc/sysconfig/iptable
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
#/etc/init.d/iptable restart
4.通过rsync客户端来同步数据
4.1 我们看看rsync服务器上提供了哪些可用的数据源;
#rsync --list-only sqyid@192.168.168.97::
+++++++++++++++++++++++++++++++++
+++ squidhome squid server ++
+++++++++++++++++++++++++++++++++
squidhome squid home data
4.2 同步数据
rsync -avzP squid@192.168.16897::squidhome /uo1/logs/squid
/uo1/logs/squid 如果没有就默认会在当前路径生成
Password: 这里要输入linuxsir的密码,是服务器端提供的,在前面的例子中,我们用的是 222222,输入的密码并不显示出来;输好后就回车;
5.让rsync 客户端自动与服务器同步数据;
5.1 第一步:创建同步脚本和密码文件
#mkdir /etc/cron.daily.rsync
#cd /etc/cron.daily.rsync
#touch squid.sh
# chmod 755 /etc/cron.daily.rsync/*.sh
# mkdir /etc/rsyncd/
#touch /etc/rsyncd/rsyncsquidsir.password
# chmod 600 /etc/rsyncd/rsyncbeinan.*
我们编辑squid.sh,内容是如下的:
#!/bin/sh
#squid.org home backup
/usr/bin/rsync -avzP --password-file=/etc/rsyncd/rsynclinuxsir.password squid@192.168.168::squidhome /uo1/log/squid/$(date +'%m-%d-%y')
5.2 第二步:修改crond服务器的配置文件
#crontab -e
# Run daily cron jobs at 4:10 every day backup linuxsir data:
0 1 * * * /usr/bin/run-parts /etc/cron.daily.rsync 1> /dev/null
第一行是注释,是说明内容,这样能自己记住。
第二行表示在每天早上4点10分的时候,运行 /etc/cron.daily.rsync 下的可执行脚本任务;
5.3 重启crond服务器;
配置好后,要重启crond 服务器;
# killall crond 注:杀死crond 服务器的进程;
# ps aux |grep crond 注:查看一下是否被杀死;
# /usr/sbin/crond 注:启动 crond 服务器;
# ps aux |grep crond 注:查看一下是否启动了?
root 3815 0.0 0.0 1860 664 ? S 14:44 0:00 /usr/sbin/crond
root 3819 0.0 0.0 2188 808 pts/1 S+ 14:45 0:00 grep crond
转载于:https://blog.51cto.com/wans0315/395673