Skip to content

Instantly share code, notes, and snippets.

@robertbeal
Created September 11, 2018 08:04
Show Gist options
  • Save robertbeal/6f9fd195c5a79aaa5e74266b7aaed083 to your computer and use it in GitHub Desktop.
Save robertbeal/6f9fd195c5a79aaa5e74266b7aaed083 to your computer and use it in GitHub Desktop.
GitLab bash backup script
#!/bin/bash
# summary:
# backup script that keeps the last 14 (16-2) days worth of gitlab backups
# usage:
# backup.sh {backup-folder-path} {gitlab-access-token}
# backup.sh /mnt/backups AF56VBJJ660PF
cd "$1" || exit
date=$(date +%Y-%m-%d)
rm -rf "$date" || true
mkdir "$date"
cd "$date" || exit
repos=$(curl --header "Private-Token: $2" "https://gitlab.com/api/v4/projects?owned=true" | jq -r .[].ssh_url_to_repo)
while read -r repo; do
git clone --mirror "$repo"
done <<< "$repos"
for dir in */; do
(cd "$dir" && git bundle create repo.bundle --all)
done
# keep the most recent 14 days (14 +2 as paths '.' and '..' are included)
ls -lt | tail -n +16 | awk '{print $9}' | xargs rm -rf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment