linux之rsync全网备份

rsync是一款开源,快速,多功能的可实现增量的本地或远程的数据镜像同步备份的优秀工具。适用于多个平台。从软件名称可以看出来是远程同步的意思(remote sync)。可使本地主机不同分区或目录之间及本地和远程两台主机之间的数据快速同步镜像,远程备份等功能。  

在同步备份时,默认情况下,rsync通过其独特的“quick check”算法,仅同步大小或者最后修改时间发生变化的文件或目录(也可根据权限,属主等变化同步,需要制定参数)。甚至是只同步一个文件里变化的内容部分,所以可以实现快速的同步数据的功能。

提示:传统的cp,scp工具拷贝每次均为完整拷贝,而rsync除了完整拷贝,还具备增量拷贝的功能,因此从此性能及效率上更胜一筹。

rsync的特性

1)支持拷贝特殊文件如链接,设备等

2)可以有排除指定文件或目录同步的功能,相当于打包命令tar

3)可以保持原来文件或目录的权限,时间,软硬链接等所有属性均不改变

4)可实现增量同步,即只同步发生变化的数据,因此数据传输效率更高

5)可以使用rcp,rsh,ssh等方式来配合传输文件,也可以通过直接的socker链接

6)支持匿名的或认证的进程模式传输,方便进行数据备份及镜像

三种模式:(rsync有限速功能哦~避免备份时带宽被占满)

本地:rsync [选项] SRC ... [DEST]

通过远程shell:不能与daemon同用

  拉取:rsync [选项...] [USER @]主机SRC ... [DEST]     

  推送:rsync [选项] SRC ... [USER @]主机:DEST

通过rsync守护进程访问:不能与远程shell同用,拉取与推送都是在客户端上执行,针对服务器进行操作的。

  拉取:rsync [选项] [USER @] HOST :: SRC ... [DEST]

            rsync [选项] rsync:// [USER @] HOST [:PORT] / SRC ... [DEST]

  推送:rsync [选项] SRC ... [USER @] HOST :: DEST

            rsync [选项] SRC ... rsync:// [USER @] HOST [:PORT] / DEST

企业使用备份:

cron+rsync 定时备份

sersync+rsync 实时备份

inotify+rsync 实时备份

lrsyncd+rsync 实时备份

安装rsync

[root@www1 ~]# tar  zxf rsync-3.1.0.tar.gz 
[root@www1 ~]# cd rsync-3.1.0
[root@www1 ~]# ./configure 
[root@www1 ~]# make 
[root@www1 ~]# make install 
或者
[root@www1 ~]# yum install rsync

本地模式:

rsync命令

-v 观察模式,打印输出
-q 与-v相反,仅展示错误信息
-r 递归,用于目录
-u 更新,若目标文件较新,则不覆盖文件
-l 复制链接文件的属性,而非链接文件的目标文件内容
-p 复制时保留源文件的权限
-g 保留源文件的所属组
-o 保留源文件的所有者
-D 保留源文件的装置属性,device
-I 大写i 忽略更新时间mtime属性,文件对比上会更快
-t 保存源文件的时间参数
-z 文件传输上加上压缩参数
-e 可以使用信道协议,如ssh
-a 同-rltopgD
--delete 从目标目录中删除文件
--exclude=PATTERN排除某个文件不比对
--exclude_from=file 排除多个文件不比对
--delete  比对,只推送比较后的差异部分,且会删除服务器端存在客户端没有的文件
--bwlimit 限制速度,以免带宽被占满

注:

[root@rsync ~]# rsync -avz /data/test/ /tmp/   #备份的是test下所有文件到tmp目录下

[root@rsync ~]# rsync -avz /data/test /tmp/     #备份的是test目录及目标下的所有文件到tmp下的test目录中

实例:

[root@rsync ~]# rsync install_httpd.sh /data/test/
[root@rsync ~]# ls -l install_httpd.sh /data/test/install_httpd.sh
-rwxr-xr-x. 1 root root 482 10月 26 17:09 /data/test/install_httpd.sh
-rwxr-xr-x. 1 root root 482 9月  29 00:54 install_httpd.sh
[root@rsync ~]# rsync  -t install_httpd.sh /data/test/                #-t 保留源文件的时间参数
[root@rsync ~]# ls -l install_httpd.sh /data/test/install_httpd.sh
-rwxr-xr-x. 1 root root 482 9月  29 00:54 /data/test/install_httpd.sh
-rwxr-xr-x. 1 root root 482 9月  29 00:54 install_httpd.sh
[root@rsync ~]# vim install_httpd.sh                           #修改文件内容
[root@rsync ~]# ls -l install_httpd.sh /data/test/install_httpd.sh
-rwxr-xr-x. 1 root root 482 9月  29 00:54 /data/test/install_httpd.sh
-rwxr-xr-x. 1 root root 488 10月 26 17:12 install_httpd.sh
[root@rsync ~]# rsync  -u  install_httpd.sh /data/test/                  #仅更新目标文件
[root@rsync ~]# ls -l install_httpd.sh /data/test/install_httpd.sh
-rwxr-xr-x. 1 root root 488 10月 26 17:12 /data/test/install_httpd.sh
-rwxr-xr-x. 1 root root 488 10月 26 17:12 install_httpd.sh
[root@rsync ~]# vim /data/test/install_httpd.sh
[root@rsync ~]# vim install_httpd.sh                           #再次更改文件内容
[root@rsync ~]# rsync -av install_httpd.sh  /data/test/install_httpd.sh        #使用-av 选项
sending incremental file list
install_httpd.sh

sent 566 bytes  received 31 bytes  1194.00 bytes/sec
total size is 482  speedup is 0.81

[root@rsync ~]# rsync -avz /data/test /tmp/
sending incremental file list
test/
test/1.log

sent 39759856 bytes  received 130 bytes  11359996.00 bytes/sec
total size is 49049701  speedup is 1.23
[root@rsync ~]# ls /tmp/
test  yum.log

[root@rsync ~]# rsync -avz /data/test/ /tmp/
sending incremental file list
./
1.log
sent 39759839 bytes  received 129 bytes  15903987.20 bytes/sec
total size is 49049701  speedup is 1.23
[root@rsync ~]# ls -l /tmp/
总用量 47916
-rwxr-xr-x. 1 nobody nobody    12877 10月 19 22:52 1.log

限速:--bwlimit=10

[root@rsync test]# rsync -avz --bwlimit=10 install.sh [email protected]:/tmp/
Address 192.168.146.132 maps to www.mtime.com, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[email protected]'s password:
sending incremental file list
install.sh

sent 308 bytes  received 31 bytes  96.86 bytes/sec
total size is 482  speedup is 1.42
[root@rsync test]#

远程shell模式:

实例:

报错rsync: Failed to exec ssh: No such file or directory (2)

[root@rsync ~]# rsync -avz /root/install.log.syslog -e 'ssh -p 22'  [email protected]:/tmp
rsync: Failed to exec ssh: No such file or directory (2)                #未安装ssh客户端
rsync error: error in IPC code (code 14) at pipe.c(84) [sender=3.0.6]
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in IPC code (code 14) at io.c(600) [sender=3.0.6]

报错rsync: Failed to exec ssh: No such file or directory (2),未安装ssh客户端

安装客户端

[root@rsync ~]# yum install openssh-clients -y                        #安装完成后执行rsync命令
[root@rsync ~]# rsync -avz /root/install.log.syslog -e 'ssh -p 22'  [email protected]:/tmp
The authenticity of host '192.168.146.132 (192.168.146.132)' can't be established.
RSA key fingerprint is c7:df:f1:9e:b8:6a:37:4b:6a:2e:09:c3:1e:e7:c6:b5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.146.132' (RSA) to the list of known hosts.
Address 192.168.146.132 maps to www.mtime.com, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[email protected]'s password:
sending incremental file list
install.log.syslog

sent 677 bytes  received 31 bytes  108.92 bytes/sec
total size is 3482  speedup is 4.92

传送多个文件:

#第一种方法

[root@rsync test]# rsync -avz [email protected]:/root/install.log :/root/install.log.syslog /tmp/
Address 192.168.146.132 maps to www.mtime.com, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[email protected]'s password:
receiving incremental file list
install.log
install.log.syslog
sent 163 bytes  received 731 bytes  255.43 bytes/sec
total size is 16359  speedup is 18.30
[root@rsync test]#

#第二种方法

[root@rsync test]# rsync -avz [email protected]:/root/{install.log,install.log.syslog} /data
Address 192.168.146.132 maps to www.mtime.com, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[email protected]'s password:
receiving incremental file list
install.log
install.log.syslog

sent 49 bytes  received 4361 bytes  1260.00 bytes/sec
total size is 16359  speedup is 3.71
[root@rsync test]#

使用守护进程rsync访问:使用端口873

启动方式:

通过为rsync指定--daemon选项来启动rsync守护程序
[root@rsync rsync]# rsync --daemon

编辑rsync配置文件rsyncd.conf,默认无此文件需要新建

[root@rsync ~]# cat /etc/rsyncd.conf
uid = rsync            #客户端连接到服务器时,使用rsync用户和用户组来操作test目录。
gid = rsync
use chroot = no
max connections = 20
timeout = 300
syslog facility = local5
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[test]
path = /data/rsync
ignore errors
read only = false
list = false
hosts allow=192.168.146.132
auth user = rsync_backup
secrets file = /etc/rsync.password
comment = whole ftp area (approx 6.1 GB)
[root@rsync ~]# useradd -s /sbin/nologin -M rsync            #需要创建实体用户,用于客户端连接时使用用户操作目录
[root@rsync ~]# rsync --daemon
[root@rsync ~]# ps aux | grep rsync
root      1595  0.2  0.0 107632   672 ?      Ss   18:58   0:00 rsync --daemon
root      1597  0.0  0.0 103264   892 pts/0    S+   18:59   0:00 grep rsync
[root@rsync ~]# netstat -antulp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 :::873                      :::*                        LISTEN      1595/rsync

[root@rsync ~]# mkdir /data/rsync                    #创建配置文件中指定的path目录,
[root@rsync ~]# chown rsync.rsync /data/rsync       #更改目录的属性,就是刚才创建的用户哦~
[root@rsync  ~]#  vim /etc/rsync.password               #创建密码文件,文件中格式是 验证用户:密码,如上的配置文件 指定的验证用户(可不是实体用户) rsync:111111

客户端也设置密码文件,需要更改文件权限,只读400的权限。
[root@www  ~]#  vim /etc/rsync.password
111111                     #只需要密码即可哦~

客户端推送数据至备份服务器

1.对比整个目录,再推送

[root@www rsync]# rsync -avz /data/rsync/ [email protected]::test/       #test为服务器rsyncd.conf中[]名称
sending incremental file list
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt

sent 490 bytes  received 201 bytes  1382.00 bytes/sec
total size is 0  speedup is 0.00
[root@www rsync]#

也可以推送某个单文件

[root@www1 ~]# rsync -avz /tmp/yum.log [email protected]::test/
sending incremental file list
yum.log
sent 64 bytes  received 27 bytes  182.00 bytes/sec
total size is 0  speedup is 0.00

拉取文件到某个目录

[root@www1 ~]# rsync -avz [email protected]::test/ /root
receiving incremental file list
./
1.log
1.txt
10.txt
10}.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
install_httpd.sh
yum.log
yum_save_tx-2018-10-08-18-13Z5upA_.yumtx
yum_save_tx-2018-10-08-18-14vT0Iop.yumtx
yum_save_tx-2018-10-08-18-16TNUlOG.yumtx
yum_save_tx-2018-10-08-18-16VmyP6K.yumtx
{1
.ICE-unix/
sent 397 bytes  received 5709 bytes  12212.00 bytes/sec
total size is 17677  speedup is 2.90
[root@www1 ~]# ls

rsyncd的配置文件/etc/rsyncd.conf:

全局参数

pid file 会将rsync守护进程写入此文件中

port 监听端口,可修改监听端口,默认873

[模块参数]

comment 对模块的描述

path 模块的实际目录

use chroot 是否使用chroot,将path指定为chroot,更安全,可做全局

daemon chroot 守护进程开始与客户端通信前进行的chroot,可做全局

numeric ids 启用chroot不启用此参数,可做全局

munge symlinks 

charset 可以单个模块指定字符集,可做全局

max connections 最大连接数量,0为不限制,可做全局

log file 指定模块的日志路径和文件名称,可作全局

syslog facility 是rsync发送给syslog的日志等级设置,若指定了log file 则此值无效

lock file 用于支持max connections的参数,让rsync确保共享模块的最大链接数量不超过此值

read only 只读,是否可上载文件到模块

write only 可写,是否可下载文件,若为真则无法下载文件,为假且文件权限正确则可以下载

list 客户端请求链接时是否列出此模块,当使用hosts allow或hosts deny拒绝访问时,rsync会隐藏模块

uid指定文件传输到该模块以及从该模块传输的用户名,结合“gid”参数,它确定了可用的文件权限

gid

daemon uid 运行rsync进程的用户id,通常是root,默认也是root

daemon gid 

exclude 排出,不做比对。多个文件使用空格分隔

exclude from 可以从文件做排除

include 

include from

auth users 列出了允许连接到此模块的用户名。用户名不需要存在于本地系统上,如果设置了“auth users”,则客户端将被要求提供用于连接到模块的用户名和密码,纯文本用户名和密码存储在“secrets file”参数指定的文件中。

secrets file 用于验证模块的密码文件,该文件是基于行的,包含认证名称和密码,密码不超过8位。超过8位不生效。

strict modes 若为真,则运行rsync的用户来验证密码文件。若为假则不生效。

hosts allow 指定可连接rsync服务器的ip、ip网段、域名等

hosts deny 不可连接rsync服务器的主机,与hosts allow不可同时存在,若同时存在allow优先

ignore errors 此参数指示rsyncd在决定是否运行传输的删除阶段时忽略守护程序上的I / O错误。通常,如果发生任何I / O错误,rsync会跳过--delete步骤,以防止由于临时资源短缺或其他I / O错误而导致的灾难性删除。在某些情况下,此测试会适得其反,因此您可以使用此参数来关闭此行为。

log format 日志格式

timeout 此参数可以确保rsync不会永远等待死客户端,超时限制

refuse options 需要拒绝使用rsync命令的选项列表

dont compress 指定不能压缩的通配符文件名,压缩很吃cpu

实例1:单模块设置

uid = rsync
gid = rsync
use chroot = no
max connections = 20
timeout = 300
syslog facility = local5
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[test]
path = /data/rsync
ignore errors
read only = false
list = false
hosts allow=192.168.146.132
auth user = rsync_backup
secrets file = /etc/rsync.password
comment = whole ftp area (approx 6.1 GB)

实例2:单模块设置2

uid = rsync
gid = rsync
use chroot = no
max connections = 20
timeout = 300
syslog facility = local5
#设置了log file 此值不生效
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[test]
comment = this is a rsync's test
charset = us                #字符集不可以设置zh_CN.UTF-8
read only = false
write only = true  #为真不可以从服务器下载文件到客户端
path = /data/rsync
ignore errors
list = false
hosts allow=192.168.146.132
auth user = yang
secrets file = /etc/yang.password

注:

1.charset = us   #字符集不可以设置zh_CN.UTF-8,亲测会报错

[root@www1 ~]# rsync -avz yang.txt [email protected]::test/
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]

2.write only = true  #为真不可以从服务器下载文件到客户端

[root@www1 ~]# rsync -avz [email protected]::test/yang.txt /tmp
receiving incremental file list
ERROR: module is write only
rsync error: syntax or usage error (code 1) at main.c(692) [sender=3.0.6]
rsync: connection unexpectedly closed (5 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [receiver=3.0.6]

3.read only = false为假可以将文件上传至服务器

[root@www1 ~]# rsync -avz yang.txt [email protected]::test/
sending incremental file list
yang.txt
sent 65 bytes  received 27 bytes  184.00 bytes/sec
total size is 0  speedup is 0.00

实例3.多模块设置

syslog facility = local5
#设置了log file 此值不生效
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[test]
uid = rsync
gid = rsync
use chroot = no
max connections = 20
timeout = 300
comment = this is a rsync's test
charset = us
read only = false
write only = false
path = /data/rsync
ignore errors
list = false
hosts allow=192.168.146.132
auth user = yang
secrets file = /etc/yang.password
[mysql]
uid = mysql
gid = mysql
use chroot = no
max connections = 2
timeout = 600
path = /data/mysql
comment = mysql rsync
charset = us
read only = false
write only =true
ignore errors
list = fales
hosts allow = 192.168.146.133
auth user = wan
secrets file = /etc/mysql.password

test

[root@web1 ~]# rsync -avz /root/test_bg.sh [email protected]::mysql/
sending incremental file list
test_bg.sh
sent 97 bytes  received 27 bytes  248.00 bytes/sec
total size is 26  speedup is 0.21
[root@web1 ~]# ifconfig eth0|awk -F '[ :]+' 'NR==2 {print $4}'
192.168.146.133
[root@web1 ~]#


猜你喜欢

转载自blog.51cto.com/12107790/2310552