Skip to content

Instantly share code, notes, and snippets.

@hushin
Last active January 7, 2024 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hushin/bb54a996d82c38889eab500b8b35f0bf to your computer and use it in GitHub Desktop.
Save hushin/bb54a996d82c38889eab500b8b35f0bf to your computer and use it in GitHub Desktop.
GitHub リポジトリをバックアップするスクリプト。Synology NAS で実行する
#!/bin/bash
GITHUB_TOKEN="ghp_xxx"
CLONE_DIR="${HOME}/backup/gist"
# Function to clone or update a Gist
clone_or_update_gist() {
gist_url=$1
gist_description=$2
gist_id=$(basename "$gist_url")
gist_dir="$CLONE_DIR/$gist_id"
authenticated_url=$(echo "$gist_url" | sed "s/https:\/\//https:\/\/${GITHUB_TOKEN}:x-oauth-basic@/g")
if [ ! -d "$gist_dir" ]; then
echo "Cloning Gist: $gist_description"
git clone "$authenticated_url" "$gist_dir"
else
echo "Updating Gist: $gist_description"
cd "$gist_dir" && git pull
fi
if [ -n "$gist_description" ]; then
echo "$gist_description" > "$gist_dir/description.md"
fi
}
# Fetch personal gists
echo "Fetching personal gists"
gists=$(curl -H "Authorization: token $GITHUB_TOKEN" -s "https://api.github.com/gists?per_page=100" | jq -r '.[] | .git_pull_url + " " + .description')
# Create clone directory if it doesn't exist
mkdir -p "$CLONE_DIR"
# Clone or update each Gist
echo "$gists" | while read -r gist_url gist_description; do
clone_or_update_gist "$gist_url" "$gist_description"
done
echo "Gist backup complete."
#!/bin/bash
# https://github.com/settings/tokens で"repo"スコープを付けたpersonal access tokenを生成
GITHUB_TOKEN="ghp_xxx"
CLONE_DIR="${HOME}/backup/github"
OWNERS=("hushin" "hushin-sandbox")
# Function to fetch repositories from GitHub API
fetch_repos() {
page=1
all_repos=""
while :; do
response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" -I "https://api.github.com/user/repos?per_page=100&page=$page")
repos=$(curl -s -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/user/repos?per_page=100&page=$page" | jq -r '.[] | .clone_url + " " + .owner.login')
all_repos+="$repos"$'\n'
# Check if there is a next page
if echo "$response" | grep -q 'rel="next"'; then
((page++))
else
break
fi
done
echo "$all_repos"
}
# Function to clone or update a repository
clone_or_update_repo() {
repo_url=$1
owner_name=$2
repo_name=$(basename "$repo_url" .git)
owner_dir="$CLONE_DIR/$owner_name"
repo_dir="$owner_dir/$repo_name"
# Create directory for the owner if it doesn't exist
mkdir -p "$owner_dir"
# Replace https URL with token authentication URL for cloning
authenticated_url=$(echo "$repo_url" | sed "s/https:\/\//https:\/\/${GITHUB_TOKEN}:x-oauth-basic@/g")
if [ ! -d "$repo_dir" ]; then
echo "Cloning $repo_name into $owner_name"
echo "$authenticated_url" "$repo_dir"
git clone "$authenticated_url" "$repo_dir"
else
echo "Updating $repo_name in $owner_name"
cd "$repo_dir" && git pull
fi
}
# Fetch all repositories
all_repos=$(fetch_repos)
# Filter and clone/update repos based on OWNERS array
for owner in "${OWNERS[@]}"; do
echo "Processing repositories for $owner"
echo "$all_repos" | while read -r repo_url repo_owner; do
if [[ "$repo_owner" == "$owner" ]]; then
clone_or_update_repo "$repo_url" "$owner"
fi
done
done
echo "Backup complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment