linux下的数据备份-rsync

来源:互联网 发布:java手机版 编辑:程序博客网 时间:2024/05/01 21:45

数据备份rsync

rsync不仅可以远程同步数据,还可以本地同步数据,并且还能自己做出判断,同步新数据或已经更改的数据。

安装rsync:

[root@shuai-01 system]# yum install -y rsync

rsync拷贝的几种格式:

  • 同步到本地某个目录下:
    rsync [选项] SRC DEST
  • 同步到远端的数据备份:
    rsync [选项] SRC [user@]HOST:DEST
    user表示用户名 ,不加就表示和当前用户一样,host表示IP地址
  • 从远端同步到本地:
    rsync [选项] [user@]HOST:SRC DEST
  • 1
    rsync [选项] [user@]HOST::SRC DEST
  • 2
    rsync [选项] SRC [user@]HOST::DEST

常用选项:

  • -a 它是一个选项集包括 -rtplgoD
  • -r 同步目录时,做连级处理。类似于cp -r
  • -v 可视化处理
  • -l 保留软连接
  • -L同步软连接时会把软连接的源文件也同步
  • -p 保持文件的权限属性
  • -o 保持文件的属主
  • -g 保持文件的属组
  • -D 保持设备的文件信息( /dev/sdb1 这样的设备文件有它的特殊性,如果不加-D 可能拷贝过去就是一个非常普通的文件,不能当设备来用吧。)
  • -t 保持文件的时间属性
  • –delete 删除DEST中SRC没有的文件
  • –exclude 过滤指定文件。如–exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步。
  • P(大P)显示同步信息,比-v更详细
  • u 加上该选项后 DEST中文件比SRC新,就不同步
  • z 传输时压缩

平时基本上用-av选项就行了。

rsync -av /root/shuai/ /tmp/shuai_dest/

把软连接文件所指向的源文件拷贝-L

rsync -avL /root/shuai/ /tmp/shuai_dest/[root@shuai-01 ~]# ls -l /root/shuai/总用量 0-rwxrwxrwx. 1 root root  0 10月 24 21:18 3.txtdrwxrwxrwx. 2 root root  6 10月 24 20:33 aolidrwxrwxrwx. 2 root root  6 10月 23 20:24 denglrwxrwxrwx. 1 root root 11 12月  5 07:52 shuai2 -> /tmp/shuai3-rw-r--r--. 1 root root  0 12月  5 07:46 shuai3[root@shuai-01 ~]# ls -l /tmp/shuai_dest/总用量 0-rwxrwxrwx. 1 root root 0 10月 24 21:18 3.txtdrwxrwxrwx. 2 root root 6 10月 24 20:33 aolidrwxrwxrwx. 2 root root 6 10月 23 20:24 deng-rw-r--r--. 1 root root 0 12月  5 07:51 shuai2-rw-r--r--. 1 root root 0 12月  5 07:46 shuai3

删除DEST中SRC没有的文件(一模一样)–delete

rsync -avL --delete /root/shuai/ /tmp/shuai_dest/

排除不需要传输的文件–exclude

rsync -avL --delete /root/shuai/ /tmp/shuai_dest/[root@shuai-01 ~]# ls -l /root/shuai/总用量 0-rwxrwxrwx. 1 root root  0 10月 24 21:18 3.txtdrwxrwxrwx. 2 root root  6 10月 24 20:33 aolidrwxrwxrwx. 2 root root  6 10月 23 20:24 denglrwxrwxrwx. 1 root root 11 12月  5 07:52 shuai2 -> /tmp/shuai3-rw-r--r--. 1 root root  0 12月  5 07:46 shuai3[root@shuai-01 ~]# ls -l /tmp/shuai_dest/总用量 0drwxrwxrwx. 2 root root 6 10月 24 20:33 aolidrwxrwxrwx. 2 root root 6 10月 23 20:24 deng-rw-r--r--. 1 root root 0 12月  5 07:51 shuai2-rw-r--r--. 1 root root 0 12月  5 07:46 shuai3

rsync通过ssh方式备份

两台机器上都要有rsync

本机同步到对方(推文件):

[root@shuai-01 ~]# rsync -av /etc/passwd 192.168.176.134:/tmp/123root@192.168.176.134's password: sending incremental file listsent 31 bytes  received 12 bytes  9.56 bytes/sectotal size is 1164  speedup is 27.07

对方同步到本机(拉文件):

[root@shuai-01 ~]# rsync -avP 192.168.176.134:/tmp/123 /tmp/123.txtroot@192.168.176.134's password: receiving incremental file listsent 11 bytes  received 33 bytes  9.78 bytes/sectotal size is 1164  speedup is 26.45

指定端口同步:

[root@shuai-01 ~]# rsync -avP -e "ssh -p 22" /etc/passwd 192.168.176.134:/tmp/123root@192.168.176.134's password: sending incremental file listsent 31 bytes  received 12 bytes  9.56 bytes/sectotal size is 1164  speedup is 27.07

rsync通过后台服务同步

在远程机器上建立一个rsync服务器。在服务器上配置好rsync的配置文件,然后将本机作为rsync的客户机远程连接rsync服务器。

配置rsync配置文件/etc/rsyncd.conf

port=873log file=/var/log/rsync.logpid file=/var/run/rsyncd.pidaddress=192.168.176.135[test]path=/tmp/rsyncuse chroot=truemax connections=4read only=nolist=trueuid=rootgid=rootauth users=testsecrets file=/etc/rsyncd.passwdhosts allow=192.168.176.134

建立目录:

mkdir /tmp/rsync

更改目录权限:

chmod 777 /tmp/rsync

将 密码 用户注释掉
auth users=test
secrets file=/etc/rsyncd.passwd
注释掉

启动rsync服务:

rsync --daemon

另一台机器上同步文件:

[root@shuai-02 ~]# rsync -avP /tmp/123 192.168.176.135::test/shuai_02.txtsending incremental file listsent 24 bytes  received 8 bytes  64.00 bytes/sectotal size is 1164  speedup is 36.38

拉文件:

[root@shuai-02 ~]# rsync -avP 192.168.176.135::test/shuai_02.txt /tmp/123.txt

几个问题:

  1. 自己定义配置文件(/etc/rsync)则在开启rsyncd服务时要加上
    rsync –deamon –confg=/etc/rsync
  2. 同步的时候

    [root@shuai-02 ~]# rsync -avP /tmp/123 192.168.176.135::test/shuai_02.txt
    rsync: failed to connect to 192.168.176.135 (192.168.176.135): No route to host (113)
    rsync error: error in socket IO (code 10) at clientserver.c(122) [sender=3.0.9]
    遇见这问题,先看能否ping通,在检测端口是否通,网络通端口不通可能是iptables问题

    [root@shuai-02 ~]# ping 192.168.176.135
    PING 192.168.176.135 (192.168.176.135) 56(84) bytes of data.
    64 bytes from 192.168.176.135: icmp_seq=1 ttl=64 time=0.684 ms
    64 bytes from 192.168.176.135: icmp_seq=2 ttl=64 time=0.856 ms
    网络通
    [root@shuai-02 ~]# telnet 192.168.176.135 873
    Trying 192.168.176.135…
    telnet: connect to address 192.168.176.135: No route to host
    端口不通

关闭两端的iptables

[root@shuai-02 ~]# systemctl stop firewalld

这时就通了。

[root@shuai-02 ~]# telnet 192.168.176.135 873Trying 192.168.176.135...Connected to 192.168.176.135.Escape character is '^]'.

rsyncd.conf配置文件详解
port:指定在哪个端口启动rsyncd服务,默认是873端口。
log file:指定日志文件。
pid file:指定pid文件,这个文件的作用涉及服务的启动、停止等进程管理操作。
address:指定启动rsyncd服务的IP。假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动。
[]:指定模块名,里面内容自定义。(模块名就是路径)
path:指定数据存放的路径。
use chroot true|false:表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true,如果你的数据当中有软连接文件,建议你设置成false。
max connections:指定最大的连接数,默认是0,即没有限制。
read only ture|false:如果为true,则不能上传到该模块指定的路径下。
list:表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏。(安全选项)
uid/gid:指定传输文件时以哪个用户/组的身份传输。
auth users:指定传输时要使用的用户名。
secrets file:指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。注意该密码文件的权限一定要是600。格式:用户名:密码
hosts allow:表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开。(安全选项)

当设置了auth users和secrets file后,客户端连服务端也需要用用户名密码了,若想在命令行中带上密码,可以设定一个密码文件
rsync -avL test@192.168.133.130::test/test1/ /tmp/test8/ –password-file=/etc/pass
其中/etc/pass内容就是一个密码,权限要改为600

几个端口的选项:

ssh同步

rsync -avPL -e "ssh -p 22"  

后台同步

rsync -avPL --port 8730 
原创粉丝点击