initial commit

This commit is contained in:
2023-12-07 21:21:35 -08:00
commit cf0336fe6f
6 changed files with 74 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*~

4
Dockerfile Normal file
View File

@@ -0,0 +1,4 @@
FROM alpine
RUN apk add --no-cache git py3-pip bash openssh && pip install --break-system-packages python-gitlab
COPY all_repos.py update.sh prep.sh /root/
ENTRYPOINT /root/prep.sh

39
README.md Normal file
View File

@@ -0,0 +1,39 @@
gitlab-repo-sync
================
I'm running my own GitLab instance at home, but I'm less than enthused about
the way GitLab handles backups. I'd previously used Duplicity to back up a
directory full of bare Git repos to cloud storage, with a monthly full backup
and daily incremental backups. This doesn't mesh too well with the way GitLab
does backups; as near as I can tell, the incremental backups end up being as
close to full backups as makes no difference.
This package allows you to mirror your GitLab repos into a directory of bare
Git repos that can more easily be backed up.
If you're running this on bare metal, you only need all_repos.py and
update.sh. You'll need to set a couple of environment variables:
* ```URL```: the URL of your GitLab instance (might also work with gitlab.com,
but this is untested)
* ```APIKEY```: the API key for the GitLab account you want to sync
You might also want to tweak the second line in update.sh to point to the
directory where you want the repos to be stored.
Since my GitLab server and Duplicity backups are running on a container host,
I've also bundled this up so that you can easily use it there. In that case,
you can invoke it with something like this:
```docker run -it --rm -v /home/salfter/git:/git -v /home/salfter/.ssh:/ssh -e URL=https://gitlab.alfter.us -e APIKEY=[REDACTED] -e UID=1000 -e GID=1000 salfter/gitlab-repo-sync```
Let's break down what's happening within:
* ```-v /home/salfter/git:/git```: The directory where you want your repos to
go gets mapped to /git.
* ```-v /home/salfter/.ssh:/ssh```: The directory with the SSH key you use
with your GitLab server needs to be mapped to /ssh.
* ```-e URL=https://gitlab.alfter.us -e APIKEY=[REDACTED]```: These are set
as described above.
* ```-e UID=1000 -e GID=1000```: Set the user and group IDs to match the owner
of your bare-repo directory.

8
all_repos.py Executable file
View File

@@ -0,0 +1,8 @@
#!/usr/bin/env python
import gitlab
import sys
gl=gitlab.Gitlab(url=sys.argv[1], private_token=sys.argv[2])
projects=gl.projects.list(get_all=True)
for p in projects:
print(p.ssh_url_to_repo)

11
prep.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
cd /root
addgroup -g $GID user
adduser -u $UID -G user -D -s /bin/bash user
cp -r /ssh ~user/.ssh
chown -R user:user ~user/.ssh
cp update.sh all_repos.py ~user/
echo "export URL=\"$URL\"" >>~user/.bash_profile
echo "export APIKEY=\"$APIKEY\"" >>~user/.bash_profile
chown user:user ~user/.bash_profile
su -l user -c 'source ~/.bash_profile && ~user/update.sh'

11
update.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
cd /git
for i in $(~/all_repos.py "$URL" "$APIKEY")
do
if [ -e $(basename $i) ]
then
(cd $(basename $i) && git fetch --all)
else
git clone --bare $i
fi
done