InfluxDB2.7.6 备份以及解决root token丢失导致cli无法备份数据的问题

今天项目需要将influxdb的数据导入一份到其他系统,总结一下我遇到的问题

influxdb导出导入参考官方文档:https://docs.influxdata.com/influxdb/v2/admin/backup-restore/backup/

本来导出导入是很简单的事情,但是因为第一次进入系统的token丢失,并且在管理后台界面还删除掉了,造成认证一直失败

在管理页面Load Data->API TOKENS->GENERATE API TOKEN,创建拥有全部权限的token,尝试使用该token进行导出
导出并指定bucket

influx.exe backup F:\influxDB\backup -t csuS6MIxxpx...vQg== --bucket example

结果提示未授权,但是创建的时候给的是全部权限

Error: failed to backup metadata: failed to download metadata snapshot: 401 Unauthorized: unauthorized access

琢磨了一会发现,在API文档中看到/api/v2/authorizations可以返回所有授权token信息,可以在游览器中先登录influxdb(也可以在文档中找到登录接口进行登录),然后在请求http://xxx:8086/api/v2/authorizations,即可查询到所有已授权token


{
    "links": {
        "self": "/api/v2/authorizations"
    },
    "authorizations": [             
        {
            "id": "0dda132dcf0d6000",
            "token": "csuS6MIxxpxuW0z7wWDc...vQg==",
             ...
            "permissions": [
                {
                    "action": "read",
                    "resource": {
                        "type": "annotations",
                        "orgID": "d008309eead47874",
                        "org": "yykj"
                    }
                }       
                 ... ... ...
                {
                    "action": "write",
                    "resource": {
                        "type": "replications"
                    }
                }
            ],
            ... ... ...
            "createdAt": "2024-04-29T17:40:22.6876761+08:00",
        }
    ]
}

通过对比permissions发现,权限不一样,新建的token权限与管理员token对比resource查看到新建的并不是管理员token,以下是某个action的对比

普通token
{
    "action": "write",
    "resource": {
        "type": "annotations",
        "orgID": "d008309eead47874",
        "org": "xxx"
    }
}
管理员token
{
    "action": "write",
    "resource": {
        "type": "buckets"
    }
}

我本来向安装文档里写的通过client命令行创建一个管理员用户,但是尝试后发现想要使用client就必须要有管理员token,那只能使用API接口创建了,我是通过postman来调用的接口所以要带上cookie,cookie可以登录influxdb后在控制台获取到,下面是完整创建过程及参数

POST
请求路径:
http://10.10.0.203:8086/api/v2/authorizations
完整请求参数:
{
    "status": "active",
    "description": "iot-center-device",
    "orgID": "d008309eead47874",
    "permissions": [
        {
            "action": "read",
            "resource": {
                "type": "authorizations"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "authorizations"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "buckets"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "buckets"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "dashboards"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "dashboards"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "orgs"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "orgs"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "sources"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "sources"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "tasks"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "tasks"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "telegrafs"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "telegrafs"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "users"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "users"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "variables"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "variables"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "scrapers"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "scrapers"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "secrets"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "secrets"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "labels"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "labels"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "views"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "views"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "documents"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "documents"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "notificationRules"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "notificationRules"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "notificationEndpoints"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "notificationEndpoints"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "checks"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "checks"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "dbrp"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "dbrp"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "notebooks"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "notebooks"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "annotations"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "annotations"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "remotes"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "remotes"
            }
        },
        {
            "action": "read",
            "resource": {
                "type": "replications"
            }
        },
        {
            "action": "write",
            "resource": {
                "type": "replications"
            }
        }
    ]
}

调用成功后接口会返回本次创建的token和描述,这个token就可以操作client了,在influxdb管理页面API TOKENS和接口/api/v2/authorizations中也都可以查看到该token

备份和恢复命令参考官方文档:https://docs.influxdata.com/influxdb/v2/admin/backup-restore/backup/

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容