Rsync教程第四章:Rsync同步程序的安装

安装Rsync很简单,因为我们给大家提供了安装脚本,只需要执行脚本,就可以安装成功了。不过根据你的ip,用户名,密码,你还是需要改一些参数descIp、syncSrcPath、pushPath。安装包rsync-3.0.8.tar.gz下载,请在我们的代码中下载:

rsync安装代码下载

#!/bin/bash

# rsync tar包安装服务端,系统需要有rpm源gcc、c++、perl
# 注意:脚本在使用时需要将变量descIp、srcPath、pushPath替换成自己的场景valvue。
set -e
export descIp=0.0.0.0/0     # 接收客户端ip
export syncSrcPath=/home/download/ # 需要同步給客户端的目录
export pushPath=/tmp/		# 需要接受客户端push到服务端的文件目录
# install rsync
yum -y install gcc gcc-c++ perl

inpath=/root
echo ${inpath}
cd ${inpath}
wget 66-ai.com/download/rsync/rsync-3.0.8.tar.gz -O /root/rsync-3.0.8.tar.gz
a=$(ls rsync-3.0.8.tar.gz)
if [[ $a==rsync-3.0.8.tar.gz ]]; then
	tar -xzf rsync-3.0.8.tar.gz&&cd rsync-3.0.8 
	./configure --prefix=/usr/local/rsync
	make && make install
fi

# config Action:edit hosts allow
cat << EOF > /etc/rsyncd.conf
[global]
uid = nobody
gid = nobody
use chroot = no
max connections = 20
hosts allow = ${descIp}
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
lock file = /var/run/rsyncd.lock
transfer logging = yes
log fomat = %t %a %m %f %b
timeout = 300

[synced_name]
read only = yes
path = ${syncSrcPath}
auth users = hellodemos
secrets file = /etc/rsyncd.pwd
timeout = 300

[push_name]
read only = no
write only = yes
path = ${pushPath}
auth users = hellodemos
secrets file = /etc/rsyncd.pwd
timeout = 300
EOF

echo 'hellodemos:hellodemos'  > /etc/rsyncd.pwd

chmod 600 /etc/rsyncd.pwd
ln -sv /usr/local/rsync/bin/rsync /usr/bin/

# start
/usr/local/rsync/bin/rsync  --daemon  --config=/etc/rsyncd.conf
sleep 2
rsync_serv_info=$(ss -tunlp |grep rsync)
echo 'rsync service INFO : ' ${rsync_serv_info}

# get rsyncd pid and sed rsyncd-conf then restart

看了这一长串代码似乎不太理解,没关系,不用着急。地球正是因为耐心,才在漫长的45.7亿年时间里,变为我们今天的样子的。我们对上面的代码,进行一些必要解释。

注意,如果你遇到错误如下的错误,可能是gcc不兼容,这时候,你可以注释掉yum -y install gcc gcc-c++ perl这句脚本。

File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax

rysnc的数据存放路径设置

想看一个3个参数:

第一个我们设置的参数,是客服端ip,只有这里设置的ip段,才能去访问rsync服务器。我的ip为125.84.181.34,所以我设置为125.84.181.0/24,当然 ,你直接设置为125.84.181.34/32,也是可以的。

export descIp=125.84.181.0/24     # 接收客户端ip

设置同步路径,我们可以设置一个客户端下载服务器哪个路径的变量,如下:

export syncSrcPath=/home/download/ # 需要同步給客户端的目录

这代表,客户端需要从上面的路径中下载数据。

同理,我们也可以设置一个上传目的路径,就是上传的数据存储在哪里,当然,可以和前面的路径一直,我们这里设置为另一个路径,这样上面的文件会被存储在/tmp目录下:

export pushPath=/tmp/		# 需要接受客户端push到服务端的文件目录

编译安装Rsync

接下来,我们编译安装rsync,首先安装一些依赖库gcc gcc-c++ perl,这里用gcc编译安装。

yum -y install gcc gcc-c++ perl

inpath=/root
echo ${inpath}
cd ${inpath}
wget 66-ai.com/download/rsync/rsync-3.0.8.tar.gz -O /root/rsync-3.0.8.tar.gz
a=$(ls rsync-3.0.8.tar.gz)
if [[ $a==rsync-3.0.8.tar.gz ]]; then
	tar -xzf rsync-3.0.8.tar.gz&&cd rsync-3.0.8 
	./configure --prefix=/usr/local/rsync
	make && make install
fi

然后其他代码是找到rsync-3.0.8.tar.gz文件,你需要上传到/root/目录下,不过没关系 ,你使用我们这节课提供的脚本,会自动到下载安装包,然后安装。

./configure --prefix=/usr/local/rsync 表示我们安装到 /usr/local/rsync 目录下。

make && make install 表示编译和安装


启动rsync

接下来是启动rsync:

ln -sv /usr/local/rsync/bin/rsync /usr/bin/

# start
/usr/local/rsync/bin/rsync  --daemon  --config=/etc/rsyncd.conf
sleep 1
rsync_serv_info=$(ss -tunlp |grep rsync)
echo 'rsync service INFO : ' ${rsync_serv_info}

如果,你执行 ss -tunlp |grep rsync,得到如下的结果,说明你安装成功了。

ss -tunlp |grep rsync tcp LISTEN 0 5 :873 : users:(("rsync",pid=1232,fd=4)) tcp LISTEN 0 5 :::873 ::: users:(("rsync",pid=1232,fd=5))

873是你的rsync的端口,注意,如果你使用的阿里云,请在安全组那里把这个端口加上运行外网访问,非常重要。


停止rsync服务器

停止很简单,只要找到进程号,然后停止就可以了。

ps -ef | grep 'rsync --daemon'

找到进程号,然后kill掉

kill pid