Last active
June 6, 2020 14:28
-
-
Save benhutchins/233aa32dcddbb65b3e0515daf99b935f to your computer and use it in GitHub Desktop.
Push a folder of git repos from a Gitlab backup back into a Gitlab server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script is a utility to re-import repos from a Gitlab backup | |
# A gitlab backup is a .tar file, usually named something like: | |
# 111111111_gitlab_backup.tar | |
# When extracted, there is a repositories/ directory inside of it | |
# inside repositories/ is a folder for each user and group in | |
# Gitlab which had a project at the time of export. | |
# This script is designed to be run inside of the a user/group | |
# directory, and it will automatically reimport all the projects | |
# into Gitlab. | |
# *Note*: User or group must exist inside the new gitlab | |
# exit this script if anything fails | |
set -e | |
# define the namespace to import everything into | |
gitlab="https://gitlab.com" | |
# default the group to the name of the current directory | |
group=${PWD##*/} | |
# determine the group id, from https://gitlab.com/api/v3/groups | |
group_id=7 | |
# get a gitlab token from https://gitlab.com/profile/personal_access_tokens | |
token="" | |
# go through each bundled archive | |
for f in *.bundle.tar; do | |
echo $f | |
# skip dots | |
if [ $f == "." ] || [ $f == ".." ]; then | |
continue | |
fi | |
# if this is a .sh, skip it | |
if [[ $f == *.sh ]]; then | |
continue | |
fi | |
tar=$f | |
repo=${tar/.bundle.tar/} | |
dir="$repo" | |
echo "Processing $repo" | |
# make the repo dir | |
mkdir -p $dir/ | |
# extract .tar, if this fails it means it is a git | |
(cd $dir && tar -xf ../$tar) | |
# create the repo on gitlab | |
url="$gitlab/api/v3/projects?name=$repo&namespace_id=$group_id&wiki_enabled=false&visibility_level=10&issues_enabled=false" | |
echo "curl --header 'PRIVATE-TOKEN: $token' -X POST '$url'" | |
(curl --header "PRIVATE-TOKEN: $token" -X POST "$url") || true | |
# determine the remote | |
remote="$gitlab/$group/$repo.git" | |
# add aptima's gitlab server as a remote | |
(cd $dir && git remote add aptima $remote) | |
# push everything up to gitlab | |
(cd $dir && git push --all aptima) | |
(cd $dir && git push --tags aptima) | |
# remove the clone and tar | |
rm -rf $dir $tar | |
# debug, break | |
# break | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment