Build rsync services in Linux

Source: Internet
Author: User
Tags chmod inotify es echo ssh file permissions rsync

1.rsync Introduction

    rsync是类unix系统下的数据镜像备份工具,从软件的命名上就可以看出来了——remote sync。rsync是Linux系统下的文件同步和数据传输工具,它采用“rsync”算法,可以将一个客户机和远程文件服务器之间的文件同步,也可以在本地系统中将数据从一个分区备份到另一个分区上。如果rsync在备份过程中出现了数据传输中断,恢复后可以继续传输不一致的部分。rsync可以执行完整备份或增量备份。它的主要特点有:    1.可以镜像保存整个目录树和文件系统;    2.可以很容易做到保持原来文件的权限、时间、软硬链接;无须特殊权限即可安装;    3.可以增量同步数据,文件传输效率高,因而同步时间短;    4.可以使用rcp、ssh等方式来传输文件,当然也可以通过直接的socket连接;    5.支持匿名传输,以方便进行网站镜象等;    6.加密传输数据,保证了数据的安全性;

Advantages and disadvantages of 2.rsync

与传统的cp、tar备份方式对比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如,定期地备份文件服务器数据到远端服务器,对本地磁盘定期进行数据镜像等。但是随着系统规模的不断扩大,rsync的缺点逐渐被暴露了出来。首先,rsync做数据同步时,需要扫描所有文件后进行对比,然后进行差量传输。如果文件很大,扫面文件是非常耗时的,而且发生变化的文件往往是很少一部分,因此rsync是非常低效的方式。其次,rsync不能实时监测、同步数据,虽然它可以通过Linux守护进程的方式触发同步,但是两次触发动作一定会有时间差,可能导致服务器端和客户端数据出现不一致

SSH authentication protocol for 3.rsync

        rsync命令来同步系统文件之前要先登入 remote主机认证,认证过程中用到的协议有2种:        ssh协议        rsync协议    rsync server 端不用启动rsync的daemon进程,只要获取remote host 的用户名和密码就可以直接rsync同步文件    rsync server端因为不用启动daemon进程,所以也不用配置文件      /etc/rsyncd.cof            ssh认证协议跟scp的原理是一样的,如果在同步过程中不想输入密码就用 ssh-keygen  -t  rsa 打通通道            //这种方式默认是省略了 -e  ssh 的下面等价:            rsync -avz /SRC -e ssh [email protected]:/DEST                      -a     //文件宿主变化,时间戳不变                      -z     //压缩数据传输            //当遇到要修改端口的时候,我们可以用:            rsync -avz /SRC -e "ssh -p2222" [email protected]:/DEST            //修改了ssh 协议的端口,默认是22

4.rsync Command common options:

      -a, --archive                            //归档      -v,   --verbose                       //啰嗦模式      -q, --quiet                             //静默模式      -r, --recursive                        //递归      -p, --perms                           //保持原有的权限属性      -z, --compress                     //在传输时压缩,节省带宽,加快传输速度

5. Example Demo:

环境说明
Server Type IP Address
Source Server 192.168.209.12
Target server 192.168.209.13

* Demand * * *
Synchronize the/etc directory on the original server to the/tmp/of the target server in real time

 On the target server, do the following: *****************rsync+inotify****************//target server-side operations first shut down the firewall [[email protected] ~]# Yum ins   Tall-y rsync [[email protected] ~]# cat >>/etc/rsyncd.conf << EOF > log file =/var/log/rsync.log > pidfile =/var/run/rsyncd.pid > Lock file =/var/run/rsync.lock > Secrets file =/etc/rsync.pass > ; [Etc_from_client] > Path =/tmp/> comment = Sync etc from Client > uid = root > gid = root > Por   t = 873 > Ignore errors > Use chroot = no > Read only = no > list = no > Max connections = 200  > timeout = > Auth users = admin >eof//Create user authentication file [[email protected] ~]# echo ' admin:123456 ' > /etc/rsync.pass [[email protected] ~]# cat/etc/rsync.passadmin:123456//Set file permissions [[email protected] ~]# chmod 600/etc/rsync* [[email protected] ~]# ll/etc/rsync*-RW-------. 1 root root 787 August 15:13/etc/rsyncd.conf-rw-------1 ROot Root 13 August 15:14/etc/rsync.pass//Start rsync service and set boot self-boot [[email protected] ~]# systemctl start RSYNCD [[E Mail protected] ~]# systemctl enable RSYNCD [[email protected] ~]# ss-antl::: 873 Port # # # #源服务器  End first off firewall//configure Yum source [[[email protected] ~]# cd/etc/yum.repos.d/[[email protected] yum.repos.d]# wget Http://mirrors.163.com/.help/CentOS7-Base-163.repo [[email protected] ~]# sed-i ' s/\ $re Leasever/7/g '/etc/yum.repos.d/centos7-ba Se-163.repo [[email protected] ~]# sed-i ' s/^enabled=.*/enabled=1/g '/ Etc/yum.repos.d/cen Tos7-base-163.repo [[email protected] ~]# yum install-y epel-release [[Email protec Ted] ~]# Yum install-y update--skip-broken//install Rsync server, need to install, do not start, do not need to configure [[email protected] ~]# yum install-y r Sync//create authentication password file [[[email protected] ~]# echo ' 123456 ' >/etc/rsync.pass [[email protected] ~]# Cat/etc/rsy Nc.pass 123456//Set file rightsLimit, only set the file owner to have read, write permission can be [[email protected] ~]# chmod 600/etc/rsync.pass [[email protected] ~]# LL/ETC/RSYNC.P ASS-RW-------. 1 root root 7 August 16:05/etc/rsync.pass//Create a test directory on the source server and run the command on the original server [[email protected] ~]# mkdir-pv/root/etc /test [[email protected] ~]# RSYNC-AVH--port 873--progress--delete/root/etc/[email protected]::etc_from_ After client--password-file=/etc/rsync.pass//This step, there is a test directory under the/TMP directory on the clients, indicating that the data synchronization is successful//check whether the server kernel supports inotify[[email  Protected] ~]# ll/proc/sys/fs/inotify///installation inotify-tools[[email protected] ~]# yum install-y make gcc gcc-c++[[ Email protected] ~]# Yum install-y inotify-tools//Write synchronization script [[email protected] ~]# mkdir/scripts[[email  Protected] ~]# touch/scripts/inotify.sh[[email protected] ~]# chmod 755/scripts/inotify.sh[[email  Protected] ~]# ll/scripts/inotify.sh-rwxr-xr-x. 1 root root 0 August 16:27/scripts/inotify.sh[[email protected] ~]# vim/scripts/inotify.sh#!/bin/bash host=192.168.209.13 src=/etc des=etc_from_client password=/etc/rsync.pass user=admin inotifywait=/usr/bin/inotifywait $ Inotifywait-mrq--timefmt '%y%m%d%h:%m '--format '%T%w%f%e '-E modify,delete,create,attrib $src | While read files;d o rsync-avzp--delete--timeout=100--password-file=${password} $src [email protected] $host:: $d Es echo "${files} was rsynced" >>/tmp/rsync.log 2>&1done//startup script [[email protected] ~]# nohup bash/ scripts/inotify.sh &[2] 3351[[email protected] ~]# nohup: Ignores input and appends output to "nohup.ou[[email protected" ~]# PS -ef|grep inotify//generate a new file on the source server [[email protected] ~]# mkdir/etc/lanzhiyong[[email protected] ~]# Echo ' I Love China ' >/etc/lanzhiyong/lan//view inotify generated log [[email protected] ~]# tail/tmp/rsync.log//Setup script boot automatically [[ Email protected] ~]# chmod +x/etc/rc.d/rc.local [[email protected] ~]# ll/etc/rc.d//rc.local [[email  Protected] ~]# echo ' nohup/bin/bash/scripts/inotify.sh ' >>/etc/rc.d/rc.local[[email protected] ~]# tail/etc/rc.d/rc.local The last example demonstrates validation: on the target server [[email protected] ~] #cd/tmp[[email  Protected] ~] #cd/etc[[email protected] ~] #cd lanzhiyong[[email protected] ~] #cat LanI love China

Setting up rsync services in Linux in detail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.