利用rsync备份生产应用

Posted 我的征途是星辰大海

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用rsync备份生产应用相关的知识,希望对你有一定的参考价值。

概述 

  上节主要是针对rsync服务端进行的配置和详解,本节主要针对在客户端上的rsync配置进行讲解。

rsync用法

本地到本地

语法:rsync [OPTION...] SRC... [DEST]

用法示例一:将/home/kl/ss目录里的内容同步到/home/kl/ss_new目录中

[[email protected] ~]$ ls -l
总用量 16
drwxr-xr-x. 4 root root 4096 7月  19 12:23 newss
drwxr-xr-x. 4 kl   kl   4096 7月  19 12:23 ss
drwxrwxr-x. 2 kl   kl   4096 8月   2 10:37 ss_new
drwxrwxr-t. 2 kl   kl   4096 7月  21 13:18 test
[[email protected]-datanode1 ~]$ rsync -azvP ss/ ss_new/
sending incremental file list
./
auditd
        3580 100%    0.00kB/s    0:00:00 (xfer#1, to-check=68/70)
blk-availability
        1343 100%    1.28MB/s    0:00:00 (xfer#2, to-check=67/70)
crond
        2826 100%    2.70MB/s    0:00:00 (xfer#3, to-check=66/70)
functions
       25419 100%    4.04MB/s    0:00:00 (xfer#4, to-check=65/70)
halt
        5985 100%  730.59kB/s    0:00:00 (xfer#5, to-check=64/70)
............
............
ss/udev-post
        2294 100%   31.11kB/s    0:00:00 (xfer#66, to-check=1/70)
ss/newss/

sent 119700 bytes  received 1281 bytes  241962.00 bytes/sec
total size is 332288  speedup is 2.75
[[email protected]-datanode1 ~]$ ls -l ss_new/
总用量 236
-rwxr-xr-x. 1 kl kl  3580 7月  19 12:03 auditd
-rwxr-xr-x. 1 kl kl  1343 7月  19 12:03 blk-availability
-rwxr-xr-x. 1 kl kl  2826 7月  19 12:03 crond
-rwxr-xr-x. 1 kl kl 25419 7月  19 12:03 functions
-rwxr-xr-x. 1 kl kl  5985 7月  19 12:03 halt
................

用法示例二:只接源目录表示列出源目录里面的文件内容,这个时候DEST参数是省略不使用的

[[email protected] ~]$ rsync ss/
drwxr-xr-x        4096 2017/07/19 12:23:20 .
-rwxr-xr-x        3580 2017/07/19 12:03:52 auditd
-rwxr-xr-x        1343 2017/07/19 12:03:52 blk-availability
-rwxr-xr-x        2826 2017/07/19 12:03:52 crond
-rwxr-xr-x       25419 2017/07/19 12:03:52 functions
-rwxr-xr-x        5985 2017/07/19 12:03:52 halt
-rwxr-xr-x       11169 2017/07/19 12:03:52 ip6tables
-rwxr-xr-x       11048 2017/07/19 12:03:52 iptables
-rwxr-xr-x        9980 2017/07/19 12:03:52 jexec
-rwxr-xr-x       20634 2017/07/19 12:03:52 kdump
-rwxr-xr-x         652 2017/07/19 12:03:52 killall
-rwxr-xr-x        2137 2017/07/19 12:03:52 lvm2-lvmetad
-rwxr-xr-x        3045 2017/07/19 12:03:52 lvm2-monitor
-rwxr-xr-x        2571 2017/07/19 12:03:52 mdmonitor
-rwxr-xr-x        2200 2017/07/19 12:03:52 messagebus
-rwxr-xr-x        2989 2017/07/19 12:03:52 netconsole
-rwxr-xr-x        5309 2017/07/19 12:03:52 netfs
-rwxr-xr-x        6406 2017/07/19 12:03:52 network
-rwxr-xr-x        3570 2017/07/19 12:03:52 nfs-rdma
-rwxr-xr-x        1923 2017/07/19 12:03:52 ntpd
...................

 

服务端同步到本地(Pull)

这种方式也分为SSH和RSYNC两种同步方式

通过SSH方式进行同步

语法:rsync [OPTION...] [[email protected]]HOST:SRC... [DEST]

实际案例:

我有两台主机cdh-datanode1和cdh-datanode2,现在我想同步cdh-datanode2上/root目录下所有的内容到cdh-datanode1的/home/kl/test目录下

在这里SRC就表示cdh-datanode2 上的/root,DEST就表示cdh-datanode1上的/home/kl/test.所以记住一点,你要进行同步的目录文件所在的设备是作为SRC的

[[email protected] test]$ rsync -azvP [email protected]:/root/ /home/kl/test/
The authenticity of host cdh-datanode2 (192.168.100.102) cant be established.
RSA key fingerprint is SHA256:BOi8rknsSr1IzI7fqGyWUGbAdp5hz/rmf0nnQ/DYKO4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added cdh-datanode2,192.168.100.102 (RSA) to the list of known hosts.
[email protected]-datanode2s password: 
bash: rsync: command not found
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: remote command not found (code 127) at io.c(600) [receiver=3.0.6]
[[email protected]-datanode1 test]$ rs
rsvg          rsvg-convert  rsvg-view     rsync         rsyslogd      
[[email protected]-datanode1 test]$ rsync -azvP [email protected]:/root/ /home/kl/test/
[email protected]-datanode2s password: 
receiving incremental file list
./
.Xauthority
          59 100%   57.62kB/s    0:00:00 (xfer#1, to-check=41/43)
.bash_history
        1095 100%    1.04MB/s    0:00:00 (xfer#2, to-check=40/43)
.bash_logout
          18 100%   17.58kB/s    0:00:00 (xfer#3, to-check=39/43)
.bash_profile
         176 100%   85.94kB/s    0:00:00 (xfer#4, to-check=38/43)
.bashrc
         176 100%   85.94kB/s    0:00:00 (xfer#5, to-check=37/43)
.cshrc
         100 100%   24.41kB/s    0:00:00 (xfer#6, to-check=36/43)
.tcshrc
         129 100%   31.49kB/s    0:00:00 (xfer#7, to-check=35/43)
.viminfo
         670 100%  130.86kB/s    0:00:00 (xfer#8, to-check=34/43)
anaconda-ks.cfg
        1096 100%  178.39kB/s    0:00:00 (xfer#9, to-check=33/43)
id_rsa.pub
         399 100%   55.66kB/s    0:00:00 (xfer#10, to-check=32/43)
install.log
       14625 100%    1.07MB/s    0:00:00 (xfer#11, to-check=31/43)
install.log.syslog
        3482 100%  242.89kB/s    0:00:00 (xfer#12, to-check=30/43)
jdk-7u80-linux-x64.rpm
   138090286 100%    5.83MB/s    0:00:22 (xfer#13, to-check=29/43)
salt-rhel6_v201603.zip
    12734065 100%    5.56MB/s    0:00:02 (xfer#14, to-check=28/43)
.ssh/
.ssh/authorized_keys
         399 100%    2.06kB/s    0:00:00 (xfer#15, to-check=25/43)
.ssh/known_hosts
         397 100%    2.04kB/s    0:00:00 (xfer#16, to-check=24/43)
salt-rhel6_v201603/
salt-rhel6_v201603/PyYAML-3.11-1.el6.x86_64.rpm
      155352 100%  692.74kB/s    0:00:00 (xfer#17, to-check=23/43)
salt-rhel6_v201603/install_saltstack.sh
        2634 100%   11.64kB/s    0:00:00 (xfer#18, to-check=22/43)
salt-rhel6_v201603/libyaml-0.1.3-4.el6.x86_64.rpm
       52624 100%  223.44kB/s    0:00:00 (xfer#19, to-check=21/43)
salt-rhel6_v201603/openpgm-5.2.122-2.el6.x86_64.rpm
      169716 100%  611.58kB/s    0:00:00 (xfer#20, to-check=20/43)
salt-rhel6_v201603/python-babel-0.9.4-5.1.el6.noarch.rpm
     1493972 100%    2.38MB/s    0:00:00 (xfer#21, to-check=19/43)
salt-rhel6_v201603/python-backports-1.0-5.el6.x86_64.rpm
        5024 100%    8.14kB/s    0:00:00 (xfer#22, to-check=18/43)
salt-rhel6_v201603/python-backports-ssl_match_hostname-3.4.0.2-2.el6.noarch.rpm
       11416 100%   18.40kB/s    0:00:00 (xfer#23, to-check=17/43)
salt-rhel6_v201603/python-chardet-2.2.1-1.el6.noarch.rpm
      234768 100%  349.49kB/s    0:00:00 (xfer#24, to-check=16/43)
salt-rhel6_v201603/python-crypto-2.6.1-2.el6.x86_64.rpm
      512828 100%  633.93kB/s    0:00:00 (xfer#25, to-check=15/43)
salt-rhel6_v201603/python-futures-3.0.3-1.el6.noarch.rpm
       27028 100%   33.28kB/s    0:00:00 (xfer#26, to-check=14/43)
salt-rhel6_v201603/python-jinja2-2.7.3-1.el6.noarch.rpm
      531148 100%  608.09kB/s    0:00:00 (xfer#27, to-check=13/43)
salt-rhel6_v201603/python-markupsafe-0.11-10.el6.x86_64.rpm
       24296 100%   27.69kB/s    0:00:00 (xfer#28, to-check=12/43)
salt-rhel6_v201603/python-msgpack-0.4.6-1.el6.x86_64.rpm
       69704 100%   78.42kB/s    0:00:00 (xfer#29, to-check=11/43)
salt-rhel6_v201603/python-ordereddict-1.1-2.el6.noarch.rpm
        7244 100%    8.09kB/s    0:00:00 (xfer#30, to-check=10/43)
salt-rhel6_v201603/python-requests-2.6.0-3.el6.noarch.rpm
       96960 100%  106.15kB/s    0:00:00 (xfer#31, to-check=9/43)
salt-rhel6_v201603/python-six-1.9.0-2.el6.noarch.rpm
       28500 100%   30.96kB/s    0:00:00 (xfer#32, to-check=8/43)
salt-rhel6_v201603/python-tornado-4.2.1-1.el6.x86_64.rpm
      676864 100%  640.50kB/s    0:00:01 (xfer#33, to-check=7/43)
salt-rhel6_v201603/python-urllib3-1.10.2-1.el6.noarch.rpm
      102716 100%    1.81MB/s    0:00:00 (xfer#34, to-check=6/43)
salt-rhel6_v201603/python-zmq-14.5.0-2.el6.x86_64.rpm
      490712 100%    2.98MB/s    0:00:00 (xfer#35, to-check=5/43)
salt-rhel6_v201603/salt-2016.3.0-1.el6.noarch.rpm
     6543724 100%    3.99MB/s    0:00:01 (xfer#36, to-check=4/43)
salt-rhel6_v201603/salt-master-2016.3.0-1.el6.noarch.rpm
     1440128 100%    1.57MB/s    0:00:00 (xfer#37, to-check=3/43)
salt-rhel6_v201603/salt-minion-2016.3.0-1.el6.noarch.rpm
       31984 100%   35.53kB/s    0:00:00 (xfer#38, to-check=2/43)
salt-rhel6_v201603/yum-utils-1.1.30-30.el6.noarch.rpm
      112332 100%  122.57kB/s    0:00:00 (xfer#39, to-check=1/43)
salt-rhel6_v201603/zeromq-4.0.5-4.el6.x86_64.rpm
      581800 100%  551.62kB/s    0:00:01 (xfer#40, to-check=0/43)

sent 782 bytes  received 162931774 bytes  4722682.78 bytes/sec
total size is 164250646  speedup is 1.01

上面第一次进行rsync同步SRC到DEST的时候出错了,经过排错发现是因为SRC设备上未安装rsync可执行程序的,在SRC设备上安装好后再次执行就好了。

通过RSYNC DEAMON进行同步

语法:

rsync [OPTION...] [[email protected]]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[[email protected]]HOST[:PORT]/SRC... [DEST]

案例见”利用rsync备份生产应用(一)” 小节”从备份服务器下载文件(pull)”,这里需要注意的是SRC里面指代的路径开头是在rsync服务器配置文件中的模块的名字,有不清楚的详看”利用rsync备份生产应用(一)”

 

本地同步到服务端(Push)

这种方式也分为SSH和RSYNC两种同步方式

通过SSH方式进行同步

语法:rsync [OPTION...] SRC... [[email protected]]HOST:DEST

实际案例:

  我有两台主机cdh-datanode1和cdh-datanode2,现在我想同步cdh-datanode1上/home/kl/test目录下所有的内容到cdh-datanode2的/root目录下

[[email protected] test]$ rsync -azvP /home/kl/test/ [email protected]:/root/
[email protected]-datanode2s password: 
sending incremental file list
./
.viminfo
         670 100%    0.00kB/s    0:00:00 (xfer#1, to-check=34/43)
.ssh/
salt-rhel6_v201603/

sent 2064 bytes  received 171 bytes  406.36 bytes/sec
total size is 164250646  speedup is 73490.22
[[email protected]-datanode1 test]$

通过RSYNC DEAMON进行同步

语法:

rsync [OPTION...] SRC... [[email protected]]HOST::DEST
rsync [OPTION...] SRC... rsync://[[email protected]]HOST[:PORT]/DEST

案例见”利用rsync备份生产应用(一)” 小节”备份文件到备份服务器(push)”,这里需要注意的是DEST里面指代的路径开头是在rsync服务器配置文件中的模块的名字,有不清楚的详看”利用rsync备份生产应用(一)”

以上是关于利用rsync备份生产应用的主要内容,如果未能解决你的问题,请参考以下文章

目录同步实战案例:用rsync+crontab实现/www目录定时同步

实战案例:用rsync+inotify+shell脚本实现/www目录实时同步

分享一个生产环境中利用SVN上线代码的脚本

Linux-Rsync项目实战(详细) 备份全网服务器数据生产架构方案

RSync实现文件备份同步详解

Rsync+Inotify 实时备份