Skip to content

线上的资源同步到本地

sync 命令是将线上的云沙箱资源同步到本地的命令:从线上反向生成一份可直接 s deploy 的 s.yaml。

命令解析

当执行命令sync -h/sync --help时,可以获取帮助文档。

参数解析

参数全称 参数缩写 Yaml 模式下必填 Cli 模式下必填 参数含义
region - 选填 必填 地域名称,取值参见支持的地域
team-name - 选填 必填 Team 名称
target-dir - 选填 选填 目标路径,默认目录是 <s.yaml 所在目录>/sync-clone

当前命令还支持部分全局参数(例如-a/--access, --debug等),详情可参考 Serverless Devs 全局参数文档

命令行参数优先于 s.yaml 中的 props。

操作案例

  • 有资源描述文件(Yaml)时,可以直接执行s sync将线上资源同步到本地;
  • 纯命令行形式(在没有资源描述 Yaml 文件时),需要指定地域与 Team 名称,例如s cli fc-sandbox sync --region cn-hangzhou --team-name dev -a default

上述命令的执行结果示例:

Sync yaml to /Users/xxx/hello-sandbox/sync-clone/cn-hangzhou_dev.yaml
The api key values are NOT synced — they are only visible right after create/reset.

sandbox:
  ymlPath: /Users/xxx/hello-sandbox/sync-clone/cn-hangzhou_dev.yaml

指定目标目录:

s sync --target-dir ./test
s cli fc-sandbox sync --region cn-hangzhou --team-name dev --target-dir /tmp/recovered -a default

产物文件名的格式是 <region>_<teamName>.yaml

同步产物示例

edition: 3.0.0
name: dev
access: default
resources:
  dev:
    component: fc-sandbox
    props:
      region: cn-hangzhou
      teamName: dev
      description: dev team
      plan: base
      volumes:
        - volumeName: data
          status: active
          ossVolumeConfig:
            bucketName: my-bkt
            bucketPath: /data
            endpoint: oss-cn-hangzhou.aliyuncs.com
            readOnly: false
      apiKeys:
        - apiKeyName: ci
          status: active
          expireTime: '2027-01-01T00:00:00Z'
          ipWhitelist:
            - ipAddress: 1.2.3.0/24
              description: office

注意事项

  • 产物绝不包含 apiKeyValuesync 用字段白名单生成 yaml,apiKeyValueapiKeyMask 都不在白名单内,同步完成后会 warn 提示:

    The api key values are NOT synced — a credential must not be written into s.yaml.
      Read one when you need it: s cli fc-sandbox apikey get --api-key-name <name> --team-name <team>
    

    需要明文时单独取回即可(DescribeApiKey 会返回),不必 reset:apikey get

  • 同步来的 s.yaml 可以直接 s deploy。ApiKey 已存在时走 update,不会重置密钥值。

  • 只写回可声明字段。服务端只读字段(teamID / volumeID / apiKeyID / createdTime / lastUsedTime 等)不会出现在产物里,因此产物是幂等可部署的,而不是一份线上状态快照——要看完整状态用 info

  • Team 不存在时直接报错(与 info 不同):

    Team "dev" not found in region cn-hangzhou
    
  • --target-dir 指向一个已存在的非目录路径时会报错:

    --target-dir "./x" exists, but is not a directory
    

权限与策略说明

使用该命令时,推荐配置系统策略:AliyunFCReadOnlyAccess