• Gitlab用户信息批量导出,批量block,unblock


    1. 查看Gitlab对应的系统用户 

    1. [root@ljettodevops-prod-38 wubo]# cat /etc/passwd | grep gitlab
    2. gitlab-www:x:994:990::/var/opt/gitlab/nginx:/bin/false
    3. git:x:993:989::/var/opt/gitlab:/bin/sh
    4. gitlab-redis:x:992:988::/var/opt/gitlab/redis:/bin/false
    5. gitlab-psql:x:991:987::/var/opt/gitlab/postgresql:/bin/sh
    6. gitlab-prometheus:x:990:986::/var/opt/gitlab/prometheus:/bin/sh

    2.根据信息登陆数据库

    [root@ljettodevops-prod-38 wubo]# su - gitlab-psql

    3. # 登陆数据库(-h指定host,-d指定数据库)

     psql -h /var/opt/gitlab/postgresql -d gitlabhq_production

    4.查看数据库

    1. gitlabhq_production=# \l
    2. List of databases
    3. Name | Owner | Encoding | Collate | Ctype | Access privileges
    4. ---------------------+-------------+----------+-------------+-------------+---------------------------------
    5. gitlabhq_production | gitlab | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
    6. postgres | gitlab-psql | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
    7. template0 | gitlab-psql | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/"gitlab-psql" +
    8. | | | | | "gitlab-psql"=CTc/"gitlab-psql"
    9. template1 | gitlab-psql | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/"gitlab-psql" +
    10. | | | | | "gitlab-psql"=CTc/"gitlab-psql"
    11. (4 rows)

    5. 使用库且查看库中包

    1. gitlabhq_production=# \c gitlabhq_production
    2. You are now connected to database "gitlabhq_production" as user "gitlab-psql".
    3. gitlabhq_production=# \dt
    4. List of relations
    5. Schema | Name | Type | Owner
    6. --------+------------------------------------------+-------+--------
    7. public | abuse_reports | table | gitlab
    8. public | appearances | table | gitlab
    9. public | application_setting_terms | table | gitlab
    10. public | application_settings | table | gitlab
    11. public | audit_events | table | gitlab
    12. public | award_emoji | table | gitlab
    13. public | badges | table | gitlab
    14. public | boards | table | gitlab
    15. public | broadcast_messages | table | gitlab
    16. public | chat_names | table | gitlab
    17. public | chat_teams | table | gitlab

    6.表结构:

    1. gitlabhq_production=# \d identities;
    2. Table "public.identities"
    3. Column | Type | Modifiers
    4. ------------+-----------------------------+---------------------------------------------------------
    5. id | integer | not null default nextval('identities_id_seq'::regclass)
    6. extern_uid | character varying |
    7. provider | character varying |
    8. user_id | integer |
    9. created_at | timestamp without time zone |
    10. updated_at | timestamp without time zone |
    11. Indexes:
    12. "identities_pkey" PRIMARY KEY, btree (id)
    13. "index_identities_on_user_id" btree (user_id)

    7 。# 查看表信息    \x 可以像mysql的\G一样 展示数据

    1、列举数据库:\l
    2、选择数据库:\c 数据库名
    3、查看该某个库中的所有表:\dt
    4、切换数据库:\c interface
    5、查看某个库中的某个表结构:\d 表名
    6、查看某个库中某个表的记录:select * from apps limit 1;
    7、显示字符集:\encoding
    8、退出psgl:\q
    设置字符集为 utf-8 就可以了.
    postgres=# \encoding utf-8       // 设置客户端的字元集
    postgres=# \encoding                  // 显示客户端的字元集
    postgres=# show client_encoding;      // 显示客户端的字元集
    postgres=# show server_encoding;      // 显示服务器的字元集

    PostgreSQL操作-psql基本命令_沈荣荣的博客-CSDN博客_psql

    1. gitlabhq_production=# \x
    2. Expanded display is on.
    3. gitlabhq_production=# select * from identities;
    4. -[ RECORD 1 ]----------------------------------------------------
    5. id | 1
    6. extern_uid | cn=sun_jian,ou=users,dc=rd,dc=hoperun,dc=com
    7. provider | ldapmain
    8. user_id | 132
    9. created_at | 2020-03-13 12:17:13.085423
    10. updated_at | 2020-03-13 12:17:13.085423
    11. -[ RECORD 2 ]-----

    8.退出数据库

    gitlabhq_production=# \q

    确定表表users中的 username , email , state 字段是需要提取的信息,进行导出操作

    1. 不是通过ldap创建的用户
    2. echo 'select users.id,users.email,users.name,users.username,users.state from users where users.id not in (select user_id from identities);' |psql -h /var/opt/gitlab/postgresql -d gitlabhq_production > no-ldap-users.info
    3. 通过ldap创建的用户
    4. echo 'select users.id,users.email,users.name,users.username,users.state,identities.extern_uid,identities.provider from identities,users where users.id=identities.user_id;' |psql -h /var/opt/gitlab/postgresql -d gitlabhq_production > ldap-users.info
    5. 所有用户
    6. echo 'select username,email,state from users;' |psql -h /var/opt/gitlab/postgresql -d gitlabhq_production > all-users.txt

    通过接口方式:
     

    1. #!/bin/bash
    2. perPage=100
    3. page=11
    4. for i in {1..11}
    5. do
    6. #curl -X GET -H "PRIVATE-TOKEN: 9yxKWbi7rN6U64cgDfMz" http://172.16.10.38/api/v4/users?per_page=$perPage&page=$i|python -m json.tool
    7. curl -X GET -H "PRIVATE-TOKEN: 9yxKWbi7rN6U64cgDfMz" http://172.16.10.38/api/v4/users?per_page=$perPage&page=$i
    8. done

    批量block

    1. [root@ljettodevops-prod-38 wubo]# cat readme
    2. echo 'select users.id,users.email,users.name,users.username,users.state from users where users.id not in (select user_id from identities);' |psql -h /var/opt/gitlab/postgresql -d gitlabhq_production > no-ldap-users.info
    3. echo 'select users.id,users.email,users.name,users.username,users.state,identities.extern_uid,identities.provider from identities,users where users.id=identities.user_id;' |psql -h /var/opt/gitlab/postgresql -d gitlabhq_production > ldap-users.info
    4. echo 'select username,email,state from users;' |psql -h /var/opt/gitlab/postgresql -d gitlabhq_production > all-users.txt
    5. curl -X GET -H "PRIVATE-TOKEN: xxxxxx" http://172.16.10.38/api/v4/users?username=wu_bo2 | python -m json.tool
    6. curl -X GET -H "PRIVATE-TOKEN: xxxxxxxxx" http://172.16.10.38/api/v4/users?username=li_jingtao | python -m json.tool | jq '.[].id'
    7. curl -X POST -H "PRIVATE-TOKEN: xxxxxxxxx" http://172.16.10.38/api/v4/users/292/block
    8. curl -X POST -H "PRIVATE-TOKEN: xxxxxxxxx" http://172.16.10.38/api/v4/users/292/unblock
    9. for i in $(cat all_names_block);do curl -X POST -H "PRIVATE-TOKEN: xxxxxxxx" http://172.16.10.38/api/v4/users/$(curl -X GET -H "PRIVATE-TOKEN: xxxxxxxx" http://172.16.10.38/api/v4/users?username=$i | python -m json.tool | jq '.[].id')/block; done
    1. [root@ljettodevops-prod-38 wubo]# cat all_names_block
    2. li_wanliang
    3. xue_min
    4. liuyang
    5. liu_jianguo
    6. jie_panjiao
    7. tang_liang
    8. zhang_yudong
    9. zhao_xueqing

  • 相关阅读:
    C#WPF框架Microsoft.Toolkit.MvvM应用实例
    vue3+elementPlus:el-tree复制粘贴数据功能,并且有弹窗组件
    18-云原生监控体系-kube-state-metrics
    neo4j下载安装配置步骤
    细谈VR全景:数字营销时代的宠儿
    图像超分经典网络ESRGAN精确解析
    Java并发编程学习:volatile关键字解析
    面试:封装DOM
    华为开发者大会HDC2022:HMS Core 持续创新,与开发者共创美好数智生活
    深入理解计算机系统:内存越界引用和缓冲区溢出
  • 原文地址:https://blog.csdn.net/Michaelwubo/article/details/126777355