ARM40-A5应用——使用rsync与Linux服务器的远程数据同步

ARM40-A5应用——使用rsync与Linux服务器的远程数据同步

2018.7.5

版权声明:本文为博主原创文章,允许转载。

  ARM40部署提要:
  (1)根据3.1节、3.2节建立各个文件;
  (2)按4.1节、4.2节测试。

  本文介绍ARM40-A5与阿里云Linux服务器的远程数据同步方法。 在有线网络,4G网络,GPRS(ppp)网络的情况下都可使用。

一、rsync 简介

  与复制命令 cp 一样,rsync 也把文件从源复制到目标。但是,与 cp 不同,rsync 操作的源和目标既可以是本地的,也可以是远程的。
  在网络计算机系统之间共享文件的工具中,最好的文件分布工具是 rsync。
  假设主机α上有文件A,主机β上有文件B,现在要让文件B和文件A保持同步。最简单的方法是将A文件直接拷贝到β主机上。但如果文件A很大,且B和A是相似的(两文件实际内容只有少部分不同),拷贝整个文件A可能会消耗不少时间。如果可以拷贝A和B不同的那一小部分,则传输过程会很快。rsync增量传输算法就充分利用了文件的相似性,解决了远程增量拷贝的问题。
  rsync 可以在中断之后恢复传输;它只传输源文件和目标文件之间不一致的部分;rsync 可以执行完整备份或增量备份。
  rsync 主要功能如下:
  (1)拷贝本地文件。
  如将/src/ 目录的内容拷贝到/dst 目录:
  rsync -a /src/ /dst
  (2)使用一个远程 shell 程序(如ssh)来实现将本地机器的内容拷贝到远程机器。
  如将本地 /etc/hosts 拷贝到远程机器 192.168.1.130 的 /home/nfs/rsync-test 目录:
  rsync -avz /etc/hosts [email protected]:/home/nfs/rsync-test
  (3)使用一个远程 shell 程序(如ssh)来实现将远程机器的内容拷贝到本地机器。
  如将远程机器 192.168.1.130 的 /home/nfs/rsync-test 目录拷贝到本地的当前目录:
  rsync -avz [email protected]:/home/nfs/rsync-test ./
  (4)从远程 rsync 服务器中拷贝文件到本地机器。
  例1:将远程机器 192.168.1.130 的ARM40_IDC模块目录拷贝到本地的当前目录:
  rsync -avz [email protected]::ARM40_IDC ./
  例2:将远程机器 192.168.1.130 的ARM40_IDC模块目录中的tst_180728 文件拷贝到本地的当前目录:
  rsync -avz --timeout=20 [email protected]::ARM40_IDC/tst_180728 ./ --password-file=/etc/rsync.passwd
  例3:Linux内核官网www.kernel.org提供rsync的下载方式,官方给出的地址是  rsync://rsync.kernel.org/pub,
  读取该服务器的linux/kernel/v3.x/linux-3.0.15.tar.sign文件到本地的 /tmp 目录:
  rsync -avzP rsync://rsync.kernel.org/pub/linux/kernel/v3.x/linux-3.0.15.tar.sign/tmp
  (5)从本地机器拷贝文件到远程 rsync 服务器中。
  例1:将本地当前目录拷贝到远程机器 192.168.1.130 的ARM40_IDC模块目录:
  rsync -avz ./ [email protected]::ARM40_IDC --password-file=/etc/rsync.passwd
  例2:将本地当前目录中的 tst_180728 文件拷贝到远程机器 47.98.140.167 的ARM40_IDC模块目录中的 tst_pol 目录中,如果该目录不存在,则新建该目录。
  rsync -avz ./tst_180728 [email protected]::ARM40_IDC/tst_pol/ --password-file=/etc/rsync.passwd
  (6)列出远程机器的文件列表。
  例1:列出远程机器 192.168.1.130 的/home/nfs/rsync-test 目录的文件列表:
  rsync -avz [email protected]:/home/nfs/rsync-test
  例2:列出远程机器 47.98.140.167 的ARM40_IDC模块目录的文件列表:
  rsync -avz [email protected]::ARM40_IDC #需要输入密码
  rsync -avz [email protected]::ARM40_IDC --password-file=/etc/rsync.passwd #自动获取密码
  例3:读取Linux内核官网服务器的部分内容:
  rsync --list-only rsync://rsync.kernel.org/pub/

  rsync 的功能较多,网上资料也很丰富,本文仅介绍个别功能。更多内容请参阅文末的“参考文章”或网上资料。

二、Linux服务器上的配置

2.1、打开873端口

  本文使用的Linux服务器为阿里云ECS上安装的ubuntu服务器。
  由于rsync默认使用的端口为873,所以需要阿里云ECS 的访问规则,打开873端口。详细情况可参阅“参考文章”中的《阿里云ecs禁止ping,禁止telnet》。
  下图是本文给出示例:
这里写图片描述
    图一 在阿里云ECS 的访问规则中打开873端口
  其中:
  22/22 端口为 ssh;
  23/23 端口为 telnet;
  ICMP 的-1/-1为 ping;
  3389/3389为远程桌面;
  873/873为 rsync。
注:
  为了验证打开873端口的过程是否正确,笔者用23端口做了一下验证。打开了23端口,开启telnet服务,然后在ARM40-A5上 telnet 47.98.140.167,输入用户名和密码后,可以登陆。开启 telnet 服务的方法可以在网上搜索资料。
  除了打开873端口,还需要打开 rsync 服务。

2.2、启动 rsync 服务

  ubuntu默认已安装rsync,rsync服务默认是没有启动的,可以用 ps -ef | grep rsync 观察是否已启动 rsync 服务。
  启动 rsync 服务步骤如下:
   ① 修改 vim /etc/default/rsync 文件中的 RSYNC_ENABLE=false 为:
  #RSYNC_ENABLE=false
  RSYNC_ENABLE=true

  ② 配置文件 /etc/rsyncd.conf
  cp /usr/share/doc/rsync/examples/rsyncd.conf /etc
  修改 /etc/rsyncd.conf 中的如下几行:

#[ftp]
[ARM40_IDC]
        path = /opt/idc
#      read only = yes
        read only = no
# uid = nobody
        uid = root
# gid = nogroup
        gid = root
#      auth users =
        auth users =ARM40-A5
        secrets file = /etc/rsyncd.secrets

  为方便理解,修改的尽量少,更多信息请参考网上资料。附(1)为 /etc/rsyncd.conf 文件的一个完整示例。

  配置文件 /etc/rsyncd.conf 里面提到的 /etc/rsyncd.secrets 和 /opt/idc 也都要配置好:
  echo "ARM40-A5:123456" > /etc/rsyncd.secrets #authusers=ARM40-A5,secrets:123456
  chmod 0600 /etc/rsyncd.secrets
  mkdir -p /opt/idc

  ③ 启动 rsync 服务
  /etc/init.d/rsync start
  如果启动成功,用 ps -ef | grep rsync | grep -v grep 可以观察到:
  root 25788 1 0 Jul04 ? 00:00:00 /usr/bin/rsync --daemon --no-detach
  在阿里云shell中,可以在命令行输入netstat -tlanp 或 netstat -tlunp,查看 873 端口是否已经处于监听状态。

三、ARM40-A5上的配置

3.1、rsync 命令和 libpopt.so.0 库文件

  拷贝 rsync 命令和 libpopt.so.0 库文件,保存在ARM40-A5如下的位置:
    /usr/bin/rsync
    /usr/lib/libpopt.so.0
  使用 rsync --version 可以查看 rsync 命令的版本信息。
  在ARM40-A5上建立密码文件,其内容为 rsync模块认证的密码:
    echo "123456" > /etc/rsync.passwd # secrets:123456
    chmod 0600 /etc/rsync.passwd

四、与Linux服务器的远程数据同步

4.1、从阿里云的服务器同步文件到ARM40-A5

  在阿里云服务器的 /opt/idc 目录下新建一个用于测试的文件a.tst,然后在ARM40-A5中输入如下命令:
  /* 将阿里云服务器 /opt/idc/ 目录下的文件同步到 ARM40-A5的 /var/www/ 目录,需要输入密码123456 */
  rsync -avz --timeout=20 [email protected]::ARM40_IDC /var/www/
  在ARM40-A5的 /var/www/ 目录可看到 a.tst 文件了。
  如果不想人工输入密码,命令如下:
  /* 服务器 —-> ARM40-A5 */
  rsync -avz --timeout=20 [email protected]::ARM40_IDC /var/www/ --password-file=/etc/rsync.passwd
  /* 将阿里云服务器 /opt/idc/ 目录下的tst_180728文件同步到 ARM40-A5的 /var/www/ 目录 */
  rsync -avz --timeout=20 [email protected]::ARM40_IDC/tst_180728 /var/www/ --password-file=/etc/rsync.passwd

4.2、将ARM40-A5上的文件同步到阿里云服务器

  在ARM40-A5的 /var/www/ 目录下新建一个用于测试的文件arm40_b.tst,然后在ARM40-A5中输入如下命令:
  /* 将ARM40-A5上/var/www/ 目录下的文件同步到阿里云服务器的 /opt/idc 目录 */
  /* ARM40-A5 —-> 服务器 */
  # rsync -avz /var/www/ [email protected]::ARM40_IDC --password-file=/etc/rsync.passwd
  在阿里云服务器的 /opt/idc/ 目录可看到 arm40_b.tst 文件了。
  /* 将ARM40-A5上/var/www/tst_180728 文件同步到阿里云服务器的 /opt/idc/tst_pol 目录 */
  rsync -avz /var/www/tst_180728 [email protected]::ARM40_IDC/tst_pol/ --password-file=/etc/rsync.passwd

五、常见问题

  1、uid、gid造成的报错(报错内容见附(2))
  修改服务器端 /etc/rsyncd.conf 文件中的 uid、gid 需改为 uid = root,gid = root .
  2、Connection refused 的报错,例如:
rsync: failed to connect to 47.98.140.167 (47.98.140.167): Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(128) [sender=3.1.1]
  该问题属于服务器端服务未启动所造成。使用 ps -ef | grep rsync | grep -v grep 检查rsync服务是否已经启动,应能看到rsync服务并没有启动。
  启动 rsync 服务:
    /etc/init.d/rsync start
  如果启动成功,用 ps -ef | grep rsync | grep -v grep 可以观察到。
  3、Permission denied 的报错,例如:
rsync: could not open password file /etc/rsync.passwd: Permission denied (13)
rsync error: syntax or usage error (code 1) at authenticate.c(187) [sender=3.1.1]
  该问题是因为对本地的文件没有权限,需要在 rsync 前加sudo,为 sudo rsync .
  4、password file must not be other-accessible 的报错,例如:
ERROR: password file must not be other-accessible
rsync error: syntax or usage error (code 1) at authenticate.c(196) [sender=3.1.0]
  该问题是因为 rsync.passwd 的权限不对,应该设置为600,chmod 600 rsync.passwd
  5、长时间无进展,也无法退出
  可能是网络异常导致。需要在rsync中增加超时,例如 --timeout=10,到10s后,rsync会自动退出。

参考文章:

  Unix/Linux 系统自动化管理——数据备份与同步篇
  https://www.ibm.com/developerworks/cn/aix/library/0908_unixsync_chengyan/index.html
  对话 UNIX——rsync 的高级应用
  https://www.ibm.com/developerworks/cn/aix/library/au-spunix_rsync/
  《鳥哥的 Linux 私房菜》11.6.2 以 rsync 進行同步鏡像備份
  http://linux.vbird.org/linux_server/0310telnetssh.php#rsync
  rsync
  https://wiki.archlinux.org/index.php/rsync
  Rsync official documentation
  https://rsync.samba.org/documentation.html
  阿里云ecs禁止ping,禁止telnet
  https://www.cnblogs.com/dadonggg/p/7885997.html
  rsync算法原理和工作流程分析
  https://www.cnblogs.com/f-ck-need-u/p/7226781.html?utm_source=itdadao&utm_medium=referral#auto_id_0
  Linux基本服务——第2章 rsync完全手册
  http://www.cnblogs.com/f-ck-need-u/p/7048359.html
  荟聚计划:共商 共建 共享 Grant

附:

 (1)服务器端 /etc/rsyncd.conf 文件示例

# sample rsyncd.conf configuration file

# GLOBAL OPTIONS

#motd file=/etc/motd
#log file=/var/log/rsyncd
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
# The init.d script does its own pid file handling,
# so omit the "pid file" line completely in that case.
# pid file=/var/run/rsyncd.pid
#syslog facility=daemon
#socket options=

# MODULE OPTIONS

#[ftp]
[ARM40_IDC]

        comment = public archive
        path = /opt/idc
        use chroot = yes
# max connections=10
        lock file = /var/lock/rsyncd
# the default for read only is yes...
# read only = yes
        read only = no
        list = yes
#        uid = nobody
#        gid = nogroup
        uid = root
        gid = root
# exclude = 
# exclude from = 
# include =
# include from =
# auth users =
        auth users =ARM40-A5
        secrets file = /etc/rsyncd.secrets
        strict modes = yes
# hosts allow =
# hosts deny =
        ignore errors = no
        ignore nonreadable = yes
        transfer logging = no
# log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
        timeout = 600
        refuse options = checksum dry-run
        dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz

 (2)uid、gid造成的报错

root@ARM40:/var/www# rsync -avz /var/www/ [email protected]::ARM40_IDC --password-file=/etc/rsync.passwd
This rsync lacks old-style --compress due to its external zlib. Try -zz.
Continuing without compression.

sending incremental file list
rsync: failed to set times on "/." (in ARM40_IDC): Operation not permitted (1)
./
arm40_b.tst
rsync: chgrp "/.arm40_b.tst.FNqusP" (in ARM40_IDC) failed: Operation not permitted (1)

sent 160 bytes received 214 bytes 748.00 bytes/sec
total size is 8 speedup is 0.02
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
root@ARM40:/var/www# 

猜你喜欢

转载自blog.csdn.net/vonchn/article/details/81904631