diff --git a/cloneall.sh b/cloneall.sh index 87fd386..1f4fabb 100755 --- a/cloneall.sh +++ b/cloneall.sh @@ -10,13 +10,14 @@ githubOrganization="" # Script configuration targetDir="./repos-$(date +"%Y-%m-%d")" cloneOrgRepos=true -cloneUserRepos=false +cloneUserRepos=true targetField="clone_url" startDir="$(pwd)" # Script mkdir -p $targetDir cd $targetDir +echo "startDir: $startDir" echo "PWD: $(pwd)" # https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-organization-repositories @@ -24,6 +25,28 @@ listOrgReposUrl="https://api.github.com/orgs/$githubOrganization/repos?per_page= if $cloneOrgRepos then orgRepositories=$(curl $listOrgReposUrl -u ${githubUser}:${githubToken} | jq -r .[].${targetField}) + echo "$orgRepositories" + mkdir -p "$startDir/$targetDir/$githubOrganization" + cd "$startDir/$targetDir/$githubOrganization" + for repository in $orgRepositories + do + printf "\nRepository found: $repository\n" + repo="${repository##*/}" + repo="${repo%.*}" + echo "$repo" + echo "$repository" + mkdir -p "$repo" + cd "$repo" + git clone --mirror "$repository" . + for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; + do + git branch --track ${branch#remotes/origin/} $branch || git branch + done + echo "PWD: $(pwd)" + git bundle create ./$repo.bundle --all + git bundle verify ./$repo.bundle + cd ../ + done else orgRepositories="" fi @@ -33,31 +56,32 @@ listUserRepoUrl="https://api.github.com/user/repos?per_page=100&type=owner" if $cloneUserRepos then userRepositories=$(curl $listUserRepoUrl -u ${githubUser}:${githubToken} | jq -r .[].${targetField}) + echo "$userRepositories" + echo "PWD: $(pwd)" + mkdir -p "$startDir/$targetDir/$githubUser" + cd "$startDir/$targetDir/$githubUser" + for repository in $userRepositories + do + printf "\nRepository found: $repository\n" + repo="${repository##*/}" + repo="${repo%.*}" + echo "$repo" + echo "$repository" + mkdir "$repo" + cd "$repo" + git clone --mirror "$repository" . + for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; + do + git branch --track ${branch#remotes/origin/} $branch || git branch + done + echo "PWD: $(pwd)" + git bundle create ./$repo.bundle --all + git bundle verify ./$repo.bundle + cd ../ + done else userRepositories="" fi -# Clone all repositories -repositories="$orgRepositories $userRepositories" -for repository in $repositories -do - printf "\nRepository found: $repository\n" - repo="${repository##*/}" - repo="${repo%.*}" - echo "$repo" - echo "$repository" - mkdir "$repo" - cd "$repo" - git clone --mirror "$repository" . - for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; - do - git branch --track ${branch#remotes/origin/} $branch || git branch - done - echo "PWD: $(pwd)" - git bundle create ./$repo.bundle --all - git bundle verify ./$repo.bundle - cd ../ -done - cd "$startDir" zip -r -m -9 "repo-archive-$(date +"%Y-%m-%d").zip" "$targetDir" \ No newline at end of file diff --git a/readme.md b/readme.md index faf3b75..cbeedd9 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,23 @@ +# Script for making a backup of a user/organisations Github repos with commit history + +This script allows a user to backup repos with commit history from organisations/users and compress them into a single zip archive + +# Running the script + +Dependancies: +``` +jq git zip +``` +1. Replace `` with your access token from https://github.com/settings/tokens Token only needs repo and org read permisions. +2. Replace `` with your github user and set `cloneUserRepos=` to `true` if your cloning a users repos or Replace `` with a org name and set `cloneOrgRepos=` to `true`. It is possible to set both to `true` this will clone both the user and the orgs repo into seperate folders in a single .zip + +3. Run the script with you might need to set the script to executable with `chmod +x ./cloneall.sh` + ``` + ./cloneall.sh + ``` + # Restore a repo from a bundle file -Borrowed from https://gist.github.com/xtream1101/fd79f3099f572967605fab24d976b179 +Restore notes borrowed from https://gist.github.com/xtream1101/fd79f3099f572967605fab24d976b179 Here we will restore the repo from the bundle and create a new remote origin that will contain all brnaches and tags 1. Clone the repo from the bundle