自动备份github仓库小程序

版权声明:科学的发展与人类精神的深化是难得的平衡 https://blog.csdn.net/guguant/article/details/78204912

痛点分析

小Go是一个好奇心泛滥的程序猿。
有一天,他想把github repositories全部备份到本地。
小Go有15个git repositories,一个个备份,恐怕要到猴年马月了。
于是,小Go想到了DevOps,AI,DeepLearning,et al. 最后,他决定用shell script实现,批量备份。

分析过程

  1. 浏览repositories的URL: https://github.com/Guguant?tab=repositories
  2. 查看源代码
    这里写图片描述
  3. 发现,https://github.com/Guguant/enjoy-code 仓库的URL为如下描述

    <a href="/Guguant/enjoy-code" itemprop="name codeRepository">
        enjoy-code</a>
  4. 综合以上分析,小Go需要做的工作是,首先下载https://github.com/Guguant?tab=repositories的源代码,然后提取出每一个仓库的名字name,之后将仓库的名字组装成仓库的URL,最后使用git clone命令clone repositories。

实现

    #!/bin/bash
    # tool
    # auto backup all repositories from github
    if [[ $# -eq 0 ]] || [[ $1 = "-h" ]];then
        echo -e "\033[31m--------------------------------------------------------\033[0m"
        echo "Usage1: $0 URL"
        echo "   e.g: $0 https://github.com/Guguant?tab=repositories"
        echo ""
        echo "Usage2: $0 Username Page"
        echo "   e.g: $0 Guguant 1"
        echo -e "\033[31m--------------------------------------------------------\033[0m"
        exit 1;
    fi

    url=""
    username=""
    if [ $# == 1 ];then
        url="https://github.com/Guguant?tab=repositories"
        username=${url%\?*}
        username=${username##*/}
    else
        username=$1
        url="https://github.com/${username}?page=${2}&tab=repositories"
    fi

    patten1="a href=\"/"${username}"/"
    r_path=`curl "${url}" | grep "${patten1}"`

    # get repositories url path
    path_head="https://github.com/"
    for p in ${r_path}
    do
        if [[ $p =~ "href" ]];then
            p=${p#*/}
            p=${p%\"*}

            p=${path_head}${p}
            echo -e "\033[33m ====== Download ====================\033[0m"
            echo -e "\033[32m $p \033[0m"
            git clone ${p}
            echo -e "\033[33m ====== $p has been Finished =================\033[0m"
        fi
    done

steps

  1. curl url,下载网页源代码
  2. grep截取仓库名
  3. 将每一个repertory name组装成完整的URL
  4. git clone

演示

    sh github-backup.sh https://github.com/Guguant?tab=repositories

demo

More

star: https://github.com/Guguant/enjoy-code
Issue: https://github.com/Guguant/enjoy-code/issues
(全文完)

没有更多推荐了,返回首页