Linux下安装Rsync - 文件同步

  • 安装
# centos:
yum -y install rsync xinetd

#  ubuntu:
sudo apt-get install rsync xinetd
  • 配置文件(默认端口873)
# 编辑配置文件
vi /etc/rsyncd.conf
# 找到你的安装路径
pid file = /var/run/rsync/rsyncd.pid
lock file = /var/run/rsync/rsyncd.lock
log file = /var/log/rsync/rsyncd.log

[120]
# 需要同步的路径
path = /www/wwwroot/www.0357120.gov.cn/
# 备注
comment = 120
# 所用账号
uid = root
gid = root
read only = false
# 权限账号此处和下面的密码文件对应账号
auth users = rsync
secrets file = /etc/rsyncd.passwd
transfer logging = yes
list = yes
ignore errors
  • 密码文件
vim /etc/rsyncd.passwd
# 密码内容
rsync:密码字符串
  • 守护进程
# 编辑文件
sudo vim /etc/default/rsync
# 找到这一行 取消注释
RSYNC_ENABLE=inetd
  • 让守护进程为rsync守护启动 配置
# 编辑文件
sudo vim /etc/xinetd.d/rsync
# 写入内容
service rsync
    {
        disable         = no
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
    }
  •  配置RSYNC权限
    sudo chmod 600 /etc/rsyncd.passwd
  • 启动/重启 xinetd
# ubuntu
sudo /etc/init.d/xinetd restart
# centos
service xinetd restart

注意:如果你找不到xinetd配置文件无法通过xinetd来达到服务重启或者进程守护,可以通过rsync自身命令:

rsync --daemon --config=/etc/rsyncd.conf

 

 

 

  • 你本地也需要配置下

      安装这里不讲了。说下配置:

      你的项目目录下新建:【rsync_exclude.txt】 这个文件是排除你不想同步的文件的。比如uploads上传文件夹,基础目录是你项目目录也就是【rsync_exclude.txt】所在目录(文章最后附一张本地项目路径图)

.DS_Store
.gitignore
.git
.idea
rsync.sh
rsync.secret
rsync_exclude.txt

你的项目目录下新建:【rsync.secret】 这个文件是保存你的【RSYNC】密码的, 就是你上面【rsyncd.passwd】的密码

密码文本

然后这个密码文件需要600权限

    sudo chmod 600 ./rsyncd.passwd

最后新建一个【rsync.sh】文件,这是核心同步语法:

#!/bin/bash
rsync -rtu --progress --exclude-from=./rsync_exclude.txt --password-file=./rsync.secret  ./ rsync@服务器IP地址::项目名

解释下:这里IP的话就是你的服务器IP,项目名是你上面配置的如截图:

 运行直接: 本地进入项目下运行rsync.sh即可

./rsync.sh

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值