rsync数据备份

10.28-10.33 rsync

虽然cp也有备份的功能,但是如果数据量大的时候,cp就会浪费太多的时间。这时可以借助rsync命令同步。rsync只同步发生了变化的文件,这样可以大大节省备份的时间和磁盘空间。

yum install -y rsync //安装rsync

[root@cent01 ~]# rsync -av /etc/passwd /tmp/1.txt  //本机同步
sending incremental file list
passwd

sent 2401 bytes  received 31 bytes  4864.00 bytes/sec
total size is 2327  speedup is 0.96



[root@cent01 ~]# rsync -av /etc/passwd 192.168.27.129:/tmp/1.txt  //跨机器远程同步
sending incremental file list
passwd

sent 2401 bytes  received 31 bytes  4864.00 bytes/sec
total size is 2327  speedup is 0.96

rsync命令格式

rsync [option] ... src dest
rsync [option] ... src [usr@]host:dest
rsync [option] ... [usr@]host:src dest
rsync [option] ... [usr@]host::src dest
rsync [option] ... src [usr@]host::dest

当不加[usr@]时表示默认root用户。

常用选项:
-a 归档模式,等同于-rlptgoD
-r 以递归模式处理
-v 打印一些信息,如文件列表,文件数量等
-l 保留软链接
-L 像处理普通文件一样处理软链接,当src文件中有软链接时,会把软链接指向的目标文件一起复制到dest
-p 保持文件权限
-o 保持文件属主信息
-g 保持文件属组信息
-D 保持设备文件信息
-t 保持文件时间信息
--delete 删除dest中src中没有的文件
--exclude=PATTERN 表示排除src中不需要传输的文件,可以用通配符如*.txt
--progress 在同步的过程中可以看到同步的过程状态
-u 把dest中比src还新的文件排除掉,不会覆盖
-z 在传输过程中压缩

但是常用的选项是-a,-v,-z,--delete和--exclude。

使用-a选项,全部复制,包括软链接

[root@cent01 rsync]# rsync -a test1/ test2/ 
[root@cent01 rsync]# ll test2
总用量 0
-rw-r--r-- 1 root root  0 11月 10 12:06 1
lrwxrwxrwx 1 root root 13 11月 10 12:07 123.txt -> /root/123.txt
-rw-r--r-- 1 root root  0 11月 10 12:06 2
-rw-r--r-- 1 root root  0 11月 10 12:06 3


[root@cent01 rsync]# rsync -a --no-l test1/ test2/  //表示排除软链接

使用-L选项,把软链接的源文件直接复制过来

[root@cent01 rsync]# rsync -avL test1/ test2/
[root@cent01 rsync]# ll test2
总用量 0
-rw-r--r-- 1 root root 0 11月 10 12:06 1
-rw-r--r-- 1 root root 0 11月 10 12:06 123.txt
-rw-r--r-- 1 root root 0 11月 10 12:06 2
-rw-r--r-- 1 root root 0 11月 10 12:06 3

-u如果dest中的文件比src更新,那么同步时忽略这些文件

[root@cent01 rsync]# rsync -avu test1/ test2/

[root@cent01 rsync]# ll test1/1 test2/1
-rw-r--r-- 1 root root 0 11月 10 12:06 test1/1
-rw-r--r-- 1 root root 3 11月 10 12:32 test2/1

–delete会删除dest中src没有的文件

[root@cent01 rsync]# ll test1 test2
test1:
总用量 0
-rw-r--r-- 1 root root 0 11月 10 12:06 1

test2:
总用量 0
-rw-r--r-- 1 root root  0 11月 10 12:06 1
lrwxrwxrwx 1 root root 13 11月 10 12:07 123.txt -> /root/123.txt

[root@cent01 rsync]# rsync -av --delete test1/ test2/

[root@cent01 rsync]# ll test1 test2
test1:
总用量 0
-rw-r--r-- 1 root root 0 11月 10 12:06 1

test2:
总用量 0
-rw-r--r-- 1 root root 0 11月 10 12:06 1

–exclude可以排除同步src中指定的文件或者某类文件

[root@cent01 rsync]# rsync -a --progress --exclude="*.txt" test1/ test2/

[root@cent01 rsync]# ll test1 test2
test1:
总用量 0
-rw-r--r-- 1 root root 0 11月 10 12:06 1
-rw-r--r-- 1 root root 0 11月 10 12:44 1.txt
-rw-r--r-- 1 root root 0 11月 10 12:06 2
-rw-r--r-- 1 root root 0 11月 10 12:44 2.txt

test2:
总用量 0
-rw-r--r-- 1 root root 0 11月 10 12:06 1
-rw-r--r-- 1 root root 0 11月 10 12:06 2

ssh同步到另外一台主机

rsync -av /etc/passwd 192.168.27.129:/tmp/1.txt

rsync -av 192.168.27.129:/etc/passwd /tmp/1.txt //从另外一台主机拉取到本地

通过后台服务的方式同步

通过ssh登录虽然简单,但是需要告诉客户机服务端的系统用户名密码,这样会导致安全问题。而rsyncd方式可以为rsyncd服务单独设置与系统账号无关的帐号密码,这样能保证服务端的安全。

rsync daemon是通过c/s的方式同步数据。这里我们有两台主机,192.168.27.128,和192.168.27.129。

在128主机上建立并配置rsync的配置文件/etc/rsyncd.conf,内容如下:

port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.27.128

[test]
path=/root/rsync
use chroot=false
max connections=4
read only=no
list=true
uid=root
gid=root
auth users=test
secrets file=/etc/rsyncd.passwd

上面的 port,log file,pid file,address都属于全局配置。[]内的test是模块名,以下属于模块配置。一个配置文件可以有多个不同模块。各个参数含义如下:

  • port 指定哪个端口启动rsync服务,默认为873
  • log file指定日志文件
  • pid file指定pid文件,这个服务的作用涉及服务的启动停止等进程操作
  • address指定启动rsync服务的IP,假如你的主机有多个ip,但是不指定- - ip的话就默认在全部ip上启动
  • [ ]里面是模块名
  • path 指定数据存放的路径
  • use chroot true|false 默认值是true,但是如果你的文件中有软链接,建设设置为false。设置为true的优点是提高了安全性,但是缺点是需要root权限,并且不能备份指向外部的软链接
  • max connection 指定最大的连接数,默认是0即无限制。这里的连接数指的是允许多少台主机同时连接他
  • read only 如果为true则不能上传的该模块指定的路径下
  • list 表示当用户查询该服务器上的可用模块时,模块是否被列出。设定为true会有一定的危险性,所以设为false更安全
  • uid gid 指定在传输时以哪个用户和用户组的身份传输
  • auth users 指定传输时需要使用的用户名
  • secrets file 指定密码文件,该参数连同上面的auth users如果不指定,则不使用密码验证。该文件的权限一定要是600
  • host allow 表示被允许连接该模块的主机,可以是ip或者网段,如果是多个中间用空格隔开
[root@cent01 test]# cat /etc/rsyncd.passwd  //在128上把账号密码设置为test:test123
test:test123

[root@cent01 test]# chmod 600 /etc/rsyncd.passwd  //更改文件权限为600,如果权限不对,则不能完成同步

[root@cent01 test]# rsync --daemon --config=/etc/rsyncd.conf  //启动rsync服务

[root@cent01 test]# cat /var/log/rsync.log  //查看日志,看端口是否启动
2017/11/11 14:37:28 [15497] rsyncd version 3.0.9 starting, listening on port 873

netstat -lnp |grep rsync  //查看端口监控
tcp        0      0 192.168.27.128:873      0.0.0.0:*               LISTEN      16504/rsync       

在同步之前,先关闭两台机器的firewalld。用stop和disable命令。

下面在客户机129上操作:

先在src内创建一个软链接1.txt,此时chroot为true

[root@cent02 tmp]# rsync -avL test@192.168.27.128::test/test1/ /tmp/test5/  //注意src和dest的末尾要加"/",否则路径会出问题。输入密码test123
Password: 
receiving incremental file list
symlink has no referent: "/test1/test.txt" (in test)
created directory /tmp/test5
./
1
1.txt
2
2.txt
3
4

sent 171 bytes  received 450 bytes  177.43 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1518) [generator=3.0.9]  //末尾提示,软链接的复制遇到了问题。


现在我们把chroot=false,结果提示不再有问题。
[root@cent02 tmp]# rsync -avL test@192.168.27.128::test/test1/ /tmp/test7/
Password: 
receiving incremental file list
created directory /tmp/test7
./
1
1.txt
2
2.txt
3
4
test.txt

sent 190 bytes  received 2778 bytes  659.56 bytes/sec
total size is 2327  speedup is 0.78

在129上编辑一个密码文件,vim /etc/pass,在文件内输入test123,并修改文件权限为600。

[root@cent02 tmp]# rsync -avL test@192.168.27.128::test/test1 /tmp/test8/ --password-file=/etc/pass  //在命令中指定密码文件的路径,这样就不用再单独输入密码
receiving incremental file list
created directory /tmp/test8
test1/
test1/1
test1/1.txt
test1/2
test1/2.txt
test1/3
test1/4
test1/test.txt

sent 191 bytes  received 2798 bytes  5978.00 bytes/sec
total size is 2327  speedup is 0.78

在rsync服务端不指定用户
删掉auth users和secrets file这两项。这样就不用密码也能登录。

[root@cent02 tmp]# rsync -avL test@192.168.27.128::test/test1/ /tmp/test9/  //成功
receiving incremental file list
created directory /tmp/test9
./
1
1.txt
2
2.txt
3
4
test.txt

sent 162 bytes  received 2737 bytes  5798.00 bytes/sec
total size is 2327  speedup is 0.80

[root@cent02 tmp]# rsync -avL 192.168.27.128::test/test1/ /tmp/test10/  //不加test用户也会成功,这样会默认为root登录
receiving incremental file list
created directory /tmp/test10
./
1
1.txt
2
2.txt
3
4
test.txt

sent 162 bytes  received 2737 bytes  5798.00 bytes/sec
total size is 2327  speedup is 0.80
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值