Ubuntu下安装配置rsync
Ubuntu下 rsync同步文件实例
一、基础知识
Rsync(remote synchronize) 是一个远程数据同步工具,可以使用“Rsync算法”同步本地和远程主机之间的文件。Rsync的好处是只同步两个文件不同的部分,相同的部分不在传递。类似于增量备份,这使的在服务器传递备份文件或者同步文件,比起scp工具要省好多时间。
rsync 分为服务器端、客户端,服务器端搭建比客户端辛苦一些(也是很简单)。
rsync 服务器是指以 deamon 方式运行 rsync 服务的服务器,需要打开 rsync deamon 和启动 xinetd 服务。默认端口873。
rsync 客户端是发起 rsync 连接的服务器,安装rsync即可。
rsync 客户端发起连接后,rsync 服务器会检查 rsync 客户端提交 rsync 服务器内建的户名和密码是否正确,如果通过认证检测,则开始文件传输,传输的过程是按要求先比对文件的大小、属性、权限、MD5值等信息,如果两端文件信息不一致,则按要求同步文件的区别块。
OS: ubuntu server 14.04
server: 192.168.46.91
client: 192.168.46.93
二、修改文件
1、ubuntu 14.04默认已安装rsync,rsync服务默认不是启动的,我们要修改下面的文件。
sudo vim /etc/default/rsync
RSYNC_ENABLE=true #false改true
2、修改配置文件
#已默认安装的软件,默认不启动的似乎都要这么做
sudo cp /usr/share/doc/rsync/examples/rsyncd.conf /etc
三、查看文件
我们先来查看一下这个文件
sudo cat /etc/rsyncd.conf
# sample rsyncd.conf configuration file
# GLOBAL OPTIONS
#motd file=/etc/motd #登录欢迎信息
#log file=/var/log/rsyncd #日志文件
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
pid file=/var/run/rsyncd.pid
#指定rsync发送日志消息给syslog时的消息级别,常见的消息级别是:uth, authpriv, cron, daemon, ftp, kern, lpr, mail, news, security, sys-log, user, uucp, local0, local1, local2, local3,local4, local5, local6和local7。默认值是daemon。
#syslog facility=daemon
#自定义tcp选项,默认是关闭的
#socket options=
#以下是模块信息,我们可以创建多个模块
# MODULE OPTIONS
[ftp]
comment = public archive #模块描述
path = /var/www/pub #需要同步的路径
use chroot = yes #默认是yes|true,如果为true,那么在rsync在传输文件以前首先chroot到path参数指定的目录下。这样做的原因是实现额外的安全防护,但是缺点是需要root权限,并且不能备份指向外部的符号连接指向的目录文件。
# max connections=10 #最大连接数
lock file = /var/lock/rsyncd #指定支持max connections参数的锁文件。
# the default for read only is yes...
read only = yes #只读选项
list = yes #客户请求时可用模块时是否列出该模块
uid = nobody #设定该模块传输文件时守护进程应该具有的uid
gid = nogroup #设定该模块传输文件时守护进程应具有的gid,此项与uid配合可以确定文件的访问权限
# exclude = #用来指定多个由空格隔开的多个模式列表,并将其添加到exclude列表中。这等同于在客户端命令中使用--exclude来指定模式,不过配置文件中指定的exlude模式不会传递给客户端,而仅仅应用于服务器。一个模块只能指定一个exlude选项,但是可以在模式前面使用"-"和"+"来指定是exclude还是include #这个我的理解是排除目录中不需同步的文件
# exclude from = #可以指定一个包含exclude模式定义的文件名
# include = #与exclude相似
# include from = #可以指定一个包含include模式定义的文件名
# auth users = #该选项指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块。这里的用户和系统用户没有任何关系。如果"auth users"被设置,那么客户端发出对该模块的连接请求以后会被rsync请求challenged进行验证身份这里使用的 challenge/response认证协议。用户的名和密码以明文方式存放在"secrets file"选项指定的文件中。默认情况下无需密码就可以连接模块(也就是匿名方式)
# secrets file = /etc/rsyncd.secrets #该文件每行包含一个username:password对,以明文方式存储,只有在auth users被定义时,此选项才生效。同时我们需要将此文件权限设置为0600
strict modes = yes #该选项指定是否监测密码文件的权限,如果该选项值为true那么密码文件只能被rsync服务器运行身份的用户访问,其他任何用户不可以访问该文件。默认值为true
# hosts allow = #允许的主机
# hosts deny = #拒绝访问的主机
ignore errors = no #设定rsync服务器在运行delete操作时是否忽略I/O错误
ignore nonreadable = yes #设定rysnc服务器忽略那些没有访问文件权限的用户
transfer logging = no #使rsync服务器使用ftp格式的文件来记录下载和上载操作在自己单独的日志中
# log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. #设定日志格式
timeout = 600 #超时设置(秒)
refuse options = checksum dry-run #定义一些不允许客户对该模块使用的命令选项列表
dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz #告诉rysnc那些文件在传输前不用压缩,默认已设定压缩包不再进行压缩
日志格式选项列表:
%h: 远程主机名
%a: 远程IP地址
%l: 文件长度字符数
%p: 该次rsync会话的进程id
%o: 操作类型:"send"或"recv"、”del.”
%f: 文件名
%P: 模块路径
%m: 模块名
%t: 当前时间
%u: 认证的用户名(匿名时是null)
%b: 实际传输的字节数
%c: 当发送文件时,该字段记录该文件的校验码
四、修改文件
(一)服务端(Server)
1、配置
rsync 配置文件安装完有时候并不存在,不必惊慌,可以手动创建。创建配置文件目录及文件如下:
| - /etc/rsyncd.conf(rsync 服务器的配置文件)
| - /etc/rsyncd(文件夹)
| - rsyncd.secrets(用户密码文件,客户端使用其中的账号密码访问,需要 600 权限)
| - rsyncd.motd(自定义用户登录后显示的服务器信息,即 messageoftoday)
mkdir /etc/rsyncd
touch /etc/rsyncd.conf #文件会存在
touch /etc/rsyncd/rsyncd.secrets
touch /etc/rsyncd/rsyncd.motd
sudo chmod 600 /etc/rsyncd/rsyncd.secrets
下面我们来定义自己的conf文件
sudo vim /etc/rsyncd.conf
# sample rsyncd.conf configuration file
# GLOBAL OPTIONS
#motd file=/etc/motd
log file=/var/log/rsyncd.log
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
# The init.d script does its own pid file handling,
# so omit the "pid file" line completely in that case.
pid file=/var/run/rsyncd.pid
port = 873 #指定监听端口,默认是873,可以自己指定
#syslog facility=daemon
syslog facility = local3 #日志级别
#socket options=
# MODULE OPTIONS
[careland]
comment = public archive
path = /home/careland
# path = /var/log
use chroot = yes
max connections=10
motd file = /etc/rsyncd/rsyncd.motd
lock file = /var/lock/rsyncd
# the default for read only is yes...
read only = no
# write only = yes
list = yes
#守护进程所属的uid,默认是nobody,可能会碰到文件或目录权限问题,此处偷懒用的 root
uid = root
##守护进程的gid
gid = root
# exclude =
exclude = error_log httpd.pid
# exclude from =
# include =
# include from =
auth users = careland
secrets file = /etc/rsyncd/rsyncd.secrets
strict modes = yes
# hosts allow =
# hosts deny =
ignore errors = yes
ignore nonreadable = yes
transfer logging = yes
log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
timeout = 600
refuse options = checksum dry-run
dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
sudo vim /etc/rsyncd/rsyncd.secrets 密码文件,用户名称与密码以冒号分隔,多个用户名和密码可以多行
careland:123
sudo vim /etc/rsyncd/rsyncd.motd 登录公告
++++++++++++++++++++++++++++++
+ John ABC rsync 2008-2099 +
++++++++++++++++++++++++++++++
2、启动服务端
启动服务端有两种方式。
(1)直接使用 --daemon 参数
/usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
(2)xinet方式
1).修改 /etc/services, sudo vim /etc/services
加入以下内容,如果已经有可以不加,如果端口改了,需要改掉 873 端口为指定端口
rsync 873/tcp # rsync
rsync 873/udp # rsync
2).修改 /etc/xinetd.d/rsync, sudo vim /etc/xinetd.d/rsync
主要是要打开rsync這個daemon, 一旦有rsync client要连接時, xinetd会把它转介給 rsyncd(port 873)
service rsync
{
disable = yes
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon --config=/etc/rsyncd/rsynd.conf
log_on_failure += USERID
}
3).重启 xinetd
sudo service xinetd restart
启动rsync
sudo /etc/init.d/rsync start
(二)客户端(Client)
创建密码文件,sudo vim /etc/rsyncd/rsyncd.secrets
,并修改为 600 权限
touch /etc/rsyncd/rsyncd.secrets
chmod 600 /etc/rsyncd/rsyncd.secrets
/etc/rsyncd/rsyncd.secrets其内密码需要和服务端内/etc/rsyncd/rsyncd.secrets指定用户的密码保持一致,不用添加用户名
123
下面我们来定义自己的conf文件
sudo vim /etc/rsyncd.conf
# sample rsyncd.conf configuration file
# GLOBAL OPTIONS
#motd file=/etc/motd #登录欢迎信息
#log file=/var/log/rsyncd #日志文件
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
pid file=/var/run/rsyncd.pid
#指定rsync发送日志消息给syslog时的消息级别,常见的消息级别是:uth, authpriv, cron, daemon, ftp
#syslog facility=daemon
#自定义tcp选项,默认是关闭的
#socket options=
#以下是模块信息,我们可以创建多个模块
# MODULE OPTIONS
[careland]
comment = public archive #模块描述
path = /home/one #需要同步的路径
use chroot = yes #默认是yes|true,如果为true,那么在rsync在传输文件以前首先chroot到pat
max connections=10 #最大连接数
lock file = /var/lock/rsyncd #指定支持max connections参数的锁文件。
# the default for read only is yes...
read only = no #只读选项
list = yes #客户请求时可用模块时是否列出该模块
### uid = nobody #设定该模块传输文件时守护进程应该具有的uid
uid = root
# gid = nogroup #设定该模块传输文件时守护进程应具有的gid,此项与uid配合可以确定文件的访>
gid = root
# exclude = #用来指定多个由空格隔开的多个模式列表,并将其添加到exclude列表中。这等同于在
# exclude from = #可以指定一个包含exclude模式定义的文件名
# include = #与exclude相似
# include from = #可以指定一个包含include模式定义的文件名
auth users = one #该选项指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块
secrets file = /etc/rsyncd/rsyncd.secrets #该文件每行包含一个username:password对,以明
strict modes = yes #该选项指定是否监测密码文件的权限,如果该选项值为true那么密码文件只
# hosts allow = #允许的主机
# hosts deny = #拒绝访问的主机
ignore errors = yes #设定rsync服务器在运行delete操作时是否忽略I/O错误
ignore nonreadable = yes #设定rysnc服务器忽略那些没有访问文件权限的用户
transfer logging = yes #使rsync服务器使用ftp格式的文件来记录下载和上载操作在自己单独的
# log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. #设定日志格式
timeout = 600 #超时设置(秒)
refuse options = checksum dry-run #定义一些不允许客户对该模块使用的命令选项列表
dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz3 *.tbz #告诉rysnc那些文件>
五、文件传输
1、客户端向服务端传输文件,如果是 873 端口,可以把 --port 去掉
# 上传
sudo rsync -vzrtopg --delete --progress test1/test1.py careland@192.168.46.91::careland/test2/2800.py --password-file=/etc/rsyncd/rsyncd.secrets --port=873
2、客户端向服务端获取文件,需要把服务器的 /etc/rsyncd.conf 中的 write only = yes 给去掉,没有则不需要
# 下载
sudo rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd/rsyncd.secrets --port=873 careland@192.168.46.91::careland/test2/crawler.log test1/crawler.log
六、其他
1、需要手动输入密码传输文件
# 需要输入密码
# 服务器文件同步到本地
# rsync -zvrtopg --progress -e 'ssh -p 22' root@服务器ip:远程项目目录 本地项目目录
rsync -zvrtopg --progress -e 'ssh -p 22' careland@192.168.46.91:test2 test1/
# 本地文件同步到服务器
# rsync -zvrtopg --progress --delete 本地项目目录 -e ssh root@服务器ip:远程项目目录
rsync -zvrtopg --progress --delete test2 -e ssh careland@192.168.46.91:test2/
2、可能遇到的问题:
(1)rsync error: some files/attrs were not transferred
原因是有文件没有写的权限,导致备份数据库权限不够,两种解决办法:
解决办法:
将服务端rsyncd.conf配置文件的uid和gid分别修改成root,重载下,/etc/rc.d/init.d/xinetd reload,再次执行同步,同步成功
注意:如果使用第一种办法,那么在执行完同步后,为了安全,记得将uid和gid修改回来,或修改成nobody
(2)rsync: failed to connect to 192.168.46.91 (192.168.46.91): Connection refused (111) rsync error: error in socket IO (code 10) at clientserver.c(128) [Receiver=3.1.1]
原因是rsync服务未打开
解决办法:
sudo /etc/init.d/rsync start
(3)ERROR: password file must be owned by root when running as root
在Ubuntu上设置rsync的时候,需要将passwordfile设置为600权限。
如果还有以上错误提示,可以使用chown命令将其文件的属主做下更改。具体操作如下:
# chown 当前用户名 目标文件
chown test rsyncd.secrets
网友评论