rsync同步备份服务器文件夹_rsync同步备份服务器文件参数介绍

更新时间:2018-10-20    来源:php安装    手机版     字体:

【www.bbyears.com--php安装】

rsync安装

目前rsync的最新版本是3.0.9

wget http://rsync.samba.org/ftp/rsync/rsync-3.0.9.tar.gz tar -xvzf rsync-3.0.9.tar.gz; cd rsync-3.0.9; ./configure; make; make install

rsync服务器端配置
1.
vi /etc/rsyncd.conf(需要手动生成)

rsyncd.conf的参数写在上边就是全局参数和写在模块里的就是模块参数

#运行RSYNC守护进程的用户 uid = root #运行RSYNC守护进程的组 gid = root #不使用chroot use chroot = no #最大连接数是4 max connections = 4 #pid文件存放位置 pid file = /var/run/rsyncd.pid #锁文件存放位置 lock file = /var/run/rsync.lock #日志文件存放位置 log file = /var/log/rsyncd.log [logs] #要同步的目录 path = /usr/local/lnmp#忽略无关的IO错误 ignore errors #只读,不能上传 read only = true#禁止查看文件列表 list = false#允许访问服务的ip #hosts allow = 192.168.1.200 #禁止访问服务的ip #hosts deny = 0.0.0.0/32 #认证的用户名,系统必须存在的用户,但是密码需要在secrets file 配置,不是系统的密码。 auth users = root #认证用户密码文件,配置auth users的密码 secrets file = /etc/backserver.pas

2.
rsync密码文件是以冒号分隔的明文。

每个用户一行,前面是用户名,后面是密码,用户需要是操作系统存在的用户。

该文件的所有者必须是root,权限是600。

vi /etc/backserver.pas,添加

root:123456

保存并退出后执行

chown root:root /etc/backserver.pas chmod 600 /etc/backserver.pas

3.
启动rsync服务

/usr/local/bin/rsync --daemon

4.
检查进程是否存在,

ps -aux |grep rsyncroot      4406  0.0  0.0   4228   588 ?        Ss   May14   0:00 /usr/local/bin/rsync --daemon netstat -an |grep 873 tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN

出现以上结果,表明rsync服务器端已经启动。

5.
打开防火墙

iptables -i INPUT -p tcp --dport 873 -j ACCEPT
iptables -L
结果如下
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:rsync

6.
结束进程:kill -9 pid的值
kill -15 进程名

7.
rsync开机启动
echo "/usr/bin/rsync --daemon" >> /etc/rc.local

(开机自动启动rsync服务)

注意事项
1) 提示密码文件不能读,需要手工输入密码时,可能就是密码文件权限不是600,或者格式不对,或者是路径不对。
2) 提示要创建新目录或文件传输失败时,可能是欲同步的目录没有权限,最好把欲同步的目录权限修改为744。
3) 从客户端同步文件到服务端时,最好单个文件目录传输,否则易出错。

rsync客户端配置

1.
客户端不用配置,直接使用rsync命令就可以,

rsync -vzrtopg --progress --delete root@118.244.216.177::logs /data/lnmp

2.
如果想rsync无密码登陆,客户端只要配置密码文件即可。

vim /etc/rsyncd.secretes ,这个文件只包含服务器端auth user的密码,不需要配置用户名
123456
chmod 600 rsyncd.secretes (密码文件权限为600,此步必须设置)

注:此处密码一定要与rsync服务器端密码文件中密码保持一致。并且应为rsync客户端密码。

rsync -vzrtopg --progress --delete --password-file=/etc/rsyncd.secretes root@118.244.216.177::logs /data/lnmp

注:上述命令是指把服务端的这个路径(/usr/local/lnmp)下的文件同步到客户端的这个路径(/data/lnmp)下。

若想反过来,把客户端的文件同步到服务端,则为如下:
rsync -vzrtopg --progress --delete --password-file=/etc/rsyncd.secretes /data/lnmp root@118.244.216.177::logs

3.
rsync定时任务

早上四点执行rsync备份

crontab -e
00 04 * * * rsync -vzrtopg --progress --delete --password-file=/etc/rsyncd.secretes root@118.244.216.177::logs /data/lnmp

4.
Rsync 同步参数说明
-vzrtopg里的v是verbose,z是压缩,r是recursive,topg都是保持文件原有属性如属主、时间的参数。
--progress是指显示出详细的进度情况
--delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除
root@xxx.xxx.xxx.xxx中的root是指定密码文件中的用户名,xxx为ip地址
logs是指在rsyncd.conf里定义的模块名
/data/lnmp 是指本地要备份目录

5.
rsync常用参数:
#rsync [option] 源路径 目标路径
其中[option]为:
a:使用archive模式,等于-rlptgoD,即保持原有的文件权限
z:表示传输时压缩数据
v:显示到屏幕中
e:使用远程shell程序(可以使用rsh或ssh)
–delete:精确保存副本,源主机删除的文件,目标主机也会同步删除
–include=PATTERN:不排除符合PATTERN的文件或目录
–exclude=PATTERN:排除所有符合PATTERN的文件或目录
–password-file:指定用于rsync服务器的用户验证密码

--------------------------------------------------------
rsync常见错误排错
1.
rsync: failed to connect to 118.244.216.177: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.6]
原因:防火墙屏蔽了端口
解决:打开873段考
iptables -i INPUT -p tcp --dport 873 -j ACCEPT
iptables -L
如果以上指令不行,可以直接停掉防火墙
/etc/init.d/iptables stop

2.
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
那估计是密码文件没有设置权限哦: chmod 600 /home/admin/security/rsync.pass
应该差不多就可以了

3.@ERROR: auth failed on module xxxxx
rsync: connection unexpectedly closed (90 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
这是因为密码设错了, 无法登入成功, 请检查一下 rsyncd.scrt 中的密码, 二端是否一致?

4.password file must not be other-accessible
continuing without password file
Password:
这表示 rsyncd.scrt 的档案权限属性不对, 应设为 600。

5.@ERROR: chroot failed
rsync: connection unexpectedly closed (75 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
这通常是您的 rsyncd.conf 中的 path 路径所设的那个目录并不存在所致.请先用 mkdir开设好要备份目录

6.@ERROR: access denied to www from unknown (192.168.1.123)
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(359)
最后原因终于找到了。因为有两个网段都需要同步该文件夹内容,但没有在hosts allow 后面添加另一个IP段
hosts allow = 192.168.1.0/24
改为
hosts allow = 192.168.1.0/24 192.168.2.0/24
重新启动rsync服务,问题解决

7.@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
client端没有设置/etc/rsync.pas这个文件,而在使用rsync命令的时候,加了这个参数--password-file=/etc/rsync.scrt

8.rsync: recv_generator: mkdir "/teacherclubBackup/rsync……" failed: No space left on device (28)
*** Skipping any contents from this failed directory ***
磁盘空间满了

9.rsync: opendir "/kexue" (in dtsChannel) failed: Permission denied (13)
同步目录的权限设置不对,改为755

10.rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(759) [receiver=3.0.5]
未启动xinetd守护进程
[root@CC02 /]# service xinetd start

11.rsync: unable to open configuration file "/etc/rsyncd.conf": No such file or directory
xnetid查找的配置文件位置默认是/etc下,在/etc下找不到rsyncd.conf文件

12.rsync: failed to connect to 203.100.192.66: Connection timed out (110)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
连接服务器超时,检查服务器的端口netstat –tunlp,远程telnet测试

13.我需要在防火墙上开放哪些端口以适应rsync?
视情况而定。rsync可以直接通过873端口的tcp连接传文件,也可以通过22端口的ssh来进行文件传递,但你也可以通过下列命令改变它的端口:
rsync --port 8730 otherhost::
或者
rsync -e "ssh -p 2002" otherhost:

14.我如何通过rsync只复制目录结构,忽略掉文件呢?
rsync -av --include "*/" --exclude "*" source-dir dest-dir

15.为什么我总会出现"Read-only file system"的错误呢?
看看是否忘了设"read only = no"了

16.@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服务器端的目录不存在或无权限。创建目录并修正权限可解决问题。

17.@ERROR: auth failed on module tee
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服务器端该模块(tee)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。提供正确的用户名密码解决此问题。

18.@ERROR: Unknown module ‘tee_nonexists’
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题。

19.权限无法复制。去掉同步权限的参数即可。(这种情况多见于Linux向Windows的时候)

本文来源:http://www.bbyears.com/jiaocheng/45191.html

热门标签

更多>>

本类排行