代码之家  ›  专栏  ›  技术社区  ›  yaccob

用于备份和恢复的RESTful API[关闭]

  •  -1
  • yaccob  · 技术社区  · 6 年前

    我将要设计一个用于备份和恢复数据库的restfulapi。

    我想知道是否有一种既合理又宁静的方法。。。

    API应支持3种操作:

    1. 备份。
    2. 列出可用备份(主要是为了选择要还原的备份)。

    前两个看起来很简单:

    paths:
      /foo/backups:
        post:
          # create a backup
      /foo/backups:
        get:
          # list available backups
    

    但是如何从备份中恢复呢?

    我看到了一些选择——到目前为止,没有一个真正让我满意:

    1. 忘记REST,使用RPC over HTTP.,就像

      paths:
        /foo/backups/{backupId}:
          post:
            requestBody:
              content:
                application/json:
                  schema:
                    object:
                      properties:
                        action: { type: string }
                  examples:
                    theBody:
                      value:
                        action: restore
      
    2. /foo/backups/{backupId} ):

      paths:
        /foo/backups/{backupId}:
          get:
            # restore the database from this backup
      

      从REST的角度来看,这可能是好的,原因如下:

      • 资源( /foo/backups/{backupId}
      • 运算是幂等的

      但是有一个非常强烈的副作用(恢复数据库),我发现GET请求完全违反直觉。

    3. 对特定备份的POST(或PUT或PATCH)请求( ):

      paths:
        /foo/backups/{backupId}:
          post:
            # restore the database from this backup
      

      在这两种情况下,都不向备份中发布任何内容或注释来触发恢复。

    4. restores 要操作的资源:在本例中 /foo/restores/{backupId} 表示已执行的数据库还原的集合(例如,带有时间戳和注释)

      paths:
        /foo/restores/{backupId}:
          post:
            description: Adds a restore record to this restore collection
            requestBody:
              content:
                application/json:
                  schema:
                    object:
                      properties:
                        comment: { type: string }
                  examples:
                    theBody:
                      value:
                        comment: Restored because of DB corruption after power failure.
      

      看起来更合理,更安静,但这并不能真正说服我。

    5. 在上提供修补程序操作 /foo/backups/{backupId} 这将创建一个新的资源-例如。 /foo/backups/{backupId}/restores/{restoreId}

      paths:
        /foo/backups/{backupId}:
          patch:
            description: Adds a restore record to the restores collection of this backup
            requestBody:
              content:
                application/json:
                  schema:
                    object:
                      properties:
                        action: { type: string }
                        comment: { type: string }
                  examples:
                    theBody:
                      value:
                        action: restore
                        comment: Restored because of DB corruption after power failure.
      

      对我来说,到目前为止,这看起来是一个非常合理的方法,但它看起来非常像RPC而不是RESTful。

    • 更好方法的想法
    • 为什么我建议的其中一个比其他的好
    • 有什么遗漏的地方需要考虑吗?
    1 回复  |  直到 6 年前
        1
  •  1
  •   Mo A    6 年前

    我认为你的很多选择都是可行的——记住,没有一个一刀切的解决方案。

    方案4 ,稍加改动就很理想了。可能有点偏颇,但我过去就是这么用的。

    POST /foo/restores
    

    请求主体将包括 backup_id .

    因此:

    GET /foo/restores -返回所有还原记录。

    GET /fee/restores/{restore_id} -返回特定还原记录。