Rsync安装和配置
一、Rsync简介
1.1什么是Rsync
Rsync是一款快速的,开源的,多功能的,可以实现全量和增量的远程和本地的数据同步和数据备份的工具。
全量的概念是:全部备份。
增量的概念是:差异化备份。对上一次基础上,对更新的部分作备份。
1.1.2 Rsync简介
Rsync具有可以使本地和远程的两台主机之间的数据快速同步镜像远程备份的功能,这个功能类似ssh带scp的命令,但是有优于scp的功能,scp每次都是全量拷贝,而rsync是增量拷贝。
Rsync还可以在本地主机的不同文件或者目录之间全量和增量的复制,类似于cp命令,cp命令是全量拷贝,而rsync是增量拷贝。
Rsync还可以实现删除文件和目录的功能,相当于rm命令
一个rsync相当于scp,cp,rm命令,并且还忧于他们每个命令,因为rsync具有增量备份的功能。
1.1.3 rsync的特性:
1)支持拷贝特殊文件如链接文件,设备等
2)可以有排除指定文件或者目录同步的功能,相当于打包命令tar的排除功能。
3)可以做到保持原文件或者目录的权限,时间,软硬链接,属组,主等所有属性均不改变
4)可以实现增量备份,既只同步发生变化的数据
5)可以勇士rcp,rsh,ssh等方式来配合传输文件
6)可以通过socket传输文件和数据
7)支持匿名的认证模式传输
1.1.4 rsync三种工作方式
1)本地模式,相当于cp和rm命令
|
1
2
3
4
5
6
7
8
9
|
[root@rsync tmp]# rsync /etc/passwd /tmp/ ##相当于cp的命令[root@rsync tmp]# lspasswd [root@rsync mnt]# rsync -avz --delete passwd /mnt/ ##--delete相当于删除的功能[root@rsync mnt]# lspasswd--delete的作用是删除的功能,本地有什么,远端就有什么。比如本地有passwd的内容,不管/mnt目录下面有什么,都只有passwd的内容,所有谨慎用--delete |
2)通道模式,一般配合ssh key免秘钥使用,结合定时任务
|
1
2
3
4
5
6
|
[root@rsync mnt]# rsync -avz -e 'ssh -p 22' /etc/passwd root@10.0.0.31:/tmp/ root@10.0.0.31's password: sending incremental file listpasswd[root@nfs tmp]# ls ##在远端查看passwd |
3)daemon模式
1.1.5 rsync的参数说明
-v :详细输出
-z :传输时进行压缩以提高传输效率。
-a :归档模式,表示以递归的方式传输文件,并保持文件的属性
--exclude :排除不需要同步传输的文件或者目录
--delete: 让目标目录和源目录的数据一致
--bwlimit: 限制带宽,默认单位是:kb(案例:某DBA做数据同步,导致用户无法访问网站)
二、Rsync服务器的安装
2.1安装准备
2.1.1查看rsync的版本号
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@rsync ~]# rsync --versionrsync version 3.0.6 protocol version 30Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others.Web site: http://rsync.samba.org/Capabilities: 64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints, socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace, append, ACLs, xattrs, iconv, symtimesrsync comes with ABSOLUTELY NO WARRANTY. This is free software, and youare welcome to redistribute it under certain conditions. See the GNUGeneral Public Licence for details. |
2.1.2 查看服务器的内核,版本信息
|
1
2
3
4
5
6
|
[root@rsync ~]# cat /etc/redhat-release CentOS release 6.7 (Final)[root@rsync ~]# uname -r2.6.32-573.el6.x86_64[root@rsync ~]# uname -mx86_64 |
2.2主要讲一下通过daemon实现数据同步案例
/etc/rsyncd.conf是rsync的默认配置文件,该配置文件不存在,需要编辑内容
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[root@oldboy ~]# cat /etc/rsyncd.conf #rsync_config_____________________________start#created by oldboy 15:01 2007-6-5##rsyncd.conf start##uid = rsync ##进程对应的用户,是虚拟用户。远端的命令使用rsync访问共享目录gid = rsync ##进程对应的用户组。use chroot = no ##安全相关max connections = 200 ##最大连接数timeout = 300 ##超时时间pid file = /var/run/rsyncd.pid ##进程对应的进程号文件lock file = /var/run/rsyncd.lock ##锁文件log file = /var/log/rsyncd.log ##日志文件[backup] ###模块名称path = /backup ###服务器提供访问的目录ignore errors ##忽略错误read only = false ##可写list = false ##不能列表hosts allow = 172.16.1.0/24 ##允许的ip地址##hosts deny = 0.0.0.0/32auth users = rsync_backup ##虚拟用户secrets file = /etc/rsync.password ###虚拟密码#rsync_config________________________end |
其中rsync用户默认是不存在的,需要创建用户
|
1
2
3
4
5
6
7
8
|
[root@rsync ~]# useradd rsync -s /sbin/nologin -M[root@rsync ~]# cat /etc/passwd|grep rsyncrsync:x:501:501::/home/rsync:/sbin/nologin[root@rsync ~]# id rsyncuid=501(rsync) gid=501(rsync) 组=501(rsync)为什么用虚拟用户?应答:文件和进程都要满足属主的要求,文件和进程的存在一定是需要用户的,也是为了安全问题。 |
创建/backup目录,并且属主和属组都属于rsync
|
1
2
3
4
|
[root@rsync ~]# mkdir /backup/ -p[root@rsync ~]# chown -R rsync.rsync /backup/[root@rsync ~]# ls -ld /backup/drwxr-xr-x 2 rsync rsync 4096 12月 9 2016 /backup/ |
创建配置文件/etc/rsync.password,默认不存在这个配置文件
|
1
2
3
4
5
|
[root@rsync ~]# cat /etc/rsync.password rsync_backup:oldboy[root@rsync ~]#chmod 600 /etc/rsync.password[root@rsync ~]# ls -l /etc/rsync.password -rw-------. 1 root root 20 11月 29 01:14 /etc/rsync.password |
|
1
2
3
4
|
启动服务:[root@rsync ~]# rsync --daemon [root@rsync ~]#ps -ef|grep rsync|grep -v grep ##查看进程有没有启动root 3046 1 0 15:19 ? 00:00:00 |
加入开机自启动
|
1
2
|
[root@rsync ~]# tail -1 /etc/rc.local /usr/bin/rsync --daemon |
三、Rsync客户端的安装
编辑配置文件/etc/rsync.passwd,该配置文件默认不存在
|
1
2
3
4
|
vim /etc/rsync.passwd[root@oldboy backup]# cat /etc/rsync.password oldboychmod 600 /etc/rsync.passwd |
创建backup目录
|
1
2
3
|
mkdir -p /backupcd /backuptouch stu{01,100} |
客户端推送:
|
1
2
3
4
|
方法1:[root@oldboy backup]# rsync -avz /backup/ rsync_backup@172.16.1.41::backup/ --password-file=/etc/rsync.password 方法2:[root@oldboy backup]# rsync -avz /backup/ rsync://rsync_backup@172.16.1.41/backup/ --password-file=/etc/rsync.password |
从客户端把服务端的东西拉回来的方案
服务端:
|
1
2
3
|
[root@oldboy backup]# touch 1 234[root@oldboy backup]# ls1 234 |
客户端:
|
1
2
3
4
5
6
7
8
9
10
|
[root@oldboy ming]# rsync -avz rsync_backup@172.16.1.41::backup/ /ming/ --password-file=/etc/rsync.password receiving incremental file list./1234 sent 105 bytes received 204 bytes 618.00 bytes/sectotal size is 0 speedup is 0.00[root@oldboy ming]# ls1 234 |
四、Rsync多模块实战
1.1.1 多模块实战
实例1:
环境:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[root@oldboy ~]# cat /etc/rsyncd.conf#rsync_config_____________________________start#created by oldboy 15:01 2007-6-5##rsyncd.conf start##uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock log file = /var/log/rsyncd.logignore errorsread only = falselist = falsehosts allow = 172.16.1.0/24hosts deny = 0.0.0.0/32auth users = rsync_backupsecrets file = /etc/rsync.password [backup] path = /backup [chen]path = /chen#rsync_config________________________end |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
服务器端:[root@oldboy ~]# mkdir /chen[root@oldboy ~]# ls -ld /chen/drwxr-xr-x 2 rsync rsync 4096 12月 2 18:58 /chen/客户端[root@oldboy ~]# ls -ld /mingdrwxr-xr-x 2 root root 4096 12月 2 18:26 /ming[root@oldboy ~]# rsync -avz /ming/ rsync_backup@172.16.1.41::chen/ --password-file=/etc/rsync.password sending incremental file list./ming1ming10ming2ming3ming4ming5ming6ming7ming8ming9 sent 463 bytes received 201 bytes 1328.00 bytes/sectotal size is 0 speedup is 0.00 |
服务端查看效果:
|
1
2
|
[root@oldboy chen]# lsming1 ming10 ming2 ming3 ming4 ming5 ming6 ming7 ming8 ming9 |
实例2:
环境:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
[root@oldboy chen]# cat /etc/rsyncd.conf#rsync_config_____________________________start#created by oldboy 15:01 2007-6-5##rsyncd.conf start##uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock log file = /var/log/rsyncd.logignore errorsread only = falselist = falsehosts allow = 172.16.1.0/24hosts deny = 0.0.0.0/32auth users = rsync_backupsecrets file = /etc/rsync.password [backup] path = /backup [chen]path = /chen[luo]path = /luoignore errorsread only = falselist = false |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
hosts allow = 172.16.1.0/24hosts deny = 0.0.0.0/32auth users = tangsecrets file = /etc/tang#rsync_config________________________end[root@oldboy chen]# mkdir /luo[root@oldboy chen]# chown rsync.rsync /luo[root@oldboy chen]# ls -ld /luo/drwxr-xr-x 2 rsync rsync 4096 12月 2 19:18 /luo/[root@oldboy chen]# cat /etc/tang tang:tangguo[root@oldboy luo]# ls /etc/tang -ld-rw------- 1 root root 13 12月 2 19:34 /etc/tang权限一定要是600 |
客户端配置:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@oldboy ming]# cat /etc/tang tangguo[root@oldboy ming]# ls /etc/tang -ld-rw------- 1 root root 8 12月 2 19:35 /etc/tang客户端权限也一定要是600[root@oldboy ming]# rsync -avz /ming/ tang@172.16.1.41::luo/ --password-file=/etc/tang sending incremental file list./ming1ming10ming2ming3ming4ming5ming6ming7ming8ming9 sent 463 bytes received 201 bytes 1328.00 bytes/sectotal size is 0 speedup is 0.00 |
五、Rsync案例排错
5.1 案例1
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@oldboy ming]# rsync -avz /ming/ tang@172.16.1.41::luo/ --password-file=/etc/tang @ERROR: auth failed on module luorsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6][root@oldboy luo]# tail -3 /var/log/rsyncd.log 2016/12/02 19:46:18 [3601] secrets file must not be other-accessible (see strict modes option)2016/12/02 19:46:18 [3601] continuing without secrets file2016/12/02 19:46:18 [3601] auth failed on module luo from unknown (172.16.1.31): missing secret for user "tang"报错的原因是服务器端的/etc/tang的权限问题没有设置为600,我们查看一下。[root@oldboy luo]# ls -ld /etc/tang -rwxr-xr-x 1 root root 13 12月 2 19:34 /etc/tang权限改为600就可以了 |
5.2 案例2
|
1
2
3
4
5
6
7
8
9
10
|
[root@oldboy ~]# rsync -avz /ming/ tang@172.16.1.41::luo/ --password-file=/etc/tang @ERROR: auth failed on module luorsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]查看日志[root@oldboy luo]# tail -3 /var/log/rsyncd.log 2016/12/02 19:52:12 [3614] name lookup failed for 172.16.1.31: Name or service not known2016/12/02 19:52:12 [3614] connect from UNKNOWN (172.16.1.31)2016/12/02 19:52:12 [3614] auth failed on module luo from unknown (172.16.1.31): password mismatchpassword mismatch,密码错误,客户端和服务器端的密码不一致导致的问题。【注意】有的客户端和服务器端密码看起来一样,实际里面有空格,也能报错,注意一下 |
5.3 案例3
|
1
2
3
4
|
[root@oldboy ~]# rsync -avz /backup/ rsync://rsync_backup@172.16.1.41/backup/ --password-file=/etc/rsync.password @ERROR: chdir failedrsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]原因:服务端的backup目录不存在 |
5.4 案例4
|
1
2
3
4
5
6
7
8
9
10
|
[root@oldboy ~]# rsync -avz /backup/ rsync://rsync_backup@172.16.1.41/backup/ --password-file=/etc/rsync.password sending incremental file list./rsync: failed to set times on "." (in backup): Operation not permitted (1)1 sent 4325 bytes received 1911 bytes 12472.00 bytes/sectotal size is 0 speedup is 0.00rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]原因:服务端backup的属组和属主问题 |
Rsync安装和配置的更多相关文章
- Linux下rsync 安装与配置
1.什么是rsync Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 程两个 ...
- rsync安装及配置
一.Server端 CentOS 6下安装yum -y install xinetd1.配置:vi /etc/xinetd.d/rsyncservice rsync{ disable = yes ...
- rsync 安装与配置
1.什么是rsync Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 程两个 ...
- rsync安装及其配置
服务端配置安装 服务器 第一步: 下载rsync 安装包(在线安装或者线下安装) wget https://download.samba.org/pub/rsync/rsync-3.1 ...
- rsync安装与配置使用 数据同步方案(centos6.5)
rsync + crond ==定时数据同步 sersync(inotify) + rsync ==实时数据同步,利用rsync实现 ##应用场景 ..1 主备服务器之间同步数据定时 = ...
- rsync定时同步配置
附上脚本 三大配置文件请看rsync安装与配置 #!/bin/sh #linuxsir.org home backup #/usr/bin/rsync -avzP --password-file=/e ...
- 【转载】CentOS 6.3下rsync服务器的安装与配置
一.rsync 简介 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件,也可以使用 Rsync 同步本地硬盘中的不同目录. Rsy ...
- CentOS 6.3下rsync服务器的安装与配置
一.rsync 简介 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件,也可以使用 Rsync 同步本地硬盘中的不同目录. Rsy ...
- rsync安装配置及故障解决完全教程[window, 文件同步]
Rsync是的全称是: remote synchronize, 也就是远程同步数据, 它是一款不错的文件同步软件,而且是免费的, 它在镜像保存整个目录树和文件系统的同时保持原来文件的权限.时间.软硬链 ...
随机推荐
- 学习Android开发看那些书好?
学习一样新事物或许有多种方式,报培训班,看视频,向高手请教等等,但一本好书往往可以让你少走很多弯路,事半功倍. 下面推荐几本个人觉得搞Android开发值得一读的书籍. Head First Java ...
- [2012-12-18 14:59:31]AS3 常用正则表达式的总结-不用google了,我帮收集的很多了
正则表达式是一种通用的标准,大部分计算机语言都支持正则表达式,包括as3,这里转摘出了一些常用的正则表达式语句,大家用到的时候就不用自己写了.红色字体为常用正则:下面这个类是我自己写的一个示例,想必大 ...
- python_threading模块实现多线程详解(转)
综述 Python这门解释性语言也有专门的线程模型,Python虚拟机使用GIL(Global Interpreter Lock,全局解释器锁)来互斥线程对共享资源的访问,但暂时无法利用多处理器的优势 ...
- String类型 在底层剖析,并比较 与StringBuilding 的区别
1.string和 stringbuilder的区别: String在任何语言中,都有它的特殊性,在.NET中也是如此.它属于基本数据类型,也是基本数据类型中唯一的引用类型.字符串可以声明为常量,但是 ...
- jquery阻止事件冒泡的方法
$("table tbody").click(function(e) { e.preventDefault(); //阻止自身的事件,并不能阻止冒泡 e.stopPropagati ...
- 编码解码--三种常见字符编码简介:ASCII、Unicode和UTF-8
什么是字符编码? 计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte),所以,一个字节能表示的最大的整数就是255( ...
- RocEDU.阅读.写作《苏菲的世界》书摘(二)
"迷信",多么奇怪的一个名词.如果你信基督教或伊斯兰教,这就叫"信仰",但如果你相信占星术或十三号星期五不吉利,就是迷信.谁有权利说别人相信的东西就是" ...
- 20145322《Java程序设计》第5次实验报告
20145322<Java程序设计>第5次实验报告 实验内容 1.根据所学内容,编写代码实现服务器与客户端 2.掌握密码技术的使用 3.设计安全传输系统,客户端中输入明文,利用DES算法加 ...
- Eye Protection FAQ
Q: Why does smart protection not work? A: Please make sure the checkbox "Eye Protection" i ...
- hadoop 3.1.1 安装
以下是本次搭建说使用的服务器 服务器IP分配 IP 节点名称 说明 192.168.172.130 master 主服务器 192.168.172.131 slave1 从服务器1 192.168.1 ...