×

Rsync 部署方式指南

hqy hqy 发表于2025-08-05 01:58:23 浏览6 评论0

抢沙发发表评论

Rsync 部署方式指南

Rsync 是一种高效的文件同步工具,广泛用于部署应用程序和网站。以下是几种常见的 Rsync 部署方式:

1. 基本Rsync 部署命令

rsync -avz --delete /local/path/ user@remotehost:/remote/path/

参数说明:

  • • -a: 归档模式,保留文件属性
  • • -v: 详细输出
  • • -z: 压缩传输
  • • --delete: 删除目标目录中源目录不存在的文件

2. 排除特定文件/目录

rsync -avz --exclude='node_modules' --exclude='.git' /local/path/ user@remotehost:/remote/path/

或使用排除文件:

rsync -avz --exclude-from='exclude-list.txt' /local/path/ user@remotehost:/remote/path/

3. SSH 密钥认证部署

  1. 1. 生成 SSH 密钥对(如果还没有):
ssh-keygen -t rsa
  1. 2. 将公钥复制到远程服务器:
ssh-copy-id user@remotehost
  1. 3. 使用 Rsync 通过 SSH 部署:
rsync -avz -e ssh /local/path/ user@remotehost:/remote/path/

4. 增量部署(仅同步更改文件)

rsync -avz --checksum /local/path/ user@remotehost:/remote/path/

5. 限速传输(避免占用过多带宽)

rsync -avz --bwlimit=1000 /local/path/ user@remotehost:/remote/path/

(单位:KB/s)


6. 部署后执行远程命令

rsync -avz /local/path/ user@remotehost:/remote/path/ && ssh user@remotehost "cd /remote/path && ./deploy.sh"

7. 使用 Rsync 守护进程模式

  1. 1. 在远程服务器配置 /etc/rsyncd.conf
[deploy]path = /remote/pathcomment = Deployment Directoryread only = noauth users = deployusersecrets file = /etc/rsyncd.secrets
  1. 2. 同步文件:
rsync -avz /local/path/ rsync://deployuser@remotehost/deploy/

8. 结合 Git 的部署流程

git pull origin master && rsync -avz --delete --exclude='.git' /local/path/ user@remotehost:/remote/path/

下行服务部署方案:

服务端配置:

1.创建文件内容

图片
图片
图片

2.备份rsync配置文件

图片

3.rsync配置文件参数调整

图片

4.登录用户配置

图片

5.服务启动

图片
图片
图片

6.客户端测试:

图片
图片

上行服务部署方案

前提:依据下行部署方案成功后,才可配置上行


  1. 1. 首先要修改服务器端模板

图片

  1. 2. 客户端测试

图片

  1. 3. 服务器查看内容是否变更

图片

辅助材料:

# delete 删除目标地址有的文件,而源地址没有的文件rsync -avz --delete rsync://rsync用户@服务器端IP/共享目录  本地目录rsync -avz --delete rsync://cc@1.1.1.1/share  /client/rsync

注意事项

  1. 1. 首次部署前建议先使用 --dry-run 参数测试
  2. 2. 生产环境部署应考虑维护版本回滚能力
  3. 3. 敏感文件(如配置文件)可能需要特殊处理
  4. 4. 大文件部署可能需要考虑断点续传

以上方法可以根据具体项目需求组合使用,构建适合您项目的部署流程。


打赏

本文链接:https://kinber.cn/post/5404.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客