2022年5月18日 星期三

備份 | 備分Github到地端

這邊是在網路上參考了兩位達人寫出的script而產生出來的備分Script,由於我想要備分的Github Repository超過100個,不能直接用網路上很多人寫好的Script,算是分兩段完成,Token要先在這邊產生,Script如下


#!/bin/bash # Author: Sarav AK # Email: hello@gritfy.com # Created Date: 19 Aug 2021 =================第一段產生所有RepositoryScript================= USERNAME=輸入使用者名稱 TOKEN=輸入github所產生的Token # No of reposoitories per page - Maximum Limit is 100 PERPAGE=100 # Change the BASEURL to your Org or User based # Org base URL BASEURL="https://api.github.com/orgs/備分Repository的名稱/repos" # User base URL# 或使用個人備分 # BASEURL="https://api.github.com/user/<your_github_username>/repos" # Calculating the Total Pages after enabling Pagination TOTALPAGES=`curl -I -i -u $USERNAME:$TOKEN -H "Accept: application/vnd.github.v3+json" -s ${BASEURL}\?per_page\=${PERPAGE} | grep -i link: 2>/dev/null|sed 's/link: //g'|awk -F',' -v ORS='\n' '{ for (i = 1; i <= NF; i++) print $i }'|grep -i last|awk '{print $1}' | tr -d '\<\>' | tr '\?\&' ' '|awk '{print $3}'| tr -d '=;page'` i=1 until [ $i -gt $TOTALPAGES ] do result=`curl -s -u $USERNAME:$TOKEN -H 'Accept: application/vnd.github.v3+json' ${BASEURL}?per_page=${PERPAGE}\&page=${i} 2>&1` echo $result > tempfile echo "Repo Name, SSH URL, Clone URL" cat tempfile | jq -r '.[] | "\(.name),\(.full_name),\(.private),\(.html_url)"' >> /tmp/reposall ((i=$i+1)) done
=================第二段備份Script=================# A script to backup Github repositories to local by Rong Zhuang. # https://github.com/jojozhuang/bash-scripts # where should the files be saved BACKUP_PATH="/mnt/backup" #輸入備份路徑 # token from https://github.com/settings/tokens OAUTH_TOKEN="輸入github所產生的Token" # number of repositories COUNT=1 # create backup directory NOW=$(date '+%F_%H%M%S') mkdir "${BACKUP_PATH}/${NOW}" -p echo "Create backup directory: ${BACKUP_PATH}/${NOW}"


fetch_fromUrl() { if [ -z "$OAUTH_TOKEN" ] then echo "Fetching public respositories from ${URL_PUBLIC}" REPOS=`curl "${URL_PUBLIC}" | jq -r '.[] | "\(.name),\(.full_name),\(.private),\(.html_url)"'` else echo "Fetching all repositories from ${URL_PRIVATE}" REPOS=`cat /tmp/reposall` fi for REPO in $REPOS do let COUNT++ REPONAME=`echo ${REPO} | cut -d ',' -f1` REPOFULLNAME=`echo ${REPO} | cut -d ',' -f2` PRIVATEFLAG=`echo ${REPO} | cut -d ',' -f3` ARCHIVEURL=`echo ${REPO} | cut -d ',' -f4` ARCHIVEURL="${ARCHIVEURL}/archive/master.zip" FILEPATH="${BACKUP_PATH}/${NOW}/${REPONAME}.zip" echo ${ARCHIVEURL} curl -H "Authorization: token ${OAUTH_TOKEN}" -L ${ARCHIVEURL} -o ${FILEPATH} echo "Saved to ${FILEPATH}" done } fetch_fromUrl echo "$((COUNT)) repositories updated"


參考網址:
第一段Script 
第二段Script 


沒有留言: