Added ability to run orgs and users at the same time

This commit is contained in:
Jack 2023-06-26 15:11:12 +01:00
parent 25a701434e
commit 0d6725042a
2 changed files with 66 additions and 24 deletions

View File

@ -10,13 +10,14 @@ githubOrganization="<Your organiasation>"
# Script configuration # Script configuration
targetDir="./repos-$(date +"%Y-%m-%d")" targetDir="./repos-$(date +"%Y-%m-%d")"
cloneOrgRepos=true cloneOrgRepos=true
cloneUserRepos=false cloneUserRepos=true
targetField="clone_url" targetField="clone_url"
startDir="$(pwd)" startDir="$(pwd)"
# Script # Script
mkdir -p $targetDir mkdir -p $targetDir
cd $targetDir cd $targetDir
echo "startDir: $startDir"
echo "PWD: $(pwd)" echo "PWD: $(pwd)"
# https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-organization-repositories # 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 if $cloneOrgRepos
then then
orgRepositories=$(curl $listOrgReposUrl -u ${githubUser}:${githubToken} | jq -r .[].${targetField}) 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 else
orgRepositories="" orgRepositories=""
fi fi
@ -33,13 +56,11 @@ listUserRepoUrl="https://api.github.com/user/repos?per_page=100&type=owner"
if $cloneUserRepos if $cloneUserRepos
then then
userRepositories=$(curl $listUserRepoUrl -u ${githubUser}:${githubToken} | jq -r .[].${targetField}) userRepositories=$(curl $listUserRepoUrl -u ${githubUser}:${githubToken} | jq -r .[].${targetField})
else echo "$userRepositories"
userRepositories="" echo "PWD: $(pwd)"
fi mkdir -p "$startDir/$targetDir/$githubUser"
cd "$startDir/$targetDir/$githubUser"
# Clone all repositories for repository in $userRepositories
repositories="$orgRepositories $userRepositories"
for repository in $repositories
do do
printf "\nRepository found: $repository\n" printf "\nRepository found: $repository\n"
repo="${repository##*/}" repo="${repository##*/}"
@ -58,6 +79,9 @@ do
git bundle verify ./$repo.bundle git bundle verify ./$repo.bundle
cd ../ cd ../
done done
else
userRepositories=""
fi
cd "$startDir" cd "$startDir"
zip -r -m -9 "repo-archive-$(date +"%Y-%m-%d").zip" "$targetDir" zip -r -m -9 "repo-archive-$(date +"%Y-%m-%d").zip" "$targetDir"

View File

@ -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 `<your access token>` with your access token from https://github.com/settings/tokens Token only needs repo and org read permisions.
2. Replace `<your github user>` with your github user and set `cloneUserRepos=` to `true` if your cloning a users repos or Replace `<Your organiasation>` 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 # 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 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 1. Clone the repo from the bundle