程序员箴言:能半自动化就不要手动,能自动化就不要半自动化。程序员的“懒惰”拯救世界。
OpenSourceX @ifeegoo https://github.com/ifeegoo。
GitLab 已经是作为公司代码管理的首选管理系统了,针对 GitLab 的数据备份也是极其重要的,除了服务器端的数据备份,这个备份相对难度较大,操作复杂一点。如果不懂服务器,同时我们只用到了 GitLab 来存储 Git 仓库,这个时候我们就可以只简单的备份 Git 仓库就可以了!
最近公司又到年度各种数据备份总结的时候了,由于服务器端通过 GitLab 整体数据备份有一点问题,然后就想通过直接 Git Clone 来进行备份,但是又不想手动的一个个的进行 Clone,看看有没有更好的方式,看到 GitLab 官方有相关的 API 文档:GitLab API,就从这里下手了。
初步版本的小工具的核心就在于获取 GitLab 当前所有 Git 仓库地址,只要拿到了所有的 Git 仓库地址,一切都是 for 循环的问题了。
我们可以快速找到一个接口可以满足我们的需求,以下接口通过传入当前用户的 Private Token,分页和每页仓库数量,就可以获取所有的当前用户相关的仓库信息。
api/v3/projects?private_token=***&per_page=***&page=***
我们可以快速找到一个接口可以满足我们的需求,以下接口通过传入当前用户的 Private Token,分页和每页仓库数量,就可以获取所有的当前用户相关的仓库信息。返回的单个仓库的 JSON 数据如下:
{ "id": 408, "description": "Alexa", "default_branch": null, "tag_list": [], "public": false, "archived": false, "visibility_level": 0, "ssh_url_to_repo": "git@192.168.0.20:cloudhearing/android-bluetooth-vehicle-icarplayer.git", "http_url_to_repo": "http://192.168.0.20/gitlab/cloudhearing/android-bluetooth-vehicle-icarplayer.git", "web_url": "http://192.168.0.20/gitlab/cloudhearing/android-bluetooth-vehicle-icarplayer", "name": "android-bluetooth-vehicle-icarplayer", "name_with_namespace": "cloudhearing / android-bluetooth-vehicle-icarplayer", "path": "android-bluetooth-vehicle-icarplayer", "path_with_namespace": "cloudhearing/android-bluetooth-vehicle-icarplayer", "issues_enabled": true, "merge_requests_enabled": true, "wiki_enabled": true, "builds_enabled": true, "snippets_enabled": false, "created_at": "2018-11-13T01:52:19.229Z", "last_activity_at": "2018-11-13T01:53:12.201Z", "shared_runners_enabled": true, "creator_id": 37, "namespace": { "id": 40, "name": "cloudhearing", "path": "cloudhearing", "owner_id": null, "created_at": "2018-04-09T02:47:24.104Z", "updated_at": "2018-04-09T02:47:24.104Z", "description": "云耳", "avatar": { "url": null }, "share_with_group_lock": false, "visibility_level": 0 }, "avatar_url": null, "star_count": 0, "forks_count": 0, "open_issues_count": 0, "public_builds": true, "permissions": { "project_access": null, "group_access": { "access_level": 50, "notification_level": 3 } } }
从以上 JSON 数据可以看出,第 9 和 10 行的数据最为重要,我们拿到了 Git 仓库的地址,一切都好办了。那么接下来我们就可以完成我们的小工具了。
环境:
macOS: Mojave version 10.14
JDK: 8.0
Terminal: 2.9(421)
GitLab: 8.7.5
Git: 2.17.2 (Apple Git-113)
本来是希望全部用 shell 命令来写,发现针对 macOS shell 的数据处理这块还不是很熟悉,就先用自己最熟悉的 Java 语言来处理数据这块,然后生成一个 macOS shell 文件,执行 shell 命令即可。网络请求我使用的是 OkHttp,解析 JSON 采用的是 fastjson,提取相关全部的 Git 仓库信息之后,生成以下格式 shell 命令:
GITLAB_REPOSITORIES_URLS=( http://192.168.0.20/gitlab/cloudhearing/android-bluetooth-vehicle-icarplayer.git http://192.168.0.20/gitlab/cloudhearing/ios-bluetooth-vehicle-icarplayer.git http://192.168.0.20/gitlab/cloudhearing/android-bluetooth-vehicle-lovecargenie.git http://192.168.0.20/gitlab/snaillove/ios-bluetooth-color-lamp-snail-bulb.git http://192.168.0.20/gitlab/snaillove/android-bluetooth-color-lamp-snail-bulb.git) GITLAB_REPOSITORIES_DIRS=( cloudhearing/android-bluetooth-vehicle-icarplayer cloudhearing/ios-bluetooth-vehicle-icarplayer cloudhearing/android-bluetooth-vehicle-lovecargenie snaillove/ios-bluetooth-color-lamp-snail-bulb snaillove/android-bluetooth-color-lamp-snail-bulb) if [ ! -d "GitLabBackup" ]; then echo "Creating folder GitLabBackup" mkdir "GitLabBackup" cd "GitLabBackup" else echo "GitLabBackup directory is exist" if [ "$(ls -A GitLabBackUp)" ]; then echo "\033[1;31mGitLabBackup directory is not empty,the iGitLabBackup Tool default directory is GitLabBackup,make sure that you are not using GitLabBackup under the directory now.\033[0m" exit fi fi for index in {0..4} do echo "\033[32mStart cloning "$index"/4\033[0m" if [ ! -d ${GITLAB_REPOSITORIES_DIRS[${index}]} ]; then echo "Creating folder "${GITLAB_REPOSITORIES_DIRS[${index}]} mkdir -p ${GITLAB_REPOSITORIES_DIRS[${index}]} git clone ${GITLAB_REPOSITORIES_URLS[${index}]} ${GITLAB_REPOSITORIES_DIRS[${index}]} fi echo "\033[32mFinished cloning "$index"/4\033[0m" done
通过运行以上 shell 命令,就可以实现循环 Git Clone 所有的当前用户底下 Git 仓库了。目前该开源项目还处于很初级的版本。希望以后一起完善。
上一篇: « macOS 上使用 Jenkins 搭建 Android/iOS 持续集成环境 下一篇: Android/iOS 应用版本的维护 »