Files
cloneorg/gitbackup.sh
Christian Richter 66281606bc initial commit
2024-09-20 09:34:05 +02:00

60 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
# Set the organization name as the first argument
ORG=$1
# Check if the organization name is provided
if [ -z "${ORG}" ]; then
echo "Usage: $0 <github-organization|user>"
exit 1
fi
# Create a temporary directory with current date and time
TEMPDIR="${ORG}-$(date +"%Y-%m-%d-%H-%M-%S")"
mkdir $TEMPDIR
pushd $TEMPDIR
echo "Getting all repos for ${ORG}"
REPOS=$(curl "https://api.github.com/users/${ORG}/repos?per_page=1000" | grep -o 'git@[^"]*'| sed 's/git@/https:\/\//g' | sed 's/\.com:/\.com\//g')
# Check if any repos are found, exit if none
if [ -z "${REPOS}" ]; then
echo "No repos found for ${ORG}"
exit 1
fi
# Clone all repos
for REPO in $REPOS; do
echo "Cloning ${REPO}"
git clone ${REPO} || ERROR_CODE=$?
if [ -n "${ERROR_CODE}" ]; then
echo "Error cloning ${REPO}"
exit 1
fi
# Remove the .git extension from the repo name & enter the directory
pushd $(echo ${REPO}|sed -e 's/.*\///' -e 's/\.git$//')
# Fetch all branches
git fetch --all
# Pull all branches
git pull --all
# Fetch all tags
git fetch --tags --all
BRANCHES=$(git branch -r | grep -v '\->' | sed 's/origin\///')
for BRANCH in $BRANCHES; do
echo "Checking out branch ${BRANCH}"
git checkout ${BRANCH}
git pull origin ${BRANCH}
done
popd
done
popd
# Compress the directory into a tarball
echo "Compressing ${TEMPDIR} into ${TEMPDIR}.tar.gz"
tar -czf ${TEMPDIR}.tar.gz $TEMPDIR
# Remove the directory
rm -rf ${TEMPDIR}
# Show the tarball details
ls -lisah ${TEMPDIR}.tar.gz